Skip to content

Commit 04a6827

Browse files
committed
Reproduce claims raised in SPR-8849
This commit introduces a test suite (Spr8849Tests) that demonstrates the claims made in SPR-8849. Specifically, if <jdbc:embedded-database id="xyz" /> is used to create an embedded HSQL database in an XML configuration file and that configuration file is imported in different sets of configuration files that are used to load ApplicationContexts for different integration tests, the embedded database will be initialized multiple times using any nested <jdbc:script /> elements. If such a script is used to create a table, for example, subsequent attempts to initialize the database named "xyz" will fail since an embedded database named "xyz" already exists in the JVM. As a work-around, this test suite uses a SpEL expression to generate a random string for each embedded database instance: id="#{T(java.util.UUID).randomUUID().toString()}" See the Javadoc in Spr8849Tests for further information. Issue: SPR-8849
1 parent 67d5a12 commit 04a6827

File tree

7 files changed

+172
-0
lines changed

7 files changed

+172
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.spr8849;
18+
19+
import org.junit.runner.RunWith;
20+
import org.junit.runners.Suite;
21+
import org.junit.runners.Suite.SuiteClasses;
22+
23+
/**
24+
* Test suite to investigate claims raised in
25+
* <a href="https://jira.springsource.org/browse/SPR-8849">SPR-8849</a>.
26+
*
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()}).
33+
*
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>.
37+
*
38+
* @author Sam Brannen
39+
* @since 3.2
40+
*/
41+
@RunWith(Suite.class)
42+
@SuiteClasses({ TestClass1.class, TestClass2.class })
43+
public class Spr8849Tests {
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
4+
5+
<import resource="datasource-config.xml"/>
6+
7+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.spr8849;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.test.context.ContextConfiguration;
25+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26+
27+
/**
28+
* This name of this class intentionally does not end with "Test" or "Tests"
29+
* since it should only be run as part of the test suite: {@link Spr8849Tests}.
30+
*
31+
* @author Mickael Leduque
32+
* @author Sam Brannen
33+
* @since 3.2
34+
* @see Spr8849Tests
35+
*/
36+
@RunWith(SpringJUnit4ClassRunner.class)
37+
@ContextConfiguration
38+
public class TestClass1 {
39+
40+
@Autowired
41+
DataSource datasource;
42+
43+
44+
@Test
45+
public void dummyTest() {
46+
// it's sufficient if the ApplicationContext loads without errors.
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
4+
5+
<import resource="datasource-config.xml"/>
6+
7+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.spr8849;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.test.context.ContextConfiguration;
25+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26+
27+
/**
28+
* This name of this class intentionally does not end with "Test" or "Tests"
29+
* since it should only be run as part of the test suite: {@link Spr8849Tests}.
30+
*
31+
* @author Mickael Leduque
32+
* @author Sam Brannen
33+
* @since 3.2
34+
* @see Spr8849Tests
35+
*/
36+
@RunWith(SpringJUnit4ClassRunner.class)
37+
@ContextConfiguration
38+
public class TestClass2 {
39+
40+
@Autowired
41+
DataSource dataSource;
42+
43+
44+
@Test
45+
public void dummyTest() {
46+
// it's sufficient if the ApplicationContext loads without errors.
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
4+
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">
7+
8+
<jdbc:embedded-database id="#{T(java.util.UUID).randomUUID().toString()}">
9+
<jdbc:script location="classpath:/org/springframework/test/context/junit4/spr8849/spr8849-schema.sql" />
10+
</jdbc:embedded-database>
11+
12+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE enigma (
2+
id INTEGER NOT NULL IDENTITY PRIMARY KEY
3+
);

0 commit comments

Comments
 (0)