Support test annotations on interfaces

Prior to Java 8 it never really made much sense to author integration
tests using interfaces. Consequently, the Spring TestContext Framework
has never supported finding test-related annotations on interfaces in
its search algorithms.

However, Java 8's support for interface default methods introduces new
testing use cases for which it makes sense to declare test
configuration (e.g., @ContextConfiguration, etc.) on an interface
containing default methods instead of on an abstract base class.

This commit ensures that all non-repeatable, class-level test
annotations in the Spring TestContext Framework can now be declared on
test interfaces. The only test annotations that cannot be declared on
interfaces are therefore @Sql and @SqlGroup.

Issue: SPR-14184
This commit is contained in:
Sam Brannen
2016-05-05 19:51:21 +02:00
parent a31f0bb3c0
commit a1a87679da
34 changed files with 973 additions and 84 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@@ -91,7 +91,10 @@ public class ProfileValueUtilsTests {
assertClassIsEnabled(EnabledAnnotatedMultiValue.class);
assertClassIsEnabled(MetaEnabledClass.class);
assertClassIsEnabled(MetaEnabledWithCustomProfileValueSourceClass.class);
assertClassIsEnabled(EnabledWithCustomProfileValueSourceOnTestInterface.class);
assertClassIsDisabled(DisabledAnnotatedSingleValue.class);
assertClassIsDisabled(DisabledAnnotatedSingleValueOnTestInterface.class);
assertClassIsDisabled(DisabledAnnotatedMultiValue.class);
assertClassIsDisabled(MetaDisabledClass.class);
assertClassIsDisabled(MetaDisabledWithCustomProfileValueSourceClass.class);
@@ -105,6 +108,7 @@ public class ProfileValueUtilsTests {
assertMethodIsEnabled(ENABLED_ANNOTATED_METHOD, EnabledAnnotatedSingleValue.class);
assertMethodIsDisabled(DISABLED_ANNOTATED_METHOD, EnabledAnnotatedSingleValue.class);
assertMethodIsEnabled(NON_ANNOTATED_METHOD, MetaEnabledAnnotatedSingleValue.class);
assertMethodIsEnabled(ENABLED_ANNOTATED_METHOD, MetaEnabledAnnotatedSingleValue.class);
assertMethodIsDisabled(DISABLED_ANNOTATED_METHOD, MetaEnabledAnnotatedSingleValue.class);
@@ -117,6 +121,8 @@ public class ProfileValueUtilsTests {
assertMethodIsDisabled(ENABLED_ANNOTATED_METHOD, DisabledAnnotatedSingleValue.class);
assertMethodIsDisabled(DISABLED_ANNOTATED_METHOD, DisabledAnnotatedSingleValue.class);
assertMethodIsDisabled(NON_ANNOTATED_METHOD, DisabledAnnotatedSingleValueOnTestInterface.class);
assertMethodIsDisabled(NON_ANNOTATED_METHOD, MetaDisabledAnnotatedSingleValue.class);
assertMethodIsDisabled(ENABLED_ANNOTATED_METHOD, MetaDisabledAnnotatedSingleValue.class);
assertMethodIsDisabled(DISABLED_ANNOTATED_METHOD, MetaDisabledAnnotatedSingleValue.class);
@@ -176,6 +182,17 @@ public class ProfileValueUtilsTests {
}
}
@IfProfileValue(name = NAME, value = VALUE + "X")
private interface IfProfileValueTestInterface {
}
@SuppressWarnings("unused")
private static class DisabledAnnotatedSingleValueOnTestInterface implements IfProfileValueTestInterface {
public void nonAnnotatedMethod() {
}
}
@SuppressWarnings("unused")
@IfProfileValue(name = NAME, values = { "foo", VALUE, "bar" })
private static class EnabledAnnotatedMultiValue {
@@ -302,4 +319,13 @@ public class ProfileValueUtilsTests {
private static class MetaDisabledWithCustomProfileValueSourceClass {
}
@ProfileValueSourceConfiguration(HardCodedProfileValueSource.class)
private interface CustomProfileValueSourceTestInterface {
}
@IfProfileValue(name = NAME, value = "42")
private static class EnabledWithCustomProfileValueSourceOnTestInterface
implements CustomProfileValueSourceTestInterface {
}
}

View File

@@ -115,6 +115,12 @@ public class TestExecutionListenersTests {
assertNumRegisteredListeners(ExplicitListenersTestCase.class, 3);
}
@Test
public void customListenersDeclaredOnInterface() {
assertRegisteredListeners(ExplicitListenersOnTestInterfaceTestCase.class,
asList(FooTestExecutionListener.class, BarTestExecutionListener.class));
}
@Test
public void nonInheritedListeners() {
assertNumRegisteredListeners(NonInheritedListenersTestCase.class, 1);
@@ -229,6 +235,13 @@ public class TestExecutionListenersTests {
static class NonInheritedListenersTestCase extends InheritedListenersTestCase {
}
@TestExecutionListeners({ FooTestExecutionListener.class, BarTestExecutionListener.class })
interface ExplicitListenersTestInterface {
}
static class ExplicitListenersOnTestInterfaceTestCase implements ExplicitListenersTestInterface {
}
@TestExecutionListeners(listeners = FooTestExecutionListener.class, value = BarTestExecutionListener.class)
static class DuplicateListenersConfigTestCase {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -30,10 +30,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
import static org.springframework.test.context.cache.ContextCacheTestUtils.*;
@@ -41,9 +38,8 @@ import static org.springframework.test.context.junit4.JUnitTestingUtils.*;
/**
* JUnit 4 based integration test which verifies correct {@linkplain ContextCache
* application context caching} in conjunction with the
* {@link SpringJUnit4ClassRunner} and {@link DirtiesContext @DirtiesContext}
* at the class level.
* application context caching} in conjunction with the {@link SpringRunner} and
* {@link DirtiesContext @DirtiesContext} at the class level.
*
* @author Sam Brannen
* @since 3.0
@@ -148,10 +144,9 @@ public class ClassLevelDirtiesContextTests {
// -------------------------------------------------------------------
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
@RunWith(SpringRunner.class)
@ContextConfiguration
public static abstract class BaseTestCase {
static abstract class BaseTestCase {
@Configuration
static class Config {

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.tests.sample.beans.Employee;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 4.3
*/
@RunWith(SpringRunner.class)
public class ActiveProfilesInterfaceTests implements ActiveProfilesTestInterface {
@Autowired
Employee employee;
@Test
public void profileFromTestInterface() {
assertNotNull(employee);
assertEquals("dev", employee.getName());
}
@Configuration
static class Config {
@Bean
@Profile("dev")
Employee employee1() {
return new Employee("dev");
}
@Bean
@Profile("prod")
Employee employee2() {
return new Employee("prod");
}
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.springframework.test.context.ActiveProfiles;
/**
* @author Sam Brannen
* @since 4.3
*/
@ActiveProfiles("dev")
interface ActiveProfilesTestInterface {
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 4.3
*/
@RunWith(SpringRunner.class)
public class BootstrapWithInterfaceTests implements BootstrapWithTestInterface {
@Autowired
String foo;
@Test
public void injectedBean() {
assertEquals("foo", foo);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import java.util.List;
import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.configuration.interfaces.BootstrapWithTestInterface.CustomTestContextBootstrapper;
import org.springframework.test.context.support.DefaultTestContextBootstrapper;
import static java.util.Collections.*;
/**
* @author Sam Brannen
* @author Phillip Webb
* @since 4.3
*/
@BootstrapWith(CustomTestContextBootstrapper.class)
interface BootstrapWithTestInterface {
static class CustomTestContextBootstrapper extends DefaultTestContextBootstrapper {
@Override
protected List<ContextCustomizerFactory> getContextCustomizerFactories() {
return singletonList(
(ContextCustomizerFactory) (testClass, configAttributes) -> (ContextCustomizer) (context,
mergedConfig) -> context.getBeanFactory().registerSingleton("foo", "foo"));
}
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.tests.sample.beans.Employee;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 4.3
*/
@RunWith(SpringRunner.class)
public class ContextConfigurationInterfaceTests implements ContextConfigurationTestInterface {
@Autowired
Employee employee;
@Test
public void profileFromTestInterface() {
assertNotNull(employee);
assertEquals("Dilbert", employee.getName());
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.configuration.interfaces.ContextConfigurationTestInterface.Config;
import org.springframework.tests.sample.beans.Employee;
/**
* @author Sam Brannen
* @since 4.3
*/
@ContextConfiguration(classes = Config.class)
interface ContextConfigurationTestInterface {
static class Config {
@Bean
Employee employee() {
return new Employee("Dilbert");
}
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 4.3
*/
@RunWith(SpringRunner.class)
public class ContextHierarchyInterfaceTests implements ContextHierarchyTestInterface {
@Autowired
String foo;
@Autowired
String bar;
@Autowired
String baz;
@Autowired
ApplicationContext context;
@Test
public void loadContextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertNull("grandparent ApplicationContext", context.getParent().getParent());
assertEquals("foo", foo);
assertEquals("bar", bar);
assertEquals("baz-child", baz);
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.hierarchies.standard.SingleTestClassWithTwoLevelContextHierarchyTests;
/**
* @author Sam Brannen
* @since 4.3
*/
@ContextHierarchy({
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyTests.ParentConfig.class),
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyTests.ChildConfig.class) })
interface ContextHierarchyTestInterface {
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
import static org.springframework.test.context.cache.ContextCacheTestUtils.*;
import static org.springframework.test.context.junit4.JUnitTestingUtils.*;
/**
* @author Sam Brannen
* @since 4.3
*/
@RunWith(JUnit4.class)
public class DirtiesContextInterfaceTests {
private static final AtomicInteger cacheHits = new AtomicInteger(0);
private static final AtomicInteger cacheMisses = new AtomicInteger(0);
@BeforeClass
public static void verifyInitialCacheState() {
resetContextCache();
// Reset static counters in case tests are run multiple times in a test suite --
// for example, via JUnit's @Suite.
cacheHits.set(0);
cacheMisses.set(0);
assertContextCacheStatistics("BeforeClass", 0, cacheHits.get(), cacheMisses.get());
}
@AfterClass
public static void verifyFinalCacheState() {
assertContextCacheStatistics("AfterClass", 0, cacheHits.get(), cacheMisses.get());
}
@Test
public void verifyDirtiesContextBehavior() throws Exception {
runTestClassAndAssertStats(ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase.class, 1);
assertContextCacheStatistics("after class-level @DirtiesContext with clean test method and default class mode",
0, cacheHits.get(), cacheMisses.incrementAndGet());
}
private void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) throws Exception {
runTestsAndAssertCounters(testClass, expectedTestCount, 0, expectedTestCount, 0, 0);
}
@RunWith(SpringRunner.class)
public static class ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase
implements DirtiesContextTestInterface {
@Autowired
ApplicationContext applicationContext;
@Test
public void verifyContextWasAutowired() {
assertNotNull("The application context should have been autowired.", this.applicationContext);
}
@Configuration
static class Config {
/* no beans */
}
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.springframework.test.annotation.DirtiesContext;
/**
* @author Sam Brannen
* @since 4.3
*/
@DirtiesContext
interface DirtiesContextTestInterface {
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.junit.Test;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 4.3
*/
public class SqlConfigInterfaceTests extends AbstractTransactionalJUnit4SpringContextTests
implements SqlConfigTestInterface {
@Test
@Sql(scripts = "/org/springframework/test/context/jdbc/schema.sql", //
config = @SqlConfig(separator = ";"))
@Sql("/org/springframework/test/context/jdbc/data-add-users-with-custom-script-syntax.sql")
public void methodLevelScripts() {
assertNumUsers(3);
}
protected void assertNumUsers(int expected) {
assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user"));
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.EmptyDatabaseConfig;
import org.springframework.test.context.jdbc.SqlConfig;
/**
* @author Sam Brannen
* @since 4.3
*/
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@DirtiesContext
@SqlConfig(commentPrefix = "`", blockCommentStartDelimiter = "#$", blockCommentEndDelimiter = "$#", separator = "@@")
interface SqlConfigTestInterface {
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
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.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 4.3
*/
@RunWith(SpringRunner.class)
public class TestPropertySourceInterfaceTests implements TestPropertySourceTestInterface {
@Autowired
Environment env;
@Test
public void propertiesAreAvailableInEnvironment() {
assertThat(property("foo"), is("bar"));
assertThat(property("enigma"), is("42"));
}
private String property(String key) {
return env.getProperty(key);
}
@Configuration
static class Config {
/* no user beans required for these tests */
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.springframework.test.context.TestPropertySource;
/**
* @author Sam Brannen
* @since 4.3
*/
@TestPropertySource(properties = { "foo = bar", "enigma: 42" })
interface TestPropertySourceTestInterface {
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 4.3
*/
@RunWith(SpringRunner.class)
public class WebAppConfigurationInterfaceTests implements WebAppConfigurationTestInterface {
@Autowired
WebApplicationContext wac;
@Test
public void wacLoaded() {
assertNotNull(wac);
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2002-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.configuration.interfaces;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.configuration.interfaces.WebAppConfigurationTestInterface.Config;
import org.springframework.test.context.web.WebAppConfiguration;
/**
* @author Sam Brannen
* @since 4.3
*/
@WebAppConfiguration
@ContextConfiguration(classes = Config.class)
interface WebAppConfigurationTestInterface {
@Configuration
static class Config {
/* no user beans required for these tests */
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@@ -40,7 +40,7 @@ import static org.junit.Assert.*;
public class SingleTestClassWithTwoLevelContextHierarchyTests {
@Configuration
static class ParentConfig {
public static class ParentConfig {
@Bean
public String foo() {
@@ -54,7 +54,7 @@ public class SingleTestClassWithTwoLevelContextHierarchyTests {
}
@Configuration
static class ChildConfig {
public static class ChildConfig {
@Bean
public String bar() {

View File

@@ -54,6 +54,7 @@ public class TransactionalTestExecutionListenerTests {
private final TransactionalTestExecutionListener listener = new TransactionalTestExecutionListener() {
@Override
protected PlatformTransactionManager getTransactionManager(TestContext testContext, String qualifier) {
return tm;
}
@@ -353,6 +354,16 @@ public class TransactionalTestExecutionListenerTests {
assertIsRollback(ClassLevelRollbackViaMetaAnnotationTestCase.class, false);
}
@Test
public void isRollbackWithClassLevelRollbackWithExplicitValueOnTestInterface() throws Exception {
assertIsRollback(ClassLevelRollbackWithExplicitValueOnTestInterfaceTestCase.class, false);
}
@Test
public void isRollbackWithClassLevelRollbackViaMetaAnnotationOnTestInterface() throws Exception {
assertIsRollback(ClassLevelRollbackViaMetaAnnotationOnTestInterfaceTestCase.class, false);
}
@Test
public void isRollbackWithRollbackAndTransactionConfigurationDeclaredAtClassLevel() throws Exception {
Class<?> clazz = ClassLevelRollbackAndTransactionConfigurationTestCase.class;
@@ -712,4 +723,25 @@ public class TransactionalTestExecutionListenerTests {
}
}
@Rollback(false)
interface RollbackFalseTestInterface {
}
static class ClassLevelRollbackWithExplicitValueOnTestInterfaceTestCase implements RollbackFalseTestInterface {
public void test() {
}
}
@Commit
interface RollbackFalseViaMetaAnnotationTestInterface {
}
static class ClassLevelRollbackViaMetaAnnotationOnTestInterfaceTestCase
implements RollbackFalseViaMetaAnnotationTestInterface {
public void test() {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -25,10 +25,13 @@ import java.lang.annotation.Target;
import org.junit.Test;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
@@ -112,10 +115,27 @@ public class MetaAnnotationUtilsTests {
@Test
public void findAnnotationDescriptorWithInheritedAnnotationOnInterface() throws Exception {
// Note: @Transactional is inherited
assertEquals(InheritedAnnotationInterface.class,
findAnnotationDescriptor(InheritedAnnotationInterface.class, Transactional.class).getRootDeclaringClass());
assertNull(findAnnotationDescriptor(SubInheritedAnnotationInterface.class, Transactional.class));
assertNull(findAnnotationDescriptor(SubSubInheritedAnnotationInterface.class, Transactional.class));
Transactional rawAnnotation = InheritedAnnotationInterface.class.getAnnotation(Transactional.class);
AnnotationDescriptor<Transactional> descriptor;
descriptor = findAnnotationDescriptor(InheritedAnnotationInterface.class, Transactional.class);
assertNotNull(descriptor);
assertEquals(InheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
assertEquals(InheritedAnnotationInterface.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
descriptor = findAnnotationDescriptor(SubInheritedAnnotationInterface.class, Transactional.class);
assertNotNull(descriptor);
assertEquals(SubInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
assertEquals(InheritedAnnotationInterface.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
descriptor = findAnnotationDescriptor(SubSubInheritedAnnotationInterface.class, Transactional.class);
assertNotNull(descriptor);
assertEquals(SubSubInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
assertEquals(InheritedAnnotationInterface.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
}
@Test
@@ -130,9 +150,21 @@ public class MetaAnnotationUtilsTests {
@Test
public void findAnnotationDescriptorForNonInheritedAnnotationOnInterface() throws Exception {
// Note: @Order is not inherited.
assertEquals(NonInheritedAnnotationInterface.class,
findAnnotationDescriptor(NonInheritedAnnotationInterface.class, Order.class).getRootDeclaringClass());
assertNull(findAnnotationDescriptor(SubNonInheritedAnnotationInterface.class, Order.class));
Order rawAnnotation = NonInheritedAnnotationInterface.class.getAnnotation(Order.class);
AnnotationDescriptor<Order> descriptor;
descriptor = findAnnotationDescriptor(NonInheritedAnnotationInterface.class, Order.class);
assertNotNull(descriptor);
assertEquals(NonInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
assertEquals(NonInheritedAnnotationInterface.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
descriptor = findAnnotationDescriptor(SubNonInheritedAnnotationInterface.class, Order.class);
assertNotNull(descriptor);
assertEquals(SubNonInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
assertEquals(NonInheritedAnnotationInterface.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
}
@Test
@@ -158,7 +190,17 @@ public class MetaAnnotationUtilsTests {
@Test
public void findAnnotationDescriptorForClassWithMetaAnnotatedInterface() {
assertNull(findAnnotationDescriptor(ClassWithMetaAnnotatedInterface.class, Component.class));
Component rawAnnotation = AnnotationUtils.findAnnotation(ClassWithMetaAnnotatedInterface.class,
Component.class);
AnnotationDescriptor<Component> descriptor;
descriptor = findAnnotationDescriptor(ClassWithMetaAnnotatedInterface.class, Component.class);
assertNotNull(descriptor);
assertEquals(ClassWithMetaAnnotatedInterface.class, descriptor.getRootDeclaringClass());
assertEquals(Meta1.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
assertEquals(Meta1.class, descriptor.getComposedAnnotation().annotationType());
}
@Test
@@ -253,11 +295,27 @@ public class MetaAnnotationUtilsTests {
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithInheritedAnnotationOnInterface() throws Exception {
// Note: @Transactional is inherited
assertEquals(
InheritedAnnotationInterface.class,
findAnnotationDescriptorForTypes(InheritedAnnotationInterface.class, Transactional.class).getRootDeclaringClass());
assertNull(findAnnotationDescriptorForTypes(SubInheritedAnnotationInterface.class, Transactional.class));
assertNull(findAnnotationDescriptorForTypes(SubSubInheritedAnnotationInterface.class, Transactional.class));
Transactional rawAnnotation = InheritedAnnotationInterface.class.getAnnotation(Transactional.class);
UntypedAnnotationDescriptor descriptor;
descriptor = findAnnotationDescriptorForTypes(InheritedAnnotationInterface.class, Transactional.class);
assertNotNull(descriptor);
assertEquals(InheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
assertEquals(InheritedAnnotationInterface.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
descriptor = findAnnotationDescriptorForTypes(SubInheritedAnnotationInterface.class, Transactional.class);
assertNotNull(descriptor);
assertEquals(SubInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
assertEquals(InheritedAnnotationInterface.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
descriptor = findAnnotationDescriptorForTypes(SubSubInheritedAnnotationInterface.class, Transactional.class);
assertNotNull(descriptor);
assertEquals(SubSubInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
assertEquals(InheritedAnnotationInterface.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
}
@Test
@@ -274,10 +332,21 @@ public class MetaAnnotationUtilsTests {
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesForNonInheritedAnnotationOnInterface() throws Exception {
// Note: @Order is not inherited.
assertEquals(
NonInheritedAnnotationInterface.class,
findAnnotationDescriptorForTypes(NonInheritedAnnotationInterface.class, Order.class).getRootDeclaringClass());
assertNull(findAnnotationDescriptorForTypes(SubNonInheritedAnnotationInterface.class, Order.class));
Order rawAnnotation = NonInheritedAnnotationInterface.class.getAnnotation(Order.class);
UntypedAnnotationDescriptor descriptor;
descriptor = findAnnotationDescriptorForTypes(NonInheritedAnnotationInterface.class, Order.class);
assertNotNull(descriptor);
assertEquals(NonInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
assertEquals(NonInheritedAnnotationInterface.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
descriptor = findAnnotationDescriptorForTypes(SubNonInheritedAnnotationInterface.class, Order.class);
assertNotNull(descriptor);
assertEquals(SubNonInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
assertEquals(NonInheritedAnnotationInterface.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
}
@Test
@@ -345,8 +414,18 @@ public class MetaAnnotationUtilsTests {
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesForClassWithMetaAnnotatedInterface() {
assertNull(findAnnotationDescriptorForTypes(ClassWithMetaAnnotatedInterface.class, Service.class,
Component.class, Order.class, Transactional.class));
Component rawAnnotation = AnnotationUtils.findAnnotation(ClassWithMetaAnnotatedInterface.class,
Component.class);
UntypedAnnotationDescriptor descriptor;
descriptor = findAnnotationDescriptorForTypes(ClassWithMetaAnnotatedInterface.class, Service.class,
Component.class, Order.class, Transactional.class);
assertNotNull(descriptor);
assertEquals(ClassWithMetaAnnotatedInterface.class, descriptor.getRootDeclaringClass());
assertEquals(Meta1.class, descriptor.getDeclaringClass());
assertEquals(rawAnnotation, descriptor.getAnnotation());
assertEquals(Meta1.class, descriptor.getComposedAnnotation().annotationType());
}
@Test