Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit b9c8f10

Browse files
committed
Merge pull request #28 from dsyer/SPR-5292
SPR-5292: test case and example code using @rule and @parameters
2 parents 0eaf133 + 239b3fe commit b9c8f10

File tree

8 files changed

+263
-0
lines changed

8 files changed

+263
-0
lines changed

SPR-5292/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.springframework.issues</groupId>
5+
<artifactId>SPR-5292</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<dependencies>
9+
<dependency>
10+
<groupId>org.springframework</groupId>
11+
<artifactId>spring-context</artifactId>
12+
<version>3.2.0.BUILD-SNAPSHOT</version>
13+
</dependency>
14+
<dependency>
15+
<groupId>org.springframework</groupId>
16+
<artifactId>spring-test</artifactId>
17+
<version>3.2.0.BUILD-SNAPSHOT</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>log4j</groupId>
21+
<artifactId>log4j</artifactId>
22+
<version>1.2.16</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>junit</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>4.8</version>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
<repositories>
32+
<repository>
33+
<id>spring-maven-snapshot</id>
34+
<name>Springframework Maven Snapshot Repository</name>
35+
<url>http://repo.springsource.org/snapshot</url>
36+
<snapshots><enabled>true</enabled></snapshots>
37+
</repository>
38+
</repositories>
39+
<properties>
40+
<project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
41+
</properties>
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<artifactId>maven-compiler-plugin</artifactId>
46+
<version>2.3.2</version>
47+
<configuration>
48+
<source>1.6</source>
49+
<target>1.6</target>
50+
</configuration>
51+
</plugin>
52+
<plugin>
53+
<artifactId>maven-surefire-plugin</artifactId>
54+
<version>2.7.2</version>
55+
<configuration>
56+
<includes>
57+
<include>**/*Tests.java</include>
58+
</includes>
59+
<excludes>
60+
<exclude>**/*Abstract*.java</exclude>
61+
</excludes>
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
67+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.springframework.issues;
2+
3+
public class Bar {
4+
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.springframework.issues;
2+
3+
public class Foo {
4+
5+
private final Bar bar;
6+
7+
public Foo(Bar bar) {
8+
this.bar = bar;
9+
}
10+
11+
public Bar getBar() {
12+
return this.bar;
13+
}
14+
}

SPR-5292/src/main/resources/.gitignore

Whitespace-only changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.springframework.issues;
2+
3+
import static org.hamcrest.CoreMatchers.sameInstance;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.junit.runners.Parameterized;
13+
import org.junit.runners.Parameterized.Parameters;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.test.context.ContextConfiguration;
16+
17+
@ContextConfiguration("classpath:org/springframework/issues/ReproTests-context.xml")
18+
@RunWith(Parameterized.class)
19+
public class ReproTests {
20+
21+
@Rule
22+
public static SpringContextRule context = new SpringContextRule();
23+
24+
@Autowired
25+
private Foo foo;
26+
27+
@Autowired
28+
private Bar bar;
29+
30+
private final String value;
31+
32+
private static Foo state;
33+
34+
@Parameters
35+
public static List<Object[]> parameters() {
36+
return Arrays.asList(new Object[] {"foo"}, new Object[] {"bar"});
37+
}
38+
39+
public ReproTests(String value) {
40+
this.value = value;
41+
}
42+
43+
@Test
44+
public void repro() {
45+
System.err.println(value);
46+
if (state==null) {
47+
state = foo;
48+
} else {
49+
// Context is only created once so the singleton instance shoul be the same
50+
assertThat(state, sameInstance(foo));
51+
}
52+
assertThat(foo.getBar(), sameInstance(bar));
53+
}
54+
55+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.springframework.issues;
2+
3+
import java.lang.reflect.Method;
4+
5+
import org.junit.rules.MethodRule;
6+
import org.junit.runners.model.FrameworkMethod;
7+
import org.junit.runners.model.Statement;
8+
import org.springframework.test.context.TestContextManager;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
11+
/**
12+
* WARNING: PROOF OF CONCEPT. DO NOT ASSUME PRODUCTION CODE ;)
13+
*
14+
* A {@Rule} alternative to {@link SpringJUnit4ClassRunner}.
15+
* <p>
16+
* Currently used as follows:
17+
*
18+
* <pre>
19+
* &#064;Rule
20+
* public static SpringContextRule rule = new SpringContextRule();
21+
* </pre>
22+
*
23+
* Code borrowed from SPR-7731. Note that {@link MethodRule} is deprecated in recent versions of JUnit but there is some
24+
* discussion about whether it ought to be re-instated (<a href="https://github.com/KentBeck/junit/issues/351">on
25+
* github</a>).
26+
*
27+
* @author Neale Upstone (skype: neale87)
28+
* @author Dave Syer
29+
*/
30+
public class SpringContextRule implements MethodRule {
31+
32+
// private static final Log logger = LogFactory.getLog(SpringContextRule.class);
33+
34+
private TestContextManager testContextManager;
35+
36+
/**
37+
* Currently the only way to create a SpringContextRule. The rule will look for the usual Spring annotations etc on
38+
* the test classes that this rule is applied to.
39+
*/
40+
public SpringContextRule() {
41+
}
42+
43+
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
44+
45+
// TODO: clean this up
46+
if (testContextManager == null) {
47+
testContextManager = createTestContextManager(target.getClass());
48+
}
49+
50+
return new Statement() {
51+
@Override
52+
public void evaluate() throws Throwable {
53+
Method testMethod = method.getMethod();
54+
getTestContextManager().prepareTestInstance(target);
55+
getTestContextManager().beforeTestMethod(target, testMethod);
56+
Exception thrown = null;
57+
try {
58+
base.evaluate();
59+
}
60+
catch (Exception e) {
61+
thrown = e;
62+
}
63+
finally {
64+
getTestContextManager().afterTestMethod(target, testMethod, thrown);
65+
}
66+
}
67+
};
68+
}
69+
70+
private TestContextManager getTestContextManager() {
71+
return testContextManager;
72+
}
73+
74+
/**
75+
* Creates a new {@link TestContextManager} for the supplied test class and the configured
76+
* <em>default <code>ContextLoader</code> class name</em>. Can be overridden by subclasses.
77+
*
78+
* @param clazz the test class to be managed
79+
* @see #getDefaultContextLoaderClassName(Class)
80+
*/
81+
protected TestContextManager createTestContextManager(Class<?> clazz) {
82+
return new TestContextManager(clazz, getDefaultContextLoaderClassName(clazz));
83+
}
84+
85+
/**
86+
* Get the name of the default <code>ContextLoader</code> class to use for the supplied test class. The named class
87+
* will be used if the test class does not explicitly declare a <code>ContextLoader</code> class via the
88+
* <code>&#064;ContextConfiguration</code> annotation.
89+
* <p>
90+
* The default implementation returns <code>null</code>, thus implying use of the <em>standard</em> default
91+
* <code>ContextLoader</code> class name. Can be overridden by subclasses.
92+
* </p>
93+
*
94+
* @param clazz the test class
95+
* @return <code>null</code>
96+
*/
97+
protected String getDefaultContextLoaderClassName(Class<?> clazz) {
98+
return null;
99+
}
100+
101+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
log4j.rootCategory=ERROR, stdout
2+
3+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5+
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
6+
7+
log4j.category.org.springframework=DEBUG
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
7+
8+
<bean id="foo" class="org.springframework.issues.Foo">
9+
<constructor-arg ref="bar"/>
10+
</bean>
11+
12+
<bean id="bar" class="org.springframework.issues.Bar"/>
13+
14+
</beans>

0 commit comments

Comments
 (0)