03329d1d958f918b150e220e424ad54d3c6d043a
[utils] / security / src / test / java / org / wamblee / usermgt / InMemoryGroupSetTest.java
1 /*
2  * Copyright 2005 the original author or authors.
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.wamblee.usermgt;
18
19 import java.sql.SQLException;
20 import java.util.Set;
21
22 import junit.framework.TestCase;
23
24 /**
25  * Tests the inmemory group set. Intended to be subclassed for other
26  * implementations of group set. 
27  */
28 public class InMemoryGroupSetTest extends TestCase {
29     
30     protected GroupSet groups; 
31    
32     /**
33      * This method must be overriden in subclasses. 
34      * @return New group set object. 
35      */
36     protected GroupSet createGroupSet() { 
37         return new InMemoryGroupSet(); 
38     }
39     
40     /* (non-Javadoc)
41      * @see org.wamblee.test.SpringTestCase#setUp()
42      */
43     @Override
44     protected void setUp() throws Exception {
45         super.setUp();
46         groups = createGroupSet(); 
47         checkGroupCount(0); 
48     }
49     
50     
51     
52     /**
53      * Additional check to be implemented by a subclass. 
54      * @param aGroup Group to check for existence. 
55      */
56     protected void checkGroupExists(String aGroup) throws SQLException { 
57         // Empty
58     }
59     
60     /**
61      * Additional check to be implemented by a subclass. 
62      * @param aGroup Group to check for non-existence. 
63      */
64     protected void checkGroupNotExists(String aGroup) throws SQLException { 
65         // Empty
66     }
67     
68     /**
69      * Additional check to be implemented by a subclass. 
70      * @param aSize Expected number of groups. 
71      */
72     protected void checkGroupCount(int aSize) throws SQLException { 
73         assertEquals(aSize, groups.size());
74     }
75     
76     /**
77      * Adds a group and verifies that the group is added using 
78      * find(), list(),  and contains().  
79      *
80      */
81     public void testAdd() throws SQLException { 
82        Group group = new Group("group1"); 
83        assertTrue(  groups.add(group) );
84        checkGroupExists(group.getName());
85        checkGroupCount(1);
86        Group group2 = groups.find("group1"); 
87        assertNotNull(group2);
88        assertEquals(group.getName(), group2.getName()); 
89        Set<Group> set = groups.list(); 
90        assertEquals(1, set.size()); 
91        assertTrue(set.contains(group));
92     }
93     
94     /**
95      * Tries to find a non-existing group. Verifies that null is
96      * returned. 
97      *
98      */
99     public void testFindUnknownGroup() throws SQLException { 
100        Group group1 = new Group("group1"); 
101        Group group2 = new Group("group2");
102        groups.add(group1); 
103        groups.add(group2);
104        checkGroupExists(group1.getName()); 
105        checkGroupExists(group2.getName()); 
106     
107        assertNull( groups.find("group3") );
108        checkGroupNotExists("group3");
109     }
110     
111     /**
112      * Adds duplicate group. Verifies that the existing group is left untouched.  
113      */
114     public void testAddDuplicateGroup() throws SQLException { 
115        Group group1 = new Group("group1"); 
116        groups.add(group1); 
117        
118        assertEquals(1, groups.list().size()); 
119        assertTrue(groups.contains(group1));
120        group1 = new Group("group1");
121        assertFalse(groups.add(group1));
122        assertEquals(1, groups.list().size());
123        
124        checkGroupExists(group1.getName()); 
125        checkGroupCount(1); 
126     }
127     
128     /**
129      * Removes a group. Verifies that the group is 
130      * removed and the return value is true. 
131      *
132      */
133     public void testRemoveGroup() throws SQLException { 
134         Group group1 = new Group("group1"); 
135         groups.add(group1); 
136         assertTrue(groups.contains(group1));
137         checkGroupCount(1);
138         
139         assertTrue(groups.remove(group1)); 
140         assertFalse(groups.contains(group1)); 
141         assertNull(groups.find(group1.getName()));
142         assertEquals(0, groups.list().size());
143         checkGroupCount(0);
144     }
145   
146     /**
147      * Removes a non-existing group. Verifies that no groups are
148      * removed an that the return value is true. 
149      *
150      */
151     public void testRemoveNonExistingGroup() throws SQLException { 
152         Group group1 = new Group("group1"); 
153         groups.add(group1); 
154         checkGroupCount(1);
155         Group nonExistingGroup = new Group("group2"); 
156         nonExistingGroup.setPrimaryKey(new Long(1000));
157         nonExistingGroup.setPersistedVersion(1000);
158         assertFalse(groups.remove(nonExistingGroup));
159         assertTrue(groups.contains(group1)); 
160         assertEquals(1, groups.list().size());
161         checkGroupCount(1); 
162     }
163     
164     /**
165      * Adds a number of groups to the set and verifies that list() 
166      * returns them all. 
167      *
168      */
169     public void testList() throws SQLException { 
170         Group group1 = new Group("group1"); 
171         Group group2 = new Group("group2"); 
172         Group group3 = new Group("group3"); 
173         assertTrue(groups.add(group1)); 
174         assertTrue(groups.add(group2)); 
175         assertTrue(groups.add(group3));
176         
177         checkGroupExists(group1.getName()); 
178         checkGroupExists(group2.getName()); 
179         checkGroupExists(group3.getName()); 
180         
181         Set<Group> set = groups.list(); 
182         assertTrue(set.contains(group1)); 
183         assertTrue(set.contains(group2)); 
184         assertTrue(set.contains(group3));
185         
186         checkGroupCount(3); 
187     }
188 }