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