Pārlūkot izejas kodu

Initial commit.

Ben Alex 21 gadi atpakaļ
vecāks
revīzija
0a17d65d37

+ 144 - 0
core/src/test/java/org/acegisecurity/providers/dao/jdbc/JdbcDaoTests.java

@@ -0,0 +1,144 @@
+/* Copyright 2004 Acegi Technology Pty Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.sf.acegisecurity.providers.dao.jdbc;
+
+import junit.framework.TestCase;
+
+import net.sf.acegisecurity.providers.dao.User;
+import net.sf.acegisecurity.providers.dao.UsernameNotFoundException;
+
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+
+
+/**
+ * Tests {@link JdbcDaoImpl}.
+ *
+ * @author Ben Alex
+ * @version $Id$
+ */
+public class JdbcDaoTests extends TestCase {
+    //~ Constructors ===========================================================
+
+    public JdbcDaoTests() {
+        super();
+    }
+
+    public JdbcDaoTests(String arg0) {
+        super(arg0);
+    }
+
+    //~ Methods ================================================================
+
+    public final void setUp() throws Exception {
+        super.setUp();
+    }
+
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(JdbcDaoTests.class);
+    }
+
+    public void testCheckDaoAccessUserSuccess() throws Exception {
+        JdbcDaoImpl dao = makePopulatedJdbcDao();
+        User user = dao.loadUserByUsername("marissa");
+        assertEquals("marissa", user.getUsername());
+        assertEquals("koala", user.getPassword());
+        assertTrue(user.isEnabled());
+        assertEquals("ROLE_TELLER", user.getAuthorities()[0].getAuthority());
+        assertEquals("ROLE_SUPERVISOR", user.getAuthorities()[1].getAuthority());
+        assertEquals(2, user.getAuthorities().length);
+    }
+
+    public void testCheckDaoOnlyReturnsGrantedAuthoritiesGrantedToUser()
+        throws Exception {
+        JdbcDaoImpl dao = makePopulatedJdbcDao();
+        User user = dao.loadUserByUsername("scott");
+        assertEquals("ROLE_TELLER", user.getAuthorities()[0].getAuthority());
+        assertEquals(1, user.getAuthorities().length);
+    }
+
+    public void testCheckDaoReturnsCorrectDisabledProperty()
+        throws Exception {
+        JdbcDaoImpl dao = makePopulatedJdbcDao();
+        User user = dao.loadUserByUsername("peter");
+        assertTrue(!user.isEnabled());
+    }
+
+    public void testLookupFailsIfUserHasNoGrantedAuthorities()
+        throws Exception {
+        JdbcDaoImpl dao = makePopulatedJdbcDao();
+
+        try {
+            dao.loadUserByUsername("cooper");
+            fail("Should have thrown UsernameNotFoundException");
+        } catch (UsernameNotFoundException expected) {
+            assertEquals("User has no GrantedAuthority", expected.getMessage());
+        }
+    }
+
+    public void testLookupFailsWithWrongUsername() throws Exception {
+        JdbcDaoImpl dao = makePopulatedJdbcDao();
+
+        try {
+            dao.loadUserByUsername("UNKNOWN_USER");
+            fail("Should have thrown UsernameNotFoundException");
+        } catch (UsernameNotFoundException expected) {
+            assertTrue(true);
+        }
+    }
+
+    public void testLookupSuccessWithMixedCase() throws Exception {
+        JdbcDaoImpl dao = makePopulatedJdbcDao();
+        assertEquals("koala", dao.loadUserByUsername("MaRiSSA").getPassword());
+        assertEquals("wombat", dao.loadUserByUsername("ScOTt").getPassword());
+    }
+
+    public void testStartupFailsIfDataSourceNotSet() throws Exception {
+        JdbcDaoImpl dao = new JdbcDaoImpl();
+
+        try {
+            dao.afterPropertiesSet();
+            fail("Should have thrown IllegalArgumentException");
+        } catch (IllegalArgumentException expected) {
+            assertTrue(true);
+        }
+    }
+
+    public void testStartupFailsIfUserMapSetToNull() throws Exception {
+        JdbcDaoImpl dao = new JdbcDaoImpl();
+
+        try {
+            dao.setDataSource(null);
+            dao.afterPropertiesSet();
+            fail("Should have thrown IllegalArgumentException");
+        } catch (IllegalArgumentException expected) {
+            assertTrue(true);
+        }
+    }
+
+    private JdbcDaoImpl makePopulatedJdbcDao() throws Exception {
+        DriverManagerDataSource ds = new DriverManagerDataSource();
+        ds.setDriverClassName("org.hsqldb.jdbcDriver");
+        ds.setUrl("jdbc:hsqldb:acegisecuritytest");
+        ds.setUsername("sa");
+        ds.setPassword("");
+
+        JdbcDaoImpl dao = new JdbcDaoImpl();
+        dao.setDataSource(ds);
+        dao.afterPropertiesSet();
+
+        return dao;
+    }
+}

+ 16 - 0
test/acegisecuritytest.properties

@@ -0,0 +1,16 @@
+#HSQL database
+#Mon Mar 29 02:55:13 GMT 2004
+sql.strict_fk=true
+readonly=false
+sql.strong_fk=true
+hsqldb.version=1.7.1
+version=1.7.1
+hsqldb.cache_scale=14
+sql.compare_in_locale=false
+sql.month=true
+hsqldb.log_size=200
+modified=no
+hsqldb.cache_version=1.7.0
+hsqldb.original_version=1.7.1
+hsqldb.compatible_version=1.7.0
+sql.enforce_size=false

+ 85 - 0
test/acegisecuritytest.script

@@ -0,0 +1,85 @@
+CREATE TABLE USERS(USERNAME VARCHAR_IGNORECASE(50) NOT NULL PRIMARY KEY,PASSWORD VARCHAR_IGNORECASE(50) NOT NULL,ENABLED BIT NOT NULL)
+CREATE TABLE AUTHORITIES(USERNAME VARCHAR_IGNORECASE(50) NOT NULL,AUTHORITY VARCHAR_IGNORECASE(50) NOT NULL,CONSTRAINT FK_AUTHORITIES_USERS FOREIGN KEY(USERNAME) REFERENCES USERS(USERNAME))
+CREATE UNIQUE INDEX IX_AUTH_USERNAME ON AUTHORITIES(USERNAME,AUTHORITY)
+GRANT ALL ON CLASS "java.lang.Math" TO PUBLIC
+GRANT ALL ON CLASS "org.hsqldb.Library" TO PUBLIC
+CREATE USER SA PASSWORD "" ADMIN
+SET IGNORECASE TRUE
+CREATE ALIAS DAYNAME FOR "org.hsqldb.Library.dayname"
+CREATE ALIAS SPACE FOR "org.hsqldb.Library.space"
+CREATE ALIAS SUBSTRING FOR "org.hsqldb.Library.substring"
+CREATE ALIAS HEXTORAW FOR "org.hsqldb.Library.hexToRaw"
+CREATE ALIAS SQRT FOR "java.lang.Math.sqrt"
+CREATE ALIAS ABS FOR "org.hsqldb.Library.abs"
+CREATE ALIAS POWER FOR "java.lang.Math.pow"
+CREATE ALIAS CHAR FOR "org.hsqldb.Library.character"
+CREATE ALIAS CONCAT FOR "org.hsqldb.Library.concat"
+CREATE ALIAS PI FOR "org.hsqldb.Library.pi"
+CREATE ALIAS RAWTOHEX FOR "org.hsqldb.Library.rawToHex"
+CREATE ALIAS SECOND FOR "org.hsqldb.Library.second"
+CREATE ALIAS TRUNCATE FOR "org.hsqldb.Library.truncate"
+CREATE ALIAS MONTH FOR "org.hsqldb.Library.month"
+CREATE ALIAS LOWER FOR "org.hsqldb.Library.lcase"
+CREATE ALIAS ATAN2 FOR "java.lang.Math.atan2"
+CREATE ALIAS REPEAT FOR "org.hsqldb.Library.repeat"
+CREATE ALIAS DAYOFMONTH FOR "org.hsqldb.Library.dayofmonth"
+CREATE ALIAS TAN FOR "java.lang.Math.tan"
+CREATE ALIAS RADIANS FOR "java.lang.Math.toRadians"
+CREATE ALIAS FLOOR FOR "java.lang.Math.floor"
+CREATE ALIAS NOW FOR "org.hsqldb.Library.now"
+CREATE ALIAS ACOS FOR "java.lang.Math.acos"
+CREATE ALIAS DAYOFWEEK FOR "org.hsqldb.Library.dayofweek"
+CREATE ALIAS CEILING FOR "java.lang.Math.ceil"
+CREATE ALIAS DAYOFYEAR FOR "org.hsqldb.Library.dayofyear"
+CREATE ALIAS LCASE FOR "org.hsqldb.Library.lcase"
+CREATE ALIAS WEEK FOR "org.hsqldb.Library.week"
+CREATE ALIAS SOUNDEX FOR "org.hsqldb.Library.soundex"
+CREATE ALIAS ASIN FOR "java.lang.Math.asin"
+CREATE ALIAS LOCATE FOR "org.hsqldb.Library.locate"
+CREATE ALIAS EXP FOR "java.lang.Math.exp"
+CREATE ALIAS MONTHNAME FOR "org.hsqldb.Library.monthname"
+CREATE ALIAS YEAR FOR "org.hsqldb.Library.year"
+CREATE ALIAS LEFT FOR "org.hsqldb.Library.left"
+CREATE ALIAS ROUNDMAGIC FOR "org.hsqldb.Library.roundMagic"
+CREATE ALIAS BITOR FOR "org.hsqldb.Library.bitor"
+CREATE ALIAS LTRIM FOR "org.hsqldb.Library.ltrim"
+CREATE ALIAS COT FOR "org.hsqldb.Library.cot"
+CREATE ALIAS COS FOR "java.lang.Math.cos"
+CREATE ALIAS MOD FOR "org.hsqldb.Library.mod"
+CREATE ALIAS SIGN FOR "org.hsqldb.Library.sign"
+CREATE ALIAS DEGREES FOR "java.lang.Math.toDegrees"
+CREATE ALIAS LOG FOR "java.lang.Math.log"
+CREATE ALIAS SIN FOR "java.lang.Math.sin"
+CREATE ALIAS CURTIME FOR "org.hsqldb.Library.curtime"
+CREATE ALIAS DIFFERENCE FOR "org.hsqldb.Library.difference"
+CREATE ALIAS INSERT FOR "org.hsqldb.Library.insert"
+CREATE ALIAS SUBSTR FOR "org.hsqldb.Library.substring"
+CREATE ALIAS DATABASE FOR "org.hsqldb.Library.database"
+CREATE ALIAS MINUTE FOR "org.hsqldb.Library.minute"
+CREATE ALIAS HOUR FOR "org.hsqldb.Library.hour"
+CREATE ALIAS IDENTITY FOR "org.hsqldb.Library.identity"
+CREATE ALIAS QUARTER FOR "org.hsqldb.Library.quarter"
+CREATE ALIAS CURDATE FOR "org.hsqldb.Library.curdate"
+CREATE ALIAS BITAND FOR "org.hsqldb.Library.bitand"
+CREATE ALIAS USER FOR "org.hsqldb.Library.user"
+CREATE ALIAS UCASE FOR "org.hsqldb.Library.ucase"
+CREATE ALIAS RTRIM FOR "org.hsqldb.Library.rtrim"
+CREATE ALIAS LOG10 FOR "org.hsqldb.Library.log10"
+CREATE ALIAS RIGHT FOR "org.hsqldb.Library.right"
+CREATE ALIAS ATAN FOR "java.lang.Math.atan"
+CREATE ALIAS UPPER FOR "org.hsqldb.Library.ucase"
+CREATE ALIAS ASCII FOR "org.hsqldb.Library.ascii"
+CREATE ALIAS RAND FOR "java.lang.Math.random"
+CREATE ALIAS LENGTH FOR "org.hsqldb.Library.length"
+CREATE ALIAS ROUND FOR "org.hsqldb.Library.round"
+CREATE ALIAS REPLACE FOR "org.hsqldb.Library.replace"
+INSERT INTO USERS VALUES('cooper','kookaburra',true)
+INSERT INTO USERS VALUES('dianne','emu',true)
+INSERT INTO USERS VALUES('marissa','koala',true)
+INSERT INTO USERS VALUES('peter','opal',false)
+INSERT INTO USERS VALUES('scott','wombat',true)
+INSERT INTO AUTHORITIES VALUES('marissa','ROLE_TELLER')
+INSERT INTO AUTHORITIES VALUES('marissa','ROLE_SUPERVISOR')
+INSERT INTO AUTHORITIES VALUES('dianne','ROLE_TELLER')
+INSERT INTO AUTHORITIES VALUES('scott','ROLE_TELLER')
+INSERT INTO AUTHORITIES VALUES('peter','ROLE_TELLER')