Refactor tests to use the new database-name attribute

This commit refactors the XML configuration used by the tests in the
Spr8849Tests test suite so that a unique database name is always
generated (via the new 'database-name' attribute that was introduced in
SPR-12835) while reusing the same bean name (i.e., 'dataSource').

This is a much more robust alternative to the previous work-around
since the name of the DataSource does not randomly change across
application contexts, thus allowing proper autowiring by name and bean
referencing within XML configuration.

Issue: SPR-8849
This commit is contained in:
Sam Brannen
2015-03-20 17:11:52 +01:00
parent c36c6cbfaa
commit ab771dfd97
6 changed files with 40 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -24,16 +24,14 @@ import org.junit.runners.Suite.SuiteClasses;
* Test suite to investigate claims raised in * Test suite to investigate claims raised in
* <a href="https://jira.spring.io/browse/SPR-8849">SPR-8849</a>. * <a href="https://jira.spring.io/browse/SPR-8849">SPR-8849</a>.
* *
* <p>By using a SpEL expression to generate a random {@code id} for the * <p>By using a SpEL expression to generate a random {@code database-name}
* embedded database (see {@code datasource-config.xml}), we ensure that each * for the embedded database (see {@code datasource-config.xml}), we ensure
* {@code ApplicationContext} that imports the common configuration will create * that each {@code ApplicationContext} that imports the common configuration
* an embedded database with a unique name (since the {@code id} is used as the * will create an embedded database with a unique name.
* database name within
* {@link org.springframework.jdbc.config.EmbeddedDatabaseBeanDefinitionParser#useIdAsDatabaseNameIfGiven()}).
* *
* <p>To reproduce the problem mentioned in SPEX-8849, change the {@code id} of * <p>To reproduce the problem mentioned in SPR-8849, delete the declaration
* the embedded database in {@code datasource-config.xml} to "dataSource" (or * of the {@code database-name} attribute of the embedded database in
* anything else that is not random) and run this <em>suite</em>. * {@code datasource-config.xml} and run this <em>suite</em>.
* *
* @author Sam Brannen * @author Sam Brannen
* @since 3.2 * @since 3.2

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,15 +16,18 @@
package org.springframework.test.context.junit4.spr8849; package org.springframework.test.context.junit4.spr8849;
import javax.annotation.Resource;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
/** /**
* This name of this class intentionally does not end with "Test" or "Tests" * This name of this class intentionally does not end with "Test" or "Tests"
* since it should only be run as part of the test suite: {@link Spr8849Tests}. * since it should only be run as part of the test suite: {@link Spr8849Tests}.
@@ -38,13 +41,19 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration @ContextConfiguration
public class TestClass1 { public class TestClass1 {
@Autowired @Configuration
DataSource datasource; @ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config.xml")
static class Config {
}
@Resource
DataSource dataSource;
@Test @Test
public void dummyTest() { public void dummyTest() {
// it's sufficient if the ApplicationContext loads without errors. assertNotNull(dataSource);
} }
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,15 +16,18 @@
package org.springframework.test.context.junit4.spr8849; package org.springframework.test.context.junit4.spr8849;
import javax.annotation.Resource;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
/** /**
* This name of this class intentionally does not end with "Test" or "Tests" * This name of this class intentionally does not end with "Test" or "Tests"
* since it should only be run as part of the test suite: {@link Spr8849Tests}. * since it should only be run as part of the test suite: {@link Spr8849Tests}.
@@ -38,13 +41,19 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration @ContextConfiguration
public class TestClass2 { public class TestClass2 {
@Autowired @Configuration
@ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config.xml")
static class Config {
}
@Resource
DataSource dataSource; DataSource dataSource;
@Test @Test
public void dummyTest() { public void dummyTest() {
// it's sufficient if the ApplicationContext loads without errors. assertNotNull(dataSource);
} }
} }

View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<import resource="datasource-config.xml"/>
</beans>

View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<import resource="datasource-config.xml"/>
</beans>

View File

@@ -2,10 +2,10 @@
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation=" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"> http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd">
<jdbc:embedded-database id="#{T(java.util.UUID).randomUUID().toString()}"> <jdbc:embedded-database id="dataSource" database-name="#{T(java.util.UUID).randomUUID().toString()}">
<jdbc:script location="classpath:/org/springframework/test/context/junit4/spr8849/spr8849-schema.sql" /> <jdbc:script location="classpath:/org/springframework/test/context/junit4/spr8849/spr8849-schema.sql" />
</jdbc:embedded-database> </jdbc:embedded-database>