@Test public void webEnvironmentSwitchedOffInListener() throws Exception { TestSpringApplication application = new TestSpringApplication( ExampleConfig.class); application.addListeners( new ApplicationListener<ApplicationEnvironmentPreparedEvent>() { @Override public void onApplicationEvent( ApplicationEnvironmentPreparedEvent event) { assertThat(event.getEnvironment()) .isInstanceOf(StandardServletEnvironment.class); TestPropertySourceUtils.addInlinedPropertiesToEnvironment( event.getEnvironment(), "foo=bar"); event.getSpringApplication().setWebEnvironment(false); } }); this.context = application.run(); assertThat(this.context.getEnvironment()) .isNotInstanceOf(StandardServletEnvironment.class); assertThat(this.context.getEnvironment().getProperty("foo")); assertThat(this.context.getEnvironment().getPropertySources().iterator().next() .getName()).isEqualTo( TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); }
@Override public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception { SpringApplication application = new SpringApplication(); application.setMainApplicationClass(config.getTestClass()); application.setWebEnvironment(false); application.setSources( new LinkedHashSet<Object>(Arrays.asList(config.getClasses()))); ConfigurableEnvironment environment = new StandardEnvironment(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("spring.jmx.enabled", "false"); properties.putAll(TestPropertySourceUtils .convertInlinedPropertiesToMap(config.getPropertySourceProperties())); environment.getPropertySources().addAfter( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, new MapPropertySource("integrationTest", properties)); application.setEnvironment(environment); return application.run(); }
@Test public void testNullApplicationProperties() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "VCAP_APPLICATION={\"application_users\":null," + "\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\"," + "\"instance_index\":0,\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\"," + "\"name\":\"foo\",\"uris\":[\"foo.cfapps.io\"]," + "\"started_at\":\"2013-05-29 02:37:59 +0000\"," + "\"started_at_timestamp\":1369795079," + "\"host\":\"0.0.0.0\",\"port\":61034," + "\"limits\":{\"mem\":128,\"disk\":1024,\"fds\":16384}," + "\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\"," + "\"name\":\"dsyerenv\",\"uris\":[\"dsyerenv.cfapps.io\"]," + "\"users\":[],\"start\":\"2013-05-29 02:37:59 +0000\"," + "\"state_timestamp\":1369795079}"); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null); assertThat(getProperty("vcap")).isNull(); }
@Test public void systemPropertiesAreSetForLoggingConfiguration() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.exception-conversion-word=conversion", "logging.file=target/log", "logging.path=path", "logging.pattern.console=console", "logging.pattern.file=file", "logging.pattern.level=level"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); assertThat(System.getProperty("CONSOLE_LOG_PATTERN")).isEqualTo("console"); assertThat(System.getProperty("FILE_LOG_PATTERN")).isEqualTo("file"); assertThat(System.getProperty("LOG_EXCEPTION_CONVERSION_WORD")) .isEqualTo("conversion"); assertThat(System.getProperty("LOG_FILE")).isEqualTo("target/log"); assertThat(System.getProperty("LOG_LEVEL_PATTERN")).isEqualTo("level"); assertThat(System.getProperty("LOG_PATH")).isEqualTo("path"); assertThat(System.getProperty("PID")).isNotNull(); }
@Test public void testServiceProperties() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "VCAP_SERVICES={\"rds-mysql-n/a\":[{" + "\"name\":\"mysql\",\"label\":\"rds-mysql-n/a\"," + "\"plan\":\"10mb\",\"credentials\":{" + "\"name\":\"d04fb13d27d964c62b267bbba1cffb9da\"," + "\"hostname\":\"mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\"," + "\"ssl\":true,\"location\":null," + "\"host\":\"mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\"," + "\"port\":3306,\"user\":\"urpRuqTf8Cpe6\",\"username\":" + "\"urpRuqTf8Cpe6\",\"password\":\"pxLsGVpsC9A5S\"}}]}"); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null); assertThat(getProperty("vcap.services.mysql.name")).isEqualTo("mysql"); assertThat(getProperty("vcap.services.mysql.credentials.port")).isEqualTo("3306"); assertThat(getProperty("vcap.services.mysql.credentials.ssl")).isEqualTo("true"); assertThat(getProperty("vcap.services.mysql.credentials.location")).isEqualTo(""); }
@Test public void testApplicationProperties() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "VCAP_APPLICATION={\"application_users\":[]," + "\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\"," + "\"instance_index\":0,\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\"," + "\"name\":\"foo\",\"uris\":[\"foo.cfapps.io\"]," + "\"started_at\":\"2013-05-29 02:37:59 +0000\"," + "\"started_at_timestamp\":1369795079," + "\"host\":\"0.0.0.0\",\"port\":61034," + "\"limits\":{\"mem\":128,\"disk\":1024,\"fds\":16384}," + "\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\"," + "\"name\":\"dsyerenv\",\"uris\":[\"dsyerenv.cfapps.io\"]," + "\"users\":[],\"start\":\"2013-05-29 02:37:59 +0000\"," + "\"state_timestamp\":1369795079}"); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null); assertThat( this.context.getEnvironment().getProperty("vcap.application.instance_id")) .isEqualTo("bb7935245adf3e650dfb7c58a06e9ece"); }
@Test public void error() { assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "spring.application.json=foo:bar"); this.processor.postProcessEnvironment(this.environment, null); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); }
@Test public void overrideConfigDoesNotExist() throws Exception { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.config=doesnotexist.xml"); this.thrown.expect(IllegalStateException.class); this.outputCapture.expect(containsString( "Logging system failed to initialize using configuration from 'doesnotexist.xml'")); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); }
@Test public void parseDebugArg() throws Exception { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "debug"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); this.logger.debug("testatdebug"); this.logger.trace("testattrace"); assertThat(this.outputCapture.toString()).contains("testatdebug"); assertThat(this.outputCapture.toString()).doesNotContain("testattrace"); }
@Test public void overrideConfigBroken() throws Exception { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.config=classpath:logback-broken.xml"); this.thrown.expect(IllegalStateException.class); this.outputCapture.expect(containsString( "Logging system failed to initialize using configuration from 'classpath:logback-broken.xml'")); this.outputCapture.expect(containsString("ConsolAppender")); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); }
@Test public void addLogFileProperty() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.config=classpath:logback-nondefault.xml", "logging.file=target/foo.log"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); logger.info("Hello world"); String output = this.outputCapture.toString().trim(); assertThat(output).startsWith("target/foo.log"); }
@Test public void addLogFilePropertyWithDefault() { assertThat(new File("target/foo.log").exists()).isFalse(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.file=target/foo.log"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); logger.info("Hello world"); assertThat(new File("target/foo.log").exists()).isTrue(); }
@Test public void addLogPathProperty() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.config=classpath:logback-nondefault.xml", "logging.path=target/foo/"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); logger.info("Hello world"); String output = this.outputCapture.toString().trim(); assertThat(output).startsWith("target/foo/spring.log"); }
@Test public void configurationPropertiesWithArrayExpansion() throws Exception { this.context = new AnnotationConfigApplicationContext(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "test.chars[4]=s"); this.context.register(PropertyWithCharArrayExpansion.class); this.context.refresh(); assertThat(this.context.getBean(PropertyWithCharArrayExpansion.class).getChars()) .isEqualTo("words".toCharArray()); }
@Test public void disabledViaApplicationProperties() throws Exception { ConfigurableEnvironment environment = new StandardEnvironment(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, "spring.config.name=ansi"); SpringApplication application = new SpringApplication(Config.class); application.setWebEnvironment(false); application.setEnvironment(environment); this.context = application.run(); assertThat(AnsiOutputEnabledValue.get()).isEqualTo(Enabled.NEVER); }
@Test public void testExceptionOnValidation() { this.context.register(ExceptionIfInvalidTestConfiguration.class); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "name:foo"); this.thrown.expectCause(Matchers.<Throwable>instanceOf(BindException.class)); this.context.refresh(); }
@Test public void placeholderResolutionWithCustomLocation() throws Exception { this.context = new AnnotationConfigApplicationContext(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "fooValue=bar"); this.context.register(CustomConfigurationLocation.class); this.context.refresh(); assertThat(this.context.getBean(CustomConfigurationLocation.class).getFoo()) .isEqualTo("bar"); }
@Test public void parseLevelsWithPlaceholder() throws Exception { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "foo=TRACE", "logging.level.org.springframework.boot=${foo}"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); this.logger.debug("testatdebug"); this.logger.trace("testattrace"); assertThat(this.outputCapture.toString()).contains("testatdebug"); assertThat(this.outputCapture.toString()).contains("testattrace"); }
@Test public void parseLevelsFails() throws Exception { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "logging.level.org.springframework.boot=GARBAGE"); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); this.logger.debug("testatdebug"); assertThat(this.outputCapture.toString()).doesNotContain("testatdebug") .contains("Cannot set level: GARBAGE"); }
@Test public void testApplicationUris() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "VCAP_APPLICATION={\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"uris\":[\"foo.cfapps.io\"]}"); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null); assertThat(this.context.getEnvironment().getProperty("vcap.application.uris[0]")) .isEqualTo("foo.cfapps.io"); }
@Override public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception { SpringApplication application = getSpringApplication(); application.setMainApplicationClass(config.getTestClass()); application.setSources(getSources(config)); ConfigurableEnvironment environment = new StandardEnvironment(); if (!ObjectUtils.isEmpty(config.getActiveProfiles())) { setActiveProfiles(environment, config.getActiveProfiles()); } TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, application.getResourceLoader() == null ? new DefaultResourceLoader(getClass().getClassLoader()) : application.getResourceLoader(), config.getPropertySourceLocations()); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(config)); application.setEnvironment(environment); List<ApplicationContextInitializer<?>> initializers = getInitializers(config, application); if (config instanceof WebMergedContextConfiguration) { application.setWebEnvironment(true); if (!isEmbeddedWebEnvironment(config)) { new WebConfigurer().configure(config, application, initializers); } } else { application.setWebEnvironment(false); } application.setInitializers(initializers); ConfigurableApplicationContext context = application.run(); return context; }
@Test public void prefixed() { assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "SPRING_APPLICATION_JSON={\"foo.bar\":\"spam\"}"); this.processor.postProcessEnvironment(this.environment, null); assertThat(this.environment.resolvePlaceholders("${foo.bar:}")).isEqualTo("spam"); }
@Test public void springProperty() throws Exception { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "my.example-property=test"); initialize("property.xml"); assertThat(this.context.getProperty("MINE")).isEqualTo("test"); }
@Test public void relaxedSpringProperty() throws Exception { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "my.EXAMPLE_PROPERTY=test"); initialize("property.xml"); assertThat(this.context.getProperty("MINE")).isEqualTo("test"); }
@Test public void loadTwoOfThreePropertiesFile() throws Exception { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "spring.config.location=classpath:application.properties," + "classpath:testproperties.properties," + "classpath:nonexistent.properties"); this.initializer.postProcessEnvironment(this.environment, this.application); String property = this.environment.getProperty("the.property"); assertThat(property).isEqualTo("frompropertiesfile"); }
@Test public void envVar() { assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "SPRING_APPLICATION_JSON={\"foo\":\"bar\"}"); this.processor.postProcessEnvironment(this.environment, null); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar"); }
@Test public void periodSeparated() { assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "spring.application.json={\"foo\":\"bar\"}"); this.processor.postProcessEnvironment(this.environment, null); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar"); }
@Test @SuppressWarnings("unchecked") public void extractNoPrefix() { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.env, "foo.ctx.first=1", "foo.ctx.second=2"); Map<String, Object> content = new PropertySourcesBinder(this.env).extractAll(""); assertThat(content.get("foo")).isInstanceOf(Map.class); Map<String, Object> foo = (Map<String, Object>) content.get("foo"); assertThat(content.get("foo")).isInstanceOf(Map.class); Map<String, Object> ctx = (Map<String, Object>) foo.get("ctx"); assertThat(ctx.get("first")).isEqualTo("1"); assertThat(ctx.get("second")).isEqualTo("2"); assertThat(ctx).hasSize(2); assertThat(foo).hasSize(1); }
@Test public void testSimpleAutoConfig() throws Exception { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "external.name=foo"); this.context.register(ExampleConfig.class); this.context.refresh(); assertThat(this.context.getBean(External.class).getName()).isEqualTo("foo"); }
private void doEnumTest(String property) { this.context = new AnnotationConfigApplicationContext(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, property); this.context.register(PropertyWithEnum.class); this.context.refresh(); assertThat(this.context.getBean(PropertyWithEnum.class).getTheValue()) .isEqualTo(FooEnum.FOO); this.context.close(); }
private void load(Class<?> config, String... environment) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); if (config != null) { ctx.register(config); } TestPropertySourceUtils.addInlinedPropertiesToEnvironment(ctx, environment); ctx.register(JmxAutoConfiguration.class, IntegrationAutoConfiguration.class); ctx.refresh(); this.context = ctx; }
@Test public void testValueBindingForDefaults() throws Exception { this.context = new AnnotationConfigApplicationContext(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "default.value=foo"); this.context.register(PropertyWithValue.class); this.context.refresh(); assertThat(this.context.getBean(PropertyWithValue.class).getValue()) .isEqualTo("foo"); }