Applied checkstyle and turned it on by default

This commit is contained in:
Marcin Grzejszczak
2019-02-01 12:50:02 +01:00
parent e024f6f5c2
commit ae314100f5
313 changed files with 8658 additions and 4416 deletions

View File

@@ -5,6 +5,7 @@ import java.util.function.Function;
import org.assertj.core.util.Lists;
import org.junit.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.context.restart.RestartEndpoint;
@@ -21,9 +22,20 @@ import static org.junit.Assert.assertThat;
/**
* @author Spencer Gibb
*/
//TODO: super slow. Port to @SpringBootTest
// TODO: super slow. Port to @SpringBootTest
public class LifecycleMvcAutoConfigurationTests {
private static ConfigurableApplicationContext getApplicationContext(
Class<?> configuration, String... properties) {
List<String> defaultProperties = Lists.newArrayList(properties);
defaultProperties.add("server.port=0");
defaultProperties.add("spring.jmx.default-domain=${random.uuid}");
return new SpringApplicationBuilder(configuration)
.properties(defaultProperties.toArray(new String[] {})).run();
}
@Test
public void environmentWebEndpointExtensionDisabled() {
beanNotCreated("environmentWebEndpointExtension",
@@ -45,47 +57,41 @@ public class LifecycleMvcAutoConfigurationTests {
// restartEndpoint
@Test
public void restartEndpointDisabled() {
beanNotCreated("restartEndpoint",
"management.endpoint.restart.enabled=false");
beanNotCreated("restartEndpoint", "management.endpoint.restart.enabled=false");
}
@Test
public void restartEndpointGloballyDisabled() {
beanNotCreated("restartEndpoint",
"management.endpoint.default.enabled=false");
beanNotCreated("restartEndpoint", "management.endpoint.default.enabled=false");
}
@Test
public void restartEndpointEnabled() {
beanCreatedAndEndpointEnabled("restartEndpoint", RestartEndpoint.class,
RestartEndpoint::restart,
"management.endpoint.restart.enabled=true");
RestartEndpoint::restart, "management.endpoint.restart.enabled=true");
}
// pauseEndpoint
@Test
public void pauseEndpointDisabled() {
beanNotCreated("pauseEndpoint",
"management.endpoint.pause.enabled=false");
beanNotCreated("pauseEndpoint", "management.endpoint.pause.enabled=false");
}
@Test
public void pauseEndpointRestartDisabled() {
beanNotCreated("pauseEndpoint",
"management.endpoint.restart.enabled=false",
beanNotCreated("pauseEndpoint", "management.endpoint.restart.enabled=false",
"management.endpoint.pause.enabled=true");
}
@Test
public void pauseEndpointGloballyDisabled() {
beanNotCreated("pauseEndpoint",
"management.endpoint.default.enabled=false");
beanNotCreated("pauseEndpoint", "management.endpoint.default.enabled=false");
}
@Test
public void pauseEndpointEnabled() {
beanCreatedAndEndpointEnabled("pauseEndpoint", RestartEndpoint.PauseEndpoint.class,
RestartEndpoint.PauseEndpoint::pause,
beanCreatedAndEndpointEnabled("pauseEndpoint",
RestartEndpoint.PauseEndpoint.class, RestartEndpoint.PauseEndpoint::pause,
"management.endpoint.restart.enabled=true",
"management.endpoint.pause.enabled=true");
}
@@ -93,48 +99,53 @@ public class LifecycleMvcAutoConfigurationTests {
// resumeEndpoint
@Test
public void resumeEndpointDisabled() {
beanNotCreated("resumeEndpoint",
"management.endpoint.restart.enabled=true",
beanNotCreated("resumeEndpoint", "management.endpoint.restart.enabled=true",
"management.endpoint.resume.enabled=false");
}
@Test
public void resumeEndpointRestartDisabled() {
beanNotCreated("resumeEndpoint",
"management.endpoint.restart.enabled=false",
beanNotCreated("resumeEndpoint", "management.endpoint.restart.enabled=false",
"management.endpoint.resume.enabled=true");
}
@Test
public void resumeEndpointGloballyDisabled() {
beanNotCreated("resumeEndpoint",
"management.endpoint.default.enabled=false");
beanNotCreated("resumeEndpoint", "management.endpoint.default.enabled=false");
}
@Test
public void resumeEndpointEnabled() {
beanCreatedAndEndpointEnabled("resumeEndpoint", RestartEndpoint.ResumeEndpoint.class,
beanCreatedAndEndpointEnabled("resumeEndpoint",
RestartEndpoint.ResumeEndpoint.class,
RestartEndpoint.ResumeEndpoint::resume,
"management.endpoint.restart.enabled=true",
"management.endpoint.resume.enabled=true");
}
private void beanNotCreated(String beanName, String... contextProperties) {
try (ConfigurableApplicationContext context = getApplicationContext(Config.class, contextProperties)) {
assertThat("bean was created", context.containsBeanDefinition(beanName), equalTo(false));
try (ConfigurableApplicationContext context = getApplicationContext(Config.class,
contextProperties)) {
assertThat("bean was created", context.containsBeanDefinition(beanName),
equalTo(false));
}
}
private void beanCreated(String beanName, String... contextProperties) {
try (ConfigurableApplicationContext context = getApplicationContext(Config.class, contextProperties)) {
assertThat("bean was not created", context.containsBeanDefinition(beanName), equalTo(true));
try (ConfigurableApplicationContext context = getApplicationContext(Config.class,
contextProperties)) {
assertThat("bean was not created", context.containsBeanDefinition(beanName),
equalTo(true));
}
}
@SuppressWarnings("unchecked")
private <T> void beanCreatedAndEndpointEnabled(String beanName, Class<T> type, Function<T, Object> function, String... properties) {
try (ConfigurableApplicationContext context = getApplicationContext(Config.class, properties)) {
assertThat("bean was not created", context.containsBeanDefinition(beanName), equalTo(true));
private <T> void beanCreatedAndEndpointEnabled(String beanName, Class<T> type,
Function<T, Object> function, String... properties) {
try (ConfigurableApplicationContext context = getApplicationContext(Config.class,
properties)) {
assertThat("bean was not created", context.containsBeanDefinition(beanName),
equalTo(true));
Object endpoint = context.getBean(beanName, type);
Object result = function.apply((T) endpoint);
@@ -144,19 +155,10 @@ public class LifecycleMvcAutoConfigurationTests {
}
}
private static ConfigurableApplicationContext getApplicationContext(
Class<?> configuration, String... properties) {
List<String> defaultProperties = Lists.newArrayList(properties);
defaultProperties.add("server.port=0");
defaultProperties.add("spring.jmx.default-domain=${random.uuid}");
return new SpringApplicationBuilder(configuration).properties(defaultProperties.toArray(new String[]{})).run();
}
@Configuration
@EnableAutoConfiguration
static class Config {
}
}

View File

@@ -2,6 +2,7 @@ package org.springframework.cloud.autoconfigure;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
@@ -17,26 +18,31 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Spencer Gibb
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions({"spring-boot-actuator-*.jar", "spring-boot-starter-actuator-*.jar"})
@ClassPathExclusions({ "spring-boot-actuator-*.jar",
"spring-boot-starter-actuator-*.jar" })
public class RefreshAutoConfigurationClassPathTests {
private static ConfigurableApplicationContext getApplicationContext(
Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).web(WebApplicationType.NONE)
.properties(properties).run();
}
@Test
public void refreshEventListenerCreated() {
try (ConfigurableApplicationContext context = getApplicationContext(
Config.class)) {
assertThat(context.getBeansOfType(RefreshEventListener.class)).as("RefreshEventListeners not created").isNotEmpty();
assertThat(context.containsBean("refreshEndpoint")).as("refreshEndpoint created").isFalse();
assertThat(context.getBeansOfType(RefreshEventListener.class))
.as("RefreshEventListeners not created").isNotEmpty();
assertThat(context.containsBean("refreshEndpoint"))
.as("refreshEndpoint created").isFalse();
}
}
private static ConfigurableApplicationContext getApplicationContext(
Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).web(WebApplicationType.NONE).properties(properties).run();
}
@Configuration
@EnableAutoConfiguration
static class Config {
}
}

View File

@@ -19,25 +19,28 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Spencer Gibb
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions({"spring-boot-actuator-autoconfigure-*.jar", "spring-boot-starter-actuator-*.jar"})
@ClassPathExclusions({ "spring-boot-actuator-autoconfigure-*.jar",
"spring-boot-starter-actuator-*.jar" })
public class RefreshAutoConfigurationMoreClassPathTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();
@Test
public void unknownClassProtected() {
try (ConfigurableApplicationContext context = getApplicationContext(
Config.class, "debug=true")) {
String output = this.outputCapture.toString();
assertThat(output).doesNotContain("Failed to introspect annotations on [class org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration")
.doesNotContain("TypeNotPresentExceptionProxy");
}
}
private static ConfigurableApplicationContext getApplicationContext(
Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).web(WebApplicationType.NONE).properties(properties).run();
return new SpringApplicationBuilder(configuration).web(WebApplicationType.NONE)
.properties(properties).run();
}
@Test
public void unknownClassProtected() {
try (ConfigurableApplicationContext context = getApplicationContext(Config.class,
"debug=true")) {
String output = this.outputCapture.toString();
assertThat(output).doesNotContain(
"Failed to introspect annotations on [class org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration")
.doesNotContain("TypeNotPresentExceptionProxy");
}
}
@Configuration
@@ -45,4 +48,5 @@ public class RefreshAutoConfigurationMoreClassPathTests {
static class Config {
}
}

View File

@@ -24,12 +24,18 @@ public class RefreshAutoConfigurationTests {
@Rule
public OutputCapture output = new OutputCapture();
private static ConfigurableApplicationContext getApplicationContext(
WebApplicationType type, Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).web(type)
.properties(properties).properties("server.port=0").run();
}
@Test
public void noWarnings() {
try (ConfigurableApplicationContext context = getApplicationContext(
WebApplicationType.NONE, Config.class)) {
assertThat(context.containsBean("refreshScope")).isTrue();
assertThat(output.toString()).doesNotContain("WARN");
assertThat(this.output.toString()).doesNotContain("WARN");
}
}
@@ -63,12 +69,6 @@ public class RefreshAutoConfigurationTests {
}
}
private static ConfigurableApplicationContext getApplicationContext(
WebApplicationType type, Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).web(type)
.properties(properties).properties("server.port=0").run();
}
@Configuration
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(ConfigProps.class)
@@ -78,7 +78,9 @@ public class RefreshAutoConfigurationTests {
@ConfigurationProperties("config")
static class ConfigProps {
private String foo;
private boolean sealed;
public String getFoo() {
@@ -92,5 +94,7 @@ public class RefreshAutoConfigurationTests {
this.foo = foo;
this.sealed = true;
}
}
}

View File

@@ -2,6 +2,7 @@ package org.springframework.cloud.bootstrap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
@@ -13,16 +14,15 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertFalse;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class,
properties = "spring.cloud.bootstrap.enabled:false")
@SpringBootTest(classes = Application.class, properties = "spring.cloud.bootstrap.enabled:false")
public class BootstrapDisabledAutoConfigurationIntegrationTests {
@Autowired
private ConfigurableEnvironment environment;
@Test
public void noBootstrapProperties() {
assertFalse(environment.getPropertySources().contains("bootstrap"));
assertFalse(this.environment.getPropertySources().contains("bootstrap"));
}
@EnableAutoConfiguration

View File

@@ -3,6 +3,7 @@ package org.springframework.cloud.bootstrap;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
@@ -25,7 +26,7 @@ public class BootstrapOrderingAutoConfigurationIntegrationTests {
private ConfigurableEnvironment environment;
@Test
@Ignore //FIXME: spring boot 2.0.0
@Ignore // FIXME: spring boot 2.0.0
public void bootstrapPropertiesExist() {
assertTrue(this.environment.getPropertySources().contains(
PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME));

View File

@@ -7,6 +7,7 @@ import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
@@ -25,8 +26,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class,
properties = { "encrypt.key:deadbeef", "spring.cloud.bootstrap.name:custom" })
@SpringBootTest(classes = Application.class, properties = { "encrypt.key:deadbeef",
"spring.cloud.bootstrap.name:custom" })
@ActiveProfiles("encrypt")
public class BootstrapOrderingCustomPropertySourceIntegrationTests {
@@ -34,7 +35,7 @@ public class BootstrapOrderingCustomPropertySourceIntegrationTests {
private ConfigurableEnvironment environment;
@Test
@Ignore //FIXME: spring boot 2.0.0
@Ignore // FIXME: spring boot 2.0.0
public void bootstrapPropertiesExist() {
assertTrue(this.environment.getPropertySources().contains(
PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME));
@@ -56,7 +57,7 @@ public class BootstrapOrderingCustomPropertySourceIntegrationTests {
protected static class PropertySourceConfiguration implements PropertySourceLocator {
public static Map<String, Object> MAP = new HashMap<String, Object>(
Collections.<String, Object> singletonMap("custom.foo",
Collections.<String, Object>singletonMap("custom.foo",
"{cipher}6154ca04d4bb6144d672c4e3d750b5147116dd381946d51fa44f8bc25dc256f4"));
@Override

View File

@@ -4,6 +4,7 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
@@ -16,10 +17,12 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class,
properties = "spring.cloud.bootstrap.name:json")
@SpringBootTest(classes = Application.class, properties = "spring.cloud.bootstrap.name:json")
public class BootstrapOrderingSpringApplicationJsonIntegrationTests {
@Autowired
private ConfigurableEnvironment environment;
@BeforeClass
public static void spikeJson() {
System.setProperty("SPRING_APPLICATION_JSON", "{\"message\":\"From JSON\"}");
@@ -30,9 +33,6 @@ public class BootstrapOrderingSpringApplicationJsonIntegrationTests {
System.clearProperty("SPRING_APPLICATION_JSON");
}
@Autowired
private ConfigurableEnvironment environment;
@Test
public void bootstrapPropertiesExist() {
assertTrue(this.environment.getPropertySources()
@@ -43,6 +43,7 @@ public class BootstrapOrderingSpringApplicationJsonIntegrationTests {
@EnableAutoConfiguration
@Configuration
protected static class Application {
}
}

View File

@@ -26,6 +26,7 @@ public class BootstrapSourcesOrderingTests {
@EnableAutoConfiguration
@Configuration
protected static class Application {
}
}

View File

@@ -26,6 +26,7 @@ import java.util.Locale;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
@@ -35,8 +36,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class,
properties = "debug=true")
@SpringBootTest(classes = TestApplication.class, properties = "debug=true")
public class MessageSourceConfigurationTests {
@Autowired
@@ -44,7 +44,8 @@ public class MessageSourceConfigurationTests {
@Test
public void loadsMessage() {
Assert.assertEquals("Hello World!", this.messageSource.getMessage("hello.message", null, Locale.getDefault()));
Assert.assertEquals("Hello World!", this.messageSource.getMessage("hello.message",
null, Locale.getDefault()));
}
@Configuration

View File

@@ -24,15 +24,16 @@ import static org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapCon
@EnableConfigurationProperties
public class TestBootstrapConfiguration {
public static List<String> fooSightings = null;
public TestBootstrapConfiguration() {
firstToBeCreated.compareAndSet(null, TestBootstrapConfiguration.class);
}
public static List<String> fooSightings = null;
@Bean
@Qualifier("foo-during-bootstrap")
public String fooDuringBootstrap(ConfigurableEnvironment environment, ApplicationEventPublisher publisher) {
public String fooDuringBootstrap(ConfigurableEnvironment environment,
ApplicationEventPublisher publisher) {
String property = environment.getProperty("test.bootstrap.foo", "undefined");
if (fooSightings != null) {

View File

@@ -12,13 +12,13 @@ import org.springframework.core.annotation.Order;
@Order(Ordered.HIGHEST_PRECEDENCE)
public class TestHigherPriorityBootstrapConfiguration {
static final AtomicReference<Class<?>> firstToBeCreated = new AtomicReference<>();
public static final AtomicInteger count = new AtomicInteger();
static final AtomicReference<Class<?>> firstToBeCreated = new AtomicReference<>();
public TestHigherPriorityBootstrapConfiguration() {
count.incrementAndGet();
firstToBeCreated.compareAndSet(null, TestHigherPriorityBootstrapConfiguration.class);
firstToBeCreated.compareAndSet(null,
TestHigherPriorityBootstrapConfiguration.class);
}
}

View File

@@ -53,13 +53,13 @@ import static org.junit.Assert.assertTrue;
*/
public class BootstrapConfigurationTests {
@Rule
public ExpectedException expected = ExpectedException.none();
private ConfigurableApplicationContext context;
private ConfigurableApplicationContext sibling;
@Rule
public ExpectedException expected = ExpectedException.none();
@After
public void close() {
// Expected.* is bound to the PropertySourceConfiguration below
@@ -111,7 +111,6 @@ public class BootstrapConfigurationTests {
/**
* Running the test from maven will start from a different directory then starting it
* from intellij
*
* @return
*/
private String getExternalProperties() {
@@ -275,10 +274,10 @@ public class BootstrapConfigurationTests {
this.context = new SpringApplicationBuilder().sources(BareConfiguration.class)
.child(BareConfiguration.class).web(WebApplicationType.NONE).run();
assertEquals(1, TestHigherPriorityBootstrapConfiguration.count.get());
assertNotNull(context.getParent());
assertEquals("bootstrap", context.getParent().getParent().getId());
assertNull(context.getParent().getParent().getParent());
assertEquals("bar", context.getEnvironment().getProperty("custom.foo"));
assertNotNull(this.context.getParent());
assertEquals("bootstrap", this.context.getParent().getParent().getId());
assertNull(this.context.getParent().getParent().getParent());
assertEquals("bar", this.context.getEnvironment().getProperty("custom.foo"));
}
@Test
@@ -294,18 +293,18 @@ public class BootstrapConfigurationTests {
.properties("spring.application.name=context")
.web(WebApplicationType.NONE).run();
assertEquals(1, TestHigherPriorityBootstrapConfiguration.count.get());
assertNotNull(context.getParent());
assertEquals("bootstrap", context.getParent().getParent().getId());
assertNull(context.getParent().getParent().getParent());
assertEquals("context", context.getEnvironment().getProperty("custom.foo"));
assertNotNull(this.context.getParent());
assertEquals("bootstrap", this.context.getParent().getParent().getId());
assertNull(this.context.getParent().getParent().getParent());
assertEquals("context", this.context.getEnvironment().getProperty("custom.foo"));
assertEquals("context",
context.getEnvironment().getProperty("spring.application.name"));
assertNotNull(sibling.getParent());
assertEquals("bootstrap", sibling.getParent().getParent().getId());
assertNull(sibling.getParent().getParent().getParent());
assertEquals("sibling", sibling.getEnvironment().getProperty("custom.foo"));
this.context.getEnvironment().getProperty("spring.application.name"));
assertNotNull(this.sibling.getParent());
assertEquals("bootstrap", this.sibling.getParent().getParent().getId());
assertNull(this.sibling.getParent().getParent().getParent());
assertEquals("sibling", this.sibling.getEnvironment().getProperty("custom.foo"));
assertEquals("sibling",
sibling.getEnvironment().getProperty("spring.application.name"));
this.sibling.getEnvironment().getProperty("spring.application.name"));
}
@Test
@@ -375,6 +374,7 @@ public class BootstrapConfigurationTests {
@Configuration
@EnableConfigurationProperties
protected static class BareConfiguration {
}
@Configuration
@@ -416,6 +416,7 @@ public class BootstrapConfigurationTests {
public void setFail(boolean fail) {
this.fail = fail;
}
}
}

View File

@@ -16,21 +16,22 @@
package org.springframework.cloud.bootstrap.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.springframework.boot.WebApplicationType.NONE;
import org.junit.Test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.springframework.boot.WebApplicationType.NONE;
/**
* Integration tests for Bootstrap Listener's functionality of adding a bootstrap context
* as the root Application Context
*
*
* @author Biju Kunjummen
*/
public class BootstrapListenerHierarchyIntegrationTests {
@@ -88,6 +89,7 @@ public class BootstrapListenerHierarchyIntegrationTests {
@Configuration
static class BasicConfiguration {
}
@Configuration
@@ -97,5 +99,7 @@ public class BootstrapListenerHierarchyIntegrationTests {
public String rootBean() {
return "rootBean";
}
}
}
}

View File

@@ -1,7 +1,7 @@
package org.springframework.cloud.bootstrap.encrypt;
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2013-2019 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.
@@ -70,17 +70,17 @@ public class EncryptionBootstrapConfigurationTests {
context.close();
}
@Test
public void rsaProperties() {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
EncryptionBootstrapConfiguration.class).web(WebApplicationType.NONE).properties(
"encrypt.key-store.location:classpath:/server.jks",
"encrypt.key-store.password:letmein",
"encrypt.key-store.alias:mytestkey", "encrypt.key-store.secret:changeme",
"encrypt.rsa.strong:true",
"encrypt.rsa.salt:foobar")
.run();
EncryptionBootstrapConfiguration.class)
.web(WebApplicationType.NONE)
.properties("encrypt.key-store.location:classpath:/server.jks",
"encrypt.key-store.password:letmein",
"encrypt.key-store.alias:mytestkey",
"encrypt.key-store.secret:changeme",
"encrypt.rsa.strong:true", "encrypt.rsa.salt:foobar")
.run();
RsaProperties properties = context.getBean(RsaProperties.class);
assertEquals("foobar", properties.getSalt());
assertTrue(properties.isStrong());
@@ -88,7 +88,6 @@ public class EncryptionBootstrapConfigurationTests {
context.close();
}
@Test
public void nonExistentKeystoreLocationShouldNotBeAllowed() {
try {
@@ -107,4 +106,5 @@ public class EncryptionBootstrapConfigurationTests {
assertThat(e).hasRootCauseInstanceOf(IllegalStateException.class);
}
}
}

View File

@@ -1,6 +1,7 @@
package org.springframework.cloud.bootstrap.encrypt;
import org.junit.Test;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -40,14 +41,17 @@ public class EncryptionIntegrationTests {
@ConfigurationProperties("foo")
protected static class PasswordProperties {
private String password;
public String getPassword() {
return password;
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
}

View File

@@ -15,14 +15,15 @@
*/
package org.springframework.cloud.bootstrap.encrypt;
import java.nio.charset.Charset;
import org.junit.Test;
import org.springframework.cloud.context.encrypt.EncryptorFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.util.StreamUtils;
import java.nio.charset.Charset;
import static org.junit.Assert.assertEquals;
/**
@@ -49,4 +50,5 @@ public class EncryptorFactoryTests {
+ "MIIEowIBAAKCAQEAwClFgrRa/PUHPIJr9gvIPL6g6Rjp/TVZmVNOf2fL96DYbkj5\n";
new EncryptorFactory().create(key);
}
}
}

View File

@@ -212,4 +212,5 @@ public class EnvironmentDecryptApplicationInitializerTests {
verify(encryptor).decrypt("bar2");
verifyNoMoreInteractions(encryptor);
}
}

View File

@@ -1,6 +1,6 @@
package org.springframework.cloud.bootstrap.encrypt;
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2013-2019 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.
@@ -35,30 +35,32 @@ import static org.hamcrest.Matchers.hasSize;
* @author Ryan Baxter
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions({"spring-security-rsa*.jar"})
@ClassPathExclusions({ "spring-security-rsa*.jar" })
public class RsaDisabledTests {
private ConfigurableApplicationContext context;
@Before
public void setUp() {
context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.sources(EncryptionBootstrapConfiguration.class).web(WebApplicationType.NONE).properties(
"encrypt.key:mykey",
"encrypt.rsa.strong:true",
"encrypt.rsa.salt:foobar").run();
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.sources(EncryptionBootstrapConfiguration.class)
.web(WebApplicationType.NONE).properties("encrypt.key:mykey",
"encrypt.rsa.strong:true", "encrypt.rsa.salt:foobar")
.run();
}
@After
public void tearDown() {
if(context != null) {
context.close();
if (this.context != null) {
this.context.close();
}
}
@Test
public void testLoadBalancedRetryFactoryBean() throws Exception {
Map<String, RsaProperties> properties = context.getBeansOfType(RsaProperties.class);
Map<String, RsaProperties> properties = this.context
.getBeansOfType(RsaProperties.class);
assertThat(properties.values(), hasSize(0));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2018 the original author or authors.
* Copyright 2006-2019 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.
@@ -72,15 +72,13 @@ public class EnvironmentManagerIntegrationTests {
@Test
public void testRefresh() throws Exception {
assertEquals("Hello scope!", properties.getMessage());
assertEquals("Hello scope!", this.properties.getMessage());
String content = property("message", "Foo");
this.mvc.perform(post(BASE_PATH + "/env")
.content(content)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
this.mvc.perform(post(BASE_PATH + "/env").content(content)
.contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(content().string("{\"message\":\"Foo\"}"));
assertEquals("Foo", properties.getMessage());
assertEquals("Foo", this.properties.getMessage());
}
private String property(String name, String value) throws JsonProcessingException {
@@ -89,23 +87,21 @@ public class EnvironmentManagerIntegrationTests {
property.put("name", name);
property.put("value", value);
return mapper.writeValueAsString(property);
return this.mapper.writeValueAsString(property);
}
@Test
public void testRefreshFails() throws Exception {
try {
this.mvc.perform(post(BASE_PATH + "/env")
.content(property("delay", "foo"))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
this.mvc.perform(post(BASE_PATH + "/env").content(property("delay", "foo"))
.contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(status().is5xxServerError());
fail("expected ServletException");
}
catch (ServletException e) {
// The underlying BindException is not handled by the dispatcher servlet
}
assertEquals(0, properties.getDelay());
assertEquals(0, this.properties.getDelay());
}
@Test
@@ -116,15 +112,15 @@ public class EnvironmentManagerIntegrationTests {
@Test
public void environmentBeansConfiguredCorrectly() {
Map<String, EnvironmentEndpoint> envbeans = this.context.getBeansOfType(EnvironmentEndpoint.class);
assertThat(envbeans).hasSize(1)
.containsKey("environmentEndpoint");
Map<String, EnvironmentEndpoint> envbeans = this.context
.getBeansOfType(EnvironmentEndpoint.class);
assertThat(envbeans).hasSize(1).containsKey("environmentEndpoint");
assertThat(envbeans.get("environmentEndpoint"))
.isInstanceOf(WritableEnvironmentEndpoint.class);
Map<String, EnvironmentEndpointWebExtension> extbeans = this.context.getBeansOfType(EnvironmentEndpointWebExtension.class);
assertThat(extbeans).hasSize(1)
.containsKey("environmentEndpointWebExtension");
Map<String, EnvironmentEndpointWebExtension> extbeans = this.context
.getBeansOfType(EnvironmentEndpointWebExtension.class);
assertThat(extbeans).hasSize(1).containsKey("environmentEndpointWebExtension");
assertThat(extbeans.get("environmentEndpointWebExtension"))
.isInstanceOf(WritableEnvironmentEndpointWebExtension.class);
}
@@ -148,7 +144,7 @@ public class EnvironmentManagerIntegrationTests {
private int delay;
public String getMessage() {
return message;
return this.message;
}
public void setMessage(String message) {
@@ -156,12 +152,13 @@ public class EnvironmentManagerIntegrationTests {
}
public int getDelay() {
return delay;
return this.delay;
}
public void setDelay(int delay) {
this.delay = delay;
}
}
}

View File

@@ -42,4 +42,4 @@ public class EnvironmentManagerTest {
assertThat(event.getKeys()).containsExactly("foo");
}
}
}

View File

@@ -35,7 +35,8 @@ public class NamedContextFactoryTests {
Bar bar = factory.getInstance("bar", Bar.class);
assertThat("bar was null", bar, is(notNullValue()));
assertThat("context names not exposed", factory.getContextNames(), hasItems("foo", "bar"));
assertThat("context names not exposed", factory.getContextNames(),
hasItems("foo", "bar"));
Bar foobar = factory.getInstance("foo", Bar.class);
assertThat("bar was not null", foobar, is(nullValue()));
@@ -60,7 +61,7 @@ public class NamedContextFactoryTests {
}
private TestSpec getSpec(String name, Class<?> configClass) {
return new TestSpec(name, new Class[]{configClass});
return new TestSpec(name, new Class[] { configClass });
}
static class TestClientFactory extends NamedContextFactory<TestSpec> {
@@ -68,9 +69,11 @@ public class NamedContextFactoryTests {
public TestClientFactory() {
super(TestSpec.class, "testfactory", "test.client.name");
}
}
static class TestSpec implements NamedContextFactory.Specification {
private String name;
private Class<?>[] configuration;
@@ -85,7 +88,7 @@ public class NamedContextFactoryTests {
@Override
public String getName() {
return name;
return this.name;
}
public void setName(String name) {
@@ -94,31 +97,43 @@ public class NamedContextFactoryTests {
@Override
public Class<?>[] getConfiguration() {
return configuration;
return this.configuration;
}
public void setConfiguration(Class<?>[] configuration) {
this.configuration = configuration;
}
}
static class BaseConfig {
@Bean
Baz baz1() {
return new Baz();
}
}
static class Baz {
}
static class Baz{}
static class FooConfig {
@Bean
Foo foo() {
return new Foo();
}
}
static class Foo {
}
static class Foo{}
static class BarConfig {
@Bean
Bar bar() {
return new Bar();
@@ -128,7 +143,11 @@ public class NamedContextFactoryTests {
Baz baz2() {
return new Baz();
}
}
static class Bar {
}
static class Bar{}
}

View File

@@ -108,6 +108,12 @@ public class ConfigurationPropertiesRebinderIntegrationTests {
assertEquals(2, this.properties.getCount());
}
interface SomeService {
void foo();
}
@Configuration
@EnableConfigurationProperties
@Import({ RefreshConfiguration.RebinderConfiguration.class,
@@ -123,27 +129,30 @@ public class ConfigurationPropertiesRebinderIntegrationTests {
@Bean
@ConfigurationProperties("some.service")
public SomeService someService() {
return ProxyFactory.getProxy(SomeService.class, (MethodInterceptor) methodInvocation -> null);
return ProxyFactory.getProxy(SomeService.class,
(MethodInterceptor) methodInvocation -> null);
}
}
interface SomeService {
void foo();
}
// Hack out a protected inner class for testing
protected static class RefreshConfiguration extends RefreshAutoConfiguration {
@Configuration
protected static class RebinderConfiguration
extends ConfigurationPropertiesRebinderAutoConfiguration {
}
}
@ConfigurationProperties
protected static class TestProperties {
private String message;
private int delay;
private int count = 0;
public int getCount() {
@@ -170,19 +179,23 @@ public class ConfigurationPropertiesRebinderIntegrationTests {
public void init() {
this.count++;
}
}
@ConfigurationProperties("config")
@ConditionalOnMissingBean(ConfigProperties.class)
public static class ConfigProperties {
private String name;
public String getName() {
return name;
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@@ -80,16 +80,20 @@ public class ConfigurationPropertiesRebinderLifecycleIntegrationTests {
// Hack out a protected inner class for testing
protected static class RefreshConfiguration extends RefreshAutoConfiguration {
@Configuration
protected static class RebinderConfiguration
extends ConfigurationPropertiesRebinderAutoConfiguration {
}
}
@ConfigurationProperties
protected static class TestProperties implements DisposableBean, InitializingBean {
private String message;
private int count = 0;
public int getCount() {
@@ -114,6 +118,7 @@ public class ConfigurationPropertiesRebinderLifecycleIntegrationTests {
this.message = "";
this.count++;
}
}
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.cloud.context.properties;
import static org.junit.Assert.assertEquals;
import java.util.List;
import java.util.Map;
@@ -25,6 +23,7 @@ import javax.annotation.PostConstruct;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -42,9 +41,10 @@ import org.springframework.core.env.PropertySource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class,
properties = "messages=one,two")
@SpringBootTest(classes = TestConfiguration.class, properties = "messages=one,two")
public class ConfigurationPropertiesRebinderListIntegrationTests {
@Autowired
@@ -114,16 +114,20 @@ public class ConfigurationPropertiesRebinderListIntegrationTests {
// Hack out a protected inner class for testing
protected static class RefreshConfiguration extends RefreshAutoConfiguration {
@Configuration
protected static class RebinderConfiguration
extends ConfigurationPropertiesRebinderAutoConfiguration {
}
}
@ConfigurationProperties
protected static class TestProperties {
private List<String> messages;
private int count;
public List<String> getMessages() {
@@ -142,6 +146,7 @@ public class ConfigurationPropertiesRebinderListIntegrationTests {
public void init() {
this.count++;
}
}
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.cloud.context.properties;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
@@ -24,6 +22,7 @@ import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
@@ -41,9 +40,11 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class,
properties = { "messages.expiry.one=168", "messages.expiry.two=76" })
@SpringBootTest(classes = TestConfiguration.class, properties = {
"messages.expiry.one=168", "messages.expiry.two=76" })
public class ConfigurationPropertiesRebinderProxyIntegrationTests {
@Autowired
@@ -81,39 +82,44 @@ public class ConfigurationPropertiesRebinderProxyIntegrationTests {
@Aspect
protected static class Interceptor {
@Before("execution(* *..TestProperties.*(..))")
public void before() {
System.err.println("Before");
}
}
// Hack out a protected inner class for testing
protected static class RefreshConfiguration extends RefreshAutoConfiguration {
@Configuration
protected static class RebinderConfiguration
extends ConfigurationPropertiesRebinderAutoConfiguration {
}
}
@ConfigurationProperties("messages")
protected static class TestProperties {
private String name;
private final Map<String, Integer> expiry = new HashMap<>();
private String name;
public Map<String, Integer> getExpiry() {
return expiry;
return this.expiry;
}
public String getName() {
return name;
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@@ -15,12 +15,11 @@
*/
package org.springframework.cloud.context.properties;
import static org.junit.Assert.assertEquals;
import javax.annotation.PostConstruct;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -38,6 +37,8 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class)
public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests {
@@ -57,31 +58,31 @@ public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests {
@Test
@DirtiesContext
public void testSimpleProperties() throws Exception {
assertEquals("Hello scope!", properties.getMessage());
assertEquals("Hello scope!", this.properties.getMessage());
// Change the dynamic property source...
TestPropertyValues.of("message:Foo").applyTo(this.environment);
// ...but don't refresh, so the bean stays the same:
assertEquals("Hello scope!", properties.getMessage());
assertEquals(1, properties.getCount());
assertEquals("Hello scope!", this.properties.getMessage());
assertEquals(1, this.properties.getCount());
}
@Test
@DirtiesContext
public void testRefresh() throws Exception {
assertEquals(1, properties.getCount());
assertEquals("Hello scope!", properties.getMessage());
assertEquals(1, properties.getCount());
assertEquals(1, this.properties.getCount());
assertEquals("Hello scope!", this.properties.getMessage());
assertEquals(1, this.properties.getCount());
// Change the dynamic property source...
TestPropertyValues.of("message:Foo").applyTo(this.environment);
// ...rebind, but the bean is not re-initialized:
rebinder.rebind();
assertEquals("Hello scope!", properties.getMessage());
assertEquals(1, properties.getCount());
this.rebinder.rebind();
assertEquals("Hello scope!", this.properties.getMessage());
assertEquals(1, this.properties.getCount());
// ...and then refresh, so the bean is re-initialized:
refreshScope.refreshAll();
assertEquals("Foo", properties.getMessage());
this.refreshScope.refreshAll();
assertEquals("Foo", this.properties.getMessage());
// It's a new instance so the initialization count is 1
assertEquals(1, properties.getCount());
assertEquals(1, this.properties.getCount());
}
@Configuration
@@ -101,16 +102,19 @@ public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests {
@ConfigurationProperties
protected static class TestProperties {
private String message;
private int delay;
private int count = 0;
public int getCount() {
return count;
return this.count;
}
public String getMessage() {
return message;
return this.message;
}
public void setMessage(String message) {
@@ -118,7 +122,7 @@ public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests {
}
public int getDelay() {
return delay;
return this.delay;
}
public void setDelay(int delay) {
@@ -129,6 +133,7 @@ public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests {
public void init() {
this.count++;
}
}
}

View File

@@ -74,7 +74,7 @@ public class ContextRefresherIntegrationTests {
public void testUpdateHikari() throws Exception {
assertEquals("Hello scope!", this.properties.getMessage());
TestPropertyValues.of("spring.datasource.hikari.read-only=true")
.applyTo(environment);
.applyTo(this.environment);
// ...and then refresh, so the bean is re-initialized:
this.refresher.refresh();
assertEquals("Hello scope!", this.properties.getMessage());
@@ -84,12 +84,15 @@ public class ContextRefresherIntegrationTests {
@EnableConfigurationProperties(TestProperties.class)
@EnableAutoConfiguration
protected static class TestConfiguration {
}
@ConfigurationProperties
@ManagedResource
protected static class TestProperties {
private String message;
private int delay;
@ManagedAttribute
@@ -109,6 +112,7 @@ public class ContextRefresherIntegrationTests {
public void setDelay(int delay) {
this.delay = delay;
}
}
}

View File

@@ -9,6 +9,7 @@ import java.util.Map;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.test.util.TestPropertyValues;
@@ -44,7 +45,7 @@ public class ContextRefresherTests {
List<String> names = names(context.getEnvironment().getPropertySources());
assertThat(names).doesNotContain(
"applicationConfig: [classpath:/bootstrap-refresh.properties]");
ContextRefresher refresher = new ContextRefresher(context, scope);
ContextRefresher refresher = new ContextRefresher(context, this.scope);
refresher.refresh();
names = names(context.getEnvironment().getPropertySources());
assertThat(names).contains(
@@ -67,7 +68,7 @@ public class ContextRefresherTests {
List<String> names = names(context.getEnvironment().getPropertySources());
System.err.println("***** " + context.getEnvironment().getPropertySources());
assertThat(names).doesNotContain("bootstrapProperties");
ContextRefresher refresher = new ContextRefresher(context, scope);
ContextRefresher refresher = new ContextRefresher(context, this.scope);
TestPropertyValues.of(
"spring.cloud.bootstrap.sources: org.springframework.cloud.context.refresh.ContextRefresherTests.PropertySourceConfiguration")
.applyTo(context.getEnvironment(), Type.MAP, "defaultProperties");
@@ -85,7 +86,7 @@ public class ContextRefresherTests {
ContextRefresherTests.class, "--spring.main.web-application-type=none",
"--debug=false", "--spring.main.bannerMode=OFF",
"--spring.cloud.bootstrap.name=refresh")) {
ContextRefresher refresher = new ContextRefresher(context, scope);
ContextRefresher refresher = new ContextRefresher(context, this.scope);
TestPropertyValues.of(
"spring.cloud.bootstrap.sources: org.springframework.cloud.context.refresh.ContextRefresherTests.PropertySourceConfiguration")
.applyTo(context);
@@ -111,7 +112,7 @@ public class ContextRefresherTests {
"--spring.main.bannerMode=OFF",
"--spring.cloud.bootstrap.name=refresh")) {
assertThat(system.getCount()).isEqualTo(4);
ContextRefresher refresher = new ContextRefresher(context, scope);
ContextRefresher refresher = new ContextRefresher(context, this.scope);
refresher.refresh();
assertThat(system.getCount()).isEqualTo(4);
}
@@ -122,21 +123,20 @@ public class ContextRefresherTests {
TestBootstrapConfiguration.fooSightings = new ArrayList<>();
try (ConfigurableApplicationContext context = SpringApplication.run(ContextRefresherTests.class,
"--spring.main.web-application-type=none", "--debug=false",
"--spring.main.bannerMode=OFF",
"--spring.cloud.bootstrap.name=refresh",
"--test.bootstrap.foo=bar")) {
try (ConfigurableApplicationContext context = SpringApplication.run(
ContextRefresherTests.class, "--spring.main.web-application-type=none",
"--debug=false", "--spring.main.bannerMode=OFF",
"--spring.cloud.bootstrap.name=refresh", "--test.bootstrap.foo=bar")) {
context.getEnvironment().setActiveProfiles("refresh");
ContextRefresher refresher = new ContextRefresher(context, scope);
ContextRefresher refresher = new ContextRefresher(context, this.scope);
refresher.refresh();
assertThat(TestBootstrapConfiguration.fooSightings).containsExactly("bar", "bar");
assertThat(TestBootstrapConfiguration.fooSightings).containsExactly("bar",
"bar");
}
TestBootstrapConfiguration.fooSightings = null;
}
private List<String> names(MutablePropertySources propertySources) {
List<String> list = new ArrayList<>();
for (PropertySource<?> p : propertySources) {
@@ -147,6 +147,7 @@ public class ContextRefresherTests {
@Configuration
protected static class Empty {
}
@Configuration

View File

@@ -34,36 +34,40 @@ public class RestartIntegrationTests {
private ConfigurableApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(TestConfiguration.class, args);
}
@After
public void close() {
if (context != null) {
context.close();
if (this.context != null) {
this.context.close();
}
}
@Test
public void testRestartTwice() throws Exception {
context = SpringApplication.run(TestConfiguration.class,
this.context = SpringApplication.run(TestConfiguration.class,
"--management.endpoint.restart.enabled=true", "--server.port=0",
"--spring.liveBeansView.mbeanDomain=livebeans");
RestartEndpoint endpoint = context.getBean(RestartEndpoint.class);
assertNotNull(context.getParent());
assertNull(context.getParent().getParent());
context = endpoint.doRestart();
RestartEndpoint endpoint = this.context.getBean(RestartEndpoint.class);
assertNotNull(this.context.getParent());
assertNull(this.context.getParent().getParent());
this.context = endpoint.doRestart();
assertNotNull(context);
assertNotNull(context.getParent());
assertNull(context.getParent().getParent());
assertNotNull(this.context);
assertNotNull(this.context.getParent());
assertNull(this.context.getParent().getParent());
RestartEndpoint next = context.getBean(RestartEndpoint.class);
RestartEndpoint next = this.context.getBean(RestartEndpoint.class);
assertNotSame(endpoint, next);
context = next.doRestart();
this.context = next.doRestart();
assertNotNull(context);
assertNotNull(context.getParent());
assertNull(context.getParent().getParent());
assertNotNull(this.context);
assertNotNull(this.context.getParent());
assertNull(this.context.getParent().getParent());
LiveBeansView beans = new LiveBeansView();
String json = beans.getSnapshotAsJson();
@@ -71,13 +75,10 @@ public class RestartIntegrationTests {
assertThat(json).containsOnlyOnce("parent\": null");
}
public static void main(String[] args) {
SpringApplication.run(TestConfiguration.class, args);
}
@Configuration
@EnableAutoConfiguration
protected static class TestConfiguration {
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.cloud.context.scope.refresh;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.test.context.SpringBootTest;
@@ -33,15 +34,15 @@ import static org.junit.Assert.assertEquals;
@SpringBootTest(classes = TestConfiguration.class)
public class ImportRefreshScopeIntegrationTests {
@Autowired
org.springframework.cloud.context.scope.refresh.RefreshScope scope;
@Autowired
private ConfigurableListableBeanFactory beanFactory;
@Autowired
private ExampleService service;
@Autowired
org.springframework.cloud.context.scope.refresh.RefreshScope scope;
@Test
public void testSimpleProperties() throws Exception {
assertEquals("Hello scope!", this.service.getMessage());
@@ -63,6 +64,7 @@ public class ImportRefreshScopeIntegrationTests {
@Configuration
@Import({ RefreshAutoConfiguration.class, ExampleService.class })
protected static class TestConfiguration {
}
}

View File

@@ -16,17 +16,13 @@
package org.springframework.cloud.context.scope.refresh;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.DisposableBean;
@@ -46,6 +42,11 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class)
public class MoreRefreshScopeIntegrationTests {
@@ -89,7 +90,8 @@ public class MoreRefreshScopeIntegrationTests {
assertEquals("Hello scope!", this.service.getMessage());
String id1 = this.service.toString();
// Change the dynamic property source...
TestPropertyValues.of("message:Foo").applyTo(this.environment, Type.MAP, "morerefreshtests");
TestPropertyValues.of("message:Foo").applyTo(this.environment, Type.MAP,
"morerefreshtests");
// ...and then refresh, so the bean is re-initialized:
this.scope.refreshAll();
String id2 = this.service.toString();
@@ -123,7 +125,6 @@ public class MoreRefreshScopeIntegrationTests {
assertEquals("Foo", this.service.getMessage());
}
public static class TestService implements InitializingBean, DisposableBean {
private static Log logger = LogFactory.getLog(TestService.class);
@@ -136,6 +137,19 @@ public class MoreRefreshScopeIntegrationTests {
private volatile long delay = 0;
public static void reset() {
initCount = 0;
destroyCount = 0;
}
public static int getInitCount() {
return initCount;
}
public static int getDestroyCount() {
return destroyCount;
}
public void setDelay(long delay) {
this.delay = delay;
}
@@ -153,24 +167,6 @@ public class MoreRefreshScopeIntegrationTests {
this.message = null;
}
public static void reset() {
initCount = 0;
destroyCount = 0;
}
public static int getInitCount() {
return initCount;
}
public static int getDestroyCount() {
return destroyCount;
}
public void setMessage(String message) {
logger.debug("Setting message: " + message);
this.message = message;
}
public String getMessage() {
logger.debug("Getting message: " + this.message);
try {
@@ -183,6 +179,11 @@ public class MoreRefreshScopeIntegrationTests {
return this.message;
}
public void setMessage(String message) {
logger.debug("Setting message: " + message);
this.message = message;
}
}
@Configuration
@@ -232,6 +233,7 @@ public class MoreRefreshScopeIntegrationTests {
public void setDelay(int delay) {
this.delay = delay;
}
}
}

View File

@@ -22,6 +22,7 @@ import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
@@ -51,9 +52,9 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ClientApp.class, properties = "management.endpoints.web.exposure.include=*", webEnvironment = RANDOM_PORT)
public class RefreshEndpointIntegrationTests {
private static final String BASE_PATH = new WebEndpointProperties().getBasePath();
@LocalServerPort
private int port;
@@ -61,9 +62,11 @@ public class RefreshEndpointIntegrationTests {
public void webAccess() throws Exception {
TestRestTemplate template = new TestRestTemplate();
template.exchange(
getUrlEncodedEntity("http://localhost:" + this.port + BASE_PATH + "/env", "message",
"Hello Dave!"), String.class);
template.postForObject("http://localhost:" + this.port + BASE_PATH + "/refresh", null, String.class);
getUrlEncodedEntity("http://localhost:" + this.port + BASE_PATH + "/env",
"message", "Hello Dave!"),
String.class);
template.postForObject("http://localhost:" + this.port + BASE_PATH + "/refresh",
null, String.class);
String message = template.getForObject("http://localhost:" + this.port + "/",
String.class);
assertEquals("Hello Dave!", message);
@@ -76,8 +79,8 @@ public class RefreshEndpointIntegrationTests {
property.put("value", value);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
RequestEntity<Map<String, String>> entity = new RequestEntity<>(
property, headers, HttpMethod.POST, new URI(uri));
RequestEntity<Map<String, String>> entity = new RequestEntity<>(property, headers,
HttpMethod.POST, new URI(uri));
return entity;
}
@@ -85,23 +88,23 @@ public class RefreshEndpointIntegrationTests {
@EnableAutoConfiguration
protected static class ClientApp {
public static void main(String[] args) {
SpringApplication.run(ClientApp.class, args);
}
@Bean
@RefreshScope
public Controller controller() {
return new Controller();
}
public static void main(String[] args) {
SpringApplication.run(ClientApp.class, args);
}
}
@RestController
protected static class Controller {
String message;
@Value("${message:Hello World!}")
public void setMessage(String message) {
this.message = message;
@@ -114,5 +117,4 @@ public class RefreshEndpointIntegrationTests {
}
}

View File

@@ -113,6 +113,7 @@ public class RefreshScopeConcurrencyTests {
private static Log logger = LogFactory.getLog(ExampleService.class);
private String message = null;
private volatile long delay = 0;
public void setDelay(long delay) {
@@ -130,11 +131,6 @@ public class RefreshScopeConcurrencyTests {
this.message = null;
}
public void setMessage(String message) {
logger.debug("Setting message: " + message);
this.message = message;
}
@Override
public String getMessage() {
logger.debug("Getting message: " + this.message);
@@ -148,6 +144,11 @@ public class RefreshScopeConcurrencyTests {
return this.message;
}
public void setMessage(String message) {
logger.debug("Setting message: " + message);
this.message = message;
}
}
@Configuration
@@ -173,7 +174,9 @@ public class RefreshScopeConcurrencyTests {
@ConfigurationProperties
@ManagedResource
protected static class TestProperties {
private String message;
private int delay;
@ManagedAttribute
@@ -193,6 +196,7 @@ public class RefreshScopeConcurrencyTests {
public void setDelay(int delay) {
this.delay = delay;
}
}
}

View File

@@ -15,9 +15,6 @@
*/
package org.springframework.cloud.context.scope.refresh;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
@@ -31,6 +28,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
@@ -51,6 +49,9 @@ import org.springframework.test.annotation.Repeat;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.ObjectUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class, properties = "logging.level.org.springframework.cloud.context.scope.refresh.RefreshScopeConfigurationScaleTests=DEBUG")
public class RefreshScopeConfigurationScaleTests {
@@ -58,11 +59,11 @@ public class RefreshScopeConfigurationScaleTests {
private static Log logger = LogFactory
.getLog(RefreshScopeConfigurationScaleTests.class);
private ExecutorService executor = Executors.newFixedThreadPool(8);
@Autowired
org.springframework.cloud.context.scope.refresh.RefreshScope scope;
private ExecutorService executor = Executors.newFixedThreadPool(8);
@Autowired
private ExampleService service;
@@ -74,11 +75,11 @@ public class RefreshScopeConfigurationScaleTests {
@DirtiesContext
public void testConcurrentRefresh() throws Exception {
scope.setEager(false);
this.scope.setEager(false);
// overload the thread pool and try to force Spring to create too many instances
int n = 80;
TestPropertyValues.of("message=Foo").applyTo(environment);
TestPropertyValues.of("message=Foo").applyTo(this.environment);
this.scope.refreshAll();
final CountDownLatch latch = new CountDownLatch(n);
List<Future<String>> results = new ArrayList<>();
@@ -124,6 +125,7 @@ public class RefreshScopeConfigurationScaleTests {
private static Log logger = LogFactory.getLog(ExampleService.class);
private String message = null;
private volatile long delay = 0;
public void setDelay(long delay) {
@@ -151,12 +153,6 @@ public class RefreshScopeConfigurationScaleTests {
this.message = null;
}
public void setMessage(String message) {
logger.debug("Setting message: " + ObjectUtils.getIdentityHexString(this)
+ ", " + message);
this.message = message;
}
@Override
public String getMessage() {
logger.debug("Returning message: " + ObjectUtils.getIdentityHexString(this)
@@ -164,6 +160,12 @@ public class RefreshScopeConfigurationScaleTests {
return this.message;
}
public void setMessage(String message) {
logger.debug("Setting message: " + ObjectUtils.getIdentityHexString(this)
+ ", " + message);
this.message = message;
}
}
@Configuration
@@ -191,7 +193,9 @@ public class RefreshScopeConfigurationScaleTests {
@ConfigurationProperties
protected static class TestProperties {
private String message;
private int delay;
public String getMessage() {
@@ -209,6 +213,7 @@ public class RefreshScopeConfigurationScaleTests {
public void setDelay(int delay) {
this.delay = delay;
}
}
}

View File

@@ -41,23 +41,26 @@ import static org.junit.Assert.assertEquals;
*
*/
public class RefreshScopeConfigurationTests {
private AnnotationConfigApplicationContext context;
@Rule
public ExpectedException expected = ExpectedException.none();
private AnnotationConfigApplicationContext context;
@After
public void init() {
if (context!=null) {
context.close();
if (this.context != null) {
this.context.close();
}
}
private void refresh() {
EnvironmentManager environmentManager = context.getBean(EnvironmentManager.class);
EnvironmentManager environmentManager = this.context
.getBean(EnvironmentManager.class);
environmentManager.setProperty("message", "Hello Dave!");
org.springframework.cloud.context.scope.refresh.RefreshScope scope = context.getBean(org.springframework.cloud.context.scope.refresh.RefreshScope.class);
org.springframework.cloud.context.scope.refresh.RefreshScope scope = this.context
.getBean(
org.springframework.cloud.context.scope.refresh.RefreshScope.class);
scope.refreshAll();
}
@@ -66,10 +69,13 @@ public class RefreshScopeConfigurationTests {
*/
@Test
public void configurationWithRefreshScope() throws Exception {
context = new AnnotationConfigApplicationContext(Application.class,
PropertyPlaceholderAutoConfiguration.class, RefreshAutoConfiguration.class, LifecycleMvcEndpointAutoConfiguration.class);
Application application = context.getBean(Application.class);
assertEquals("refresh", context.getBeanDefinition("scopedTarget.application").getScope());
this.context = new AnnotationConfigApplicationContext(Application.class,
PropertyPlaceholderAutoConfiguration.class,
RefreshAutoConfiguration.class,
LifecycleMvcEndpointAutoConfiguration.class);
Application application = this.context.getBean(Application.class);
assertEquals("refresh",
this.context.getBeanDefinition("scopedTarget.application").getScope());
application.hello();
refresh();
String message = application.hello();
@@ -78,9 +84,11 @@ public class RefreshScopeConfigurationTests {
@Test
public void refreshScopeOnBean() throws Exception {
context = new AnnotationConfigApplicationContext(ClientApp.class,
PropertyPlaceholderAutoConfiguration.class, RefreshAutoConfiguration.class, LifecycleMvcEndpointAutoConfiguration.class);
Controller application = context.getBean(Controller.class);
this.context = new AnnotationConfigApplicationContext(ClientApp.class,
PropertyPlaceholderAutoConfiguration.class,
RefreshAutoConfiguration.class,
LifecycleMvcEndpointAutoConfiguration.class);
Controller application = this.context.getBean(Controller.class);
application.hello();
refresh();
String message = application.hello();
@@ -89,9 +97,11 @@ public class RefreshScopeConfigurationTests {
@Test
public void refreshScopeOnNested() throws Exception {
context = new AnnotationConfigApplicationContext(NestedApp.class,
PropertyPlaceholderAutoConfiguration.class, RefreshAutoConfiguration.class, LifecycleMvcEndpointAutoConfiguration.class);
NestedController application = context.getBean(NestedController.class);
this.context = new AnnotationConfigApplicationContext(NestedApp.class,
PropertyPlaceholderAutoConfiguration.class,
RefreshAutoConfiguration.class,
LifecycleMvcEndpointAutoConfiguration.class);
NestedController application = this.context.getBean(NestedController.class);
application.hello();
refresh();
String message = application.hello();
@@ -101,7 +111,11 @@ public class RefreshScopeConfigurationTests {
// WTF? Maven can't compile without the FQN on this one (not the others).
@org.springframework.context.annotation.Configuration
protected static class NestedApp {
public static void main(String[] args) {
SpringApplication.run(ClientApp.class, args);
}
@RestController
@RefreshScope
protected static class NestedController {
@@ -111,15 +125,11 @@ public class RefreshScopeConfigurationTests {
@RequestMapping("/")
public String hello() {
return message;
return this.message;
}
}
public static void main(String[] args) {
SpringApplication.run(ClientApp.class, args);
}
}
@Configuration("application")
@@ -129,30 +139,30 @@ public class RefreshScopeConfigurationTests {
@Value("${message:Hello World!}")
String message = "Hello World";
@RequestMapping("/")
public String hello() {
return message;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RequestMapping("/")
public String hello() {
return this.message;
}
}
@Configuration
protected static class ClientApp {
public static void main(String[] args) {
SpringApplication.run(ClientApp.class, args);
}
@Bean
@RefreshScope
public Controller controller() {
return new Controller();
}
public static void main(String[] args) {
SpringApplication.run(ClientApp.class, args);
}
}
@RestController
@@ -164,7 +174,7 @@ public class RefreshScopeConfigurationTests {
@RequestMapping("/")
// Deliberately use package scope
String hello() {
return message;
return this.message;
}
}

View File

@@ -143,12 +143,29 @@ public class RefreshScopeIntegrationTests {
private static Log logger = LogFactory.getLog(ExampleService.class);
private volatile static int initCount = 0;
private volatile static int destroyCount = 0;
private volatile static RefreshScopeRefreshedEvent event;
private String message = null;
private volatile long delay = 0;
public static void reset() {
initCount = 0;
destroyCount = 0;
event = null;
}
public static int getInitCount() {
return initCount;
}
public static int getDestroyCount() {
return destroyCount;
}
public void setDelay(long delay) {
this.delay = delay;
}
@@ -166,25 +183,6 @@ public class RefreshScopeIntegrationTests {
this.message = null;
}
public static void reset() {
initCount = 0;
destroyCount = 0;
event = null;
}
public static int getInitCount() {
return initCount;
}
public static int getDestroyCount() {
return destroyCount;
}
public void setMessage(String message) {
logger.debug("Setting message: " + message);
this.message = message;
}
@Override
public String getMessage() {
logger.debug("Getting message: " + this.message);
@@ -198,6 +196,11 @@ public class RefreshScopeIntegrationTests {
return this.message;
}
public void setMessage(String message) {
logger.debug("Setting message: " + message);
this.message = message;
}
@Override
public String throwsException() throws ServiceException {
throw new ServiceException();
@@ -207,10 +210,13 @@ public class RefreshScopeIntegrationTests {
public void onApplicationEvent(RefreshScopeRefreshedEvent e) {
event = e;
}
}
@SuppressWarnings("serial")
public static class ServiceException extends Exception {}
public static class ServiceException extends Exception {
}
@Configuration
@EnableConfigurationProperties(TestProperties.class)
@@ -234,7 +240,9 @@ public class RefreshScopeIntegrationTests {
@ConfigurationProperties
@ManagedResource
protected static class TestProperties {
private String message;
private int delay;
@ManagedAttribute
@@ -254,6 +262,7 @@ public class RefreshScopeIntegrationTests {
public void setDelay(int delay) {
this.delay = delay;
}
}
}

View File

@@ -136,12 +136,29 @@ public class RefreshScopeLazyIntegrationTests {
private static Log logger = LogFactory.getLog(ExampleService.class);
private volatile static int initCount = 0;
private volatile static int destroyCount = 0;
private volatile static RefreshScopeRefreshedEvent event;
private String message = null;
private volatile long delay = 0;
public static void reset() {
initCount = 0;
destroyCount = 0;
event = null;
}
public static int getInitCount() {
return initCount;
}
public static int getDestroyCount() {
return destroyCount;
}
public void setDelay(long delay) {
this.delay = delay;
}
@@ -159,25 +176,6 @@ public class RefreshScopeLazyIntegrationTests {
this.message = null;
}
public static void reset() {
initCount = 0;
destroyCount = 0;
event = null;
}
public static int getInitCount() {
return initCount;
}
public static int getDestroyCount() {
return destroyCount;
}
public void setMessage(String message) {
logger.debug("Setting message: " + message);
this.message = message;
}
@Override
public String getMessage() {
logger.debug("Getting message: " + this.message);
@@ -191,10 +189,16 @@ public class RefreshScopeLazyIntegrationTests {
return this.message;
}
public void setMessage(String message) {
logger.debug("Setting message: " + message);
this.message = message;
}
@Override
public void onApplicationEvent(RefreshScopeRefreshedEvent e) {
event = e;
}
}
@Configuration
@@ -227,7 +231,9 @@ public class RefreshScopeLazyIntegrationTests {
@ConfigurationProperties
@ManagedResource
protected static class TestProperties {
private String message;
private int delay;
@ManagedAttribute
@@ -247,6 +253,7 @@ public class RefreshScopeLazyIntegrationTests {
public void setDelay(int delay) {
this.delay = delay;
}
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.cloud.context.scope.refresh;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
@@ -31,7 +32,8 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {RefreshScopeNullBeanIntegrationTests.TestConfiguration.class})
@SpringBootTest(classes = {
RefreshScopeNullBeanIntegrationTests.TestConfiguration.class })
public class RefreshScopeNullBeanIntegrationTests {
@Autowired
@@ -49,7 +51,9 @@ public class RefreshScopeNullBeanIntegrationTests {
// this.scope.refreshAll();
}
protected static class OptionalService { }
protected static class OptionalService {
}
public static class MyCustomComponent {
@@ -58,6 +62,7 @@ public class RefreshScopeNullBeanIntegrationTests {
public MyCustomComponent(OptionalService optionalService) {
this.optionalService = optionalService;
}
}
@Configuration
@@ -68,10 +73,12 @@ public class RefreshScopeNullBeanIntegrationTests {
public OptionalService service() {
return null;
}
}
@Configuration
@Import({ RefreshAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, OptionalConfiguration.class })
@Import({ RefreshAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
OptionalConfiguration.class })
protected static class TestConfiguration {
@Autowired(required = false)
@@ -79,8 +86,9 @@ public class RefreshScopeNullBeanIntegrationTests {
@Bean
public MyCustomComponent myCustomComponent() {
return new MyCustomComponent(optionalService);
return new MyCustomComponent(this.optionalService);
}
}
}

View File

@@ -15,9 +15,6 @@
*/
package org.springframework.cloud.context.scope.refresh;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
@@ -31,6 +28,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
@@ -47,18 +45,22 @@ import org.springframework.test.annotation.Repeat;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.ObjectUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class)
// , properties="logging.level.org.springframework.cloud.context.scope.refresh.RefreshScopePureScaleTests=DEBUG")
// ,
// properties="logging.level.org.springframework.cloud.context.scope.refresh.RefreshScopePureScaleTests=DEBUG")
public class RefreshScopePureScaleTests {
private static Log logger = LogFactory.getLog(RefreshScopePureScaleTests.class);
private ExecutorService executor = Executors.newFixedThreadPool(8);
@Autowired
org.springframework.cloud.context.scope.refresh.RefreshScope scope;
private ExecutorService executor = Executors.newFixedThreadPool(8);
@Autowired
private ExampleService service;
@@ -91,7 +93,8 @@ public class RefreshScopePureScaleTests {
public void run() {
logger.debug("Refreshing.");
RefreshScopePureScaleTests.this.scope.refreshAll();
}});
}
});
}
assertTrue(latch.await(15000, TimeUnit.MILLISECONDS));
assertEquals("Foo", this.service.getMessage());
@@ -112,6 +115,7 @@ public class RefreshScopePureScaleTests {
private static Log logger = LogFactory.getLog(ExampleService.class);
private String message = null;
private volatile long delay = 0;
public void setDelay(long delay) {
@@ -120,33 +124,38 @@ public class RefreshScopePureScaleTests {
@Override
public void afterPropertiesSet() throws Exception {
logger.debug("Initializing: " + ObjectUtils.getIdentityHexString(this) + ", " + this.message);
logger.debug("Initializing: " + ObjectUtils.getIdentityHexString(this) + ", "
+ this.message);
try {
Thread.sleep(this.delay);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
logger.debug("Initialized: " + ObjectUtils.getIdentityHexString(this) + ", " + this.message);
logger.debug("Initialized: " + ObjectUtils.getIdentityHexString(this) + ", "
+ this.message);
}
@Override
public void destroy() throws Exception {
logger.debug("Destroying message: " + ObjectUtils.getIdentityHexString(this) + ", " + this.message);
logger.debug("Destroying message: " + ObjectUtils.getIdentityHexString(this)
+ ", " + this.message);
this.message = null;
}
public void setMessage(String message) {
logger.debug("Setting message: " + ObjectUtils.getIdentityHexString(this) + ", " + message);
this.message = message;
}
@Override
public String getMessage() {
logger.debug("Returning message: " + ObjectUtils.getIdentityHexString(this) + ", " + this.message);
logger.debug("Returning message: " + ObjectUtils.getIdentityHexString(this)
+ ", " + this.message);
return this.message;
}
public void setMessage(String message) {
logger.debug("Setting message: " + ObjectUtils.getIdentityHexString(this)
+ ", " + message);
this.message = message;
}
}
@Configuration

View File

@@ -56,11 +56,11 @@ public class RefreshScopeScaleTests {
private static Log logger = LogFactory.getLog(RefreshScopeScaleTests.class);
private ExecutorService executor = Executors.newFixedThreadPool(8);
@Autowired
org.springframework.cloud.context.scope.refresh.RefreshScope scope;
private ExecutorService executor = Executors.newFixedThreadPool(8);
@Autowired
private ExampleService service;
@@ -113,10 +113,12 @@ public class RefreshScopeScaleTests {
private static Log logger = LogFactory.getLog(ExampleService.class);
private String message = null;
private volatile long delay = 0;
private static volatile int count;
private String message = null;
private volatile long delay = 0;
public void setDelay(long delay) {
this.delay = delay;
}
@@ -139,17 +141,17 @@ public class RefreshScopeScaleTests {
this.message = null;
}
public void setMessage(String message) {
logger.debug("Setting message: " + message);
this.message = message;
}
@Override
public String getMessage() {
logger.debug("Returning message: " + this.message);
return this.message;
}
public void setMessage(String message) {
logger.debug("Setting message: " + message);
this.message = message;
}
}
@Configuration
@@ -175,7 +177,9 @@ public class RefreshScopeScaleTests {
@ConfigurationProperties
@ManagedResource
protected static class TestProperties {
private String message;
private int delay;
@ManagedAttribute
@@ -195,6 +199,7 @@ public class RefreshScopeScaleTests {
public void setDelay(int delay) {
this.delay = delay;
}
}
}

View File

@@ -79,4 +79,5 @@ public class RefreshScopeSerializationTests {
protected static class TestBean {
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.cloud.context.scope.refresh;
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.beans.factory.config.ConfigurableListableBeanFactory;
@@ -44,43 +45,44 @@ public class RefreshScopeWebIntegrationTests {
@Autowired
private org.springframework.cloud.context.scope.refresh.RefreshScope scope;
@Autowired
private EnvironmentManager environmentManager;
@Autowired
private Client application;
@Autowired
private ConfigurableListableBeanFactory beanFactory;
@Test
public void scopeOnBeanDefinition() throws Exception {
assertEquals("refresh", beanFactory.getBeanDefinition("scopedTarget.application").getScope());
assertEquals("refresh", this.beanFactory
.getBeanDefinition("scopedTarget.application").getScope());
}
@Test
public void beanAccess() throws Exception {
application.hello();
environmentManager.setProperty("message", "Hello Dave!");
scope.refreshAll();
String message = application.hello();
this.application.hello();
this.environmentManager.setProperty("message", "Hello Dave!");
this.scope.refreshAll();
String message = this.application.hello();
assertEquals("Hello Dave!", message);
}
@Configuration
@EnableAutoConfiguration
protected static class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
@RefreshScope
public Client application() {
return new Client();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@@ -92,7 +94,7 @@ public class RefreshScopeWebIntegrationTests {
@RequestMapping("/")
public String hello() {
return message;
return this.message;
}
}

View File

@@ -73,7 +73,7 @@ public class RefreshEndpointTests {
.properties("spring.cloud.bootstrap.name:none").run();
RefreshScope scope = new RefreshScope();
scope.setApplicationContext(this.context);
context.getEnvironment().setActiveProfiles("local");
this.context.getEnvironment().setActiveProfiles("local");
ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
Collection<String> keys = endpoint.refresh();
@@ -87,7 +87,7 @@ public class RefreshEndpointTests {
.properties("spring.cloud.bootstrap.name:none").run();
RefreshScope scope = new RefreshScope();
scope.setApplicationContext(this.context);
context.getEnvironment().setActiveProfiles("override");
this.context.getEnvironment().setActiveProfiles("override");
ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
Collection<String> keys = endpoint.refresh();
@@ -148,8 +148,8 @@ public class RefreshEndpointTests {
@Test
public void shutdownHooksCleaned() {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(Empty.class)
.web(WebApplicationType.NONE).bannerMode(Mode.OFF).run()) {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
Empty.class).web(WebApplicationType.NONE).bannerMode(Mode.OFF).run()) {
RefreshScope scope = new RefreshScope();
scope.setApplicationContext(context);
ContextRefresher contextRefresher = new ContextRefresher(context, scope);
@@ -173,8 +173,8 @@ public class RefreshEndpointTests {
@Configuration
protected static class Empty implements SmartApplicationListener {
private List<ApplicationEvent> events = new ArrayList<ApplicationEvent>();
private List<ApplicationEvent> events = new ArrayList<ApplicationEvent>();
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
@@ -184,11 +184,12 @@ public class RefreshEndpointTests {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof EnvironmentChangeEvent ||
event instanceof RefreshScopeRefreshedEvent) {
if (event instanceof EnvironmentChangeEvent
|| event instanceof RefreshScopeRefreshedEvent) {
this.events.add(event);
}
}
}
@Component
@@ -202,4 +203,5 @@ public class RefreshEndpointTests {
}
}
}

View File

@@ -38,19 +38,20 @@ public class RefreshScopeHealthIndicatorTests {
@SuppressWarnings("unchecked")
private ObjectProvider<RefreshScope> scopeProvider = mock(ObjectProvider.class);
private ConfigurationPropertiesRebinder rebinder =
mock(ConfigurationPropertiesRebinder.class);
private ConfigurationPropertiesRebinder rebinder = mock(
ConfigurationPropertiesRebinder.class);
private RefreshScope scope = mock(RefreshScope.class);
private RefreshScopeHealthIndicator indicator = new RefreshScopeHealthIndicator(
this.scopeProvider, this.rebinder);
@Before
public void init() {
BDDMockito.willReturn(scope).given(scopeProvider).getIfAvailable();
when(this.rebinder.getErrors())
.thenReturn(Collections.emptyMap());
when(this.scope.getErrors())
.thenReturn(Collections.emptyMap());
BDDMockito.willReturn(this.scope).given(this.scopeProvider).getIfAvailable();
when(this.rebinder.getErrors()).thenReturn(Collections.emptyMap());
when(this.scope.getErrors()).thenReturn(Collections.emptyMap());
}
@Test
@@ -60,24 +61,24 @@ public class RefreshScopeHealthIndicatorTests {
@Test
public void binderError() {
when(this.rebinder.getErrors()).thenReturn(Collections
.singletonMap("foo", new RuntimeException("FOO")));
when(this.rebinder.getErrors())
.thenReturn(Collections.singletonMap("foo", new RuntimeException("FOO")));
assertEquals(Status.DOWN, this.indicator.health().getStatus());
}
@Test
public void scopeError() {
when(this.scope.getErrors()).thenReturn(Collections
.singletonMap("foo", new RuntimeException("FOO")));
when(this.scope.getErrors())
.thenReturn(Collections.singletonMap("foo", new RuntimeException("FOO")));
assertEquals(Status.DOWN, this.indicator.health().getStatus());
}
@Test
public void bothError() {
when(this.rebinder.getErrors()).thenReturn(Collections
.singletonMap("foo", new RuntimeException("FOO")));
when(this.scope.getErrors()).thenReturn(Collections
.singletonMap("bar", new RuntimeException("BAR")));
when(this.rebinder.getErrors())
.thenReturn(Collections.singletonMap("foo", new RuntimeException("FOO")));
when(this.scope.getErrors())
.thenReturn(Collections.singletonMap("bar", new RuntimeException("BAR")));
assertEquals(Status.DOWN, this.indicator.health().getStatus());
}

View File

@@ -15,9 +15,6 @@
*/
package org.springframework.cloud.logging;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import org.junit.After;
@@ -31,6 +28,9 @@ import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.core.env.StandardEnvironment;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*
@@ -38,6 +38,7 @@ import org.springframework.core.env.StandardEnvironment;
public class LoggingRebinderTests {
private LoggingRebinder rebinder = new LoggingRebinder();
private Logger logger = LoggerFactory.getLogger("org.springframework.web");
@After