Browse Source

Added TestDataSource class to cut down verbosity of in-memory test databases and to implement DisposableBean, so the database is destroyed when the application context containing it is closed.

Luke Taylor 17 năm trước cách đây
mục cha
commit
28153f2c7f

+ 0 - 1
core/src/main/java/org/springframework/security/config/LdapUserServiceBeanDefinitionParser.java

@@ -7,7 +7,6 @@ import org.springframework.beans.factory.xml.ParserContext;
 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
 import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.beans.factory.config.RuntimeBeanReference;
-import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.util.StringUtils;
 
 import org.w3c.dom.Element;

+ 2 - 8
core/src/test/java/org/springframework/security/PopulatedDatabase.java

@@ -16,7 +16,6 @@
 package org.springframework.security;
 
 import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.datasource.DriverManagerDataSource;
 
 import javax.sql.DataSource;
 
@@ -30,7 +29,7 @@ import javax.sql.DataSource;
 public class PopulatedDatabase {
     //~ Static fields/initializers =====================================================================================
 
-    private static DriverManagerDataSource dataSource = null;
+    private static TestDataSource dataSource = null;
 
     //~ Constructors ===================================================================================================
 
@@ -47,12 +46,7 @@ public class PopulatedDatabase {
     }
 
     private static void setupDataSource() {
-        dataSource = new DriverManagerDataSource();
-        dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
-        dataSource.setUrl("jdbc:hsqldb:mem:springsecuritytest");
-        dataSource.setUsername("sa");
-        dataSource.setPassword("");
-
+        dataSource = new TestDataSource("springsecuritytest");
         JdbcTemplate template = new JdbcTemplate(dataSource);
 
         template.execute(

+ 29 - 0
core/src/test/java/org/springframework/security/TestDataSource.java

@@ -0,0 +1,29 @@
+package org.springframework.security;
+
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.beans.factory.DisposableBean;
+
+/**
+ * A Datasource bean which starts an in-memory HSQL database with the supplied name and
+ * shuts down the database when the application context it is defined in is closed.
+ *
+ * @author Luke Taylor
+ * @version $Id$
+ */
+public class TestDataSource extends DriverManagerDataSource implements DisposableBean {
+    String name;
+
+    public TestDataSource(String databaseName) {
+        name = databaseName;
+        setDriverClassName("org.hsqldb.jdbcDriver");
+        setUrl("jdbc:hsqldb:mem:" + databaseName);
+        setUsername("sa");
+        setPassword("");
+    }
+
+    public void destroy() throws Exception {
+        System.out.println("Shutting down database: " + name);
+        new JdbcTemplate(this).execute("SHUTDOWN");
+    }
+}

+ 10 - 3
core/src/test/java/org/springframework/security/acls/jdbc/BasicLookupStrategyTests.java

@@ -9,14 +9,16 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.junit.AfterClass;
+
 import org.springframework.context.ApplicationContext;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.datasource.DriverManagerDataSource;
 import org.springframework.security.GrantedAuthority;
 import org.springframework.security.GrantedAuthorityImpl;
 import org.springframework.security.MockApplicationContext;
+import org.springframework.security.TestDataSource;
 import org.springframework.security.acls.AuditableAccessControlEntry;
 import org.springframework.security.acls.MutableAcl;
 import org.springframework.security.acls.domain.AclAuthorizationStrategy;
@@ -38,13 +40,13 @@ public class BasicLookupStrategyTests {
 
     private LookupStrategy strategy;
 
-    private static DriverManagerDataSource dataSource;
+    private static TestDataSource dataSource;
 
     //~ Methods ========================================================================================================
 
     @BeforeClass
     public static void createDatabase() throws Exception {
-        dataSource = new DriverManagerDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:lookupstrategytest", "sa", "");
+        dataSource = new TestDataSource("lookupstrategytest");
         jdbcTemplate = new JdbcTemplate(dataSource);
 
         Resource resource = new ClassPathResource("org/springframework/security/acls/jdbc/testData.sql");
@@ -52,6 +54,11 @@ public class BasicLookupStrategyTests {
         jdbcTemplate.execute(sql);
     }
 
+    @AfterClass
+    public static void dropDatabase() throws Exception {
+        dataSource.destroy();
+    }    
+
     @Before
     public void populateDatabase() {
         String query = "INSERT INTO acl_sid(ID,PRINCIPAL,SID) VALUES (1,1,'ben');"

+ 1 - 6
core/src/test/java/org/springframework/security/config/DataSourcePopulator.java

@@ -28,7 +28,7 @@ import org.springframework.util.Assert;
  * @author Ben Alex
  * @version $Id: DataSourcePopulator.java 2291 2007-12-03 02:56:52Z benalex $
  */
-public class DataSourcePopulator implements InitializingBean, DisposableBean {
+public class DataSourcePopulator implements InitializingBean {
     //~ Instance fields ================================================================================================
 
     JdbcTemplate template;
@@ -71,9 +71,4 @@ public class DataSourcePopulator implements InitializingBean, DisposableBean {
     public void setDataSource(DataSource dataSource) {
         this.template = new JdbcTemplate(dataSource);
     }
-
-    public void destroy() throws Exception {
-        template.execute("DROP TABLE AUTHORITIES");
-        template.execute("DROP TABLE USERS");
-    }
 }

+ 5 - 5
core/src/test/java/org/springframework/security/config/JdbcUserServiceBeanDefinitionParserTests.java

@@ -10,6 +10,8 @@ import org.springframework.security.util.InMemoryXmlApplicationContext;
 import org.springframework.security.AuthenticationManager;
 import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
 
+import javax.sql.DataSource;
+
 /**
  * @author Ben Alex
  * @author Luke Taylor
@@ -22,11 +24,9 @@ public class JdbcUserServiceBeanDefinitionParserTests {
             "    <b:bean id='populator' class='org.springframework.security.config.DataSourcePopulator'>" +
             "        <b:property name='dataSource' ref='dataSource'/>" +
             "    </b:bean>" +
-            "    <b:bean id='dataSource' class='org.springframework.jdbc.datasource.DriverManagerDataSource'>" +
-            "        <b:property name='driverClassName' value='org.hsqldb.jdbcDriver'/>" +
-            "        <b:property name='url' value='jdbc:hsqldb:mem:jdbcnamespaces'/>" +
-            "        <b:property name='username' value='sa'/>" +
-            "        <b:property name='password' value=''/>" +
+
+            "    <b:bean id='dataSource' class='org.springframework.security.TestDataSource'>" +
+            "        <b:constructor-arg value='jdbcnamespaces'/>" +
             "    </b:bean>";
 
     @After

+ 10 - 2
core/src/test/java/org/springframework/security/providers/ldap/LdapAuthenticationProviderTests.java

@@ -89,8 +89,8 @@ public class LdapAuthenticationProviderTests extends TestCase {
     }
 
     public void testNormalUsage() {
-        LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
-                new MockAuthoritiesPopulator());
+        MockAuthoritiesPopulator populator = new MockAuthoritiesPopulator();
+        LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(), populator);
         LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
         userMapper.setRoleAttributes(new String[] {"ou"});
         ldapProvider.setUserDetailsContextMapper(userMapper);
@@ -104,6 +104,7 @@ public class LdapAuthenticationProviderTests extends TestCase {
         assertEquals(2, user.getAuthorities().length);
         assertEquals("{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=", user.getPassword());
         assertEquals("ben", user.getUsername());
+        assertEquals("ben", populator.getRequestedUsername());
 
         ArrayList authorities = new ArrayList();
         authorities.add(user.getAuthorities()[0].getAuthority());
@@ -162,8 +163,15 @@ public class LdapAuthenticationProviderTests extends TestCase {
     }
 
     class MockAuthoritiesPopulator implements LdapAuthoritiesPopulator {
+        String username;
+
         public GrantedAuthority[] getGrantedAuthorities(DirContextOperations userCtx, String username) {
+            this.username = username;
             return new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_FROM_POPULATOR")};
         }
+
+        String getRequestedUsername() {
+            return username;
+        }
     }
 }

+ 5 - 4
core/src/test/java/org/springframework/security/ui/rememberme/JdbcTokenRepositoryImplTests.java

@@ -1,7 +1,7 @@
 package org.springframework.security.ui.rememberme;
 
+import org.springframework.security.TestDataSource;
 import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.datasource.DriverManagerDataSource;
 
 import org.junit.After;
 import org.junit.AfterClass;
@@ -20,17 +20,18 @@ import java.util.Map;
  * @version $Id$
  */
 public class JdbcTokenRepositoryImplTests {
-    private static DriverManagerDataSource dataSource;
+    private static TestDataSource dataSource;
     private JdbcTokenRepositoryImpl repo;
     private JdbcTemplate template;
 
     @BeforeClass
     public static void createDataSource() {
-        dataSource = new DriverManagerDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:tokenrepotest", "sa", "");
+        dataSource = new TestDataSource("tokenrepotest");
     }
 
     @AfterClass
-    public static void clearDataSource() {
+    public static void clearDataSource() throws Exception {
+        dataSource.destroy();
         dataSource = null;
     }
 

+ 5 - 4
core/src/test/java/org/springframework/security/userdetails/jdbc/JdbcUserDetailsManagerTests.java

@@ -7,6 +7,7 @@ import org.springframework.security.MockAuthenticationManager;
 import org.springframework.security.PopulatedDatabase;
 import org.springframework.security.GrantedAuthority;
 import org.springframework.security.GrantedAuthorityImpl;
+import org.springframework.security.TestDataSource;
 import org.springframework.security.context.SecurityContextHolder;
 import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
 import org.springframework.security.providers.dao.UserCache;
@@ -14,7 +15,6 @@ import org.springframework.security.userdetails.User;
 import org.springframework.security.userdetails.UserDetails;
 import org.springframework.security.util.AuthorityUtils;
 import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.datasource.DriverManagerDataSource;
 
 import org.junit.After;
 import org.junit.AfterClass;
@@ -43,18 +43,19 @@ public class JdbcUserDetailsManagerTests {
     private static final UserDetails joe = new User("joe", "password", true, true, true, true,
             AuthorityUtils.stringArrayToAuthorityArray(new String[]{"A","C","B"}));
 
-    private static DriverManagerDataSource dataSource;
+    private static TestDataSource dataSource;
     private JdbcUserDetailsManager manager;
     private MockUserCache cache;
     private JdbcTemplate template;
 
     @BeforeClass
     public static void createDataSource() {
-        dataSource = new DriverManagerDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:jdbcusermgrtest", "sa", "");
+        dataSource = new TestDataSource("jdbcusermgrtest");
     }
 
     @AfterClass
-    public static void clearDataSource() {
+    public static void clearDataSource() throws Exception {
+        dataSource.destroy();
         dataSource = null;
     }
 

+ 2 - 14
core/src/test/resources/org/springframework/security/acls/jdbc/applicationContext-test.xml

@@ -64,20 +64,8 @@
 		<constructor-arg ref="aclCache"/>
 	</bean>
 
-    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
-        <property name="driverClassName">
-            <value>org.hsqldb.jdbcDriver</value>
-        </property>
-        <property name="url">
-            <value>jdbc:hsqldb:mem:test</value>
-            <!--  <value>jdbc:hsqldb:hsql://localhost/acl</value> -->
-        </property>
-        <property name="username">
-            <value>sa</value>
-        </property>
-        <property name="password">
-            <value></value>
-        </property>
+    <bean id="dataSource" class="org.springframework.security.TestDataSource">
+        <constructor-arg value="test" />
     </bean>
 
 </beans>