1 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 20 public class ThreadLocalTestCase extends TestCase 21 { 22 private static InheritableThreadLocal thread_principal = new InheritableThreadLocal (); 23 private static InheritableThreadLocal thread_credential = new InheritableThreadLocal (); 24 private static String USER = "jduke"; 25 private static String PASSWORD = "theduke"; 26 27 public ThreadLocalTestCase(String name) 28 { 29 super(name); 30 } 31 32 public void testSecurityPropagation() throws Exception 33 { 34 SimplePrincipal user = new SimplePrincipal(USER); 36 thread_principal.set(user); 37 thread_credential.set(PASSWORD); 38 Thread t = new Thread (new Child(), "testSecurityPropagation"); 40 t.start(); 41 t.join(); 42 } 43 44 public void testSecurityPropagation2() throws Exception 45 { 46 SimplePrincipal user = new SimplePrincipal(USER); 48 thread_principal.set(user); 49 thread_credential.set(PASSWORD); 50 Thread t = new Thread (new Child(), "testSecurityPropagation"); 52 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 60 { 61 public void run() 62 { 63 Thread 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 password = (String ) 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 [] 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 |