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: * ValidatorTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.security;
025:
026: import junit.framework.*;
027:
028: // java includes
029: import java.util.Set;
030: import java.util.HashSet;
031:
032: /**
033: *
034: * @author Brett Chaldecott
035: */
036: public class ValidatorTest extends TestCase {
037:
038: public ValidatorTest(String testName) {
039: super (testName);
040: }
041:
042: protected void setUp() throws Exception {
043: }
044:
045: protected void tearDown() throws Exception {
046: }
047:
048: public static Test suite() {
049: TestSuite suite = new TestSuite(ValidatorTest.class);
050:
051: return suite;
052: }
053:
054: /**
055: * Test of validate method, of class com.rift.coad.lib.security.Validator.
056: */
057: public void testValidate() throws Exception {
058: System.out.println("validate");
059:
060: Class ref = null;
061: String roleName = "";
062:
063: // initialize the session manager
064: ThreadsPermissionContainer permissionContainer = new ThreadsPermissionContainer();
065: SessionManager.init(permissionContainer);
066: SessionManager.getInstance().initSession();
067:
068: // add a user to the session for the current thread
069: RoleManager.getInstance();
070:
071: try {
072: Validator.validate(getClass(), "test");
073: fail("The test case is a prototype.");
074: } catch (Exception ex) {
075: System.out.println("Access failed success : "
076: + ex.getMessage());
077: }
078:
079: // add a new user object and add to the permission
080: Set set = new HashSet();
081: set.add("test");
082: UserSession user = new UserSession("testuser", set);
083: permissionContainer.putSession(new Long(Thread.currentThread()
084: .getId()), new ThreadPermissionSession(new Long(Thread
085: .currentThread().getId()), user));
086:
087: try {
088: Validator.validate(getClass(), "test");
089: } catch (Exception ex) {
090: fail("The test case is a prototype : " + ex.getMessage());
091: }
092:
093: permissionContainer.pushRole("middle");
094: permissionContainer.pushRole("master");
095:
096: try {
097: Validator.validate(getClass(), "master");
098: } catch (Exception ex) {
099: fail("The test case is a prototype : " + ex.getMessage());
100: }
101:
102: permissionContainer.popRole("master");
103: permissionContainer.popRole("middle");
104:
105: try {
106: Validator.validate(getClass(), "master");
107: fail("Invalid role should not have role master");
108: } catch (Exception ex) {
109: // ignore
110: }
111:
112: try {
113: Validator.validate(getClass(), "test");
114: } catch (Exception ex) {
115: fail("The test case is a prototype : " + ex.getMessage());
116: }
117:
118: }
119:
120: }
|