KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > ThreadLocalTestCase


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test;
8
9 import junit.framework.TestCase;
10 import junit.framework.TestSuite;
11
12 import org.jboss.security.SimplePrincipal;
13
14 /** Tests of propagating the security identity across threads using
15 InheritableThreadLocal.
16
17 @author Scott.Stark@jboss.org
18 @version $Revision: 1.1.26.1 $
19 */

20 public class ThreadLocalTestCase extends TestCase
21 {
22     private static InheritableThreadLocal JavaDoc thread_principal = new InheritableThreadLocal JavaDoc();
23     private static InheritableThreadLocal JavaDoc thread_credential = new InheritableThreadLocal JavaDoc();
24     private static String JavaDoc USER = "jduke";
25     private static String JavaDoc PASSWORD = "theduke";
26
27     public ThreadLocalTestCase(String JavaDoc name)
28     {
29         super(name);
30     }
31
32     public void testSecurityPropagation() throws Exception JavaDoc
33     {
34         // Assign the principal & crendentials for this thread
35
SimplePrincipal user = new SimplePrincipal(USER);
36         thread_principal.set(user);
37         thread_credential.set(PASSWORD);
38         // Spawn a thread
39
Thread JavaDoc t = new Thread JavaDoc(new Child(), "testSecurityPropagation");
40         t.start();
41         t.join();
42     }
43
44     public void testSecurityPropagation2() throws Exception JavaDoc
45     {
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 JavaDoc t = new Thread JavaDoc(new Child(), "testSecurityPropagation");
52         // See that changing the current thread info is not seen by children threads
53
thread_principal.set(new SimplePrincipal("other"));
54         thread_credential.set("otherpass");
55         t.start();
56         t.join();
57     }
58
59     static class Child implements Runnable JavaDoc
60     {
61         public void run()
62         {
63             Thread JavaDoc t = Thread.currentThread();
64             System.out.println("Child.run begin, t="+t);
65             if( t.getName().equals("testSecurityPropagation") )
66             {
67                 SimplePrincipal user = (SimplePrincipal) thread_principal.get();
68                 String JavaDoc password = (String JavaDoc) thread_credential.get();
69                 if( user.getName().equals(USER) == false )
70                     fail("Thread user != "+USER);
71                 if( password.equals(PASSWORD) == false )
72                     fail("Thread password != "+PASSWORD);
73             }
74             System.out.println("Child.run end, t="+t);
75         }
76     }
77
78     public static void main(java.lang.String JavaDoc[] args)
79     {
80         System.setErr(System.out);
81         TestSuite suite = new TestSuite(ThreadLocalTestCase.class);
82         junit.textui.TestRunner.run(suite);
83     }
84     
85 }
86
Popular Tags