diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java b/org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java index 5046af26abe..31228dc2417 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java @@ -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); } diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/TestContextManager.java b/org.springframework.test/src/main/java/org/springframework/test/context/TestContextManager.java index ee48c6b842b..e463c325f0e 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/TestContextManager.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/TestContextManager.java @@ -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; /** *

@@ -201,9 +202,23 @@ public class TestContextManager { logger.trace("Retrieved @TestExecutionListeners [" + testExecutionListeners + "] for declaring class [" + declaringClass + "]."); } - Class[] classes = testExecutionListeners.value(); - if (classes != null) { - classesList.addAll(0, Arrays.> asList(classes)); + + Class[] valueListenerClasses = testExecutionListeners.value(); + Class[] 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.> asList(listenerClasses)); } declaringClass = (testExecutionListeners.inheritListeners() ? AnnotationUtils.findAnnotationDeclaringClass( annotationType, declaringClass.getSuperclass()) diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/TestExecutionListeners.java b/org.springframework.test/src/main/java/org/springframework/test/context/TestExecutionListeners.java index b9e6e9cb108..a7517418ffe 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/TestExecutionListeners.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/TestExecutionListeners.java @@ -52,7 +52,12 @@ public @interface TestExecutionListeners { * @see org.springframework.test.context.support.DirtiesContextTestExecutionListener * @see org.springframework.test.context.transaction.TransactionalTestExecutionListener */ - Class[] value(); + Class[] listeners() default {}; + + /** + * Alias for {@link #listeners() listeners}. + */ + Class[] value() default {}; /** *

diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/TestExecutionListenersTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/TestExecutionListenersTests.java index 062df252ad4..0161eff57b1 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/TestExecutionListenersTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/TestExecutionListenersTests.java @@ -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 @@ 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; /** *

- * JUnit 4 based unit test for the - * {@link TestExecutionListeners @TestExecutionListeners} annotation, which - * verifies: + * JUnit 4 based unit test for the {@link TestExecutionListeners + * @TestExecutionListeners} annotation, which verifies: *

* - * + * * @author Sam Brannen * @since 2.5 */ @@ -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 { 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 { static class EnigmaTestExecutionListener extends AbstractTestExecutionListener { } -} +} \ No newline at end of file