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.userdetails.jdbc;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.PopulatedDatabase;
021:
022: import org.acegisecurity.userdetails.UserDetails;
023: import org.acegisecurity.userdetails.UsernameNotFoundException;
024:
025: import org.springframework.jdbc.object.MappingSqlQuery;
026:
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029:
030: import java.util.HashSet;
031:
032: /**
033: * Tests {@link JdbcDaoImpl}.
034: *
035: * @author Ben Alex
036: * @version $Id: JdbcDaoTests.java 1496 2006-05-23 13:38:33Z benalex $
037: */
038: public class JdbcDaoTests extends TestCase {
039: //~ Constructors ===================================================================================================
040:
041: public JdbcDaoTests() {
042: super ();
043: }
044:
045: public JdbcDaoTests(String arg0) {
046: super (arg0);
047: }
048:
049: //~ Methods ========================================================================================================
050:
051: public static void main(String[] args) {
052: junit.textui.TestRunner.run(JdbcDaoTests.class);
053: }
054:
055: private JdbcDaoImpl makePopulatedJdbcDao() throws Exception {
056: JdbcDaoImpl dao = new JdbcDaoImpl();
057: dao.setDataSource(PopulatedDatabase.getDataSource());
058: dao.afterPropertiesSet();
059:
060: return dao;
061: }
062:
063: private JdbcDaoImpl makePopulatedJdbcDaoWithRolePrefix()
064: throws Exception {
065: JdbcDaoImpl dao = new JdbcDaoImpl();
066: dao.setDataSource(PopulatedDatabase.getDataSource());
067: dao.setRolePrefix("ARBITRARY_PREFIX_");
068: dao.afterPropertiesSet();
069:
070: return dao;
071: }
072:
073: public final void setUp() throws Exception {
074: super .setUp();
075: }
076:
077: public void testCheckDaoAccessUserSuccess() throws Exception {
078: JdbcDaoImpl dao = makePopulatedJdbcDao();
079: UserDetails user = dao.loadUserByUsername("marissa");
080: assertEquals("marissa", user.getUsername());
081: assertEquals("koala", user.getPassword());
082: assertTrue(user.isEnabled());
083:
084: HashSet authorities = new HashSet(2);
085: authorities.add(user.getAuthorities()[0].getAuthority());
086: authorities.add(user.getAuthorities()[1].getAuthority());
087: assertTrue(authorities.contains("ROLE_TELLER"));
088: assertTrue(authorities.contains("ROLE_SUPERVISOR"));
089: }
090:
091: public void testCheckDaoOnlyReturnsGrantedAuthoritiesGrantedToUser()
092: throws Exception {
093: JdbcDaoImpl dao = makePopulatedJdbcDao();
094: UserDetails user = dao.loadUserByUsername("scott");
095: assertEquals("ROLE_TELLER", user.getAuthorities()[0]
096: .getAuthority());
097: assertEquals(1, user.getAuthorities().length);
098: }
099:
100: public void testCheckDaoReturnsCorrectDisabledProperty()
101: throws Exception {
102: JdbcDaoImpl dao = makePopulatedJdbcDao();
103: UserDetails user = dao.loadUserByUsername("peter");
104: assertTrue(!user.isEnabled());
105: }
106:
107: public void testGettersSetters() {
108: JdbcDaoImpl dao = new JdbcDaoImpl();
109: dao.setAuthoritiesByUsernameQuery("SELECT * FROM FOO");
110: assertEquals("SELECT * FROM FOO", dao
111: .getAuthoritiesByUsernameQuery());
112:
113: dao.setUsersByUsernameQuery("SELECT USERS FROM FOO");
114: assertEquals("SELECT USERS FROM FOO", dao
115: .getUsersByUsernameQuery());
116: }
117:
118: public void testLookupFailsIfUserHasNoGrantedAuthorities()
119: throws Exception {
120: JdbcDaoImpl dao = makePopulatedJdbcDao();
121:
122: try {
123: dao.loadUserByUsername("cooper");
124: fail("Should have thrown UsernameNotFoundException");
125: } catch (UsernameNotFoundException expected) {
126: assertEquals("User has no GrantedAuthority", expected
127: .getMessage());
128: }
129: }
130:
131: public void testLookupFailsWithWrongUsername() throws Exception {
132: JdbcDaoImpl dao = makePopulatedJdbcDao();
133:
134: try {
135: dao.loadUserByUsername("UNKNOWN_USER");
136: fail("Should have thrown UsernameNotFoundException");
137: } catch (UsernameNotFoundException expected) {
138: assertTrue(true);
139: }
140: }
141:
142: public void testLookupSuccessWithMixedCase() throws Exception {
143: JdbcDaoImpl dao = makePopulatedJdbcDao();
144: assertEquals("koala", dao.loadUserByUsername("MaRiSSA")
145: .getPassword());
146: assertEquals("wombat", dao.loadUserByUsername("ScOTt")
147: .getPassword());
148: }
149:
150: public void testRolePrefixWorks() throws Exception {
151: JdbcDaoImpl dao = makePopulatedJdbcDaoWithRolePrefix();
152: assertEquals("ARBITRARY_PREFIX_", dao.getRolePrefix());
153:
154: UserDetails user = dao.loadUserByUsername("marissa");
155: assertEquals("marissa", user.getUsername());
156: assertEquals(2, user.getAuthorities().length);
157:
158: HashSet authorities = new HashSet(2);
159: authorities.add(user.getAuthorities()[0].getAuthority());
160: authorities.add(user.getAuthorities()[1].getAuthority());
161: assertTrue(authorities.contains("ARBITRARY_PREFIX_ROLE_TELLER"));
162: assertTrue(authorities
163: .contains("ARBITRARY_PREFIX_ROLE_SUPERVISOR"));
164: }
165:
166: public void testStartupFailsIfDataSourceNotSet() throws Exception {
167: JdbcDaoImpl dao = new JdbcDaoImpl();
168:
169: try {
170: dao.afterPropertiesSet();
171: fail("Should have thrown IllegalArgumentException");
172: } catch (IllegalArgumentException expected) {
173: assertTrue(true);
174: }
175: }
176:
177: public void testStartupFailsIfUserMapSetToNull() throws Exception {
178: JdbcDaoImpl dao = new JdbcDaoImpl();
179:
180: try {
181: dao.setDataSource(null);
182: dao.afterPropertiesSet();
183: fail("Should have thrown IllegalArgumentException");
184: } catch (IllegalArgumentException expected) {
185: assertTrue(true);
186: }
187: }
188:
189: //~ Inner Classes ==================================================================================================
190:
191: private class MockMappingSqlQuery extends MappingSqlQuery {
192: protected Object mapRow(ResultSet arg0, int arg1)
193: throws SQLException {
194: return null;
195: }
196: }
197: }
|