1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.acl.basic.jdbc;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.PopulatedDatabase;
21  import org.acegisecurity.acl.basic.AclObjectIdentity;
22  import org.acegisecurity.acl.basic.BasicAclEntry;
23  import org.acegisecurity.acl.basic.NamedEntityObjectIdentity;
24  
25  import org.springframework.jdbc.object.MappingSqlQuery;
26  
27  import java.sql.ResultSet;
28  import java.sql.SQLException;
29  
30  
31  /***
32   * Tests {@link JdbcDaoImpl}.
33   *
34   * @author Ben Alex
35   * @version $Id: JdbcDaoImplTests.java,v 1.4 2005/11/17 00:56:48 benalex Exp $
36   */
37  public class JdbcDaoImplTests extends TestCase {
38      //~ Static fields/initializers =============================================
39  
40      public static final String OBJECT_IDENTITY = "org.acegisecurity.acl.DomainObject";
41  
42      //~ Constructors ===========================================================
43  
44      public JdbcDaoImplTests() {
45          super();
46      }
47  
48      public JdbcDaoImplTests(String arg0) {
49          super(arg0);
50      }
51  
52      //~ Methods ================================================================
53  
54      public final void setUp() throws Exception {
55          super.setUp();
56      }
57  
58      public static void main(String[] args) {
59          junit.textui.TestRunner.run(JdbcDaoImplTests.class);
60      }
61  
62      public void testExceptionThrownIfBasicAclEntryClassNotFound()
63          throws Exception {
64          JdbcDaoImpl dao = makePopulatedJdbcDao();
65          AclObjectIdentity identity = new NamedEntityObjectIdentity(OBJECT_IDENTITY,
66                  "7");
67  
68          try {
69              dao.getAcls(identity);
70              fail("Should have thrown IllegalArgumentException");
71          } catch (IllegalArgumentException expected) {
72              assertTrue(true);
73          }
74      }
75  
76      public void testGetsEntriesWhichExistInDatabaseAndHaveAcls()
77          throws Exception {
78          JdbcDaoImpl dao = makePopulatedJdbcDao();
79          AclObjectIdentity identity = new NamedEntityObjectIdentity(OBJECT_IDENTITY,
80                  "2");
81          BasicAclEntry[] acls = dao.getAcls(identity);
82          assertEquals(2, acls.length);
83      }
84  
85      public void testGetsEntriesWhichExistInDatabaseButHaveNoAcls()
86          throws Exception {
87          JdbcDaoImpl dao = makePopulatedJdbcDao();
88          AclObjectIdentity identity = new NamedEntityObjectIdentity(OBJECT_IDENTITY,
89                  "5");
90          BasicAclEntry[] acls = dao.getAcls(identity);
91          assertEquals(1, acls.length);
92          assertEquals(JdbcDaoImpl.RECIPIENT_USED_FOR_INHERITENCE_MARKER,
93              acls[0].getRecipient());
94      }
95  
96      public void testGetsEntriesWhichHaveNoParent() throws Exception {
97          JdbcDaoImpl dao = makePopulatedJdbcDao();
98          AclObjectIdentity identity = new NamedEntityObjectIdentity(OBJECT_IDENTITY,
99                  "1");
100         BasicAclEntry[] acls = dao.getAcls(identity);
101         assertEquals(1, acls.length);
102         assertNull(acls[0].getAclObjectParentIdentity());
103     }
104 
105     public void testGettersSetters() throws Exception {
106         JdbcDaoImpl dao = makePopulatedJdbcDao();
107         dao.setAclsByObjectIdentity(new MockMappingSqlQuery());
108         assertNotNull(dao.getAclsByObjectIdentity());
109 
110         dao.setAclsByObjectIdentityQuery("foo");
111         assertEquals("foo", dao.getAclsByObjectIdentityQuery());
112 
113         dao.setObjectPropertiesQuery("foobar");
114         assertEquals("foobar", dao.getObjectPropertiesQuery());
115     }
116 
117     public void testNullReturnedIfEntityNotFound() throws Exception {
118         JdbcDaoImpl dao = makePopulatedJdbcDao();
119         AclObjectIdentity identity = new NamedEntityObjectIdentity(OBJECT_IDENTITY,
120                 "NOT_VALID_ID");
121         BasicAclEntry[] result = dao.getAcls(identity);
122         assertNull(result);
123     }
124 
125     public void testReturnsNullForUnNamedEntityObjectIdentity()
126         throws Exception {
127         JdbcDaoImpl dao = new JdbcDaoImpl();
128         AclObjectIdentity identity = new AclObjectIdentity() {}
129         ;
130 
131         assertNull(dao.getAcls(identity));
132     }
133 
134     private JdbcDaoImpl makePopulatedJdbcDao() throws Exception {
135         JdbcDaoImpl dao = new JdbcDaoImpl();
136         dao.setDataSource(PopulatedDatabase.getDataSource());
137         dao.afterPropertiesSet();
138 
139         return dao;
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 }