Tidy up compiler warnings

This commit is contained in:
Dave Syer
2017-11-07 17:06:16 +00:00
parent 93a0365422
commit 1b219b086c
8 changed files with 58 additions and 55 deletions

View File

@@ -2,12 +2,11 @@ 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;
import org.springframework.cloud.bootstrap.BootstrapOrderingSpringApplicationJsonIntegrationTests.Application;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@@ -17,13 +16,11 @@ import static org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapCon
@SpringBootTest(classes = Application.class)
public class BootstrapSourcesOrderingTests {
@Autowired
private ConfigurableEnvironment environment;
@Test
public void sourcesAreOrderedCorrectly() {
Class<?> firstConstructedClass = firstToBeCreated.get();
assertThat(firstConstructedClass).as("bootstrap sources not ordered correctly").isEqualTo(TestHigherPriorityBootstrapConfiguration.class);
assertThat(firstConstructedClass).as("bootstrap sources not ordered correctly")
.isEqualTo(TestHigherPriorityBootstrapConfiguration.class);
}
@EnableAutoConfiguration

View File

@@ -19,6 +19,7 @@ import java.util.Collections;
import java.util.Map;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -65,9 +66,9 @@ public class EnvironmentDecryptApplicationInitializerTests {
public void propertySourcesOrderedCorrectly() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
addEnvironment(context, "foo: {cipher}bar");
context.getEnvironment().getPropertySources().addFirst(new MapPropertySource(
"test_override",
Collections.<String, Object> singletonMap("foo", "{cipher}spam")));
context.getEnvironment().getPropertySources()
.addFirst(new MapPropertySource("test_override",
Collections.<String, Object>singletonMap("foo", "{cipher}spam")));
this.listener.initialize(context);
assertEquals("spam", context.getEnvironment().getProperty("foo"));
}
@@ -98,8 +99,10 @@ public class EnvironmentDecryptApplicationInitializerTests {
@SuppressWarnings("unchecked")
public void indexedPropertiesCopied() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
// tests that collections in another property source don't get copied into "decrypted" property source
addEnvironment(context, "yours[0].someValue: yourFoo", "yours[1].someValue: yourBar");
// tests that collections in another property source don't get copied into
// "decrypted" property source
addEnvironment(context, "yours[0].someValue: yourFoo",
"yours[1].someValue: yourBar");
// collection with some encrypted keys and some not encrypted
addEnvironment("combinedTest", context.getEnvironment(), "mine[0].someValue: Foo",
@@ -107,27 +110,31 @@ public class EnvironmentDecryptApplicationInitializerTests {
"mine[1].someKey: {cipher}Bar1", "nonindexed: nonindexval");
this.listener.initialize(context);
assertEquals("Foo", context.getEnvironment().getProperty("mine[0].someValue"));
assertEquals("Foo", context.getEnvironment().getProperty("mine[0].someValue"));
assertEquals("Foo0", context.getEnvironment().getProperty("mine[0].someKey"));
assertEquals("Bar", context.getEnvironment().getProperty("mine[1].someValue"));
assertEquals("Bar", context.getEnvironment().getProperty("mine[1].someValue"));
assertEquals("Bar1", context.getEnvironment().getProperty("mine[1].someKey"));
assertEquals("yourFoo", context.getEnvironment().getProperty("yours[0].someValue"));
assertEquals("yourBar", context.getEnvironment().getProperty("yours[1].someValue"));
assertEquals("yourFoo",
context.getEnvironment().getProperty("yours[0].someValue"));
assertEquals("yourBar",
context.getEnvironment().getProperty("yours[1].someValue"));
MutablePropertySources propertySources = context.getEnvironment().getPropertySources();
PropertySource<Map> decrypted = (PropertySource<Map>) propertySources.get(DECRYPTED_PROPERTY_SOURCE_NAME);
assertThat("decrypted property source had wrong size", decrypted.getSource().size(), is(4));
MutablePropertySources propertySources = context.getEnvironment()
.getPropertySources();
PropertySource<Map<?, ?>> decrypted = (PropertySource<Map<?, ?>>) propertySources
.get(DECRYPTED_PROPERTY_SOURCE_NAME);
assertThat("decrypted property source had wrong size",
decrypted.getSource().size(), is(4));
}
@Test
public void testDecryptNonStandardParent() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
EnvironmentDecryptApplicationInitializer initializer =
new EnvironmentDecryptApplicationInitializer(Encryptors.noOpText());
EnvironmentDecryptApplicationInitializer initializer = new EnvironmentDecryptApplicationInitializer(
Encryptors.noOpText());
addEnvironment(ctx, "key:{cipher}value");
ApplicationContext ctxParent = mock(ApplicationContext.class);
when(ctxParent.getEnvironment()).thenReturn(mock(Environment.class));

View File

@@ -1,45 +1,45 @@
package org.springframework.cloud.context.environment;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
public class EnvironmentManagerTest {
@Test
public void testCorrectEvents() {
MockEnvironment environment = new MockEnvironment();
ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
EnvironmentManager environmentManager = new EnvironmentManager(environment);
environmentManager.setApplicationEventPublisher(publisher);
@Test
public void testCorrectEvents() {
MockEnvironment environment = new MockEnvironment();
ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
EnvironmentManager environmentManager = new EnvironmentManager(environment);
environmentManager.setApplicationEventPublisher(publisher);
environmentManager.setProperty("foo", "bar");
environmentManager.setProperty("foo", "bar");
assertThat(environment.getProperty("foo")).isEqualTo("bar");
ArgumentCaptor<ApplicationEvent> eventCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
verify(publisher, times(1)).publishEvent(eventCaptor.capture());
assertThat(eventCaptor.getValue()).isInstanceOf(EnvironmentChangeEvent.class);
EnvironmentChangeEvent event = (EnvironmentChangeEvent) eventCaptor.getValue();
assertThat(event.getKeys()).containsExactly("foo");
assertThat(environment.getProperty("foo")).isEqualTo("bar");
ArgumentCaptor<ApplicationEvent> eventCaptor = ArgumentCaptor
.forClass(ApplicationEvent.class);
verify(publisher, times(1)).publishEvent(eventCaptor.capture());
assertThat(eventCaptor.getValue()).isInstanceOf(EnvironmentChangeEvent.class);
EnvironmentChangeEvent event = (EnvironmentChangeEvent) eventCaptor.getValue();
assertThat(event.getKeys()).containsExactly("foo");
reset(publisher);
reset(publisher);
environmentManager.reset();
assertThat(environment.getProperty("foo")).isNull();
verify(publisher, times(1)).publishEvent(eventCaptor.capture());
assertThat(eventCaptor.getValue()).isInstanceOf(EnvironmentChangeEvent.class);
event = (EnvironmentChangeEvent) eventCaptor.getValue();
assertThat(event.getKeys()).containsExactly("foo");
}
environmentManager.reset();
assertThat(environment.getProperty("foo")).isNull();
verify(publisher, times(1)).publishEvent(eventCaptor.capture());
assertThat(eventCaptor.getValue()).isInstanceOf(EnvironmentChangeEvent.class);
event = (EnvironmentChangeEvent) eventCaptor.getValue();
assertThat(event.getKeys()).containsExactly("foo");
}
}

View File

@@ -22,6 +22,7 @@ import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
@@ -40,14 +41,12 @@ import org.springframework.core.env.PropertySource;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class,
properties = "messages=one,two")
@SpringBootTest(classes = TestConfiguration.class, properties = "messages=one,two")
public class RefreshScopeListBindingIntegrationTests {
@Autowired