KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > interceptor > InterceptorPermissionStackTest


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * InterceptorPermissionStackTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.interceptor;
25
26 import junit.framework.*;
27 import java.util.Stack JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Set JavaDoc;
30 import org.apache.log4j.Logger;
31 import com.rift.coad.lib.security.ThreadsPermissionContainer;
32 import com.rift.coad.lib.security.ThreadPermissionSession;
33 import com.rift.coad.lib.security.UserSession;
34
35 /**
36  *
37  * @author Brett Chaldecott
38  */

39 public class InterceptorPermissionStackTest extends TestCase {
40     
41     public InterceptorPermissionStackTest(String JavaDoc testName) {
42         super(testName);
43     }
44     
45     protected void setUp() throws Exception JavaDoc {
46     }
47     
48     protected void tearDown() throws Exception JavaDoc {
49     }
50     
51     public static Test suite() {
52         TestSuite suite = new TestSuite(InterceptorPermissionStackTest.class);
53         
54         return suite;
55     }
56     
57     /**
58      * Test of InterceptorPermissionStack, of class com.rift.coad.lib.interceptor.InterceptorPermissionStack.
59      */

60     public void testInterceptorPermissionStack() throws Exception JavaDoc {
61         System.out.println("InterceptorPermissionStack");
62         
63         // initialize the session manager
64
ThreadsPermissionContainer permissionContainer =
65                 new ThreadsPermissionContainer();
66         
67         InterceptorPermissionStack instance = new InterceptorPermissionStack(
68                 permissionContainer);
69         
70         Set JavaDoc set = new HashSet JavaDoc();
71         set.add("test");
72         UserSession user = new UserSession("testuser", set);
73         
74         instance.push(user);
75         
76         // retrieve the permission session
77
ThreadPermissionSession permissionSession =
78                 permissionContainer.getSession(Thread.currentThread().getId());
79         if (!permissionSession.getUser().getName().equals("testuser")) {
80             fail("Username is not testuser");
81         }
82         
83         Set JavaDoc currentPrincipalSet = permissionSession.getPrincipals();
84         if (currentPrincipalSet.size() != 1) {
85             fail("The principal set is not the correct size");
86         }
87         if (!currentPrincipalSet.contains("test")) {
88             fail("Principal set does not contain the test principal");
89         }
90         
91         Set JavaDoc newSet = new HashSet JavaDoc();
92         newSet.add("fred");
93         newSet.add("mary");
94         UserSession newUser = new UserSession("fred", newSet);
95         UserSession newUser2 = (UserSession)newUser.clone();
96         if (!newUser2.getName().equals(newUser.getName())) {
97             fail("The clone did not work");
98         }
99         
100         
101         instance.push(newUser);
102         
103         permissionSession =
104                 permissionContainer.getSession(Thread.currentThread().getId());
105         if (!permissionSession.getUser().getName().equals("fred")) {
106             fail("Username is not fred");
107         }
108         
109         currentPrincipalSet = permissionSession.getPrincipals();
110         if (currentPrincipalSet.size() != 2) {
111             fail("Failed the principal sets are not equal");
112         }
113         if (!currentPrincipalSet.contains("mary")) {
114             fail("Principal set does not contain the fred principal");
115         }
116         
117         
118         instance.pop();
119         
120         permissionSession =
121                 permissionContainer.getSession(Thread.currentThread().getId());
122         if (!permissionSession.getUser().getName().equals("testuser")) {
123             fail("Username is not testuser");
124         }
125         
126         currentPrincipalSet = permissionSession.getPrincipals();
127         if (currentPrincipalSet.size() != 1) {
128             fail("The principal set is not the correct size");
129         }
130         if (!currentPrincipalSet.contains("test")) {
131             fail("Principal set does not contain the test principal");
132         }
133         
134         instance.pop();
135         
136         permissionSession =
137                 permissionContainer.getSession(Thread.currentThread().getId());
138         if (permissionSession != null) {
139             fail("The permissions were not removed");
140         }
141     }
142     
143     
144 }
145
Popular Tags