@ -1,11 +1,18 @@
@@ -1,11 +1,18 @@
package org.springframework.jdbc.config ;
import static org.hamcrest.CoreMatchers.* ;
import static org.junit.Assert.* ;
import javax.sql.DataSource ;
import org.junit.Test ;
import org.springframework.beans.PropertyValue ;
import org.springframework.beans.factory.config.BeanDefinition ;
import org.springframework.beans.factory.support.DefaultListableBeanFactory ;
import org.springframework.beans.factory.xml.XmlBeanFactory ;
import org.springframework.context.ConfigurableApplicationContext ;
import org.springframework.context.support.ClassPathXmlApplicationContext ;
import org.springframework.core.io.ClassPathResource ;
import org.springframework.jdbc.core.JdbcTemplate ;
public class JdbcNamespaceIntegrationTest {
@ -14,13 +21,7 @@ public class JdbcNamespaceIntegrationTest {
@@ -14,13 +21,7 @@ public class JdbcNamespaceIntegrationTest {
public void testCreateEmbeddedDatabase ( ) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
"org/springframework/jdbc/config/jdbc-config.xml" ) ;
try {
assertCorrectSetup ( context . getBean ( "dataSource" , DataSource . class ) ) ;
assertCorrectSetup ( context . getBean ( "h2DataSource" , DataSource . class ) ) ;
assertCorrectSetup ( context . getBean ( "derbyDataSource" , DataSource . class ) ) ;
} finally {
context . close ( ) ;
}
assertCorrectSetup ( context , "dataSource" , "h2DataSource" , "derbyDataSource" ) ;
}
@Test
@ -28,26 +29,44 @@ public class JdbcNamespaceIntegrationTest {
@@ -28,26 +29,44 @@ public class JdbcNamespaceIntegrationTest {
// If Derby isn't cleaned up properly this will fail...
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
"org/springframework/jdbc/config/jdbc-config.xml" ) ;
try {
assertCorrectSetup ( context . getBean ( "derbyDataSource" , DataSource . class ) ) ;
} finally {
context . close ( ) ;
}
assertCorrectSetup ( context , "derbyDataSource" ) ;
}
@Test
public void testCreateWithResourcePattern ( ) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
"org/springframework/jdbc/config/jdbc-config-pattern.xml" ) ;
assertCorrectSetup ( context , "dataSource" ) ;
}
@Test
public void testMultipleDataSourcesHaveDifferentDatabaseNames ( ) throws Exception {
DefaultListableBeanFactory factory = new XmlBeanFactory ( new ClassPathResource (
"org/springframework/jdbc/config/jdbc-config-multiple-datasources.xml" ) ) ;
assertBeanPropertyValueOf ( "databaseName" , "firstDataSource" , factory ) ;
assertBeanPropertyValueOf ( "databaseName" , "secondDataSource" , factory ) ;
}
private void assertBeanPropertyValueOf ( String propertyName , String expected , DefaultListableBeanFactory factory ) {
BeanDefinition bean = factory . getBeanDefinition ( expected ) ;
PropertyValue value = bean . getPropertyValues ( ) . getPropertyValue ( propertyName ) ;
assertThat ( value , is ( notNullValue ( ) ) ) ;
assertThat ( value . getValue ( ) . toString ( ) , is ( expected ) ) ;
}
private void assertCorrectSetup ( ConfigurableApplicationContext context , String . . . dataSources ) {
try {
assertCorrectSetup ( context . getBean ( "dataSource" , DataSource . class ) ) ;
for ( String dataSourceName : dataSources ) {
DataSource dataSource = context . getBean ( dataSourceName , DataSource . class ) ;
JdbcTemplate t = new JdbcTemplate ( dataSource ) ;
assertEquals ( 1 , t . queryForInt ( "select count(*) from T_TEST" ) ) ;
}
} finally {
context . close ( ) ;
}
}
private void assertCorrectSetup ( DataSource dataSource ) {
JdbcTemplate t = new JdbcTemplate ( dataSource ) ;
assertEquals ( 1 , t . queryForInt ( "select count(*) from T_TEST" ) ) ;
}
}