Extract spring-boot-test.jar

Relocate the `org.springframework.boot.test` package from the
`spring-boot.jar` to `spring-boot-test.jar`.

Fixes gh-5184
This commit is contained in:
Phillip Webb
2016-02-19 11:14:36 -08:00
parent 4b55144d80
commit 89b7704977
102 changed files with 612 additions and 139 deletions

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2012-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.boot.test;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import org.springframework.asm.Opcodes;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Abstract base class for {@code @Configuration} sanity checks.
*
* @author Andy Wilkinson
*/
public abstract class AbstractConfigurationClassTests {
private ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
@Test
public void allBeanMethodsArePublic() throws IOException, ClassNotFoundException {
Set<String> nonPublicBeanMethods = new HashSet<String>();
for (AnnotationMetadata configurationClass : findConfigurationClasses()) {
Set<MethodMetadata> beanMethods = configurationClass
.getAnnotatedMethods(Bean.class.getName());
for (MethodMetadata methodMetadata : beanMethods) {
if (!isPublic(methodMetadata)) {
nonPublicBeanMethods.add(methodMetadata.getDeclaringClassName() + "."
+ methodMetadata.getMethodName());
}
}
}
assertThat(nonPublicBeanMethods).as("Found non-public @Bean methods").isEmpty();
}
private Set<AnnotationMetadata> findConfigurationClasses() throws IOException {
Set<AnnotationMetadata> configurationClasses = new HashSet<AnnotationMetadata>();
Resource[] resources = this.resolver.getResources("classpath*:"
+ getClass().getPackage().getName().replace(".", "/") + "/**/*.class");
for (Resource resource : resources) {
if (!isTestClass(resource)) {
MetadataReader metadataReader = new SimpleMetadataReaderFactory()
.getMetadataReader(resource);
AnnotationMetadata annotationMetadata = metadataReader
.getAnnotationMetadata();
if (annotationMetadata.getAnnotationTypes()
.contains(Configuration.class.getName())) {
configurationClasses.add(annotationMetadata);
}
}
}
return configurationClasses;
}
private boolean isTestClass(Resource resource) throws IOException {
return resource.getFile().getAbsolutePath()
.contains("target" + File.separator + "test-classes");
}
private boolean isPublic(MethodMetadata methodMetadata) {
int access = (Integer) new DirectFieldAccessor(methodMetadata)
.getPropertyValue("access");
return (access & Opcodes.ACC_PUBLIC) != 0;
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2012-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.boot.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link ApplicationContextTestUtils}.
*
* @author Stephane Nicoll
*/
public class ApplicationContextTestUtilsTests {
@Test
public void closeNull() {
ApplicationContextTestUtils.closeAll(null);
}
@Test
public void closeNonClosableContext() {
ApplicationContext mock = mock(ApplicationContext.class);
ApplicationContextTestUtils.closeAll(mock);
}
@Test
public void closeContextAndParent() {
ConfigurableApplicationContext mock = mock(ConfigurableApplicationContext.class);
ConfigurableApplicationContext parent = mock(
ConfigurableApplicationContext.class);
given(mock.getParent()).willReturn(parent);
given(parent.getParent()).willReturn(null);
ApplicationContextTestUtils.closeAll(mock);
verify(mock).getParent();
verify(mock).close();
verify(parent).getParent();
verify(parent).close();
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2012-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.boot.test;
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.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ConfigFileApplicationContextInitializer}.
*
* @author Phillip Webb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@ContextConfiguration(classes = ConfigFileApplicationContextInitializerTests.Config.class, initializers = ConfigFileApplicationContextInitializer.class)
public class ConfigFileApplicationContextInitializerTests {
@Autowired
private Environment environment;
@Test
public void initializerPopulatesEnvironment() {
assertThat(this.environment.getProperty("foo")).isEqualTo("bucket");
}
@Configuration
public static class Config {
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2012-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.boot.test;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link EnvironmentTestUtils}.
*
* @author Stephane Nicoll
*/
public class EnvironmentTestUtilsTests {
private final ConfigurableEnvironment environment = new StandardEnvironment();
@Test
public void addSimplePairEqual() {
testAddSimplePair("my.foo", "bar", "=");
}
@Test
public void addSimplePairColon() {
testAddSimplePair("my.foo", "bar", ":");
}
@Test
public void addSimplePairEqualWithEqualInValue() {
testAddSimplePair("my.foo", "b=ar", "=");
}
@Test
public void addSimplePairEqualWithColonInValue() {
testAddSimplePair("my.foo", "b:ar", "=");
}
@Test
public void addSimplePairColonWithColonInValue() {
testAddSimplePair("my.foo", "b:ar", ":");
}
@Test
public void addSimplePairColonWithEqualInValue() {
testAddSimplePair("my.foo", "b=ar", ":");
}
@Test
public void addPairNoValue() {
String propertyName = "my.foo+bar";
assertThat(this.environment.containsProperty(propertyName)).isFalse();
EnvironmentTestUtils.addEnvironment(this.environment, propertyName);
assertThat(this.environment.containsProperty(propertyName)).isTrue();
assertThat(this.environment.getProperty(propertyName)).isEqualTo("");
}
private void testAddSimplePair(String key, String value, String delimiter) {
assertThat(this.environment.containsProperty(key)).isFalse();
EnvironmentTestUtils.addEnvironment(this.environment, key + delimiter + value);
assertThat(this.environment.getProperty(key)).isEqualTo(value);
}
@Test
public void testConfigHasHigherPrecedence() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("my.foo", "bar");
MapPropertySource source = new MapPropertySource("sample", map);
this.environment.getPropertySources().addFirst(source);
assertThat(this.environment.getProperty("my.foo")).isEqualTo("bar");
EnvironmentTestUtils.addEnvironment(this.environment, "my.foo=bar2");
assertThat(this.environment.getProperty("my.foo")).isEqualTo("bar2");
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2012-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.boot.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SpringApplicationContextLoader} with active profiles. See gh-1469.
*
* @author Phillip Webb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration
@IntegrationTest("spring.config.name=enableother")
@ActiveProfiles("override")
public class SpringApplicationConfigurationActiveProfileTests {
@Autowired
private ApplicationContext context;
@Test
public void profiles() throws Exception {
assertThat(this.context.getEnvironment().getActiveProfiles())
.containsExactly("override");
}
@Configuration
protected static class Config {
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2012-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.boot.test;
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.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SpringApplicationContextLoader} (detectDefaultConfigurationClasses).
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration
public class SpringApplicationConfigurationDefaultConfigurationTests {
@Autowired
private Config config;
@Test
public void nestedConfigClasses() {
assertThat(this.config).isNotNull();
}
@Configuration
protected static class Config {
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2012-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.boot.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SpringApplicationContextLoader} (detectDefaultConfigurationClasses).
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration(locations = "classpath:test.groovy")
public class SpringApplicationConfigurationGroovyConfigurationTests {
@Autowired
private String foo;
@Test
public void groovyConfigLoaded() {
assertThat(this.foo).isNotNull();
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2012-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.boot.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SpringApplicationContextLoader} finding groovy config.
*
* @author Phillip Webb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration
public class SpringApplicationConfigurationGroovyConventionConfigurationTests {
@Autowired
private String foo;
@Test
public void groovyConfigLoaded() {
assertThat(this.foo).isEqualTo("World");
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2012-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.boot.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfigurationJmxTests.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for disabling JMX by default
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration(Config.class)
@IntegrationTest
public class SpringApplicationConfigurationJmxTests {
@Value("${spring.jmx.enabled}")
private boolean jmx;
@Test
public void disabledByDefault() {
assertThat(this.jmx).isFalse();
}
@Configuration
protected static class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2012-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.boot.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfigurationMixedConfigurationTests.Config;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SpringApplicationContextLoader}.
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration(classes = Config.class, locations = "classpath:test.groovy")
public class SpringApplicationConfigurationMixedConfigurationTests {
@Autowired
private String foo;
@Autowired
private Config config;
@Test
public void mixedConfigClasses() {
assertThat(this.foo).isNotNull();
assertThat(this.config).isNotNull();
}
@Configuration
protected static class Config {
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2012-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.boot.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SpringApplicationContextLoader} finding XML config.
*
* @author Phillip Webb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration
public class SpringApplicationConfigurationXmlConventionConfigurationTests {
@Autowired
private String foo;
@Test
public void xmlConfigLoaded() {
assertThat(this.foo).isEqualTo("World");
}
}

View File

@@ -0,0 +1,157 @@
/*
* Copyright 2012-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.boot.test;
import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SpringApplicationContextLoader}
*
* @author Stephane Nicoll
*/
public class SpringApplicationContextLoaderTests {
@Test
public void environmentPropertiesSimple() throws Exception {
Map<String, Object> config = getEnvironmentProperties(SimpleConfig.class);
assertKey(config, "key", "myValue");
assertKey(config, "anotherKey", "anotherValue");
}
@Test
public void environmentPropertiesOverrideDefaults() throws Exception {
Map<String, Object> config = getEnvironmentProperties(OverrideConfig.class);
assertKey(config, "server.port", "2345");
}
@Test
public void environmentPropertiesAppend() throws Exception {
Map<String, Object> config = getEnvironmentProperties(AppendConfig.class);
assertKey(config, "key", "myValue");
assertKey(config, "otherKey", "otherValue");
}
@Test
public void environmentPropertiesSeparatorInValue() throws Exception {
Map<String, Object> config = getEnvironmentProperties(SameSeparatorInValue.class);
assertKey(config, "key", "my=Value");
assertKey(config, "anotherKey", "another:Value");
}
@Test
public void environmentPropertiesAnotherSeparatorInValue() throws Exception {
Map<String, Object> config = getEnvironmentProperties(
AnotherSeparatorInValue.class);
assertKey(config, "key", "my:Value");
assertKey(config, "anotherKey", "another=Value");
}
@Test
@Ignore
public void environmentPropertiesNewLineInValue() throws Exception {
// gh-4384
Map<String, Object> config = getEnvironmentProperties(NewLineInValue.class);
assertKey(config, "key", "myValue");
assertKey(config, "variables", "foo=FOO\n bar=BAR");
}
private Map<String, Object> getEnvironmentProperties(Class<?> testClass)
throws Exception {
TestContext context = new ExposedTestContextManager(testClass)
.getExposedTestContext();
new IntegrationTestPropertiesListener().prepareTestInstance(context);
MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils
.getField(context, "mergedContextConfiguration");
return TestPropertySourceUtils
.convertInlinedPropertiesToMap(config.getPropertySourceProperties());
}
private void assertKey(Map<String, Object> actual, String key, Object value) {
assertThat(actual.containsKey(key)).as("Key '" + key + "' not found").isTrue();
assertThat(actual.get(key)).isEqualTo(value);
}
@IntegrationTest({ "key=myValue", "anotherKey:anotherValue" })
@ContextConfiguration(classes = Config.class)
static class SimpleConfig {
}
@IntegrationTest({ "server.port=2345" })
@ContextConfiguration(classes = Config.class)
static class OverrideConfig {
}
@IntegrationTest({ "key=myValue", "otherKey=otherValue" })
@ContextConfiguration(classes = Config.class)
static class AppendConfig {
}
@IntegrationTest({ "key=my=Value", "anotherKey:another:Value" })
@ContextConfiguration(classes = Config.class)
static class SameSeparatorInValue {
}
@IntegrationTest({ "key=my:Value", "anotherKey:another=Value" })
@ContextConfiguration(classes = Config.class)
static class AnotherSeparatorInValue {
}
@IntegrationTest({ "key=myValue", "variables=foo=FOO\n bar=BAR" })
@ContextConfiguration(classes = Config.class)
static class NewLineInValue {
}
@Configuration
static class Config {
}
/**
* {@link TestContextManager} which exposes the {@link TestContext}.
*/
private static class ExposedTestContextManager extends TestContextManager {
ExposedTestContextManager(Class<?> testClass) {
super(testClass);
}
public final TestContext getExposedTestContext() {
return super.getTestContext();
}
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2012-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.boot.test;
import javax.annotation.PostConstruct;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationIntegrationTestPropertyLocationTests.MoreConfig;
import org.springframework.boot.test.SpringApplicationIntegrationTestTests.Config;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link IntegrationTest} with {@link TestPropertySource} locations.
*
* @author Phillip Webb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration({ Config.class, MoreConfig.class })
@WebAppConfiguration
@IntegrationTest({ "server.port=0", "value1=123" })
@TestPropertySource(properties = "value2=456", locations = "classpath:/test-property-source-annotation.properties")
public class SpringApplicationIntegrationTestPropertyLocationTests {
@Autowired
private Environment environment;
@Test
public void loadedProperties() throws Exception {
assertThat(this.environment.getProperty("value1")).isEqualTo("123");
assertThat(this.environment.getProperty("value2")).isEqualTo("456");
assertThat(this.environment.getProperty("annotation-referenced"))
.isEqualTo("fromfile");
}
@Configuration
static class MoreConfig {
@Value("${value1}")
private String value1;
@Value("${value2}")
private String value2;
@Value("${annotation-referenced}")
private String annotationReferenced;
@PostConstruct
void checkValues() {
assertThat(this.value1).isEqualTo("123");
assertThat(this.value2).isEqualTo("456");
assertThat(this.annotationReferenced).isEqualTo("fromfile");
}
}
}

View File

@@ -0,0 +1,120 @@
/*
* Copyright 2012-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.boot.test;
import javax.servlet.ServletContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.test.SpringApplicationIntegrationTestTests.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link IntegrationTest}
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration(Config.class)
@WebAppConfiguration
@IntegrationTest({ "server.port=0", "value=123" })
public class SpringApplicationIntegrationTestTests {
@Value("${local.server.port}")
private int port = 0;
@Value("${value}")
private int value = 0;
@Autowired
private WebApplicationContext context;
@Autowired
private ServletContext servletContext;
@Test
public void runAndTestHttpEndpoint() {
assertThat(this.port).isNotEqualTo(8080).isNotEqualTo(0);
String body = new RestTemplate()
.getForObject("http://localhost:" + this.port + "/", String.class);
assertThat(body).isEqualTo("Hello World");
}
@Test
public void annotationAttributesOverridePropertiesFile() throws Exception {
assertThat(this.value).isEqualTo(123);
}
@Test
public void validateWebApplicationContextIsSet() {
assertThat(this.context).isSameAs(
WebApplicationContextUtils.getWebApplicationContext(this.servletContext));
}
@Configuration
@EnableWebMvc
@RestController
protected static class Config {
@Value("${server.port:8080}")
private int port = 8080;
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Bean
public EmbeddedServletContainerFactory embeddedServletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(this.port);
return factory;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholder() {
return new PropertySourcesPlaceholderConfigurer();
}
@RequestMapping("/")
public String home() {
return "Hello World";
}
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright 2012-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.boot.test;
import javax.servlet.ServletContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationMockMvcTests.Config;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Tests for {@link WebAppConfiguration} integration.
*
* @author Stephane Nicoll
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration(classes = Config.class)
@WebAppConfiguration
public class SpringApplicationMockMvcTests {
@Autowired
private WebApplicationContext context;
@Autowired
private ServletContext servletContext;
private MockMvc mvc;
@Before
public void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
@Test
public void testMockHttpEndpoint() throws Exception {
this.mvc.perform(get("/")).andExpect(status().isOk())
.andExpect(content().string("Hello World"));
}
@Test
public void validateWebApplicationContextIsSet() {
assertThat(this.context).isSameAs(
WebApplicationContextUtils.getWebApplicationContext(this.servletContext));
}
@Configuration
@EnableWebMvc
@RestController
protected static class Config {
@RequestMapping("/")
public String home() {
return "Hello World";
}
}
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright 2012-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.boot.test;
import javax.servlet.ServletContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.test.SpringApplicationWebIntegrationTestTests.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link IntegrationTest}
*
* @author Phillip Webb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration(Config.class)
@WebIntegrationTest({ "server.port=0", "value=123" })
public class SpringApplicationWebIntegrationTestTests {
@Value("${local.server.port}")
private int port = 0;
@Value("${value}")
private int value = 0;
@Autowired
private WebApplicationContext context;
@Autowired
private ServletContext servletContext;
@Test
public void runAndTestHttpEndpoint() {
assertThat(this.port).isNotEqualTo(8080).isNotEqualTo(0);
String body = new RestTemplate()
.getForObject("http://localhost:" + this.port + "/", String.class);
assertThat(body).isEqualTo("Hello World");
}
@Test
public void annotationAttributesOverridePropertiesFile() throws Exception {
assertThat(this.value).isEqualTo(123);
}
@Test
public void validateWebApplicationContextIsSet() {
assertThat(this.context).isSameAs(
WebApplicationContextUtils.getWebApplicationContext(this.servletContext));
}
@Configuration
@EnableWebMvc
@RestController
protected static class Config {
@Value("${server.port:8080}")
private int port = 8080;
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Bean
public EmbeddedServletContainerFactory embeddedServletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(this.port);
return factory;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholder() {
return new PropertySourcesPlaceholderConfigurer();
}
@RequestMapping("/")
public String home() {
return "Hello World";
}
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2012-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.boot.test;
import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import javax.servlet.ServletContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.ServletContextAware;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.nullValue;
/**
* Tests for {@link SpringBootMockServletContext}.
*
* @author Phillip Webb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@SpringApplicationConfiguration(SpringBootMockServletContextTests.Config.class)
@WebAppConfiguration("src/test/webapp")
public class SpringBootMockServletContextTests implements ServletContextAware {
private ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Test
public void getResourceLocation() throws Exception {
testResource("/inwebapp", "src/test/webapp");
testResource("/inmetainfresources", "/META-INF/resources");
testResource("/inresources", "/resources");
testResource("/instatic", "/static");
testResource("/inpublic", "/public");
}
private void testResource(String path, String expectedLocation)
throws MalformedURLException {
URL resource = this.servletContext.getResource(path);
assertThat(resource).isNotNull();
assertThat(resource.getPath()).contains(expectedLocation);
}
// gh-2654
@Test
public void getRootUrlExistsAndIsEmpty() throws Exception {
SpringBootMockServletContext context = new SpringBootMockServletContext(
"src/test/doesntexist") {
@Override
protected String getResourceLocation(String path) {
// Don't include the Spring Boot defaults for this test
return getResourceBasePathLocation(path);
};
};
URL resource = context.getResource("/");
assertThat(resource).isNotEqualTo(nullValue());
File file = new File(URLDecoder.decode(resource.getPath(), "UTF-8"));
assertThat(file).exists().isDirectory();
String[] contents = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return !(".".equals(name) || "..".equals(name));
}
});
assertThat(contents).isNotEqualTo(nullValue());
assertThat(contents.length).isEqualTo(0);
}
@Configuration
static class Config {
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2012-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.boot.test;
import org.apache.http.client.config.RequestConfig;
import org.junit.Test;
import org.springframework.boot.test.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory;
import org.springframework.boot.test.TestRestTemplate.HttpClientOption;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link TestRestTemplate}.
*
* @author Dave Syer
* @author Phillip Webb
*/
public class TestRestTemplateTests {
@Test
public void simple() {
// The Apache client is on the classpath so we get the fully-fledged factory
assertThat(new TestRestTemplate().getRequestFactory())
.isInstanceOf(HttpComponentsClientHttpRequestFactory.class);
}
@Test
public void authenticated() {
assertThat(new TestRestTemplate("user", "password").getRequestFactory())
.isInstanceOf(InterceptingClientHttpRequestFactory.class);
}
@Test
public void options() throws Exception {
TestRestTemplate template = new TestRestTemplate(
HttpClientOption.ENABLE_REDIRECTS);
CustomHttpComponentsClientHttpRequestFactory factory = (CustomHttpComponentsClientHttpRequestFactory) template
.getRequestFactory();
RequestConfig config = factory.getRequestConfig();
assertThat(config.isRedirectsEnabled()).isTrue();
}
}

View File

@@ -0,0 +1,5 @@
foo: bucket
value: 1234
my.property: fromapplicationproperties
sample.app.test.prop: *
my.placeholder: ${my.fallback}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="foo" class="java.lang.String">
<constructor-arg>
<value>World</value>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1 @@
annotation-referenced=fromfile

View File

@@ -0,0 +1,3 @@
beans {
foo String, "World"
}