Polish
This commit is contained in:
@@ -89,10 +89,7 @@ import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
*/
|
||||
public class EmbeddedWebApplicationContext extends GenericWebApplicationContext {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final String SERVER = "server";
|
||||
private static final String DEFAULT_SERVER_NAME = "server";
|
||||
|
||||
/**
|
||||
* Constant value for the DispatcherServlet bean name. A Servlet bean with this name
|
||||
@@ -166,7 +163,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
|
||||
EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();
|
||||
this.embeddedServletContainer = containerFactory
|
||||
.getEmbeddedServletContainer(getSelfInitializer());
|
||||
this.containers.put(SERVER, this.embeddedServletContainer);
|
||||
this.containers.put(DEFAULT_SERVER_NAME, this.embeddedServletContainer);
|
||||
}
|
||||
else if (getServletContext() != null) {
|
||||
try {
|
||||
@@ -391,7 +388,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
|
||||
try {
|
||||
this.embeddedServletContainer.stop();
|
||||
this.embeddedServletContainer = null;
|
||||
this.containers.remove(SERVER);
|
||||
this.containers.remove(DEFAULT_SERVER_NAME);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
@@ -439,7 +436,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
|
||||
* A registry of embedded containers by name. The
|
||||
* {@link #getEmbeddedServletContainer() canonical container} is called "server".
|
||||
* Anyone else who creates one can register it with whatever name they please.
|
||||
*
|
||||
* @return the containers
|
||||
*/
|
||||
public Map<String, EmbeddedServletContainer> getEmbeddedServletContainers() {
|
||||
|
||||
@@ -70,7 +70,6 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord
|
||||
|
||||
/**
|
||||
* Flag to indicate that the registration is enabled.
|
||||
*
|
||||
* @param enabled the enabled to set
|
||||
*/
|
||||
public void setEnabled(boolean enabled) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
@@ -18,34 +18,36 @@ package org.springframework.boot.test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
|
||||
/**
|
||||
* Listener that injects the server port (if one is discoverable from the application
|
||||
* context)into a field annotated with {@link Value @Value("dynamic.port")}.
|
||||
* Listener that injects the server port into an {@link Environment} property named
|
||||
* {@literal local.<server>.port}. Useful when the server is running on a dynamic
|
||||
* port.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class EmbeddedServletContainerListener extends AbstractTestExecutionListener {
|
||||
public class EmbeddedServletContainerTestExecutionListener extends
|
||||
AbstractTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void prepareTestInstance(TestContext testContext) throws Exception {
|
||||
ApplicationContext context = testContext.getApplicationContext();
|
||||
if (!(context instanceof EmbeddedWebApplicationContext)) {
|
||||
return;
|
||||
if (context instanceof EmbeddedWebApplicationContext) {
|
||||
prepareTestInstance((EmbeddedWebApplicationContext) context);
|
||||
}
|
||||
EmbeddedWebApplicationContext embedded = (EmbeddedWebApplicationContext) context;
|
||||
Map<String, EmbeddedServletContainer> containers = embedded
|
||||
.getEmbeddedServletContainers();
|
||||
for (String name : containers.keySet()) {
|
||||
int port = containers.get(name).getPort();
|
||||
EnvironmentTestUtils.addEnvironment(embedded, "local." + name + ".port:"
|
||||
+ port);
|
||||
}
|
||||
|
||||
private void prepareTestInstance(EmbeddedWebApplicationContext context) {
|
||||
for (Map.Entry<String, EmbeddedServletContainer> entry : context
|
||||
.getEmbeddedServletContainers().entrySet()) {
|
||||
EnvironmentTestUtils.addEnvironment(context, "local." + entry.getKey()
|
||||
+ ".port:" + entry.getValue().getPort());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,6 @@ public abstract class EnvironmentTestUtils {
|
||||
* Add additional (high priority) values to an {@link Environment} owned by an
|
||||
* {@link ApplicationContext}. Name-value pairs can be specified with colon (":") or
|
||||
* equals ("=") separators.
|
||||
*
|
||||
* @param context the context with an environment to modify
|
||||
* @param pairs the name:value pairs
|
||||
*/
|
||||
@@ -49,7 +48,6 @@ public abstract class EnvironmentTestUtils {
|
||||
/**
|
||||
* Add additional (high priority) values to an {@link Environment}. Name-value pairs
|
||||
* can be specified with colon (":") or equals ("=") separators.
|
||||
*
|
||||
* @param environment the environment to modify
|
||||
* @param pairs the name:value pairs
|
||||
*/
|
||||
@@ -61,7 +59,6 @@ public abstract class EnvironmentTestUtils {
|
||||
/**
|
||||
* Add additional (high priority) values to an {@link Environment}. Name-value pairs
|
||||
* can be specified with colon (":") or equals ("=") separators.
|
||||
*
|
||||
* @param environment the environment to modify
|
||||
* @param name the property source name
|
||||
* @param pairs the name:value pairs
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||
@@ -40,11 +41,17 @@ import org.springframework.test.context.transaction.TransactionalTestExecutionLi
|
||||
@Target(ElementType.TYPE)
|
||||
// Leave out the ServletTestExecutionListener because it only deals with Mock* servlet
|
||||
// stuff. A real embedded application will not need the mocks.
|
||||
@TestExecutionListeners(listeners = { EmbeddedServletContainerListener.class,
|
||||
@TestExecutionListeners(listeners = {
|
||||
EmbeddedServletContainerTestExecutionListener.class,
|
||||
DependencyInjectionTestExecutionListener.class,
|
||||
DirtiesContextTestExecutionListener.class,
|
||||
TransactionalTestExecutionListener.class })
|
||||
public @interface IntegrationTest {
|
||||
|
||||
String[] value() default "";
|
||||
/**
|
||||
* Properties in form {@literal key=value} that should be added to the Spring
|
||||
* {@link Environment} before the test runs.
|
||||
*/
|
||||
String[] value() default {};
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.boot.test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -66,19 +67,18 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
public class SpringApplicationContextLoader extends AbstractContextLoader {
|
||||
|
||||
@Override
|
||||
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig)
|
||||
public ApplicationContext loadContext(MergedContextConfiguration config)
|
||||
throws Exception {
|
||||
|
||||
SpringApplication application = getSpringApplication();
|
||||
application.setSources(getSources(mergedConfig));
|
||||
if (!ObjectUtils.isEmpty(mergedConfig.getActiveProfiles())) {
|
||||
application.setAdditionalProfiles(mergedConfig.getActiveProfiles());
|
||||
application.setSources(getSources(config));
|
||||
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
|
||||
application.setAdditionalProfiles(config.getActiveProfiles());
|
||||
}
|
||||
application.setDefaultProperties(getArgs(mergedConfig));
|
||||
List<ApplicationContextInitializer<?>> initializers = getInitializers(
|
||||
mergedConfig, application);
|
||||
if (mergedConfig instanceof WebMergedContextConfiguration) {
|
||||
new WebConfigurer().configure(mergedConfig, application, initializers);
|
||||
application.setDefaultProperties(getEnvironmentProperties(config));
|
||||
List<ApplicationContextInitializer<?>> initializers = getInitializers(config,
|
||||
application);
|
||||
if (config instanceof WebMergedContextConfiguration) {
|
||||
new WebConfigurer().configure(config, application, initializers);
|
||||
}
|
||||
else {
|
||||
application.setWebEnvironment(false);
|
||||
@@ -134,32 +134,41 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
|
||||
.detectDefaultConfigurationClasses(declaringClass);
|
||||
}
|
||||
|
||||
private Map<String, Object> getArgs(MergedContextConfiguration mergedConfig) {
|
||||
Map<String, Object> args = new LinkedHashMap<String, Object>();
|
||||
private Map<String, Object> getEnvironmentProperties(MergedContextConfiguration config) {
|
||||
Map<String, Object> properties = new LinkedHashMap<String, Object>();
|
||||
// JMX bean names will clash if the same bean is used in multiple contexts
|
||||
args.put("spring.jmx.enabled", "false");
|
||||
disableJmx(properties);
|
||||
IntegrationTest annotation = AnnotationUtils.findAnnotation(
|
||||
mergedConfig.getTestClass(), IntegrationTest.class);
|
||||
if (annotation == null) {
|
||||
// Not running an embedded server, just setting up web context
|
||||
args.put("server.port", "-1");
|
||||
}
|
||||
else {
|
||||
args.putAll(extractProperties(annotation.value()));
|
||||
}
|
||||
return args;
|
||||
config.getTestClass(), IntegrationTest.class);
|
||||
properties.putAll(getEnvironmentProperties(annotation));
|
||||
return properties;
|
||||
}
|
||||
|
||||
private Map<String, String> extractProperties(String[] values) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
private void disableJmx(Map<String, Object> properties) {
|
||||
properties.put("spring.jmx.enabled", "false");
|
||||
}
|
||||
|
||||
private Map<String, String> getEnvironmentProperties(IntegrationTest annotation) {
|
||||
if (annotation == null) {
|
||||
return getDefaultEnvironmentProperties();
|
||||
}
|
||||
return extractEnvironmentProperties(annotation.value());
|
||||
}
|
||||
|
||||
private Map<String, String> getDefaultEnvironmentProperties() {
|
||||
return Collections.singletonMap("server.port", "-1");
|
||||
}
|
||||
|
||||
private Map<String, String> extractEnvironmentProperties(String[] values) {
|
||||
Map<String, String> properties = new HashMap<String, String>();
|
||||
for (String pair : values) {
|
||||
int index = pair.indexOf(":");
|
||||
index = index < 0 ? index = pair.indexOf("=") : index;
|
||||
index = (index < 0 ? index = pair.indexOf("=") : index);
|
||||
String key = pair.substring(0, index > 0 ? index : pair.length());
|
||||
String value = index > 0 ? pair.substring(index + 1) : "";
|
||||
map.put(key.trim(), value.trim());
|
||||
String value = (index > 0 ? pair.substring(index + 1) : "");
|
||||
properties.put(key.trim(), value.trim());
|
||||
}
|
||||
return map;
|
||||
return properties;
|
||||
}
|
||||
|
||||
private List<ApplicationContextInitializer<?>> getInitializers(
|
||||
|
||||
@@ -264,7 +264,7 @@ public class RelaxedDataBinderTests {
|
||||
bind(target, "nested.foo: bar\n" + "nested.value: 123");
|
||||
assertEquals("123", target.getNested().get("value"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBindNestedMapOfString() throws Exception {
|
||||
TargetWithNestedMapOfString target = new TargetWithNestedMapOfString();
|
||||
@@ -510,7 +510,7 @@ public class RelaxedDataBinderTests {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static class TargetWithNestedUntypedMap {
|
||||
|
||||
@@ -526,7 +526,6 @@ public class RelaxedDataBinderTests {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class TargetWithNestedMapOfString {
|
||||
|
||||
private Map<String, String> nested;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
@@ -23,6 +23,8 @@ import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests for {@link RandomValuePropertySource}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class RandomValuePropertySourceTests {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
@@ -21,6 +21,8 @@ import org.junit.Test;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests for {@link PropertySourcesLoader}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class PropertySourcesLoaderTests {
|
||||
@@ -28,7 +30,7 @@ public class PropertySourcesLoaderTests {
|
||||
private PropertySourcesLoader loader = new PropertySourcesLoader();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
public void fileExtensions() {
|
||||
assertTrue(this.loader.getAllFileExtensions().contains("yml"));
|
||||
assertTrue(this.loader.getAllFileExtensions().contains("yaml"));
|
||||
assertTrue(this.loader.getAllFileExtensions().contains("properties"));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
@@ -24,6 +24,8 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* Tests for {@link YamlPropertySourceLoader}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class YamlPropertySourceLoaderTests {
|
||||
@@ -31,9 +33,9 @@ public class YamlPropertySourceLoaderTests {
|
||||
private YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
PropertySource<?> source = this.loader.load("resource", new ByteArrayResource(
|
||||
"foo:\n bar: spam".getBytes()), null);
|
||||
public void load() throws Exception {
|
||||
ByteArrayResource resource = new ByteArrayResource("foo:\n bar: spam".getBytes());
|
||||
PropertySource<?> source = this.loader.load("resource", resource, null);
|
||||
assertNotNull(source);
|
||||
assertEquals("spam", source.getProperty("foo.bar"));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2014 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.
|
||||
@@ -23,6 +23,8 @@ import org.springframework.http.client.InterceptingClientHttpRequestFactory;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests for {@link TestRestTemplate}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class TestRestTemplateTests {
|
||||
|
||||
Reference in New Issue
Block a user