Browse Source

[SPR-6104] @TestExecutionListeners now supports a 'listeners' alias for its existing 'value' attribute.

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1872 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Sam Brannen 17 years ago
parent
commit
c0d38136cc
  1. 11
      org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java
  2. 21
      org.springframework.test/src/main/java/org/springframework/test/context/TestContextManager.java
  3. 7
      org.springframework.test/src/main/java/org/springframework/test/context/TestExecutionListeners.java
  4. 62
      org.springframework.test/src/test/java/org/springframework/test/context/TestExecutionListenersTests.java

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

@ -245,13 +245,10 @@ public class TestContext extends AttributeAccessorSupport { @@ -245,13 +245,10 @@ public class TestContext extends AttributeAccessorSupport {
String[] valueLocations = contextConfiguration.value();
String[] locations = contextConfiguration.locations();
if (!ObjectUtils.isEmpty(valueLocations) && !ObjectUtils.isEmpty(locations)) {
String msg = "Test class ["
+ declaringClass
+ "] has been configured with @ContextConfiguration's 'value' ["
+ ObjectUtils.nullSafeToString(valueLocations)
+ "] and 'locations' ["
+ ObjectUtils.nullSafeToString(locations)
+ "] attributes. Only one declaration of resource locations is permitted per @ContextConfiguration annotation.";
String msg = String.format(
"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.",
declaringClass, ObjectUtils.nullSafeToString(valueLocations),
ObjectUtils.nullSafeToString(locations));
logger.error(msg);
throw new IllegalStateException(msg);
}

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

@ -30,6 +30,7 @@ import org.springframework.beans.BeanUtils; @@ -30,6 +30,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* <p>
@ -201,9 +202,23 @@ public class TestContextManager { @@ -201,9 +202,23 @@ public class TestContextManager {
logger.trace("Retrieved @TestExecutionListeners [" + testExecutionListeners
+ "] for declaring class [" + declaringClass + "].");
}
Class<? extends TestExecutionListener>[] classes = testExecutionListeners.value();
if (classes != null) {
classesList.addAll(0, Arrays.<Class<? extends TestExecutionListener>> asList(classes));
Class<? extends TestExecutionListener>[] valueListenerClasses = testExecutionListeners.value();
Class<? extends TestExecutionListener>[] listenerClasses = testExecutionListeners.listeners();
if (!ObjectUtils.isEmpty(valueListenerClasses) && !ObjectUtils.isEmpty(listenerClasses)) {
String msg = String.format(
"Test class [%s] has been configured with @TestExecutionListeners' 'value' [%s] and 'listeners' [%s] attributes. Use one or the other, but not both.",
declaringClass, ObjectUtils.nullSafeToString(valueListenerClasses),
ObjectUtils.nullSafeToString(listenerClasses));
logger.error(msg);
throw new IllegalStateException(msg);
}
else if (!ObjectUtils.isEmpty(valueListenerClasses)) {
listenerClasses = valueListenerClasses;
}
if (listenerClasses != null) {
classesList.addAll(0, Arrays.<Class<? extends TestExecutionListener>> asList(listenerClasses));
}
declaringClass = (testExecutionListeners.inheritListeners() ? AnnotationUtils.findAnnotationDeclaringClass(
annotationType, declaringClass.getSuperclass())

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

@ -52,7 +52,12 @@ public @interface TestExecutionListeners { @@ -52,7 +52,12 @@ public @interface TestExecutionListeners {
* @see org.springframework.test.context.support.DirtiesContextTestExecutionListener
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
*/
Class<? extends TestExecutionListener>[] value();
Class<? extends TestExecutionListener>[] listeners() default {};
/**
* Alias for {@link #listeners() listeners}.
*/
Class<? extends TestExecutionListener>[] value() default {};
/**
* <p>

62
org.springframework.test/src/test/java/org/springframework/test/context/TestExecutionListenersTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2009 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.
@ -17,17 +17,14 @@ @@ -17,17 +17,14 @@
package org.springframework.test.context;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.springframework.test.context.support.AbstractTestExecutionListener;
/**
* <p>
* JUnit 4 based unit test for the
* {@link TestExecutionListeners @TestExecutionListeners} annotation, which
* verifies:
* JUnit 4 based unit test for the {@link TestExecutionListeners
* &#064;TestExecutionListeners} annotation, which verifies:
* </p>
* <ul>
* <li>Proper registering of {@link TestExecutionListener listeners} in
@ -36,7 +33,7 @@ import org.springframework.test.context.support.AbstractTestExecutionListener; @@ -36,7 +33,7 @@ import org.springframework.test.context.support.AbstractTestExecutionListener;
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896"
* target="_blank">SPR-3896</a></li>
* </ul>
*
*
* @author Sam Brannen
* @since 2.5
*/
@ -46,75 +43,78 @@ public class TestExecutionListenersTests { @@ -46,75 +43,78 @@ public class TestExecutionListenersTests {
public void verifyNumDefaultListenersRegistered() throws Exception {
TestContextManager testContextManager = new TestContextManager(DefaultListenersExampleTestCase.class);
assertEquals("Verifying the number of registered TestExecutionListeners for DefaultListenersExampleTest.", 3,
testContextManager.getTestExecutionListeners().size());
testContextManager.getTestExecutionListeners().size());
}
@Test
public void verifyNumNonInheritedDefaultListenersRegistered() throws Exception {
TestContextManager testContextManager = new TestContextManager(NonInheritedDefaultListenersExampleTestCase.class);
TestContextManager testContextManager = new TestContextManager(
NonInheritedDefaultListenersExampleTestCase.class);
assertEquals(
"Verifying the number of registered TestExecutionListeners for NonInheritedDefaultListenersExampleTest.",
1, testContextManager.getTestExecutionListeners().size());
"Verifying the number of registered TestExecutionListeners for NonInheritedDefaultListenersExampleTest.",
1, testContextManager.getTestExecutionListeners().size());
}
@Test
public void verifyNumInheritedDefaultListenersRegistered() throws Exception {
TestContextManager testContextManager = new TestContextManager(InheritedDefaultListenersExampleTestCase.class);
assertEquals(
"Verifying the number of registered TestExecutionListeners for InheritedDefaultListenersExampleTest.",
1, testContextManager.getTestExecutionListeners().size());
"Verifying the number of registered TestExecutionListeners for InheritedDefaultListenersExampleTest.", 1,
testContextManager.getTestExecutionListeners().size());
testContextManager = new TestContextManager(SubInheritedDefaultListenersExampleTestCase.class);
assertEquals(
"Verifying the number of registered TestExecutionListeners for SubInheritedDefaultListenersExampleTest.",
1, testContextManager.getTestExecutionListeners().size());
"Verifying the number of registered TestExecutionListeners for SubInheritedDefaultListenersExampleTest.",
1, testContextManager.getTestExecutionListeners().size());
testContextManager = new TestContextManager(SubSubInheritedDefaultListenersExampleTestCase.class);
assertEquals(
"Verifying the number of registered TestExecutionListeners for SubSubInheritedDefaultListenersExampleTest.",
2, testContextManager.getTestExecutionListeners().size());
"Verifying the number of registered TestExecutionListeners for SubSubInheritedDefaultListenersExampleTest.",
2, testContextManager.getTestExecutionListeners().size());
}
@Test
public void verifyNumListenersRegistered() throws Exception {
TestContextManager testContextManager = new TestContextManager(ExampleTestCase.class);
assertEquals("Verifying the number of registered TestExecutionListeners for ExampleTest.", 3,
testContextManager.getTestExecutionListeners().size());
testContextManager.getTestExecutionListeners().size());
}
@Test
public void verifyNumNonInheritedListenersRegistered() throws Exception {
TestContextManager testContextManager = new TestContextManager(NonInheritedListenersExampleTestCase.class);
assertEquals("Verifying the number of registered TestExecutionListeners for NonInheritedListenersExampleTest.",
1, testContextManager.getTestExecutionListeners().size());
1, testContextManager.getTestExecutionListeners().size());
}
@Test
public void verifyNumInheritedListenersRegistered() throws Exception {
TestContextManager testContextManager = new TestContextManager(InheritedListenersExampleTestCase.class);
assertEquals("Verifying the number of registered TestExecutionListeners for InheritedListenersExampleTest.", 4,
testContextManager.getTestExecutionListeners().size());
testContextManager.getTestExecutionListeners().size());
}
@Test(expected = IllegalStateException.class)
public void verifyDuplicateListenersConfigThrowsException() throws Exception {
new TestContextManager(DuplicateListenersConfigExampleTestCase.class);
}
static class DefaultListenersExampleTestCase {
}
@TestExecutionListeners( { QuuxTestExecutionListener.class })
@TestExecutionListeners(QuuxTestExecutionListener.class)
static class InheritedDefaultListenersExampleTestCase extends DefaultListenersExampleTestCase {
public void testDoSomething() {
fail("whaa?");
}
}
static class SubInheritedDefaultListenersExampleTestCase extends InheritedDefaultListenersExampleTestCase {
}
@TestExecutionListeners( { EnigmaTestExecutionListener.class })
@TestExecutionListeners(EnigmaTestExecutionListener.class)
static class SubSubInheritedDefaultListenersExampleTestCase extends SubInheritedDefaultListenersExampleTestCase {
}
@TestExecutionListeners(value = { QuuxTestExecutionListener.class }, inheritListeners = false)
@TestExecutionListeners(listeners = { QuuxTestExecutionListener.class }, inheritListeners = false)
static class NonInheritedDefaultListenersExampleTestCase extends InheritedDefaultListenersExampleTestCase {
}
@ -123,14 +123,18 @@ public class TestExecutionListenersTests { @@ -123,14 +123,18 @@ public class TestExecutionListenersTests {
static class ExampleTestCase {
}
@TestExecutionListeners( { QuuxTestExecutionListener.class })
@TestExecutionListeners(QuuxTestExecutionListener.class)
static class InheritedListenersExampleTestCase extends ExampleTestCase {
}
@TestExecutionListeners(value = { QuuxTestExecutionListener.class }, inheritListeners = false)
@TestExecutionListeners(listeners = QuuxTestExecutionListener.class, inheritListeners = false)
static class NonInheritedListenersExampleTestCase extends InheritedListenersExampleTestCase {
}
@TestExecutionListeners(listeners = FooTestExecutionListener.class, value = BarTestExecutionListener.class)
static class DuplicateListenersConfigExampleTestCase {
}
static class FooTestExecutionListener extends AbstractTestExecutionListener {
}
@ -146,4 +150,4 @@ public class TestExecutionListenersTests { @@ -146,4 +150,4 @@ public class TestExecutionListenersTests {
static class EnigmaTestExecutionListener extends AbstractTestExecutionListener {
}
}
}
Loading…
Cancel
Save