001: package com.ecyrd.jspwiki.auth.authorize;
002:
003: import java.io.File;
004: import java.security.Principal;
005: import java.sql.Connection;
006: import java.sql.SQLException;
007: import java.util.Properties;
008:
009: import javax.naming.Context;
010: import javax.naming.InitialContext;
011: import javax.sql.DataSource;
012:
013: import junit.framework.TestCase;
014:
015: import com.ecyrd.jspwiki.*;
016: import com.ecyrd.jspwiki.auth.NoSuchPrincipalException;
017: import com.ecyrd.jspwiki.auth.WikiPrincipal;
018: import com.ecyrd.jspwiki.auth.WikiSecurityException;
019:
020: /**
021: * @author Andrew Jaquith
022: */
023: public class JDBCGroupDatabaseTest extends TestCase {
024: private Connection m_conn = null;
025:
026: private JDBCGroupDatabase m_db = null;
027:
028: private String m_wiki;
029:
030: /**
031: * @see junit.framework.TestCase#setUp()
032: */
033: protected void setUp() throws Exception {
034: super .setUp();
035:
036: Properties props = new Properties();
037: props.load(TestEngine.findTestProperties());
038: WikiEngine engine = new TestEngine(props);
039: m_wiki = engine.getApplicationName();
040:
041: // Set up the mock JNDI initial context
042: TestJNDIContext.initialize();
043: Context initCtx = new InitialContext();
044: initCtx.bind("java:comp/env", new TestJNDIContext());
045: Context ctx = (Context) initCtx.lookup("java:comp/env");
046: DataSource ds = new TestJDBCDataSource(new File(
047: "build.properties"));
048: ctx.bind(JDBCGroupDatabase.DEFAULT_GROUPDB_DATASOURCE, ds);
049:
050: // Get the JDBC connection and init tables
051:
052: try {
053: m_conn = ds.getConnection();
054: } catch (SQLException e) {
055: System.err
056: .println("Looks like your database could not be connected to - "
057: + "please make sure that you have started your database "
058: + "(e.g. by running ant hsql-start)");
059:
060: throw (SQLException) e.fillInStackTrace();
061: }
062:
063: // Initialize the user database
064: m_db = new JDBCGroupDatabase();
065: m_db.initialize(engine, new Properties());
066: }
067:
068: public void tearDown() throws Exception {
069: if (m_conn != null) {
070: m_conn.close();
071: }
072: }
073:
074: public void testDelete() throws WikiException {
075: // First, count the number of groups in the db now.
076: int oldUserCount = m_db.groups().length;
077:
078: // Create a new group with random name
079: String name = "TestGroup"
080: + String.valueOf(System.currentTimeMillis());
081: Group group = new Group(name, m_wiki);
082: Principal al = new WikiPrincipal("Al");
083: Principal bob = new WikiPrincipal("Bob");
084: group.add(al);
085: group.add(bob);
086: m_db.save(group, new WikiPrincipal("Tester"));
087:
088: // Make sure the profile saved successfully
089: group = backendGroup(name);
090: assertEquals(name, group.getName());
091: assertEquals(oldUserCount + 1, m_db.groups().length);
092:
093: // Now delete the profile; should be back to old count
094: m_db.delete(group);
095: assertEquals(oldUserCount, m_db.groups().length);
096: }
097:
098: public void testGroups() throws WikiSecurityException {
099: // Test file has 4 groups in it: TV, Literature, Art, and Admin
100: Group[] groups = m_db.groups();
101: assertEquals(4, groups.length);
102:
103: Group group;
104:
105: // Group TV has 3 members
106: group = backendGroup("TV");
107: assertEquals("TV", group.getName());
108: assertEquals(3, group.members().length);
109:
110: // Group Literature has 2 members
111: group = backendGroup("Literature");
112: assertEquals("Literature", group.getName());
113: assertEquals(2, group.members().length);
114:
115: // Group Art has no members
116: group = backendGroup("Art");
117: assertEquals("Art", group.getName());
118: assertEquals(0, group.members().length);
119:
120: // Group Admin has 1 member (Administrator)
121: group = backendGroup("Admin");
122: assertEquals("Admin", group.getName());
123: assertEquals(1, group.members().length);
124: assertEquals("Administrator", group.members()[0].getName());
125:
126: // Group Archaeology doesn't exist
127: try {
128: group = backendGroup("Archaeology");
129: // We should never get here
130: assertTrue(false);
131: } catch (NoSuchPrincipalException e) {
132: assertTrue(true);
133: }
134: }
135:
136: public void testSave() throws Exception {
137: // Create a new group with random name
138: String name = "TestGroup"
139: + String.valueOf(System.currentTimeMillis());
140: Group group = new Group(name, m_wiki);
141: Principal al = new WikiPrincipal("Al");
142: Principal bob = new WikiPrincipal("Bob");
143: Principal cookie = new WikiPrincipal("Cookie");
144: group.add(al);
145: group.add(bob);
146: group.add(cookie);
147: m_db.save(group, new WikiPrincipal("Tester"));
148:
149: // Make sure the profile saved successfully
150: group = backendGroup(name);
151: assertEquals(name, group.getName());
152: assertEquals(3, group.members().length);
153: assertTrue(group.isMember(new WikiPrincipal("Al")));
154: assertTrue(group.isMember(new WikiPrincipal("Bob")));
155: assertTrue(group.isMember(new WikiPrincipal("Cookie")));
156:
157: // The back-end should have timestamped the create/modify fields
158: assertNotNull(group.getCreator());
159: assertEquals("Tester", group.getCreator());
160: assertNotNull(group.getCreated());
161: assertNotNull(group.getModifier());
162: assertEquals("Tester", group.getModifier());
163: assertNotNull(group.getLastModified());
164: assertNotSame(group.getCreated(), group.getLastModified());
165:
166: // Remove the group
167: m_db.delete(group);
168: }
169:
170: public void testResave() throws Exception {
171: // Create a new group with random name & 3 members
172: String name = "TestGroup"
173: + String.valueOf(System.currentTimeMillis());
174: Group group = new Group(name, m_wiki);
175: Principal al = new WikiPrincipal("Al");
176: Principal bob = new WikiPrincipal("Bob");
177: Principal cookie = new WikiPrincipal("Cookie");
178: group.add(al);
179: group.add(bob);
180: group.add(cookie);
181: m_db.save(group, new WikiPrincipal("Tester"));
182:
183: // Make sure the profile saved successfully
184: group = backendGroup(name);
185: assertEquals(name, group.getName());
186:
187: // Modify the members by adding the group; re-add Al while we're at it
188: Principal dave = new WikiPrincipal("Dave");
189: group.add(al);
190: group.add(dave);
191: m_db.save(group, new WikiPrincipal("SecondTester"));
192:
193: // We should see 4 members and new timestamp info
194: Principal[] members = group.members();
195: assertEquals(4, members.length);
196: assertNotNull(group.getCreator());
197: assertEquals("Tester", group.getCreator());
198: assertNotNull(group.getCreated());
199: assertNotNull(group.getModifier());
200: assertEquals("SecondTester", group.getModifier());
201: assertNotNull(group.getLastModified());
202:
203: // Check the back-end; We should see the same thing
204: group = backendGroup(name);
205: members = group.members();
206: assertEquals(4, members.length);
207: assertNotNull(group.getCreator());
208: assertEquals("Tester", group.getCreator());
209: assertNotNull(group.getCreated());
210: assertNotNull(group.getModifier());
211: assertEquals("SecondTester", group.getModifier());
212: assertNotNull(group.getLastModified());
213:
214: // Remove the group
215: m_db.delete(group);
216: }
217:
218: private Group backendGroup(String name)
219: throws WikiSecurityException {
220: Group[] groups = m_db.groups();
221: for (int i = 0; i < groups.length; i++) {
222: Group group = groups[i];
223: if (group.getName().equals(name)) {
224: return group;
225: }
226: }
227: throw new NoSuchPrincipalException("No group named " + name);
228: }
229: }
|