01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.test;
23:
24: import junit.framework.TestCase;
25: import junit.framework.TestSuite;
26:
27: import org.jboss.security.SimplePrincipal;
28:
29: /** Tests of propagating the security identity across threads using
30: InheritableThreadLocal.
31:
32: @author Scott.Stark@jboss.org
33: @version $Revision: 37390 $
34: */
35: public class ThreadLocalTestCase extends TestCase {
36: private static InheritableThreadLocal thread_principal = new InheritableThreadLocal();
37: private static InheritableThreadLocal thread_credential = new InheritableThreadLocal();
38: private static String USER = "jduke";
39: private static String PASSWORD = "theduke";
40:
41: public ThreadLocalTestCase(String name) {
42: super (name);
43: }
44:
45: public void testSecurityPropagation() throws Exception {
46: // Assign the principal & crendentials for this thread
47: SimplePrincipal user = new SimplePrincipal(USER);
48: thread_principal.set(user);
49: thread_credential.set(PASSWORD);
50: // Spawn a thread
51: Thread t = new Thread(new Child(), "testSecurityPropagation");
52: t.start();
53: t.join();
54: }
55:
56: public void testSecurityPropagation2() throws Exception {
57: // Assign the principal & crendentials for this thread
58: SimplePrincipal user = new SimplePrincipal(USER);
59: thread_principal.set(user);
60: thread_credential.set(PASSWORD);
61: // Spawn a thread
62: Thread t = new Thread(new Child(), "testSecurityPropagation");
63: // See that changing the current thread info is not seen by children threads
64: thread_principal.set(new SimplePrincipal("other"));
65: thread_credential.set("otherpass");
66: t.start();
67: t.join();
68: }
69:
70: static class Child implements Runnable {
71: public void run() {
72: Thread t = Thread.currentThread();
73: System.out.println("Child.run begin, t=" + t);
74: if (t.getName().equals("testSecurityPropagation")) {
75: SimplePrincipal user = (SimplePrincipal) thread_principal
76: .get();
77: String password = (String) thread_credential.get();
78: if (user.getName().equals(USER) == false)
79: fail("Thread user != " + USER);
80: if (password.equals(PASSWORD) == false)
81: fail("Thread password != " + PASSWORD);
82: }
83: System.out.println("Child.run end, t=" + t);
84: }
85: }
86:
87: public static void main(java.lang.String[] args) {
88: System.setErr(System.out);
89: TestSuite suite = new TestSuite(ThreadLocalTestCase.class);
90: junit.textui.TestRunner.run(suite);
91: }
92:
93: }
|