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  import java.util.Iterator;
20  import java.util.HashMap;
21  
22  // Junit imports
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  import org.apache.turbine.services.TurbineServices;
27  import org.apache.turbine.util.TurbineConfig;
28  import org.apache.turbine.util.StringUtils;
29  
30  // Jetspeed imports
31  import org.apache.jetspeed.test.JetspeedTestCase;
32  import org.apache.jetspeed.om.security.Group;
33  import org.apache.jetspeed.om.security.JetspeedGroupFactory;
34  
35  /***
36   * Unit test for GroupManagement interface
37   * 
38   * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
39   * @version $Id: TestGroupManagement.java,v 1.1 2004/04/07 22:02:43 jford Exp $
40   */
41  
42  public class TestGroupManagement extends JetspeedTestCase {    
43      
44      /***
45       * Defines the testcase name for JUnit.
46       *
47       * @param name the testcase's name.
48       */
49      public TestGroupManagement( String name ) {
50          super( name );
51      }
52      
53      /***
54       * Start the tests.
55       *
56       * @param args the arguments. Not used
57       */
58      public static void main(String args[]) 
59      {
60          junit.awtui.TestRunner.main( new String[] { TestGroupManagement.class.getName() } );
61      }
62   
63      public void setup() 
64      {
65          //System.out.println("Setup: Testing Turbine Group Management");         
66      }
67  
68      /***
69       * Creates the test suite.
70       *
71       * @return a test suite (<code>TestSuite</code>) that includes all methods
72       *         starting with "test"
73       */
74      public static Test suite() 
75      {
76          // All methods starting with "test" will be executed in the test suite.
77          return new TestSuite( TestGroupManagement.class );
78      }
79  
80      /***
81       * Tests getGroups method
82       * @throws Exception
83       */
84  
85      public void testGetGroups() throws Exception 
86      {
87          GroupManagement service = getService();
88          Group group = null;
89          HashMap map = new HashMap();
90  
91          try
92          {
93              Iterator groups = service.getGroups();
94              while (groups.hasNext())
95              {
96                  group = (Group)groups.next();
97                  map.put(group.getName(), group);
98              }
99              assertTrue(map.get("apache") != null);
100             assertTrue(map.get("Jetspeed") != null);
101             assertTrue(map.get("bogusGroup") == null);
102         }
103         catch (Exception e)
104         {
105             fail(StringUtils.stackTrace(e));
106         }
107 
108         System.out.println("Completed getGroups Test OK ");
109 
110     }
111 
112     /***
113      * Tests getGroups method
114      * @throws Exception
115      */
116 
117     public void testGetGroupsForUser() throws Exception 
118     {
119         GroupManagement service = getService();
120         Group group = null;
121         HashMap map = new HashMap();
122 
123         try
124         {
125             Iterator groups = service.getGroups("turbine");
126             while (groups.hasNext())
127             {
128                 group = (Group)groups.next();
129                 map.put(group.getName(), group);
130                 System.out.println("[turbine] group = " + group.getName());
131             }
132             assertTrue(map.get("Jetspeed") != null);
133             assertTrue(map.get("apache") == null);
134 
135             map.clear();
136             groups = service.getGroups("admin");            
137             while (groups.hasNext())
138             {
139                 group = (Group)groups.next();
140                 map.put(group.getName(), group);
141                 System.out.println("[admin] group = " + group.getName());
142             }
143             assertTrue(map.get("Jetspeed") != null);
144 
145         }
146         catch (Exception e)
147         {
148             fail(StringUtils.stackTrace(e));
149         }
150 
151         System.out.println("Completed getGroups Test OK ");
152 
153     }
154 
155     /***
156      * Tests addGroup method 
157      * @throws Exception
158      */
159 
160     public void testAddGroup() throws Exception 
161     {
162         GroupManagement service = getService();
163         Group group = null;
164 
165         try
166         {
167             group = JetspeedGroupFactory.getInstance();
168             group.setName("bogus");
169             service.addGroup(group);
170             System.out.println("new group id = " + group.getId());
171             assertTrue(group.getId() != null);
172         }
173         catch(Exception e)
174         {
175             fail(StringUtils.stackTrace(e));
176         }
177         try
178         {
179             group = JetspeedGroupFactory.getInstance();
180             group.setName("bogus");
181             service.addGroup(group);
182             fail("Should've thrown a dup key exception on group");
183         }
184         catch(Exception e)
185         {
186             assertTrue(e instanceof GroupException);           
187         }
188 
189         System.out.println("Completed addGroup Test OK ");
190 
191     }
192 
193     /***
194      * Tests getRemoveGroup method 
195      * @throws Exception
196      */
197 
198     public void testRemoveGroup() throws Exception 
199     {
200         GroupManagement service = getService();
201         Group group = null;
202 
203         try
204         {
205             service.removeGroup("bogus");
206         }
207         catch(Exception e)
208         {
209             fail(StringUtils.stackTrace(e));
210         }
211         try
212         {
213             service.removeGroup("catchmeifyoucan");
214             fail("Should've thrown a not found exception on group");
215         }
216         catch(Exception e)
217         {
218             assertTrue(e instanceof GroupException);
219         }
220 
221         System.out.println("Completed addGroup Test OK ");
222 
223     }
224 
225     /***
226      * Tests getGroup method
227      * @throws Exception
228      */
229 
230     public void testGetGroup() throws Exception 
231     {
232         GroupManagement service = getService();
233 
234         try
235         {
236             Group group = service.getGroup("Jetspeed");
237             System.out.println("*** group nm = " + group.getName());
238             System.out.println("*** group id = " + group.getId());
239             assertTrue(group.getName().equals("Jetspeed"));
240         }
241         catch (Exception e)
242         {
243             fail(StringUtils.stackTrace(e));
244         }
245 
246         System.out.println("Completed getGroup Test OK ");
247 
248     }
249 
250     /***
251      * Tests saveGroup method 
252      * @throws Exception
253      */
254 
255     public void testSaveGroup() throws Exception 
256     {
257         GroupManagement service = getService();
258 
259         try
260         {
261             Group group = service.getGroup("apache");
262             service.saveGroup(group);
263         }
264         catch(Exception e)
265         {
266             fail(StringUtils.stackTrace(e));
267         }
268 
269         System.out.println("Completed saveGroup Test OK ");
270 
271     }
272 
273     /***
274      * Tests joinGroup method 
275      * @throws Exception
276      */
277     public void testJoinGroup() throws Exception 
278     {
279         GroupManagement service = getService();
280         Group group = null;
281 
282         try
283         {
284             service.joinGroup("turbine", "apache");
285         }
286         catch(Exception e)
287         {
288             fail(StringUtils.stackTrace(e));
289         }
290         try
291         {
292             service.joinGroup("baduser", "apache");
293             fail("Should've thrown a bad user exception on join");
294         }
295         catch(Exception e)
296         {
297             assertTrue(e instanceof GroupException);           
298         }
299 
300         System.out.println("Completed joinGroup Test OK ");
301 
302     }
303 
304     /***
305      * Tests unjoinGroup method 
306      * @throws Exception
307      */
308     public void testUnjoinGroup() throws Exception 
309     {
310         GroupManagement service = getService();
311         Group group = null;
312 
313         try
314         {
315             service.unjoinGroup("turbine", "apache");
316         }
317         catch(Exception e)
318         {
319             fail(StringUtils.stackTrace(e));
320         }
321         try
322         {
323             service.unjoinGroup("baduser", "apache");
324             fail("Should've thrown a bad user exception on unjoin");
325         }
326         catch(Exception e)
327         {
328             assertTrue(e instanceof GroupException);           
329         }
330 
331         System.out.println("Completed unjoinGroup Test OK ");
332 
333     }
334 
335     /***
336      * Tests inGroup method 
337      * @throws Exception
338      */
339     public void testInGroup() throws Exception 
340     {
341         GroupManagement service = getService();
342         Group group = null;
343 
344         try
345         {
346             boolean in = service.inGroup("admin", "Jetspeed");
347             assertTrue(true == in);
348         }
349         catch(Exception e)
350         {
351             fail(StringUtils.stackTrace(e));
352         }
353         try
354         {
355             boolean in = service.inGroup("turbine", "apache");
356             assertTrue(false == in);
357         }
358         catch(Exception e)
359         {
360             fail(StringUtils.stackTrace(e));
361         }
362 
363         System.out.println("Completed inGroup Test OK ");
364 
365     }
366 
367   /*
368     Configuration object to run Turbine outside a servlet container
369     ( uses turbine.properties )
370     */
371     private static TurbineConfig config = null;
372     
373     /***
374     Sets up TurbineConfig using the system property:
375     <pre>turbine.properties</pre>
376     */
377     static
378     {
379         try
380         {
381             config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties");
382             config.init();
383         }
384         catch (Exception e)
385         {
386             fail(StringUtils.stackTrace(e));
387         }
388     }
389 
390     private static GroupManagement getService()
391     {
392         return (GroupManagement)TurbineServices
393                 .getInstance()
394                 .getService(GroupManagement.SERVICE_NAME);
395     }
396 
397 }
398 
399 
400 
401 
402 
403