KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > SAInheritableThreadLocalUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * 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.security.test;
23
24 import java.io.FilePermission JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.security.CodeSource JavaDoc;
27 import java.security.Permission JavaDoc;
28 import java.security.PermissionCollection JavaDoc;
29 import java.security.Policy JavaDoc;
30 import java.security.Principal JavaDoc;
31
32 import javax.security.auth.Subject JavaDoc;
33
34 import junit.framework.TestCase;
35 import org.jboss.security.SecurityAssociation;
36 import org.jboss.security.SimplePrincipal;
37
38 /**
39  * Test that the security context thread locals propagate to child threads
40  *
41  @author Scott.Stark@jboss.org
42  @version $Revision: 37406 $
43  */

44 public class SAInheritableThreadLocalUnitTestCase extends TestCase
45 {
46    private Principal JavaDoc authPrincipal;
47    private Subject JavaDoc authSubject;
48
49    public SAInheritableThreadLocalUnitTestCase(String JavaDoc name)
50    {
51       super(name);
52    }
53
54    /**
55     * Test the expected security context exists via the SecurityAssociation accessors
56     */

57    public void testSecurityContext()
58    {
59       authPrincipal = new SimplePrincipal("jduke");
60       authSubject = new Subject JavaDoc();
61       authSubject.getPrincipals().add(authPrincipal);
62       SecurityAssociation.pushSubjectContext(authSubject, authPrincipal, "theduke");
63       validateSettings(false);
64    }
65    /**
66     * Validate that a child thread sees its parent
67    */

68    public void testInheritableThreadLocal() throws Exception JavaDoc
69    {
70       authPrincipal = new SimplePrincipal("jduke");
71       authSubject = new Subject JavaDoc();
72       authSubject.getPrincipals().add(authPrincipal);
73       SecurityAssociation.pushSubjectContext(authSubject, authPrincipal, "theduke");
74       validateSettings(false);
75
76       TestThread t = new TestThread("testInheritableThreadLocal", false);
77       t.start();
78       t.join();
79       if( t.error != null )
80       {
81          t.error.printStackTrace();
82          fail("TestThread saw an error");
83       }
84    }
85
86    /**
87     * SecurityAssociation.getSubject() == authSubject
88     * SecurityAssociation.getPrincipal() == authPrincipal
89     */

90    private void validateSettings(boolean expectNull)
91    {
92       Subject JavaDoc s = SecurityAssociation.getSubject();
93       Principal JavaDoc p = SecurityAssociation.getPrincipal();
94
95       if( expectNull )
96       {
97          assertNull("getSubject() == null", s);
98          assertNull("getPrincipal() == null", p);
99       }
100       else
101       {
102          assertTrue("getSubject() == authSubject", authSubject.equals(s));
103          assertTrue("getPrincipal() == authPrincipal", authPrincipal.equals(p));
104       }
105    }
106    
107    class TestThread extends Thread JavaDoc
108    {
109       Throwable JavaDoc error;
110       boolean expectNull;
111       TestThread(String JavaDoc name, boolean expectNull)
112       {
113          super(name);
114          this.expectNull = expectNull;
115       }
116
117       public void run()
118       {
119          try
120          {
121             validateSettings(expectNull);
122          }
123          catch(Throwable JavaDoc e)
124          {
125             error = e;
126          }
127       }
128    }
129
130    protected void setUp()
131    {
132       System.setProperty("org.jboss.security.SecurityAssociation.ThreadLocal", "false");
133       SecurityAssociation.setServer();
134    }
135 }
136
Popular Tags