@Test public void contextLoads() { assertThat(controller.getMessage()).isNotEqualTo("Hello test"); TestPropertyValues .of("message:Hello test") .applyTo(environment); assertThat(controller.getMessage()).isNotEqualTo("Hello test"); refresher.refresh(); assertThat(controller.getMessage()).isEqualTo("Hello test"); }
@Test public void shouldUseSSLEncryptionIfConfigured() { this.context.register(EnableAutoConfig.class); TestPropertyValues .of("edison.application.management.base-path=/internal") .and("edison.ldap.enabled=true") .and("edison.ldap.encryptionType=SSL") .and("edison.ldap.host=localhost") .and("edison.ldap.base-dn=test-dn") .and("edison.ldap.rdn-identifier=test-rdn") .applyTo(context); this.context.refresh(); assertThat(this.context.getBean("ldapConnectionFactory").getClass().getSimpleName(), is(SSLLdapConnectionFactory.class.getSimpleName())); }
private TestPropertyValues and(Stream<Pair> pairs) { Map<String, Object> properties = new LinkedHashMap<>(this.properties); pairs.filter(Objects::nonNull).forEach((pair) -> pair.addTo(properties)); return new TestPropertyValues(properties); }
/** * Return a new {@link TestPropertyValues} with the underlying map populated with the * given property pairs. Name-value pairs can be specified with colon (":") or equals * ("=") separators. * @param pairs the name-value pairs for properties that need to be added to the * environment * @return the new instance */ public static TestPropertyValues of(Stream<String> pairs) { if (pairs == null) { return empty(); } return empty().and(pairs.map(Pair::parse)); }
/** * Return a new {@link TestPropertyValues} with the underlying map populated with the * given property pairs. Name-value pairs can be specified with colon (":") or equals * ("=") separators. * @param pairs the name-value pairs for properties that need to be added to the * environment * @return the new instance */ public static TestPropertyValues of(Iterable<String> pairs) { if (pairs == null) { return empty(); } return of(StreamSupport.stream(pairs.spliterator(), false)); }
/** * Add the properties from the underlying map to the environment. The default property * source used is {@link MapPropertySource}. * @param environment the environment that needs to be modified */ public void applyTo(ConfigurableEnvironment environment) { applyTo(environment, Type.MAP); }
/** * Return a new {@link TestPropertyValues} with the underlying map populated with the * given property pairs. Name-value pairs can be specified with colon (":") or equals * ("=") separators. * @param pairs the name-value pairs for properties that need to be added to the * environment * @return the new instance */ public static TestPropertyValues of(String... pairs) { return of(Stream.of(pairs)); }
/** * Builder method to add more properties. * @param pairs the property pairs to add * @return a new {@link TestPropertyValues} instance */ public TestPropertyValues and(String... pairs) { return and(Arrays.stream(pairs).map(Pair::parse)); }
/** * Add the properties from the underlying map to the environment owned by an * {@link ApplicationContext}. * @param context the context with an environment to modify */ public void applyTo(ConfigurableApplicationContext context) { applyTo(context.getEnvironment()); }
/** * Add the specified {@link System} property pairs. Key-value pairs can be specified * with colon (":") or equals ("=") separators. System properties are added before the * context is {@link #run(ContextConsumer) run} and restored when the context is * closed. * @param pairs the key-value pairs for properties that need to be added to the system * @return a new instance with the updated system properties * @see TestPropertyValues * @see #withSystemProperties(String...) */ public SELF withSystemProperties(String... pairs) { return newInstance(this.contextFactory, this.initializers, this.environmentProperties, this.systemProperties.and(pairs), this.classLoader, this.parent, this.configurations); }
@Test public void shouldUseStartTLSEncryptionIfConfigured() { this.context.register(EnableAutoConfig.class); TestPropertyValues .of("edison.application.management.base-path=/internal") .and("edison.ldap.enabled=true") .and("edison.ldap.encryptionType=StartTLS") .and("edison.ldap.host=localhost") .and("edison.ldap.base-dn=test-dn") .and("edison.ldap.rdn-identifier=test-rdn") .applyTo(context); this.context.refresh(); assertThat(this.context.getBean("ldapConnectionFactory").getClass().getSimpleName(), is(StartTlsLdapConnectionFactory.class.getSimpleName())); }
/** * Add the properties from the underlying map to the environment using the specified * property source type. * @param environment the environment that needs to be modified * @param type the type of {@link PropertySource} to be added. See {@link Type} */ public void applyTo(ConfigurableEnvironment environment, Type type) { applyTo(environment, type, type.applySuffix("test")); }
/** * Add the specified {@link Environment} property pairs. Key-value pairs can be * specified with colon (":") or equals ("=") separators. Override matching keys that * might have been specified previously. * @param pairs the key-value pairs for properties that need to be added to the * environment * @return a new instance with the updated property values * @see TestPropertyValues * @see #withSystemProperties(String...) */ public SELF withPropertyValues(String... pairs) { return newInstance(this.contextFactory, this.initializers, this.environmentProperties.and(pairs), this.systemProperties, this.classLoader, this.parent, this.configurations); }
@Test public void shouldUseStartTLSEncryptionAsDefault() { this.context.register(EnableAutoConfig.class); TestPropertyValues .of("edison.application.management.base-path=/internal") .and("edison.ldap.enabled=true") .and("edison.ldap.host=localhost") .and("edison.ldap.base-dn=test-dn") .and("edison.ldap.rdn-identifier=test-rdn") .applyTo(context); this.context.refresh(); assertThat(this.context.getBean("ldapConnectionFactory").getClass().getSimpleName(), is(StartTlsLdapConnectionFactory.class.getSimpleName())); }
private void configureContext(C context) { if (this.parent != null) { context.setParent(this.parent); } if (this.classLoader != null) { Assert.isInstanceOf(DefaultResourceLoader.class, context); ((DefaultResourceLoader) context).setClassLoader(this.classLoader); } this.environmentProperties.applyTo(context); Class<?>[] classes = Configurations.getClasses(this.configurations); if (classes.length > 0) { ((AnnotationConfigRegistry) context).register(classes); } this.initializers.forEach((initializer) -> initializer.initialize(context)); context.refresh(); }
@Test public void useRemoteTokenServices() { TestPropertyValues.of("security.oauth2.resource.tokenInfoUri:http://example.com") .applyTo(this.environment); this.context = new SpringApplicationBuilder(ResourceConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run(); RemoteTokenServices services = this.context.getBean(RemoteTokenServices.class); assertThat(services).isNotNull(); }
@Test public void shouldUseMongoStateRepositoryIfEnabled() { this.context.register(MongoTogglzTestConfiguration.class); TestPropertyValues .of("edison.togglz.mongo.enabled=true") .and("edison.mongo.db=db") .and("edison.mongo.user=test") .and("edison.mongo.password=test") .applyTo(context); this.context.refresh(); assertThat(this.context.containsBean("stateRepository"), is(true)); assertThat(this.context.getBean("stateRepository", StateRepository.class), is(instanceOf(MongoTogglzRepository.class))); }
@Test public void clientCredentialsWithClientId() throws Exception { TestPropertyValues.of("security.oauth2.client.client-id=acme") .applyTo(this.environment); initializeContext(OAuth2RestOperationsConfiguration.class, true); assertThat(this.context.getBean(OAuth2RestOperationsConfiguration.class)) .isNotNull(); assertThat(this.context.getBean(ClientCredentialsResourceDetails.class)) .isNotNull(); }