001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * InterceptorPermissionStackTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.interceptor;
025:
026: import junit.framework.*;
027: import java.util.Stack;
028: import java.util.HashSet;
029: import java.util.Set;
030: import org.apache.log4j.Logger;
031: import com.rift.coad.lib.security.ThreadsPermissionContainer;
032: import com.rift.coad.lib.security.ThreadPermissionSession;
033: import com.rift.coad.lib.security.UserSession;
034:
035: /**
036: *
037: * @author Brett Chaldecott
038: */
039: public class InterceptorPermissionStackTest extends TestCase {
040:
041: public InterceptorPermissionStackTest(String testName) {
042: super (testName);
043: }
044:
045: protected void setUp() throws Exception {
046: }
047:
048: protected void tearDown() throws Exception {
049: }
050:
051: public static Test suite() {
052: TestSuite suite = new TestSuite(
053: InterceptorPermissionStackTest.class);
054:
055: return suite;
056: }
057:
058: /**
059: * Test of InterceptorPermissionStack, of class com.rift.coad.lib.interceptor.InterceptorPermissionStack.
060: */
061: public void testInterceptorPermissionStack() throws Exception {
062: System.out.println("InterceptorPermissionStack");
063:
064: // initialize the session manager
065: ThreadsPermissionContainer permissionContainer = new ThreadsPermissionContainer();
066:
067: InterceptorPermissionStack instance = new InterceptorPermissionStack(
068: permissionContainer);
069:
070: Set set = new HashSet();
071: set.add("test");
072: UserSession user = new UserSession("testuser", set);
073:
074: instance.push(user);
075:
076: // retrieve the permission session
077: ThreadPermissionSession permissionSession = permissionContainer
078: .getSession(Thread.currentThread().getId());
079: if (!permissionSession.getUser().getName().equals("testuser")) {
080: fail("Username is not testuser");
081: }
082:
083: Set currentPrincipalSet = permissionSession.getPrincipals();
084: if (currentPrincipalSet.size() != 1) {
085: fail("The principal set is not the correct size");
086: }
087: if (!currentPrincipalSet.contains("test")) {
088: fail("Principal set does not contain the test principal");
089: }
090:
091: Set newSet = new HashSet();
092: newSet.add("fred");
093: newSet.add("mary");
094: UserSession newUser = new UserSession("fred", newSet);
095: UserSession newUser2 = (UserSession) newUser.clone();
096: if (!newUser2.getName().equals(newUser.getName())) {
097: fail("The clone did not work");
098: }
099:
100: instance.push(newUser);
101:
102: permissionSession = permissionContainer.getSession(Thread
103: .currentThread().getId());
104: if (!permissionSession.getUser().getName().equals("fred")) {
105: fail("Username is not fred");
106: }
107:
108: currentPrincipalSet = permissionSession.getPrincipals();
109: if (currentPrincipalSet.size() != 2) {
110: fail("Failed the principal sets are not equal");
111: }
112: if (!currentPrincipalSet.contains("mary")) {
113: fail("Principal set does not contain the fred principal");
114: }
115:
116: instance.pop();
117:
118: permissionSession = permissionContainer.getSession(Thread
119: .currentThread().getId());
120: if (!permissionSession.getUser().getName().equals("testuser")) {
121: fail("Username is not testuser");
122: }
123:
124: currentPrincipalSet = permissionSession.getPrincipals();
125: if (currentPrincipalSet.size() != 1) {
126: fail("The principal set is not the correct size");
127: }
128: if (!currentPrincipalSet.contains("test")) {
129: fail("Principal set does not contain the test principal");
130: }
131:
132: instance.pop();
133:
134: permissionSession = permissionContainer.getSession(Thread
135: .currentThread().getId());
136: if (permissionSession != null) {
137: fail("The permissions were not removed");
138: }
139: }
140:
141: }
|