Skip to content

Commit f1bfcf0

Browse files
committed
[SPR-6104] @TestExecutionListeners now supports a 'listeners' alias for its existing 'value' attribute.
1 parent fe16447 commit f1bfcf0

File tree

4 files changed

+61
-40
lines changed

4 files changed

+61
-40
lines changed

org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,10 @@ private String[] retrieveContextLocations(ContextLoader contextLoader, Class<?>
245245
String[] valueLocations = contextConfiguration.value();
246246
String[] locations = contextConfiguration.locations();
247247
if (!ObjectUtils.isEmpty(valueLocations) && !ObjectUtils.isEmpty(locations)) {
248-
String msg = "Test class ["
249-
+ declaringClass
250-
+ "] has been configured with @ContextConfiguration's 'value' ["
251-
+ ObjectUtils.nullSafeToString(valueLocations)
252-
+ "] and 'locations' ["
253-
+ ObjectUtils.nullSafeToString(locations)
254-
+ "] attributes. Only one declaration of resource locations is permitted per @ContextConfiguration annotation.";
248+
String msg = String.format(
249+
"Test class [%s] has been configured with @ContextConfiguration's 'value' [%s] and 'locations' [%s] attributes. Only one declaration of resource locations is permitted per @ContextConfiguration annotation.",
250+
declaringClass, ObjectUtils.nullSafeToString(valueLocations),
251+
ObjectUtils.nullSafeToString(locations));
255252
logger.error(msg);
256253
throw new IllegalStateException(msg);
257254
}

org.springframework.test/src/main/java/org/springframework/test/context/TestContextManager.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.context.ApplicationContext;
3131
import org.springframework.core.annotation.AnnotationUtils;
3232
import org.springframework.util.Assert;
33+
import org.springframework.util.ObjectUtils;
3334

3435
/**
3536
* <p>
@@ -201,9 +202,23 @@ private TestExecutionListener[] retrieveTestExecutionListeners(Class<?> clazz) {
201202
logger.trace("Retrieved @TestExecutionListeners [" + testExecutionListeners
202203
+ "] for declaring class [" + declaringClass + "].");
203204
}
204-
Class<? extends TestExecutionListener>[] classes = testExecutionListeners.value();
205-
if (classes != null) {
206-
classesList.addAll(0, Arrays.<Class<? extends TestExecutionListener>> asList(classes));
205+
206+
Class<? extends TestExecutionListener>[] valueListenerClasses = testExecutionListeners.value();
207+
Class<? extends TestExecutionListener>[] listenerClasses = testExecutionListeners.listeners();
208+
if (!ObjectUtils.isEmpty(valueListenerClasses) && !ObjectUtils.isEmpty(listenerClasses)) {
209+
String msg = String.format(
210+
"Test class [%s] has been configured with @TestExecutionListeners' 'value' [%s] and 'listeners' [%s] attributes. Use one or the other, but not both.",
211+
declaringClass, ObjectUtils.nullSafeToString(valueListenerClasses),
212+
ObjectUtils.nullSafeToString(listenerClasses));
213+
logger.error(msg);
214+
throw new IllegalStateException(msg);
215+
}
216+
else if (!ObjectUtils.isEmpty(valueListenerClasses)) {
217+
listenerClasses = valueListenerClasses;
218+
}
219+
220+
if (listenerClasses != null) {
221+
classesList.addAll(0, Arrays.<Class<? extends TestExecutionListener>> asList(listenerClasses));
207222
}
208223
declaringClass = (testExecutionListeners.inheritListeners() ? AnnotationUtils.findAnnotationDeclaringClass(
209224
annotationType, declaringClass.getSuperclass())

org.springframework.test/src/main/java/org/springframework/test/context/TestExecutionListeners.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@
5252
* @see org.springframework.test.context.support.DirtiesContextTestExecutionListener
5353
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
5454
*/
55-
Class<? extends TestExecutionListener>[] value();
55+
Class<? extends TestExecutionListener>[] listeners() default {};
56+
57+
/**
58+
* Alias for {@link #listeners() listeners}.
59+
*/
60+
Class<? extends TestExecutionListener>[] value() default {};
5661

5762
/**
5863
* <p>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2009 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.
@@ -17,17 +17,14 @@
1717
package org.springframework.test.context;
1818

1919
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.fail;
2120

2221
import org.junit.Test;
23-
2422
import org.springframework.test.context.support.AbstractTestExecutionListener;
2523

2624
/**
2725
* <p>
28-
* JUnit 4 based unit test for the
29-
* {@link TestExecutionListeners @TestExecutionListeners} annotation, which
30-
* verifies:
26+
* JUnit 4 based unit test for the {@link TestExecutionListeners
27+
* &#064;TestExecutionListeners} annotation, which verifies:
3128
* </p>
3229
* <ul>
3330
* <li>Proper registering of {@link TestExecutionListener listeners} in
@@ -36,7 +33,7 @@
3633
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896"
3734
* target="_blank">SPR-3896</a></li>
3835
* </ul>
39-
*
36+
*
4037
* @author Sam Brannen
4138
* @since 2.5
4239
*/
@@ -46,75 +43,78 @@ public class TestExecutionListenersTests {
4643
public void verifyNumDefaultListenersRegistered() throws Exception {
4744
TestContextManager testContextManager = new TestContextManager(DefaultListenersExampleTestCase.class);
4845
assertEquals("Verifying the number of registered TestExecutionListeners for DefaultListenersExampleTest.", 3,
49-
testContextManager.getTestExecutionListeners().size());
46+
testContextManager.getTestExecutionListeners().size());
5047
}
5148

5249
@Test
5350
public void verifyNumNonInheritedDefaultListenersRegistered() throws Exception {
54-
TestContextManager testContextManager = new TestContextManager(NonInheritedDefaultListenersExampleTestCase.class);
51+
TestContextManager testContextManager = new TestContextManager(
52+
NonInheritedDefaultListenersExampleTestCase.class);
5553
assertEquals(
56-
"Verifying the number of registered TestExecutionListeners for NonInheritedDefaultListenersExampleTest.",
57-
1, testContextManager.getTestExecutionListeners().size());
54+
"Verifying the number of registered TestExecutionListeners for NonInheritedDefaultListenersExampleTest.",
55+
1, testContextManager.getTestExecutionListeners().size());
5856
}
5957

6058
@Test
6159
public void verifyNumInheritedDefaultListenersRegistered() throws Exception {
6260
TestContextManager testContextManager = new TestContextManager(InheritedDefaultListenersExampleTestCase.class);
6361
assertEquals(
64-
"Verifying the number of registered TestExecutionListeners for InheritedDefaultListenersExampleTest.",
65-
1, testContextManager.getTestExecutionListeners().size());
62+
"Verifying the number of registered TestExecutionListeners for InheritedDefaultListenersExampleTest.", 1,
63+
testContextManager.getTestExecutionListeners().size());
6664

6765
testContextManager = new TestContextManager(SubInheritedDefaultListenersExampleTestCase.class);
6866
assertEquals(
69-
"Verifying the number of registered TestExecutionListeners for SubInheritedDefaultListenersExampleTest.",
70-
1, testContextManager.getTestExecutionListeners().size());
67+
"Verifying the number of registered TestExecutionListeners for SubInheritedDefaultListenersExampleTest.",
68+
1, testContextManager.getTestExecutionListeners().size());
7169

7270
testContextManager = new TestContextManager(SubSubInheritedDefaultListenersExampleTestCase.class);
7371
assertEquals(
74-
"Verifying the number of registered TestExecutionListeners for SubSubInheritedDefaultListenersExampleTest.",
75-
2, testContextManager.getTestExecutionListeners().size());
72+
"Verifying the number of registered TestExecutionListeners for SubSubInheritedDefaultListenersExampleTest.",
73+
2, testContextManager.getTestExecutionListeners().size());
7674
}
7775

7876
@Test
7977
public void verifyNumListenersRegistered() throws Exception {
8078
TestContextManager testContextManager = new TestContextManager(ExampleTestCase.class);
8179
assertEquals("Verifying the number of registered TestExecutionListeners for ExampleTest.", 3,
82-
testContextManager.getTestExecutionListeners().size());
80+
testContextManager.getTestExecutionListeners().size());
8381
}
8482

8583
@Test
8684
public void verifyNumNonInheritedListenersRegistered() throws Exception {
8785
TestContextManager testContextManager = new TestContextManager(NonInheritedListenersExampleTestCase.class);
8886
assertEquals("Verifying the number of registered TestExecutionListeners for NonInheritedListenersExampleTest.",
89-
1, testContextManager.getTestExecutionListeners().size());
87+
1, testContextManager.getTestExecutionListeners().size());
9088
}
9189

9290
@Test
9391
public void verifyNumInheritedListenersRegistered() throws Exception {
9492
TestContextManager testContextManager = new TestContextManager(InheritedListenersExampleTestCase.class);
9593
assertEquals("Verifying the number of registered TestExecutionListeners for InheritedListenersExampleTest.", 4,
96-
testContextManager.getTestExecutionListeners().size());
94+
testContextManager.getTestExecutionListeners().size());
95+
}
96+
97+
@Test(expected = IllegalStateException.class)
98+
public void verifyDuplicateListenersConfigThrowsException() throws Exception {
99+
new TestContextManager(DuplicateListenersConfigExampleTestCase.class);
97100
}
98101

99102

100103
static class DefaultListenersExampleTestCase {
101104
}
102105

103-
@TestExecutionListeners( { QuuxTestExecutionListener.class })
106+
@TestExecutionListeners(QuuxTestExecutionListener.class)
104107
static class InheritedDefaultListenersExampleTestCase extends DefaultListenersExampleTestCase {
105-
public void testDoSomething() {
106-
fail("whaa?");
107-
}
108108
}
109109

110110
static class SubInheritedDefaultListenersExampleTestCase extends InheritedDefaultListenersExampleTestCase {
111111
}
112112

113-
@TestExecutionListeners( { EnigmaTestExecutionListener.class })
113+
@TestExecutionListeners(EnigmaTestExecutionListener.class)
114114
static class SubSubInheritedDefaultListenersExampleTestCase extends SubInheritedDefaultListenersExampleTestCase {
115115
}
116116

117-
@TestExecutionListeners(value = { QuuxTestExecutionListener.class }, inheritListeners = false)
117+
@TestExecutionListeners(listeners = { QuuxTestExecutionListener.class }, inheritListeners = false)
118118
static class NonInheritedDefaultListenersExampleTestCase extends InheritedDefaultListenersExampleTestCase {
119119
}
120120

@@ -123,14 +123,18 @@ static class NonInheritedDefaultListenersExampleTestCase extends InheritedDefaul
123123
static class ExampleTestCase {
124124
}
125125

126-
@TestExecutionListeners( { QuuxTestExecutionListener.class })
126+
@TestExecutionListeners(QuuxTestExecutionListener.class)
127127
static class InheritedListenersExampleTestCase extends ExampleTestCase {
128128
}
129129

130-
@TestExecutionListeners(value = { QuuxTestExecutionListener.class }, inheritListeners = false)
130+
@TestExecutionListeners(listeners = QuuxTestExecutionListener.class, inheritListeners = false)
131131
static class NonInheritedListenersExampleTestCase extends InheritedListenersExampleTestCase {
132132
}
133133

134+
@TestExecutionListeners(listeners = FooTestExecutionListener.class, value = BarTestExecutionListener.class)
135+
static class DuplicateListenersConfigExampleTestCase {
136+
}
137+
134138
static class FooTestExecutionListener extends AbstractTestExecutionListener {
135139
}
136140

@@ -146,4 +150,4 @@ static class QuuxTestExecutionListener extends AbstractTestExecutionListener {
146150
static class EnigmaTestExecutionListener extends AbstractTestExecutionListener {
147151
}
148152

149-
}
153+
}

0 commit comments

Comments
 (0)