KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > security > TestSecurityCache


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.services.security;
18
19 // Junit imports
20
import junit.framework.Test;
21 import junit.framework.TestSuite;
22
23 import org.apache.turbine.services.TurbineServices;
24 import org.apache.turbine.util.TurbineConfig;
25 import org.apache.turbine.util.StringUtils;
26
27 // Jetspeed imports
28
import org.apache.jetspeed.test.JetspeedTestCase;
29 import org.apache.jetspeed.services.JetspeedSecurity;
30 import org.apache.jetspeed.om.security.Role;
31 import org.apache.jetspeed.om.security.Permission;
32 import org.apache.jetspeed.om.security.JetspeedUser;
33
34 /**
35  * Unit test for SecurityCacheService
36  *
37  * @author <a HREF="mailto:david@bluesunrise.com">David Sean Taylor</a>
38  * @version $Id: TestSecurityCache.java,v 1.1 2004/04/07 22:02:43 jford Exp $
39  */

40
41 public class TestSecurityCache extends JetspeedTestCase {
42
43     /**
44      * Defines the testcase name for JUnit.
45      *
46      * @param name the testcase's name.
47      */

48     public TestSecurityCache( String JavaDoc name ) {
49         super( name );
50     }
51     
52     /**
53      * Start the tests.
54      *
55      * @param args the arguments. Not used
56      */

57     public static void main(String JavaDoc args[])
58     {
59         junit.awtui.TestRunner.main( new String JavaDoc[] { TestSecurityCache.class.getName() } );
60     }
61  
62     public void setup()
63     {
64         //System.out.println("Setup: Testing Turbine Role Management");
65
}
66
67     /**
68      * Creates the test suite.
69      *
70      * @return a test suite (<code>TestSuite</code>) that includes all methods
71      * starting with "test"
72      */

73     public static Test suite()
74     {
75         // All methods starting with "test" will be executed in the test suite.
76
return new TestSuite( TestSecurityCache.class );
77     }
78
79     /**
80      * Tests getRoles method
81      * @throws Exception
82      */

83
84     public void testLoadCache() throws Exception JavaDoc
85     {
86         SecurityCacheService service = getService();
87
88         try
89         {
90             JetspeedUser user = JetspeedSecurity.getUser("turbine");
91             service.load(user.getUserName());
92             Role role = service.getRole(user.getUserName(), "user");
93             assertTrue(role.getName().equals("user"));
94             assertTrue(service.hasRole(user.getUserName(), "user"));
95             assertTrue(service.hasPermission("user", "view"));
96             assertTrue(service.hasPermission("user", "customize"));
97             assertTrue(service.hasPermission("user", "maximize"));
98             assertTrue(!service.hasPermission("user", "failure"));
99         }
100         catch (Exception JavaDoc e)
101         {
102             fail(StringUtils.stackTrace(e));
103         }
104
105         System.out.println("Completed loadCache Test OK ");
106
107     }
108
109
110     public void testAddRemoveFromCache() throws Exception JavaDoc
111     {
112         SecurityCacheService service = getService();
113
114         try
115         {
116             JetspeedUser user = JetspeedSecurity.getUser("anon");
117             service.load(user.getUserName());
118
119             Role role1 = service.getRole(user.getUserName(), "guest");
120             assertTrue(role1.getName().equals("guest"));
121             assertTrue(service.hasPermission("guest", "view"));
122
123             // add role
124
Role role2 = JetspeedSecurity.getRole("user");
125             service.addRole(user.getUserName(), role2);
126             assertTrue(service.hasRole(user.getUserName(), "user"));
127             assertTrue(service.getRole(user.getUserName(),"user").getName().equals("user"));
128
129             // remove role
130
service.removeRole(user.getUserName(), "user");
131             assertTrue(!service.hasRole(user.getUserName(), "user"));
132             Role role3 = service.getRole(user.getUserName(),"user");
133             assertTrue(null == role3);
134
135             // add permission
136
Permission perm1 = JetspeedSecurity.getPermission("info");
137             assertTrue(null != perm1);
138             service.addPermission("guest", perm1);
139             Permission permission = service.getPermission("guest", "info");
140             assertTrue(permission.getName().equals("info"));
141             assertTrue(service.hasPermission("guest", "info"));
142
143             // remove permission
144
service.removePermission("guest", "info");
145             assertTrue(!service.hasPermission( "guest", "info"));
146             Permission perm2 = service.getPermission( "guest", "info");
147             assertTrue(null == perm2);
148
149         }
150         catch (Exception JavaDoc e)
151         {
152             fail(StringUtils.stackTrace(e));
153         }
154
155         System.out.println("Completed addRemoveFromCache Test OK ");
156
157     }
158
159     public void testRemoveAll() throws Exception JavaDoc
160     {
161         SecurityCacheService service = getService();
162         try
163         {
164             Role role = JetspeedSecurity.getRole("admin");
165             JetspeedUser anon = JetspeedSecurity.getUser("anon");
166             service.load(anon.getUserName());
167             JetspeedUser turbine = JetspeedSecurity.getUser("turbine");
168             service.load(turbine.getUserName());
169
170             service.addRole(anon.getUserName(), role);
171             service.addRole(turbine.getUserName(), role);
172
173             assertTrue(service.hasRole(anon.getUserName(), role.getName()));
174             assertTrue(service.hasRole(turbine.getUserName(), role.getName()));
175
176             service.removeAllRoles("admin");
177
178             assertTrue(!service.hasRole(anon.getUserName(), role.getName()));
179             assertTrue(!service.hasRole(turbine.getUserName(), role.getName()));
180
181         }
182         catch (Exception JavaDoc e)
183         {
184             fail(StringUtils.stackTrace(e));
185         }
186
187         System.out.println("Completed removeAll Test OK ");
188
189     }
190
191   /*
192     Configuration object to run Turbine outside a servlet container
193     ( uses turbine.properties )
194     */

195     private static TurbineConfig config = null;
196     
197     /**
198     Sets up TurbineConfig using the system property:
199     <pre>turbine.properties</pre>
200     */

201     static
202     {
203         try
204         {
205             config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties");
206             config.init();
207         }
208         catch (Exception JavaDoc e)
209         {
210             fail(StringUtils.stackTrace(e));
211         }
212     }
213
214     private static SecurityCacheService getService()
215     {
216         return (SecurityCacheService)TurbineServices
217                 .getInstance()
218                 .getService(SecurityCacheService.SERVICE_NAME);
219     }
220
221 }
222
223
224
225
226
227
228
Popular Tags