Skip to content

Commit ab771df

Browse files
committed
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
1 parent c36c6cb commit ab771df

File tree

6 files changed

+40
-38
lines changed

6 files changed

+40
-38
lines changed

spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/Spr8849Tests.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,16 +24,14 @@
2424
* Test suite to investigate claims raised in
2525
* <a href="https://jira.spring.io/browse/SPR-8849">SPR-8849</a>.
2626
*
27-
* <p>By using a SpEL expression to generate a random {@code id} for the
28-
* embedded database (see {@code datasource-config.xml}), we ensure that each
29-
* {@code ApplicationContext} that imports the common configuration will create
30-
* an embedded database with a unique name (since the {@code id} is used as the
31-
* database name within
32-
* {@link org.springframework.jdbc.config.EmbeddedDatabaseBeanDefinitionParser#useIdAsDatabaseNameIfGiven()}).
27+
* <p>By using a SpEL expression to generate a random {@code database-name}
28+
* for the embedded database (see {@code datasource-config.xml}), we ensure
29+
* that each {@code ApplicationContext} that imports the common configuration
30+
* will create an embedded database with a unique name.
3331
*
34-
* <p>To reproduce the problem mentioned in SPEX-8849, change the {@code id} of
35-
* the embedded database in {@code datasource-config.xml} to "dataSource" (or
36-
* anything else that is not random) and run this <em>suite</em>.
32+
* <p>To reproduce the problem mentioned in SPR-8849, delete the declaration
33+
* of the {@code database-name} attribute of the embedded database in
34+
* {@code datasource-config.xml} and run this <em>suite</em>.
3735
*
3836
* @author Sam Brannen
3937
* @since 3.2

spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass1.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,18 @@
1616

1717
package org.springframework.test.context.junit4.spr8849;
1818

19+
import javax.annotation.Resource;
1920
import javax.sql.DataSource;
2021

2122
import org.junit.Test;
2223
import org.junit.runner.RunWith;
23-
24-
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.context.annotation.ImportResource;
2526
import org.springframework.test.context.ContextConfiguration;
2627
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2728

29+
import static org.junit.Assert.*;
30+
2831
/**
2932
* This name of this class intentionally does not end with "Test" or "Tests"
3033
* since it should only be run as part of the test suite: {@link Spr8849Tests}.
@@ -38,13 +41,19 @@
3841
@ContextConfiguration
3942
public class TestClass1 {
4043

41-
@Autowired
42-
DataSource datasource;
44+
@Configuration
45+
@ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config.xml")
46+
static class Config {
47+
}
48+
49+
50+
@Resource
51+
DataSource dataSource;
4352

4453

4554
@Test
4655
public void dummyTest() {
47-
// it's sufficient if the ApplicationContext loads without errors.
56+
assertNotNull(dataSource);
4857
}
4958

5059
}

spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass2.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,18 @@
1616

1717
package org.springframework.test.context.junit4.spr8849;
1818

19+
import javax.annotation.Resource;
1920
import javax.sql.DataSource;
2021

2122
import org.junit.Test;
2223
import org.junit.runner.RunWith;
23-
24-
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.context.annotation.ImportResource;
2526
import org.springframework.test.context.ContextConfiguration;
2627
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2728

29+
import static org.junit.Assert.*;
30+
2831
/**
2932
* This name of this class intentionally does not end with "Test" or "Tests"
3033
* since it should only be run as part of the test suite: {@link Spr8849Tests}.
@@ -38,13 +41,19 @@
3841
@ContextConfiguration
3942
public class TestClass2 {
4043

41-
@Autowired
44+
@Configuration
45+
@ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config.xml")
46+
static class Config {
47+
}
48+
49+
50+
@Resource
4251
DataSource dataSource;
4352

4453

4554
@Test
4655
public void dummyTest() {
47-
// it's sufficient if the ApplicationContext loads without errors.
56+
assertNotNull(dataSource);
4857
}
4958

5059
}

spring-test/src/test/resources/org/springframework/test/context/junit4/spr8849/TestClass1-context.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

spring-test/src/test/resources/org/springframework/test/context/junit4/spr8849/TestClass2-context.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

spring-test/src/test/resources/org/springframework/test/context/junit4/spr8849/datasource-config.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
44
xsi:schemaLocation="
5-
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
6-
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
5+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd">
77

8-
<jdbc:embedded-database id="#{T(java.util.UUID).randomUUID().toString()}">
8+
<jdbc:embedded-database id="dataSource" database-name="#{T(java.util.UUID).randomUUID().toString()}">
99
<jdbc:script location="classpath:/org/springframework/test/context/junit4/spr8849/spr8849-schema.sql" />
1010
</jdbc:embedded-database>
1111

0 commit comments

Comments
 (0)