提交 ab771dfd 编写于 作者: S Sam Brannen

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
上级 c36c6cbf
/*
* 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");
* 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
* <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
* 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()}).
* <p>By using a SpEL expression to generate a random {@code database-name}
* 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.
*
* <p>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 <em>suite</em>.
* <p>To reproduce the problem mentioned in SPR-8849, delete the declaration
* of the {@code database-name} attribute of the embedded database in
* {@code datasource-config.xml} and run this <em>suite</em>.
*
* @author Sam Brannen
* @since 3.2
......
/*
* 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");
* you may not use this file except in compliance with the License.
......@@ -16,15 +16,18 @@
package org.springframework.test.context.junit4.spr8849;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
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"
* 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
public class TestClass1 {
@Autowired
DataSource datasource;
@Configuration
@ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config.xml")
static class Config {
}
@Resource
DataSource dataSource;
@Test
public void dummyTest() {
// it's sufficient if the ApplicationContext loads without errors.
assertNotNull(dataSource);
}
}
\ No newline at end of file
/*
* 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");
* you may not use this file except in compliance with the License.
......@@ -16,15 +16,18 @@
package org.springframework.test.context.junit4.spr8849;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
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"
* 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
public class TestClass2 {
@Autowired
@Configuration
@ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config.xml")
static class Config {
}
@Resource
DataSource dataSource;
@Test
public void dummyTest() {
// it's sufficient if the ApplicationContext loads without errors.
assertNotNull(dataSource);
}
}
\ No newline at end of file
<?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>
<?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>
......@@ -2,10 +2,10 @@
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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-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:embedded-database>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册