From 04a682729020af09c70ebbf6a210f42b8cc1c36b Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 10 Jun 2012 00:31:05 +0200 Subject: [PATCH] Reproduce claims raised in SPR-8849 This commit introduces a test suite (Spr8849Tests) that demonstrates the claims made in SPR-8849. Specifically, if 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 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 --- .../context/junit4/spr8849/Spr8849Tests.java | 45 +++++++++++++++++ .../junit4/spr8849/TestClass1-context.xml | 7 +++ .../context/junit4/spr8849/TestClass1.java | 49 +++++++++++++++++++ .../junit4/spr8849/TestClass2-context.xml | 7 +++ .../context/junit4/spr8849/TestClass2.java | 49 +++++++++++++++++++ .../junit4/spr8849/datasource-config.xml | 12 +++++ .../context/junit4/spr8849/spr8849-schema.sql | 3 ++ 7 files changed, 172 insertions(+) create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/Spr8849Tests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass1-context.xml create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass1.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass2-context.xml create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass2.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/datasource-config.xml create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/spr8849-schema.sql diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/Spr8849Tests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/Spr8849Tests.java new file mode 100644 index 00000000000..fa0412abc1b --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/Spr8849Tests.java @@ -0,0 +1,45 @@ +/* + * Copyright 2002-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.junit4.spr8849; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +/** + * Test suite to investigate claims raised in + * SPR-8849. + * + *

By using a SpEL expression to generate a random {@code id} for the + * embedded database (see {@code datasource-config.xml}), we ensure that each + * {@code ApplicationContext} that imports the common configuration will create + * an embedded database with a unique name (since the {@code id} is used as the + * database name within + * {@link org.springframework.jdbc.config.EmbeddedDatabaseBeanDefinitionParser#useIdAsDatabaseNameIfGiven()}). + * + *

To reproduce the problem mentioned in SPEX-8849, change the {@code id} of + * the embedded database in {@code datasource-config.xml} to "dataSource" (or + * anything else that is not random) and run this suite. + * + * @author Sam Brannen + * @since 3.2 + */ +@RunWith(Suite.class) +@SuiteClasses({ TestClass1.class, TestClass2.class }) +public class Spr8849Tests { + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass1-context.xml b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass1-context.xml new file mode 100644 index 00000000000..63314700662 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass1-context.xml @@ -0,0 +1,7 @@ + + + + + + diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass1.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass1.java new file mode 100644 index 00000000000..b45b6d76a71 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass1.java @@ -0,0 +1,49 @@ +/* + * Copyright 2002-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.junit4.spr8849; + +import javax.sql.DataSource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * 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}. + * + * @author Mickael Leduque + * @author Sam Brannen + * @since 3.2 + * @see Spr8849Tests + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class TestClass1 { + + @Autowired + DataSource datasource; + + + @Test + public void dummyTest() { + // it's sufficient if the ApplicationContext loads without errors. + } + +} \ No newline at end of file diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass2-context.xml b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass2-context.xml new file mode 100644 index 00000000000..63314700662 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass2-context.xml @@ -0,0 +1,7 @@ + + + + + + diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass2.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass2.java new file mode 100644 index 00000000000..bc8a7800549 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/TestClass2.java @@ -0,0 +1,49 @@ +/* + * Copyright 2002-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.junit4.spr8849; + +import javax.sql.DataSource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * 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}. + * + * @author Mickael Leduque + * @author Sam Brannen + * @since 3.2 + * @see Spr8849Tests + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class TestClass2 { + + @Autowired + DataSource dataSource; + + + @Test + public void dummyTest() { + // it's sufficient if the ApplicationContext loads without errors. + } + +} \ No newline at end of file diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/datasource-config.xml b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/datasource-config.xml new file mode 100644 index 00000000000..ed82b6f301b --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/datasource-config.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/spr8849-schema.sql b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/spr8849-schema.sql new file mode 100644 index 00000000000..a17d13a9d92 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr8849/spr8849-schema.sql @@ -0,0 +1,3 @@ +CREATE TABLE enigma ( + id INTEGER NOT NULL IDENTITY PRIMARY KEY +);