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:
026: import org.springframework.jdbc.object.MappingSqlQuery;
027:
028: import java.sql.ResultSet;
029: import java.sql.SQLException;
030:
031: /**
032: * Tests {@link JdbcDaoImpl}.
033: *
034: * @author Ben Alex
035: * @version $Id: JdbcDaoImplTests.java 1496 2006-05-23 13:38:33Z benalex $
036: */
037: public class JdbcDaoImplTests extends TestCase {
038: //~ Static fields/initializers =====================================================================================
039:
040: public static final String OBJECT_IDENTITY = "org.acegisecurity.acl.DomainObject";
041:
042: //~ Constructors ===================================================================================================
043:
044: public JdbcDaoImplTests() {
045: super ();
046: }
047:
048: public JdbcDaoImplTests(String arg0) {
049: super (arg0);
050: }
051:
052: //~ Methods ========================================================================================================
053:
054: public static void main(String[] args) {
055: junit.textui.TestRunner.run(JdbcDaoImplTests.class);
056: }
057:
058: private JdbcDaoImpl makePopulatedJdbcDao() throws Exception {
059: JdbcDaoImpl dao = new JdbcDaoImpl();
060: dao.setDataSource(PopulatedDatabase.getDataSource());
061: dao.afterPropertiesSet();
062:
063: return dao;
064: }
065:
066: public final void setUp() throws Exception {
067: super .setUp();
068: }
069:
070: public void testExceptionThrownIfBasicAclEntryClassNotFound()
071: throws Exception {
072: JdbcDaoImpl dao = makePopulatedJdbcDao();
073: AclObjectIdentity identity = new NamedEntityObjectIdentity(
074: OBJECT_IDENTITY, "7");
075:
076: try {
077: dao.getAcls(identity);
078: fail("Should have thrown IllegalArgumentException");
079: } catch (IllegalArgumentException expected) {
080: assertTrue(true);
081: }
082: }
083:
084: public void testGetsEntriesWhichExistInDatabaseAndHaveAcls()
085: throws Exception {
086: JdbcDaoImpl dao = makePopulatedJdbcDao();
087: AclObjectIdentity identity = new NamedEntityObjectIdentity(
088: OBJECT_IDENTITY, "2");
089: BasicAclEntry[] acls = dao.getAcls(identity);
090: assertEquals(2, acls.length);
091: }
092:
093: public void testGetsEntriesWhichExistInDatabaseButHaveNoAcls()
094: throws Exception {
095: JdbcDaoImpl dao = makePopulatedJdbcDao();
096: AclObjectIdentity identity = new NamedEntityObjectIdentity(
097: OBJECT_IDENTITY, "5");
098: BasicAclEntry[] acls = dao.getAcls(identity);
099: assertEquals(1, acls.length);
100: assertEquals(JdbcDaoImpl.RECIPIENT_USED_FOR_INHERITENCE_MARKER,
101: acls[0].getRecipient());
102: }
103:
104: public void testGetsEntriesWhichHaveNoParent() throws Exception {
105: JdbcDaoImpl dao = makePopulatedJdbcDao();
106: AclObjectIdentity identity = new NamedEntityObjectIdentity(
107: OBJECT_IDENTITY, "1");
108: BasicAclEntry[] acls = dao.getAcls(identity);
109: assertEquals(1, acls.length);
110: assertNull(acls[0].getAclObjectParentIdentity());
111: }
112:
113: public void testGettersSetters() throws Exception {
114: JdbcDaoImpl dao = makePopulatedJdbcDao();
115: dao.setAclsByObjectIdentity(new MockMappingSqlQuery());
116: assertNotNull(dao.getAclsByObjectIdentity());
117:
118: dao.setAclsByObjectIdentityQuery("foo");
119: assertEquals("foo", dao.getAclsByObjectIdentityQuery());
120:
121: dao.setObjectPropertiesQuery("foobar");
122: assertEquals("foobar", dao.getObjectPropertiesQuery());
123: }
124:
125: public void testNullReturnedIfEntityNotFound() throws Exception {
126: JdbcDaoImpl dao = makePopulatedJdbcDao();
127: AclObjectIdentity identity = new NamedEntityObjectIdentity(
128: OBJECT_IDENTITY, "NOT_VALID_ID");
129: BasicAclEntry[] result = dao.getAcls(identity);
130: assertNull(result);
131: }
132:
133: public void testReturnsNullForUnNamedEntityObjectIdentity()
134: throws Exception {
135: JdbcDaoImpl dao = new JdbcDaoImpl();
136: AclObjectIdentity identity = new AclObjectIdentity() {
137: };
138:
139: assertNull(dao.getAcls(identity));
140: }
141:
142: //~ Inner Classes ==================================================================================================
143:
144: private class MockMappingSqlQuery extends MappingSqlQuery {
145: protected Object mapRow(ResultSet arg0, int arg1)
146: throws SQLException {
147: return null;
148: }
149: }
150: }
|