KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > ValidatorTest


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  * ValidatorTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.security;
25
26 import junit.framework.*;
27
28 // java includes
29
import java.util.Set JavaDoc;
30 import java.util.HashSet JavaDoc;
31
32 /**
33  *
34  * @author Brett Chaldecott
35  */

36 public class ValidatorTest extends TestCase {
37     
38     public ValidatorTest(String JavaDoc testName) {
39         super(testName);
40     }
41
42     protected void setUp() throws Exception JavaDoc {
43     }
44
45     protected void tearDown() throws Exception JavaDoc {
46     }
47
48     public static Test suite() {
49         TestSuite suite = new TestSuite(ValidatorTest.class);
50         
51         return suite;
52     }
53
54     /**
55      * Test of validate method, of class com.rift.coad.lib.security.Validator.
56      */

57     public void testValidate() throws Exception JavaDoc {
58         System.out.println("validate");
59         
60         Class JavaDoc ref = null;
61         String JavaDoc roleName = "";
62         
63         // initialize the session manager
64
ThreadsPermissionContainer permissionContainer =
65                 new ThreadsPermissionContainer();
66         SessionManager.init(permissionContainer);
67         SessionManager.getInstance().initSession();
68         
69         // add a user to the session for the current thread
70
RoleManager.getInstance();
71         
72         try {
73             Validator.validate(getClass(), "test");
74             fail("The test case is a prototype.");
75         } catch (Exception JavaDoc ex) {
76             System.out.println("Access failed success : " + ex.getMessage());
77         }
78         
79         // add a new user object and add to the permission
80
Set JavaDoc set = new HashSet JavaDoc();
81         set.add("test");
82         UserSession user = new UserSession("testuser", set);
83         permissionContainer.putSession(new Long JavaDoc(Thread.currentThread().getId()),
84                 new ThreadPermissionSession(
85                 new Long JavaDoc(Thread.currentThread().getId()),user));
86         
87         try {
88             Validator.validate(getClass(), "test");
89         } catch (Exception JavaDoc ex) {
90             fail("The test case is a prototype : " + ex.getMessage());
91         }
92         
93         permissionContainer.pushRole("middle");
94         permissionContainer.pushRole("master");
95         
96         try {
97             Validator.validate(getClass(), "master");
98         } catch (Exception JavaDoc ex) {
99             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 JavaDoc ex) {
109             // ignore
110
}
111
112         try {
113             Validator.validate(getClass(), "test");
114         } catch (Exception JavaDoc ex) {
115             fail("The test case is a prototype : " + ex.getMessage());
116         }
117
118     }
119     
120 }
121
Popular Tags