diff --git a/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/LoggerContextCleanupExtension.java b/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/LoggerContextCleanupExtension.java index 62e5fe87f0f..f8211a4904c 100644 --- a/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/LoggerContextCleanupExtension.java +++ b/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/LoggerContextCleanupExtension.java @@ -10,16 +10,19 @@ public class LoggerContextCleanupExtension implements BeforeAllCallback, AfterAllCallback { - private URI configLocation = null; - @Override public void beforeAll(ExtensionContext context) { LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); - configLocation = loggerContext.getConfigLocation(); + + ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.create(context.getRequiredTestClass())); + store.put("configLocation", loggerContext.getConfigLocation()); } @Override public void afterAll(ExtensionContext context) { + ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.create(context.getRequiredTestClass())); + URI configLocation = store.get("configLocation", URI.class); + LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); loggerContext.setConfigLocation(configLocation); } diff --git a/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/SpringProfileCleanupExtension.java b/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/SpringProfileCleanupExtension.java index 09029fa4851..9f45dae34b5 100644 --- a/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/SpringProfileCleanupExtension.java +++ b/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/SpringProfileCleanupExtension.java @@ -6,15 +6,17 @@ public class SpringProfileCleanupExtension implements BeforeAllCallback, AfterAllCallback { - private static String activeSpringProfiles; - @Override public void beforeAll(ExtensionContext context) { - activeSpringProfiles = System.getProperty("spring.profiles.active"); + ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.create(context.getRequiredTestClass())); + store.put("spring.profiles.active", System.getProperty("spring.profiles.active")); } @Override public void afterAll(ExtensionContext context) { + ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.create(context.getRequiredTestClass())); + String activeSpringProfiles = store.get("spring.profiles.active", String.class); + if (activeSpringProfiles != null) { System.setProperty("spring.profiles.active", activeSpringProfiles); } else { diff --git a/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/YamlServletProfileInitializerTest.java b/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/YamlServletProfileInitializerTest.java index 25d100bc228..05f9f83cd57 100644 --- a/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/YamlServletProfileInitializerTest.java +++ b/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/YamlServletProfileInitializerTest.java @@ -1,73 +1,357 @@ package org.cloudfoundry.identity.uaa.impl.config; import org.cloudfoundry.identity.uaa.security.PollutionPreventionExtension; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; +import org.springframework.core.env.PropertySource; +import org.springframework.core.env.StandardEnvironment; +import org.springframework.core.io.ByteArrayResource; import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.web.MockServletContext; +import org.springframework.security.web.session.HttpSessionEventPublisher; import org.springframework.util.StringUtils; +import org.springframework.web.context.ConfigurableWebApplicationContext; +import org.springframework.web.context.support.StandardServletEnvironment; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import java.util.Enumeration; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; +import static org.springframework.util.StringUtils.hasText; @ExtendWith(PollutionPreventionExtension.class) @ExtendWith(SpringProfileCleanupExtension.class) +@ExtendWith(LoggerContextCleanupExtension.class) class YamlServletProfileInitializerTest { private YamlServletProfileInitializer initializer; - private MockEnvironment environment; - private MockServletContext context; + private ConfigurableWebApplicationContext context; + private StandardServletEnvironment environment; + private ServletConfig servletConfig; + private ServletContext servletContext; @BeforeEach void setup() { initializer = new YamlServletProfileInitializer(); - environment = new MockEnvironment(); - context = new MockServletContext(); + context = mock(ConfigurableWebApplicationContext.class); + environment = new StandardServletEnvironment(); + servletConfig = mock(ServletConfig.class); + servletContext = mock(ServletContext.class); + + Mockito.when(servletConfig.getInitParameterNames()).thenReturn(new EmptyEnumerationOfString()); + Mockito.when(servletContext.getInitParameterNames()).thenReturn(new EmptyEnumerationOfString()); + + Mockito.when(context.getServletConfig()).thenReturn(servletConfig); + Mockito.when(context.getServletContext()).thenReturn(servletContext); + Mockito.when(context.getEnvironment()).thenReturn(environment); + Mockito.doAnswer((Answer) invocation -> { + System.err.println(invocation.getArguments()[0]); + return null; + }).when(servletContext).log(ArgumentMatchers.anyString()); + Mockito.when(servletContext.getContextPath()).thenReturn("/context"); + } + + @AfterEach + void cleanup() { + System.clearProperty("APPLICATION_CONFIG_URL"); + System.clearProperty("LOG_FILE"); + System.clearProperty("LOG_PATH"); + } + + @Test + void loadDefaultResource() { + Mockito.when(context.getResource(ArgumentMatchers.contains("${APPLICATION_CONFIG_URL}"))).thenReturn( + new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); + + initializer.initialize(context); + + assertEquals("bar", environment.getProperty("foo")); + assertEquals("baz", environment.getProperty("spam.foo")); + } + + @Test + void loadSessionEventPublisher() { + Mockito.when(context.getResource(ArgumentMatchers.contains("${APPLICATION_CONFIG_URL}"))).thenReturn( + new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); + + initializer.initialize(context); + + ArgumentCaptor httpSessionEventPublisherArgumentCaptor = ArgumentCaptor.forClass(HttpSessionEventPublisher.class); + verify(servletContext, atLeastOnce()).addListener(httpSessionEventPublisherArgumentCaptor.capture()); + assertNotNull(httpSessionEventPublisherArgumentCaptor.getValue()); + } + + @Test + void activeProfiles() { + System.setProperty("spring.profiles.active", "foo"); + + Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn( + new ByteArrayResource("spring_profiles: bar".getBytes())); + + initializer.initialize(context); + + assertEquals("bar", environment.getActiveProfiles()[0]); + } + + @Test + void activeProfilesFromYaml() { + Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn( + new ByteArrayResource("spring_profiles: bar".getBytes())); + + initializer.initialize(context); + + assertEquals("bar", environment.getActiveProfiles()[0]); + } + + @Test + void log4jFileFromYaml() { + Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn( + new ByteArrayResource("logging:\n file: /tmp/bar.log".getBytes())); + initializer.initialize(context); + assertEquals("/tmp/bar.log", System.getProperty("LOG_FILE")); + } + + @Test + void log4jPathFromYaml() { + Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn( + new ByteArrayResource("logging:\n path: /tmp/log/bar".getBytes())); + initializer.initialize(context); + assertEquals("/tmp/log/bar", System.getProperty("LOG_PATH")); + } + + @Test + void log4jConfigurationFromYaml() { + Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn( + new ByteArrayResource("logging:\n config: bar".getBytes())); + initializer.initialize(context); + } + + @Test + void loadServletConfiguredFilename() { + Mockito.when(servletConfig.getInitParameter("APPLICATION_CONFIG_FILE")).thenReturn("/config/path/foo.yml"); + Mockito.when(context.getResource(ArgumentMatchers.eq("file:/config/path/foo.yml"))).thenReturn( + new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); + + initializer.initialize(context); + + assertEquals("bar", environment.getProperty("foo")); + assertEquals("baz", environment.getProperty("spam.foo")); + } + + @Test + void loadServletConfiguredResource() { + Mockito.when(servletConfig.getInitParameter("environmentConfigLocations")).thenReturn("foo.yml"); + Mockito.when(context.getResource(ArgumentMatchers.eq("foo.yml"))).thenReturn( + new ByteArrayResource("foo: bar\nspam:\n foo: baz-from-config".getBytes())); + + initializer.initialize(context); + + assertEquals("bar", environment.getProperty("foo")); + assertEquals("baz-from-config", environment.getProperty("spam.foo")); } @Test - void tokenizeToStringArray_RemovesSpaces() { - String profileString = " database , ldap "; - String[] profiles = StringUtils.tokenizeToStringArray(profileString, ",", true, true); - assertThat(profiles.length, is(2)); - assertThat(profiles[0], is("database")); - assertThat(profiles[1], is("ldap")); - // And show what's wrong with commaDelimitedListToStringArray - profiles = StringUtils.commaDelimitedListToStringArray(profileString); - assertThat(profiles.length, is(2)); - assertThat(profiles[0], is(" database ")); - assertThat(profiles[1], is(" ldap ")); + void loadContextConfiguredResource() { + Mockito.when(servletContext.getInitParameter("environmentConfigLocations")).thenReturn("foo.yml"); + Mockito.when(context.getResource(ArgumentMatchers.eq("foo.yml"))).thenReturn( + new ByteArrayResource("foo: bar\nspam:\n foo: baz-from-context".getBytes())); + + initializer.initialize(context); + + assertEquals("bar", environment.getProperty("foo")); + assertEquals("baz-from-context", environment.getProperty("spam.foo")); + } + + @Test + void loadReplacedResource() { + System.setProperty("APPLICATION_CONFIG_URL", "file:foo/uaa.yml"); + + Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( + new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); + + initializer.initialize(context); + + assertEquals("bar", environment.getProperty("foo")); + assertEquals("baz", environment.getProperty("spam.foo")); + } + + @Test + void loadReplacedResourceFromFileLocation() { + System.setProperty("APPLICATION_CONFIG_FILE", "foo/uaa.yml"); + + Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( + new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); + + initializer.initialize(context); + + assertEquals("bar", environment.getProperty("foo")); + assertEquals("baz", environment.getProperty("spam.foo")); } @Test - void ifNoProfilesAreSetUseHsqldb() { - System.clearProperty("spring.profiles.active"); - initializer.applySpringProfiles(environment, context); - assertArrayEquals(new String[]{"hsqldb"}, environment.getActiveProfiles()); + void loggingConfigVariableWorks() { + System.setProperty("APPLICATION_CONFIG_FILE", "foo/uaa.yml"); + Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( + new ByteArrayResource("logging:\n config: /some/path".getBytes())); + initializer.initialize(context); + assertEquals("/some/path", environment.getProperty("logging.config")); + assertNull(environment.getProperty("smtp.host")); + assertNull(environment.getProperty("smtp.port")); } @Test - void ifProfilesAreSetUseThem() { - System.setProperty("spring.profiles.active", "hsqldb,default"); - initializer.applySpringProfiles(environment, context); - assertArrayEquals(new String[]{"hsqldb", "default"}, environment.getActiveProfiles()); + void readingYamlFromEnvironment_WithNullVariableName() { + readingYamlFromEnvironment(null); } @Test - void defaultProfileUnset() { - System.setProperty("spring.profiles.active", "hsqldb"); - initializer.applySpringProfiles(environment, context); - assertArrayEquals(new String[]{"hsqldb"}, environment.getActiveProfiles()); - assertArrayEquals(new String[0], environment.getDefaultProfiles()); + void readingYamlFromEnvironment_WithNonNullVariableName() { + readingYamlFromEnvironment("Renaming environment variable"); + } + + private void readingYamlFromEnvironment(String variableName) { + if (hasText(variableName)) { + initializer.setYamlEnvironmentVariableName(variableName); + } + SystemEnvironmentAccessor env = new SystemEnvironmentAccessor() { + @Override + public String getEnvironmentVariable(String name) { + return name.equals(initializer.getYamlEnvironmentVariableName()) ? + "uaa.url: http://uaa.test.url/\n" + + "login.url: http://login.test.url/\n" + + "smtp:\n" + + " host: mail.server.host\n" + + " port: 3535\n" : + null; + } + }; + initializer.setEnvironmentAccessor(env); + initializer.initialize(context); + assertEquals("mail.server.host", environment.getProperty("smtp.host")); + assertEquals("3535", environment.getProperty("smtp.port")); + assertEquals("http://uaa.test.url/", environment.getProperty("uaa.url")); + assertEquals("http://login.test.url/", environment.getProperty("login.url")); } @Test - void yamlConfiguredProfilesAreUsed() { - System.setProperty("spring.profiles.active", "hsqldb,default"); - environment.setProperty("spring_profiles", "mysql,default"); - initializer.applySpringProfiles(environment, context); - assertArrayEquals(new String[]{"mysql", "default"}, environment.getActiveProfiles()); + void ignoreDashDTomcatLoggingConfigVariable() { + final String tomcatLogConfig = "-Djava.util.logging.config=/some/path/logging.properties"; + System.setProperty("APPLICATION_CONFIG_FILE", "foo/uaa.yml"); + ArgumentCaptor servletLogCaptor = ArgumentCaptor.forClass(String.class); + Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( + new ByteArrayResource(("logging:\n config: " + tomcatLogConfig).getBytes())); + environment.getPropertySources().addFirst(new PropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME) { + @Override + public boolean containsProperty(String name) { + if ("LOGGING_CONFIG".equals(name)) { + return true; + } else { + return super.containsProperty(name); + } + } + + @Override + public Object getProperty(String name) { + if ("LOGGING_CONFIG".equals(name)) { + return tomcatLogConfig; + } else { + return System.getenv(name); + } + + } + }); + initializer.initialize(context); + assertEquals("-Djava.util.logging.config=/some/path/logging.properties", environment.getProperty("logging.config")); + Mockito.verify(servletContext, atLeastOnce()).log(servletLogCaptor.capture()); + boolean logEntryFound = false; + for (String s : servletLogCaptor.getAllValues()) { + if (s.startsWith("Ignoring Log Config Location") && s.contains("Tomcat startup script environment variable")) { + logEntryFound = true; + } + } + assertTrue("Expected to find a log entry indicating that the LOGGING_CONFIG variable was found.", logEntryFound); } + + private static class EmptyEnumerationOfString implements Enumeration { + @Override + public boolean hasMoreElements() { + return false; + } + + @Override + public String nextElement() { + return null; + } + } + + @ExtendWith(PollutionPreventionExtension.class) + @ExtendWith(SpringProfileCleanupExtension.class) + @Nested + class ApplySpringProfiles { + + private MockEnvironment environment; + private MockServletContext context; + + @BeforeEach + void setup() { + initializer = new YamlServletProfileInitializer(); + environment = new MockEnvironment(); + context = new MockServletContext(); + } + + @Test + void tokenizeToStringArray_RemovesSpaces() { + String profileString = " database , ldap "; + String[] profiles = StringUtils.tokenizeToStringArray(profileString, ",", true, true); + assertThat(profiles.length, is(2)); + assertThat(profiles[0], is("database")); + assertThat(profiles[1], is("ldap")); + // And show what's wrong with commaDelimitedListToStringArray + profiles = StringUtils.commaDelimitedListToStringArray(profileString); + assertThat(profiles.length, is(2)); + assertThat(profiles[0], is(" database ")); + assertThat(profiles[1], is(" ldap ")); + } + + @Test + void ifNoProfilesAreSetUseHsqldb() { + System.clearProperty("spring.profiles.active"); + initializer.applySpringProfiles(environment, context); + assertArrayEquals(new String[]{"hsqldb"}, environment.getActiveProfiles()); + } + + @Test + void ifProfilesAreSetUseThem() { + System.setProperty("spring.profiles.active", "hsqldb,default"); + initializer.applySpringProfiles(environment, context); + assertArrayEquals(new String[]{"hsqldb", "default"}, environment.getActiveProfiles()); + } + + @Test + void defaultProfileUnset() { + System.setProperty("spring.profiles.active", "hsqldb"); + initializer.applySpringProfiles(environment, context); + assertArrayEquals(new String[]{"hsqldb"}, environment.getActiveProfiles()); + assertArrayEquals(new String[0], environment.getDefaultProfiles()); + } + + @Test + void yamlConfiguredProfilesAreUsed() { + System.setProperty("spring.profiles.active", "hsqldb,default"); + environment.setProperty("spring_profiles", "mysql,default"); + initializer.applySpringProfiles(environment, context); + assertArrayEquals(new String[]{"mysql", "default"}, environment.getActiveProfiles()); + } + } + } diff --git a/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/YamlServletProfileInitializerTests.java b/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/YamlServletProfileInitializerTests.java deleted file mode 100644 index a321c884557..00000000000 --- a/server/src/test/java/org/cloudfoundry/identity/uaa/impl/config/YamlServletProfileInitializerTests.java +++ /dev/null @@ -1,291 +0,0 @@ -package org.cloudfoundry.identity.uaa.impl.config; - -import org.cloudfoundry.identity.uaa.security.PollutionPreventionExtension; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.ArgumentCaptor; -import org.mockito.ArgumentMatchers; -import org.mockito.Mockito; -import org.mockito.stubbing.Answer; -import org.springframework.core.env.PropertySource; -import org.springframework.core.env.StandardEnvironment; -import org.springframework.core.io.ByteArrayResource; -import org.springframework.security.web.session.HttpSessionEventPublisher; -import org.springframework.web.context.ConfigurableWebApplicationContext; -import org.springframework.web.context.support.StandardServletEnvironment; - -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import java.util.Enumeration; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; -import static org.springframework.util.StringUtils.hasText; - -@ExtendWith(PollutionPreventionExtension.class) -@ExtendWith(SpringProfileCleanupExtension.class) -@ExtendWith(LoggerContextCleanupExtension.class) -class YamlServletProfileInitializerTests { - - private YamlServletProfileInitializer initializer; - private ConfigurableWebApplicationContext context; - private StandardServletEnvironment environment; - private ServletConfig servletConfig; - private ServletContext servletContext; - - @BeforeEach - void setup() { - initializer = new YamlServletProfileInitializer(); - context = mock(ConfigurableWebApplicationContext.class); - environment = new StandardServletEnvironment(); - servletConfig = mock(ServletConfig.class); - servletContext = mock(ServletContext.class); - - Mockito.when(servletConfig.getInitParameterNames()).thenReturn(new EmptyEnumerationOfString()); - Mockito.when(servletContext.getInitParameterNames()).thenReturn(new EmptyEnumerationOfString()); - - Mockito.when(context.getServletConfig()).thenReturn(servletConfig); - Mockito.when(context.getServletContext()).thenReturn(servletContext); - Mockito.when(context.getEnvironment()).thenReturn(environment); - Mockito.doAnswer((Answer) invocation -> { - System.err.println(invocation.getArguments()[0]); - return null; - }).when(servletContext).log(ArgumentMatchers.anyString()); - Mockito.when(servletContext.getContextPath()).thenReturn("/context"); - } - - @AfterEach - void cleanup() { - System.clearProperty("APPLICATION_CONFIG_URL"); - System.clearProperty("LOG_FILE"); - System.clearProperty("LOG_PATH"); - } - - @Test - void loadDefaultResource() { - Mockito.when(context.getResource(ArgumentMatchers.contains("${APPLICATION_CONFIG_URL}"))).thenReturn( - new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); - - initializer.initialize(context); - - assertEquals("bar", environment.getProperty("foo")); - assertEquals("baz", environment.getProperty("spam.foo")); - } - - @Test - void loadSessionEventPublisher() { - Mockito.when(context.getResource(ArgumentMatchers.contains("${APPLICATION_CONFIG_URL}"))).thenReturn( - new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); - - initializer.initialize(context); - - ArgumentCaptor httpSessionEventPublisherArgumentCaptor = ArgumentCaptor.forClass(HttpSessionEventPublisher.class); - verify(servletContext, atLeastOnce()).addListener(httpSessionEventPublisherArgumentCaptor.capture()); - assertNotNull(httpSessionEventPublisherArgumentCaptor.getValue()); - } - - @Test - void activeProfiles() { - System.setProperty("spring.profiles.active", "foo"); - - Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn( - new ByteArrayResource("spring_profiles: bar".getBytes())); - - initializer.initialize(context); - - assertEquals("bar", environment.getActiveProfiles()[0]); - } - - @Test - void activeProfilesFromYaml() { - Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn( - new ByteArrayResource("spring_profiles: bar".getBytes())); - - initializer.initialize(context); - - assertEquals("bar", environment.getActiveProfiles()[0]); - } - - @Test - void log4jFileFromYaml() { - Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn( - new ByteArrayResource("logging:\n file: /tmp/bar.log".getBytes())); - initializer.initialize(context); - assertEquals("/tmp/bar.log", System.getProperty("LOG_FILE")); - } - - @Test - void log4jPathFromYaml() { - Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn( - new ByteArrayResource("logging:\n path: /tmp/log/bar".getBytes())); - initializer.initialize(context); - assertEquals("/tmp/log/bar", System.getProperty("LOG_PATH")); - } - - @Test - void log4jConfigurationFromYaml() { - Mockito.when(context.getResource(ArgumentMatchers.anyString())).thenReturn( - new ByteArrayResource("logging:\n config: bar".getBytes())); - initializer.initialize(context); - } - - @Test - void loadServletConfiguredFilename() { - Mockito.when(servletConfig.getInitParameter("APPLICATION_CONFIG_FILE")).thenReturn("/config/path/foo.yml"); - Mockito.when(context.getResource(ArgumentMatchers.eq("file:/config/path/foo.yml"))).thenReturn( - new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); - - initializer.initialize(context); - - assertEquals("bar", environment.getProperty("foo")); - assertEquals("baz", environment.getProperty("spam.foo")); - } - - @Test - void loadServletConfiguredResource() { - Mockito.when(servletConfig.getInitParameter("environmentConfigLocations")).thenReturn("foo.yml"); - Mockito.when(context.getResource(ArgumentMatchers.eq("foo.yml"))).thenReturn( - new ByteArrayResource("foo: bar\nspam:\n foo: baz-from-config".getBytes())); - - initializer.initialize(context); - - assertEquals("bar", environment.getProperty("foo")); - assertEquals("baz-from-config", environment.getProperty("spam.foo")); - } - - @Test - void loadContextConfiguredResource() { - Mockito.when(servletContext.getInitParameter("environmentConfigLocations")).thenReturn("foo.yml"); - Mockito.when(context.getResource(ArgumentMatchers.eq("foo.yml"))).thenReturn( - new ByteArrayResource("foo: bar\nspam:\n foo: baz-from-context".getBytes())); - - initializer.initialize(context); - - assertEquals("bar", environment.getProperty("foo")); - assertEquals("baz-from-context", environment.getProperty("spam.foo")); - } - - @Test - void loadReplacedResource() { - System.setProperty("APPLICATION_CONFIG_URL", "file:foo/uaa.yml"); - - Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( - new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); - - initializer.initialize(context); - - assertEquals("bar", environment.getProperty("foo")); - assertEquals("baz", environment.getProperty("spam.foo")); - } - - @Test - void loadReplacedResourceFromFileLocation() { - System.setProperty("APPLICATION_CONFIG_FILE", "foo/uaa.yml"); - - Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( - new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); - - initializer.initialize(context); - - assertEquals("bar", environment.getProperty("foo")); - assertEquals("baz", environment.getProperty("spam.foo")); - } - - @Test - void loggingConfigVariableWorks() { - System.setProperty("APPLICATION_CONFIG_FILE", "foo/uaa.yml"); - Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( - new ByteArrayResource("logging:\n config: /some/path".getBytes())); - initializer.initialize(context); - assertEquals("/some/path", environment.getProperty("logging.config")); - assertNull(environment.getProperty("smtp.host")); - assertNull(environment.getProperty("smtp.port")); - } - - @Test - void readingYamlFromEnvironment_WithNullVariableName() { - readingYamlFromEnvironment(null); - } - - @Test - void readingYamlFromEnvironment_WithNonNullVariableName() { - readingYamlFromEnvironment("Renaming environment variable"); - } - - private void readingYamlFromEnvironment(String variableName) { - if (hasText(variableName)) { - initializer.setYamlEnvironmentVariableName(variableName); - } - SystemEnvironmentAccessor env = new SystemEnvironmentAccessor() { - @Override - public String getEnvironmentVariable(String name) { - return name.equals(initializer.getYamlEnvironmentVariableName()) ? - "uaa.url: http://uaa.test.url/\n" + - "login.url: http://login.test.url/\n" + - "smtp:\n" + - " host: mail.server.host\n" + - " port: 3535\n" : - null; - } - }; - initializer.setEnvironmentAccessor(env); - initializer.initialize(context); - assertEquals("mail.server.host", environment.getProperty("smtp.host")); - assertEquals("3535", environment.getProperty("smtp.port")); - assertEquals("http://uaa.test.url/", environment.getProperty("uaa.url")); - assertEquals("http://login.test.url/", environment.getProperty("login.url")); - } - - @Test - void ignoreDashDTomcatLoggingConfigVariable() { - final String tomcatLogConfig = "-Djava.util.logging.config=/some/path/logging.properties"; - System.setProperty("APPLICATION_CONFIG_FILE", "foo/uaa.yml"); - ArgumentCaptor servletLogCaptor = ArgumentCaptor.forClass(String.class); - Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( - new ByteArrayResource(("logging:\n config: " + tomcatLogConfig).getBytes())); - environment.getPropertySources().addFirst(new PropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME) { - @Override - public boolean containsProperty(String name) { - if ("LOGGING_CONFIG".equals(name)) { - return true; - } else { - return super.containsProperty(name); - } - } - - @Override - public Object getProperty(String name) { - if ("LOGGING_CONFIG".equals(name)) { - return tomcatLogConfig; - } else { - return System.getenv(name); - } - - } - }); - initializer.initialize(context); - assertEquals("-Djava.util.logging.config=/some/path/logging.properties", environment.getProperty("logging.config")); - Mockito.verify(servletContext, atLeastOnce()).log(servletLogCaptor.capture()); - boolean logEntryFound = false; - for (String s : servletLogCaptor.getAllValues()) { - if (s.startsWith("Ignoring Log Config Location") && s.contains("Tomcat startup script environment variable")) { - logEntryFound = true; - } - } - assertTrue("Expected to find a log entry indicating that the LOGGING_CONFIG variable was found.", logEntryFound); - } - - private static class EmptyEnumerationOfString implements Enumeration { - @Override - public boolean hasMoreElements() { - return false; - } - - @Override - public String nextElement() { - return null; - } - } -}