@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"); }
/** * 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)); }
@Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { TestPropertyValues .of(EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY + "=false") .applyTo(context); }
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) { TestPropertyValues .of("spring.profiles.active=" + StringUtils.arrayToCommaDelimitedString(profiles)) .applyTo(environment); }
@Test public void asymmetricJwt() { TestPropertyValues.of("security.oauth2.resource.jwt.keyValue=" + PUBLIC_KEY) .applyTo(this.environment); this.context = new SpringApplicationBuilder(ResourceConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run(); DefaultTokenServices services = this.context.getBean(DefaultTokenServices.class); assertThat(services).isNotNull(); }
@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(); }
@Test public void userInfoWithAuthorities() { TestPropertyValues.of("security.oauth2.resource.userInfoUri:http://example.com") .applyTo(this.environment); this.context = new SpringApplicationBuilder(AuthoritiesConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run(); UserInfoTokenServices services = this.context .getBean(UserInfoTokenServices.class); assertThat(services).isNotNull(); assertThat(services).extracting("authoritiesExtractor") .containsExactly(this.context.getBean(AuthoritiesExtractor.class)); }
@Test public void configureWhenKeyStoreIsProvidedButNoAliasThenThrowsException() { TestPropertyValues.of("security.oauth2.resource.jwt.key-store=classpath:" + "org/springframework/boot/autoconfigure/security/oauth2/resource/keystore.jks", "security.oauth2.resource.jwt.key-store-password=changeme") .applyTo(this.environment); assertThatCode(() -> new SpringApplicationBuilder(ResourceConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run()) .isInstanceOf(UnsatisfiedDependencyException.class); }
@Test public void configureWhenKeyStoreIsProvidedButNoPasswordThenThrowsException() { TestPropertyValues.of("security.oauth2.authorization.jwt.key-store=classpath:" + "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks", "security.oauth2.authorization.jwt.key-alias=jwt") .applyTo(this.environment); assertThatCode( () -> new SpringApplicationBuilder(AuthorizationServerConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run()) .isInstanceOf(UnsatisfiedDependencyException.class); }
@Test public void configureWhenKeyStoreIsProvidedButNoPasswordThenThrowsException() { TestPropertyValues.of("security.oauth2.resource.jwt.key-store=classpath:" + "org/springframework/boot/autoconfigure/security/oauth2/resource/keystore.jks", "security.oauth2.resource.jwt.key-alias=jwt").applyTo(this.environment); assertThatCode(() -> new SpringApplicationBuilder(ResourceConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run()); }
@Test public void jwtTokenStoreShouldBeConditionalOnMissingBean() throws Exception { TestPropertyValues.of("security.oauth2.resource.jwt.keyValue=" + PUBLIC_KEY) .applyTo(this.environment); this.context = new SpringApplicationBuilder(JwtTokenStoreConfiguration.class, ResourceConfiguration.class).environment(this.environment) .web(WebApplicationType.NONE).run(); assertThat(this.context.getBeansOfType(JwtTokenStore.class)).hasSize(1); }
@Test public void configureWhenKeyStoreIsProvidedThenExposesJwtAccessTokenConverter() { TestPropertyValues.of("security.oauth2.authorization.jwt.key-store=classpath:" + "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks", "security.oauth2.authorization.jwt.key-store-password=changeme", "security.oauth2.authorization.jwt.key-alias=jwt") .applyTo(this.environment); this.context = new SpringApplicationBuilder( AuthorizationServerConfiguration.class).environment(this.environment) .web(WebApplicationType.NONE).run(); assertThat(this.context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1); }
@Test public void requestScopedWithClientId() throws Exception { TestPropertyValues.of("security.oauth2.client.client-id=acme") .applyTo(this.environment); initializeContext(ConfigForRequestScopedConfiguration.class, false); assertThat(this.context.containsBean("oauth2ClientContext")).isTrue(); }
@Test public void siteSwitcherHandlerInterceptorEnabled() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); TestPropertyValues.of("spring.mobile.siteswitcher.enabled:true") .applyTo(this.context); this.context.register(Config.class); this.context.refresh(); assertThat(this.context.getBean(SiteSwitcherHandlerInterceptor.class)) .isNotNull(); }
@Test public void sitePreferenceHandlerInterceptorEnabled() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); TestPropertyValues.of("spring.mobile.sitepreference.enabled:true") .applyTo(this.context); this.context.register(SitePreferenceAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(SitePreferenceHandlerInterceptor.class)) .isNotNull(); }
@Test(expected = BeanCreationException.class) public void templateLocationDoesNotExist() throws Exception { TestPropertyValues.of("spring.jade4j.prefix:classpath:/no-such-directory/").applyTo(context); this.context.register(Jade4JAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); }
@Test public void bindInvalidProperties() { this.context.register(ServiceBrokerPropertiesConfiguration.class); TestPropertyValues.of("spring.cloud.openservicebroker.catalog.services[0].id:service-one-id") .applyTo(this.context); this.thrown.expect(BeanCreationException.class); this.context.refresh(); }
@Test(expected = NoSuchBeanDefinitionException.class) public void siteSwitcherHandlerInterceptorDisabled() { this.context = new AnnotationConfigWebApplicationContext(); TestPropertyValues.of("spring.mobile.siteswitcher.enabled:false") .applyTo(this.context); this.context.register(Config.class); this.context.refresh(); this.context.getBean(SiteSwitcherHandlerInterceptor.class); }
@Test public void shouldValidateProperties() { this.context.register(EnableAutoConfig.class); TestPropertyValues .of("edison.application.management.base-path=/internal") .and("edison.ldap.enabled=true") .applyTo(context); assertThrows(UnsatisfiedDependencyException.class, () -> { this.context.refresh(); }); }
@Test(expected = NoSuchBeanDefinitionException.class) public void sitePreferenceHandlerInterceptorDisabled() { this.context = new AnnotationConfigWebApplicationContext(); TestPropertyValues.of("spring.mobile.sitepreference.enabled:false") .applyTo(this.context); this.context.register(SitePreferenceAutoConfiguration.class); this.context.refresh(); this.context.getBean(SitePreferenceHandlerInterceptor.class); }