KickJava   Java API By Example, From Geeks To Geeks.

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


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 do NOT propagate to child threads
40  *
41  @author Scott.Stark@jboss.org
42  @version $Revision: 37406 $
43  */

44 public class SAThreadLocalUnitTestCase extends TestCase
45 {
46    private Principal JavaDoc authPrincipal;
47    private Subject JavaDoc authSubject;
48
49    public SAThreadLocalUnitTestCase(String JavaDoc name)
50    {
51       super(name);
52    }
53
54    /**
55     * Test the order of PermissionNames
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    public void testThreadLocal() throws Exception JavaDoc
66    {
67       testSecurityContext();
68       TestThread t = new TestThread("testThreadLocal", true);
69       t.start();
70       t.join();
71       if( t.error != null )
72       {
73          t.error.printStackTrace();
74          fail("TestThread saw an error");
75       }
76    }
77
78    /**
79     * SecurityAssociation.getSubject() == authSubject
80     * SecurityAssociation.getPrincipal() == authPrincipal
81     */

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