001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.acl.basic.jdbc;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.PopulatedDatabase;
021:
022: import org.acegisecurity.acl.basic.AclObjectIdentity;
023: import org.acegisecurity.acl.basic.BasicAclEntry;
024: import org.acegisecurity.acl.basic.NamedEntityObjectIdentity;
025: import org.acegisecurity.acl.basic.SimpleAclEntry;
026:
027: import org.springframework.dao.DataIntegrityViolationException;
028: import org.springframework.dao.DataRetrievalFailureException;
029:
030: import org.springframework.jdbc.object.MappingSqlQuery;
031:
032: import java.sql.ResultSet;
033: import java.sql.SQLException;
034:
035: /**
036: * Tests {@link JdbcExtendedDaoImpl}.
037: *
038: * @author Ben Alex
039: * @version $Id: JdbcExtendedDaoImplTests.java 1496 2006-05-23 13:38:33Z benalex $
040: */
041: public class JdbcExtendedDaoImplTests extends TestCase {
042: //~ Static fields/initializers =====================================================================================
043:
044: public static final String OBJECT_IDENTITY = "org.acegisecurity.acl.DomainObject";
045:
046: //~ Constructors ===================================================================================================
047:
048: public JdbcExtendedDaoImplTests() {
049: super ();
050: }
051:
052: public JdbcExtendedDaoImplTests(String arg0) {
053: super (arg0);
054: }
055:
056: //~ Methods ========================================================================================================
057:
058: public static void main(String[] args) {
059: junit.textui.TestRunner.run(JdbcExtendedDaoImplTests.class);
060: }
061:
062: private JdbcExtendedDaoImpl makePopulatedJdbcDao() throws Exception {
063: JdbcExtendedDaoImpl dao = new JdbcExtendedDaoImpl();
064: dao.setDataSource(PopulatedDatabase.getDataSource());
065: dao.afterPropertiesSet();
066:
067: return dao;
068: }
069:
070: public final void setUp() throws Exception {
071: super .setUp();
072: }
073:
074: public void testChangeMask() throws Exception {
075: JdbcExtendedDaoImpl dao = makePopulatedJdbcDao();
076: AclObjectIdentity identity = new NamedEntityObjectIdentity(
077: OBJECT_IDENTITY, "204");
078: AclObjectIdentity parentIdentity = new NamedEntityObjectIdentity(
079: OBJECT_IDENTITY, "1");
080:
081: // Create a BasicAclEntry for this AclObjectIdentity
082: SimpleAclEntry simpleAcl1 = new SimpleAclEntry("marissa",
083: identity, parentIdentity, SimpleAclEntry.CREATE);
084: dao.create(simpleAcl1);
085:
086: // Create another BasicAclEntry for this AclObjectIdentity
087: SimpleAclEntry simpleAcl2 = new SimpleAclEntry("scott",
088: identity, parentIdentity, SimpleAclEntry.READ);
089: dao.create(simpleAcl2);
090:
091: // Check creation was successful
092: BasicAclEntry[] acls = dao.getAcls(identity);
093: assertEquals(2, acls.length);
094: assertEquals(SimpleAclEntry.CREATE, acls[0].getMask());
095: assertEquals(SimpleAclEntry.READ, acls[1].getMask());
096:
097: // Attempt to change mask
098: dao.changeMask(identity, "marissa", new Integer(
099: SimpleAclEntry.ADMINISTRATION));
100: dao.changeMask(identity, "scott", new Integer(
101: SimpleAclEntry.NOTHING));
102: acls = dao.getAcls(identity);
103: assertEquals(2, acls.length);
104: assertEquals("marissa", acls[0].getRecipient());
105: assertEquals(SimpleAclEntry.ADMINISTRATION, acls[0].getMask());
106: assertEquals("scott", acls[1].getRecipient());
107: assertEquals(SimpleAclEntry.NOTHING, acls[1].getMask());
108: }
109:
110: public void testChangeMaskThrowsExceptionWhenExistingRecordNotFound()
111: throws Exception {
112: JdbcExtendedDaoImpl dao = makePopulatedJdbcDao();
113: AclObjectIdentity identity = new NamedEntityObjectIdentity(
114: OBJECT_IDENTITY, "205");
115: AclObjectIdentity parentIdentity = new NamedEntityObjectIdentity(
116: OBJECT_IDENTITY, "1");
117:
118: // Create at least one record for this AclObjectIdentity
119: SimpleAclEntry simpleAcl1 = new SimpleAclEntry("marissa",
120: identity, parentIdentity, SimpleAclEntry.CREATE);
121: dao.create(simpleAcl1);
122:
123: // Attempt to change mask, but for a recipient we don't have
124: try {
125: dao.changeMask(identity, "scott", new Integer(
126: SimpleAclEntry.ADMINISTRATION));
127: fail("Should have thrown DataRetrievalFailureException");
128: } catch (DataRetrievalFailureException expected) {
129: assertTrue(true);
130: }
131: }
132:
133: public void testConvertAclObjectIdentity() throws Exception {
134: JdbcExtendedDaoImpl dao = makePopulatedJdbcDao();
135:
136: try {
137: dao
138: .convertAclObjectIdentityToString(new AclObjectIdentity() {
139: // not a NamedEntityObjectIdentity
140: });
141: fail("Should have thrown IllegalArgumentException");
142: } catch (IllegalArgumentException expected) {
143: assertTrue(true);
144: }
145: }
146:
147: public void testCreationOfIdentityThenAclInSeparateInvocations()
148: throws Exception {
149: JdbcExtendedDaoImpl dao = makePopulatedJdbcDao();
150: AclObjectIdentity identity = new NamedEntityObjectIdentity(
151: OBJECT_IDENTITY, "206");
152: AclObjectIdentity parentIdentity = new NamedEntityObjectIdentity(
153: OBJECT_IDENTITY, "1");
154:
155: // Create just the object identity (NB: recipient and mask is null)
156: SimpleAclEntry simpleAcl1 = new SimpleAclEntry();
157: simpleAcl1.setAclObjectIdentity(identity);
158: simpleAcl1.setAclObjectParentIdentity(parentIdentity);
159: dao.create(simpleAcl1);
160:
161: // Delete it
162: dao.delete(identity);
163: }
164:
165: public void testDeletionOfAllRecipients() throws Exception {
166: JdbcExtendedDaoImpl dao = makePopulatedJdbcDao();
167: AclObjectIdentity identity = new NamedEntityObjectIdentity(
168: OBJECT_IDENTITY, "203");
169:
170: // Create a BasicAclEntry for this AclObjectIdentity
171: SimpleAclEntry simpleAcl1 = new SimpleAclEntry("marissa",
172: identity, null, SimpleAclEntry.CREATE);
173: dao.create(simpleAcl1);
174:
175: // Create another BasicAclEntry for this AclObjectIdentity
176: SimpleAclEntry simpleAcl2 = new SimpleAclEntry("scott",
177: identity, null, SimpleAclEntry.READ);
178: dao.create(simpleAcl2);
179:
180: // Check creation was successful
181: BasicAclEntry[] acls = dao.getAcls(identity);
182: assertEquals(2, acls.length);
183:
184: // Attempt deletion and check delete successful
185: dao.delete(identity);
186: assertNull(dao.getAcls(identity));
187: }
188:
189: public void testDeletionOfSpecificRecipient() throws Exception {
190: JdbcExtendedDaoImpl dao = makePopulatedJdbcDao();
191: AclObjectIdentity identity = new NamedEntityObjectIdentity(
192: OBJECT_IDENTITY, "202");
193: AclObjectIdentity parentIdentity = new NamedEntityObjectIdentity(
194: OBJECT_IDENTITY, "1");
195:
196: // Create a BasicAclEntry for this AclObjectIdentity
197: SimpleAclEntry simpleAcl1 = new SimpleAclEntry("marissa",
198: identity, parentIdentity, SimpleAclEntry.CREATE);
199: dao.create(simpleAcl1);
200:
201: // Create another BasicAclEntry for this AclObjectIdentity
202: SimpleAclEntry simpleAcl2 = new SimpleAclEntry("scott",
203: identity, parentIdentity, SimpleAclEntry.READ);
204: dao.create(simpleAcl2);
205:
206: // Check creation was successful
207: BasicAclEntry[] acls = dao.getAcls(identity);
208: assertEquals(2, acls.length);
209:
210: // Attempt deletion and check delete successful
211: dao.delete(identity, "scott");
212: acls = dao.getAcls(identity);
213: assertEquals(1, acls.length);
214: assertEquals(simpleAcl1.getRecipient(), acls[0].getRecipient());
215: }
216:
217: public void testGettersSetters() throws Exception {
218: JdbcExtendedDaoImpl dao = makePopulatedJdbcDao();
219:
220: assertNotNull(dao.getAclObjectIdentityDelete());
221: dao.setAclObjectIdentityDelete(null);
222: assertNull(dao.getAclObjectIdentityDelete());
223:
224: assertNotNull(dao.getAclObjectIdentityInsert());
225: dao.setAclObjectIdentityInsert(null);
226: assertNull(dao.getAclObjectIdentityInsert());
227:
228: assertNotNull(dao.getAclPermissionDelete());
229: dao.setAclPermissionDelete(null);
230: assertNull(dao.getAclPermissionDelete());
231:
232: assertNotNull(dao.getAclPermissionInsert());
233: dao.setAclPermissionInsert(null);
234: assertNull(dao.getAclPermissionInsert());
235:
236: assertNotNull(dao.getAclPermissionUpdate());
237: dao.setAclPermissionUpdate(null);
238: assertNull(dao.getAclPermissionUpdate());
239:
240: assertNotNull(dao.getAclsByObjectIdentity());
241: dao.setAclsByObjectIdentity(null);
242: assertNull(dao.getAclsByObjectIdentity());
243:
244: assertNotNull(dao.getLookupPermissionIdMapping());
245: dao.setLookupPermissionIdMapping(null);
246: assertNull(dao.getLookupPermissionIdMapping());
247:
248: assertNotNull(dao.getAclObjectIdentityDeleteStatement());
249: dao.setAclObjectIdentityDeleteStatement("SELECT ...");
250: assertEquals("SELECT ...", dao
251: .getAclObjectIdentityDeleteStatement());
252:
253: assertNotNull(dao.getAclObjectIdentityInsertStatement());
254: dao.setAclObjectIdentityInsertStatement("SELECT ...");
255: assertEquals("SELECT ...", dao
256: .getAclObjectIdentityInsertStatement());
257:
258: assertNotNull(dao.getAclPermissionDeleteStatement());
259: dao.setAclPermissionDeleteStatement("SELECT ...");
260: assertEquals("SELECT ...", dao
261: .getAclPermissionDeleteStatement());
262:
263: assertNotNull(dao.getAclPermissionInsertStatement());
264: dao.setAclPermissionInsertStatement("SELECT ...");
265: assertEquals("SELECT ...", dao
266: .getAclPermissionInsertStatement());
267:
268: assertNotNull(dao.getAclPermissionUpdateStatement());
269: dao.setAclPermissionUpdateStatement("SELECT ...");
270: assertEquals("SELECT ...", dao
271: .getAclPermissionUpdateStatement());
272:
273: assertNotNull(dao.getAclsByObjectIdentityQuery());
274: dao.setAclsByObjectIdentityQuery("SELECT ...");
275: assertEquals("SELECT ...", dao.getAclsByObjectIdentityQuery());
276:
277: assertNotNull(dao.getLookupPermissionIdQuery());
278: dao.setLookupPermissionIdQuery("SELECT ...");
279: assertEquals("SELECT ...", dao.getLookupPermissionIdQuery());
280: }
281:
282: public void testNormalCreationAndDuplicateDetection()
283: throws Exception {
284: JdbcExtendedDaoImpl dao = makePopulatedJdbcDao();
285: AclObjectIdentity identity = new NamedEntityObjectIdentity(
286: OBJECT_IDENTITY, "200");
287: AclObjectIdentity parentIdentity = new NamedEntityObjectIdentity(
288: OBJECT_IDENTITY, "1");
289:
290: // Create a BasicAclEntry for this AclObjectIdentity
291: SimpleAclEntry simpleAcl1 = new SimpleAclEntry("marissa",
292: identity, parentIdentity, SimpleAclEntry.CREATE);
293: dao.create(simpleAcl1);
294:
295: // Create another BasicAclEntry for this AclObjectIdentity
296: SimpleAclEntry simpleAcl2 = new SimpleAclEntry("scott",
297: identity, parentIdentity, SimpleAclEntry.READ);
298: dao.create(simpleAcl2);
299:
300: // Check creation was successful
301: BasicAclEntry[] acls = dao.getAcls(identity);
302: assertEquals(2, acls.length);
303: assertEquals(simpleAcl1.getRecipient(), acls[0].getRecipient());
304: assertEquals(simpleAcl1.getMask(), acls[0].getMask());
305: assertEquals(simpleAcl2.getRecipient(), acls[1].getRecipient());
306: assertEquals(simpleAcl2.getMask(), acls[1].getMask());
307:
308: // Check it rejects an attempt to create another identical entry
309: try {
310: dao.create(simpleAcl1);
311: fail("Should have thrown DataIntegrityViolationException");
312: } catch (DataIntegrityViolationException expected) {
313: assertTrue(true);
314: }
315: }
316:
317: public void testRejectsInvalidParent() throws Exception {
318: JdbcExtendedDaoImpl dao = makePopulatedJdbcDao();
319: AclObjectIdentity identity = new NamedEntityObjectIdentity(
320: OBJECT_IDENTITY, "201");
321: AclObjectIdentity parentIdentity = new NamedEntityObjectIdentity(
322: OBJECT_IDENTITY, "987987987987986");
323: SimpleAclEntry simpleAcl = new SimpleAclEntry("marissa",
324: identity, parentIdentity, SimpleAclEntry.CREATE);
325:
326: try {
327: dao.create(simpleAcl);
328: fail("Should have thrown DataRetrievalFailureException");
329: } catch (DataRetrievalFailureException expected) {
330: assertTrue(true);
331: }
332: }
333:
334: //~ Inner Classes ==================================================================================================
335:
336: private class MockMappingSqlQuery extends MappingSqlQuery {
337: protected Object mapRow(ResultSet arg0, int arg1)
338: throws SQLException {
339: return null;
340: }
341: }
342: }
|