001: package com.ecyrd.jspwiki.auth.authorize;
002:
003: import java.security.Principal;
004: import java.util.Properties;
005:
006: import junit.framework.TestCase;
007:
008: import com.ecyrd.jspwiki.TestEngine;
009: import com.ecyrd.jspwiki.WikiEngine;
010: import com.ecyrd.jspwiki.WikiException;
011: import com.ecyrd.jspwiki.auth.NoSuchPrincipalException;
012: import com.ecyrd.jspwiki.auth.WikiPrincipal;
013: import com.ecyrd.jspwiki.auth.WikiSecurityException;
014:
015: /**
016: * @author Andrew Jaquith
017: */
018: public class XMLGroupDatabaseTest extends TestCase {
019:
020: private XMLGroupDatabase m_db;
021:
022: private String m_wiki;
023:
024: /**
025: * @see junit.framework.TestCase#setUp()
026: */
027: protected void setUp() throws Exception {
028: super .setUp();
029: Properties props = new Properties();
030: props.load(TestEngine.findTestProperties());
031: WikiEngine engine = new TestEngine(props);
032: m_db = new XMLGroupDatabase();
033: m_db.initialize(engine, props);
034: m_wiki = engine.getApplicationName();
035: }
036:
037: public void testDelete() throws WikiException {
038: // First, count the number of groups in the db now.
039: int oldUserCount = m_db.groups().length;
040:
041: // Create a new group with random name
042: String name = "TestGroup"
043: + String.valueOf(System.currentTimeMillis());
044: Group group = new Group(name, m_wiki);
045: Principal al = new WikiPrincipal("Al");
046: Principal bob = new WikiPrincipal("Bob");
047: group.add(al);
048: group.add(bob);
049: m_db.save(group, new WikiPrincipal("Tester"));
050:
051: // Make sure the profile saved successfully
052: group = backendGroup(name);
053: assertEquals(name, group.getName());
054: assertEquals(oldUserCount + 1, m_db.groups().length);
055:
056: // Now delete the profile; should be back to old count
057: m_db.delete(group);
058: assertEquals(oldUserCount, m_db.groups().length);
059: }
060:
061: public void testGroups() throws WikiSecurityException {
062: // Test file has 4 groups in it: TV, Literature, Art, and Admin
063: Group[] groups = m_db.groups();
064: assertEquals(4, groups.length);
065:
066: Group group;
067:
068: // Group TV has 3 members
069: group = backendGroup("TV");
070: assertEquals("TV", group.getName());
071: assertEquals(3, group.members().length);
072:
073: // Group Literature has 2 members
074: group = backendGroup("Literature");
075: assertEquals("Literature", group.getName());
076: assertEquals(2, group.members().length);
077:
078: // Group Art has no members
079: group = backendGroup("Art");
080: assertEquals("Art", group.getName());
081: assertEquals(0, group.members().length);
082:
083: // Group Admin has 1 member (Administrator)
084: group = backendGroup("Admin");
085: assertEquals("Admin", group.getName());
086: assertEquals(1, group.members().length);
087: assertEquals("Administrator", group.members()[0].getName());
088:
089: // Group Archaeology doesn't exist
090: try {
091: group = backendGroup("Archaeology");
092: // We should never get here
093: assertTrue(false);
094: } catch (NoSuchPrincipalException e) {
095: assertTrue(true);
096: }
097: }
098:
099: public void testSave() throws Exception {
100: // Create a new group with random name
101: String name = "TestGroup"
102: + String.valueOf(System.currentTimeMillis());
103: Group group = new Group(name, m_wiki);
104: Principal al = new WikiPrincipal("Al");
105: Principal bob = new WikiPrincipal("Bob");
106: Principal cookie = new WikiPrincipal("Cookie");
107: group.add(al);
108: group.add(bob);
109: group.add(cookie);
110: m_db.save(group, new WikiPrincipal("Tester"));
111:
112: // Make sure the profile saved successfully
113: group = backendGroup(name);
114: assertEquals(name, group.getName());
115: assertEquals(3, group.members().length);
116: assertTrue(group.isMember(new WikiPrincipal("Al")));
117: assertTrue(group.isMember(new WikiPrincipal("Bob")));
118: assertTrue(group.isMember(new WikiPrincipal("Cookie")));
119:
120: // The back-end should have timestamped the create/modify fields
121: assertNotNull(group.getCreator());
122: assertEquals("Tester", group.getCreator());
123: assertNotNull(group.getCreated());
124: assertNotNull(group.getModifier());
125: assertEquals("Tester", group.getModifier());
126: assertNotNull(group.getLastModified());
127: assertNotSame(group.getCreated(), group.getLastModified());
128:
129: // Remove the group
130: m_db.delete(group);
131: }
132:
133: public void testResave() throws Exception {
134: // Create a new group with random name & 3 members
135: String name = "TestGroup"
136: + String.valueOf(System.currentTimeMillis());
137: Group group = new Group(name, m_wiki);
138: Principal al = new WikiPrincipal("Al");
139: Principal bob = new WikiPrincipal("Bob");
140: Principal cookie = new WikiPrincipal("Cookie");
141: group.add(al);
142: group.add(bob);
143: group.add(cookie);
144: m_db.save(group, new WikiPrincipal("Tester"));
145:
146: // Make sure the profile saved successfully
147: group = backendGroup(name);
148: assertEquals(name, group.getName());
149:
150: // Modify the members by adding the group; re-add Al while we're at it
151: Principal dave = new WikiPrincipal("Dave");
152: group.add(al);
153: group.add(dave);
154: m_db.save(group, new WikiPrincipal("SecondTester"));
155:
156: // We should see 4 members and new timestamp info
157: Principal[] members = group.members();
158: assertEquals(4, members.length);
159: assertNotNull(group.getCreator());
160: assertEquals("Tester", group.getCreator());
161: assertNotNull(group.getCreated());
162: assertNotNull(group.getModifier());
163: assertEquals("SecondTester", group.getModifier());
164: assertNotNull(group.getLastModified());
165:
166: // Check the back-end; We should see the same thing
167: group = backendGroup(name);
168: members = group.members();
169: assertEquals(4, members.length);
170: assertNotNull(group.getCreator());
171: assertEquals("Tester", group.getCreator());
172: assertNotNull(group.getCreated());
173: assertNotNull(group.getModifier());
174: assertEquals("SecondTester", group.getModifier());
175: assertNotNull(group.getLastModified());
176:
177: // Remove the group
178: m_db.delete(group);
179: }
180:
181: private Group backendGroup(String name)
182: throws WikiSecurityException {
183: Group[] groups = m_db.groups();
184: for (int i = 0; i < groups.length; i++) {
185: Group group = groups[i];
186: if (group.getName().equals(name)) {
187: return group;
188: }
189: }
190: throw new NoSuchPrincipalException("No group named " + name);
191: }
192: }
|