From 2c6fb3d1c93f91c3d059eab39707af4f76abc1fd Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Thu, 31 Jan 2008 20:30:37 +0000 Subject: [PATCH] Added extra tests for jdbc-user-details service to make sure it works within an element. --- .../security/config/DataSourcePopulator.java | 13 ++++- ...cUserServiceBeanDefinitionParserTests.java | 52 +++++++++++++------ ...pUserServiceBeanDefinitionParserTests.java | 13 +++++ .../security/config/jdbc-user-details.xml | 24 --------- 4 files changed, 60 insertions(+), 42 deletions(-) delete mode 100644 core/src/test/resources/org/springframework/security/config/jdbc-user-details.xml diff --git a/core/src/test/java/org/springframework/security/config/DataSourcePopulator.java b/core/src/test/java/org/springframework/security/config/DataSourcePopulator.java index f6b77e1a21..96067ada33 100644 --- a/core/src/test/java/org/springframework/security/config/DataSourcePopulator.java +++ b/core/src/test/java/org/springframework/security/config/DataSourcePopulator.java @@ -17,8 +17,13 @@ package org.springframework.security.config; import javax.sql.DataSource; import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.DisposableBean; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.Assert; +import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationListener; +import org.springframework.context.ApplicationEvent; /** @@ -27,7 +32,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 { +public class DataSourcePopulator implements InitializingBean, DisposableBean { //~ Instance fields ================================================================================================ JdbcTemplate template; @@ -50,7 +55,7 @@ public class DataSourcePopulator implements InitializingBean { Encoded password for jane is "wombat" */ - template.execute("INSERT INTO USERS VALUES('rod','a564de63c2d0da68cf47586ee05984d7',TRUE);"); + template.execute("INSERT INTO USERS VALUES('rod','koala',TRUE);"); template.execute("INSERT INTO USERS VALUES('dianne','65d15fe9156f9c4bbffd98085992a44e',TRUE);"); template.execute("INSERT INTO USERS VALUES('scott','2b58af6dddbd072ed27ffc86725d7d3a',TRUE);"); template.execute("INSERT INTO USERS VALUES('peter','22b5c9accc6e1ba628cedc63a72d57f8',FALSE);"); @@ -71,4 +76,8 @@ public class DataSourcePopulator implements InitializingBean { this.template = new JdbcTemplate(dataSource); } + public void destroy() throws Exception { + template.execute("DROP TABLE AUTHORITIES"); + template.execute("DROP TABLE USERS"); + } } diff --git a/core/src/test/java/org/springframework/security/config/JdbcUserServiceBeanDefinitionParserTests.java b/core/src/test/java/org/springframework/security/config/JdbcUserServiceBeanDefinitionParserTests.java index 2f71244c65..752654f3a4 100644 --- a/core/src/test/java/org/springframework/security/config/JdbcUserServiceBeanDefinitionParserTests.java +++ b/core/src/test/java/org/springframework/security/config/JdbcUserServiceBeanDefinitionParserTests.java @@ -2,15 +2,13 @@ package org.springframework.security.config; import static org.junit.Assert.assertTrue; -import java.util.List; - -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; -import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.security.providers.ProviderManager; -import org.springframework.security.providers.dao.DaoAuthenticationProvider; +import org.junit.After; + import org.springframework.security.userdetails.jdbc.JdbcUserDetailsManager; +import org.springframework.security.util.InMemoryXmlApplicationContext; +import org.springframework.security.AuthenticationManager; +import org.springframework.security.providers.UsernamePasswordAuthenticationToken; /** * @author Ben Alex @@ -18,15 +16,21 @@ import org.springframework.security.userdetails.jdbc.JdbcUserDetailsManager; * @version $Id$ */ public class JdbcUserServiceBeanDefinitionParserTests { - private static ClassPathXmlApplicationContext appContext; + private InMemoryXmlApplicationContext appContext; - @BeforeClass - public static void loadContext() { - appContext = new ClassPathXmlApplicationContext("org/springframework/security/config/jdbc-user-details.xml"); - } + private static String DATA_SOURCE = + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " "; - @AfterClass - public static void closeAppContext() { + @After + public void closeAppContext() { if (appContext != null) { appContext.close(); } @@ -34,12 +38,28 @@ public class JdbcUserServiceBeanDefinitionParserTests { @Test public void validUsernameIsFound() { - JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean(BeanIds.USER_DETAILS_SERVICE); + setContext("" + DATA_SOURCE); + JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean(BeanIds.USER_DETAILS_SERVICE); assertTrue(mgr.loadUserByUsername("rod") != null); } @Test public void beanIdIsParsedCorrectly() { - JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean("customUserService"); + setContext("" + DATA_SOURCE); + JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean("customUserService"); + } + + @Test + public void isSupportedByAuthenticationProviderElement() { + setContext( + "" + + " " + + "" + DATA_SOURCE); + AuthenticationManager mgr = (AuthenticationManager) appContext.getBean(BeanIds.AUTHENTICATION_MANAGER); + mgr.authenticate(new UsernamePasswordAuthenticationToken("rod", "koala")); + } + + private void setContext(String context) { + appContext = new InMemoryXmlApplicationContext(context); } } diff --git a/core/src/test/java/org/springframework/security/config/LdapUserServiceBeanDefinitionParserTests.java b/core/src/test/java/org/springframework/security/config/LdapUserServiceBeanDefinitionParserTests.java index 357fdb7c37..f4d72dbd1e 100644 --- a/core/src/test/java/org/springframework/security/config/LdapUserServiceBeanDefinitionParserTests.java +++ b/core/src/test/java/org/springframework/security/config/LdapUserServiceBeanDefinitionParserTests.java @@ -3,6 +3,10 @@ package org.springframework.security.config; import org.springframework.security.util.InMemoryXmlApplicationContext; import org.springframework.security.userdetails.UserDetailsService; import org.springframework.security.userdetails.UserDetails; +import org.springframework.security.AuthenticationManager; +import org.springframework.security.providers.UsernamePasswordAuthenticationToken; +import org.springframework.security.providers.ProviderManager; +import org.springframework.security.providers.dao.DaoAuthenticationProvider; import org.junit.Test; import org.junit.After; @@ -48,6 +52,15 @@ public class LdapUserServiceBeanDefinitionParserTests { assertEquals("Joe Smeth", joe.getUsername()); } + @Test + public void isSupportedByAuthenticationProviderElement() { + setContext( + "" + + "" + + " " + + ""); + } + private void setContext(String context) { appCtx = new InMemoryXmlApplicationContext(context); } diff --git a/core/src/test/resources/org/springframework/security/config/jdbc-user-details.xml b/core/src/test/resources/org/springframework/security/config/jdbc-user-details.xml deleted file mode 100644 index de17637e09..0000000000 --- a/core/src/test/resources/org/springframework/security/config/jdbc-user-details.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - - - - - \ No newline at end of file