Java tutorial
package org.jasypt.spring31.annotation; /* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.util.Iterator; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.env.Environment; import org.springframework.core.env.MutablePropertySources; /** * Tests the processing of @EncryptablePropertySource annotations on @Configuration * classes. * * @author Michael Wirth * @since 1.9.3 */ public class EncryptablePropertySourcePostProcessorTest { @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void withExplicitName() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithExplicitName.class); ctx.refresh(); assertTrue("property source p1 was not added", ctx.getEnvironment().getPropertySources().contains("p1")); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); // assert that the property source was added last to the set of sources String name; MutablePropertySources sources = ctx.getEnvironment().getPropertySources(); Iterator<org.springframework.core.env.PropertySource<?>> iterator = sources.iterator(); do { name = iterator.next().getName(); } while (iterator.hasNext()); assertThat(name, is("p1")); ctx.close(); } @Test public void withImplicitName() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithImplicitName.class); ctx.refresh(); assertTrue("property source p1 was not added", ctx.getEnvironment().getPropertySources() .contains("class path resource [org/jasypt/spring31/annotation/basic-encrypted-p1.properties]")); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); ctx.close(); } /** * Tests the LIFO behavior of @EncryptablePropertySource annotaitons. The * last one registered should 'win'. */ @Test public void orderingIsLifo() { { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithImplicitName.class, P2Config.class); ctx.refresh(); // p2 should 'win' as it was registered last assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p2TestBean")); ctx.close(); } { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(P2Config.class, ConfigWithImplicitName.class); ctx.refresh(); // p1 should 'win' as it was registered last assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); ctx.close(); } } @Test public void withUnresolvablePlaceholder() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithUnresolvablePlaceholder.class); try { ctx.refresh(); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof IllegalArgumentException); } ctx.close(); } @Test public void withUnresolvablePlaceholderAndDefault() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithUnresolvablePlaceholderAndDefault.class); ctx.refresh(); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); ctx.close(); } @Test public void withResolvablePlaceholder() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithResolvablePlaceholder.class); System.setProperty("path.to.properties", "org/jasypt/spring31/annotation"); ctx.refresh(); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); System.clearProperty("path.to.properties"); ctx.close(); } @Test public void withResolvablePlaceholderAndFactoryBean() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithResolvablePlaceholderAndFactoryBean.class); System.setProperty("path.to.properties", "org/jasypt/spring31/annotation"); ctx.refresh(); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); System.clearProperty("path.to.properties"); ctx.close(); } @Test public void withEmptyResourceLocations() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithEmptyResourceLocations.class); try { ctx.refresh(); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof IllegalArgumentException); } ctx.close(); } @Test public void withNameAndMultipleResourceLocations() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( ConfigWithNameAndMultipleResourceLocations.class); assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true)); assertThat(ctx.getEnvironment().containsProperty("from.p3"), is(true)); // p2 should 'win' as it was registered last assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p3TestBean")); ctx.close(); } @Test public void withMultipleResourceLocations() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( ConfigWithMultipleResourceLocations.class); assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true)); assertThat(ctx.getEnvironment().containsProperty("from.p3"), is(true)); // p2 should 'win' as it was registered last assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p3TestBean")); ctx.close(); } // @Test // public void withPropertySources() { // AnnotationConfigApplicationContext ctx = new // AnnotationConfigApplicationContext(ConfigWithPropertySources.class); // assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true)); // assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true)); // // p2 should 'win' as it was registered last // assertThat(ctx.getEnvironment().getProperty("testbean.name"), // equalTo("p2TestBean")); // } // // @Test // public void withNamedPropertySources() { // AnnotationConfigApplicationContext ctx = new // AnnotationConfigApplicationContext( // ConfigWithNamedPropertySources.class); // assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true)); // assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true)); // // p2 should 'win' as it was registered last // assertThat(ctx.getEnvironment().getProperty("testbean.name"), // equalTo("p2TestBean")); // } // // @Test // public void withMissingPropertySource() { // thrown.expect(BeanDefinitionStoreException.class); // thrown.expectCause(isA(FileNotFoundException.class)); // new // AnnotationConfigApplicationContext(ConfigWithMissingPropertySource.class); // } // // @Test // public void withIgnoredPropertySource() { // AnnotationConfigApplicationContext ctx = new // AnnotationConfigApplicationContext( // ConfigWithIgnoredPropertySource.class); // assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true)); // assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true)); // } // // @Test // public void withSameSourceImportedInDifferentOrder() { // AnnotationConfigApplicationContext ctx = new // AnnotationConfigApplicationContext( // ConfigWithSameSourceImportedInDifferentOrder.class); // assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true)); // assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true)); // assertThat(ctx.getEnvironment().getProperty("testbean.name"), // equalTo("p2TestBean")); // } @Test public void orderingWithAndWithoutNameAndMultipleResourceLocations() { // SPR-10820: p2 should 'win' as it was registered last AnnotationConfigApplicationContext ctxWithName = new AnnotationConfigApplicationContext( ConfigWithNameAndMultipleResourceLocations.class); AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext( ConfigWithMultipleResourceLocations.class); assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p3TestBean")); assertThat(ctxWithName.getEnvironment().getProperty("testbean.name"), equalTo("p3TestBean")); ctxWithName.close(); ctxWithoutName.close(); } // @Test // public void orderingWithAndWithoutNameAndFourResourceLocations() { // // SPR-12198: p4 should 'win' as it was registered last // AnnotationConfigApplicationContext ctxWithoutName = new // AnnotationConfigApplicationContext( // ConfigWithFourResourceLocations.class); // assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), // equalTo("p4TestBean")); // } // @Test // public void orderingDoesntReplaceExisting() throws Exception { // // SPR-12198: mySource should 'win' as it was registered manually // AnnotationConfigApplicationContext ctxWithoutName = new // AnnotationConfigApplicationContext(); // MapPropertySource mySource = new MapPropertySource("mine", // Collections.singletonMap("testbean.name", // "myTestBean")); // ctxWithoutName.getEnvironment().getPropertySources().addLast(mySource); // ctxWithoutName.register(ConfigWithFourResourceLocations.class); // ctxWithoutName.refresh(); // assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), // equalTo("myTestBean")); // } static class TestBean { private String name; public TestBean(String name) { this.name = name; } public Object getName() { // TODO Auto-generated method stub return name; } } @Configuration static class PostProcessorConfiguration { @Bean public static EncryptablePropertySourcePostProcessor encryptedPropertySourcePostProcessor() { return new EncryptablePropertySourcePostProcessor(); } } @Configuration @Import(PostProcessorConfiguration.class) @EncryptablePropertySource(value = "classpath:${unresolvable}/basic-encrypted-p1.properties", password = "s3cr3t") static class ConfigWithUnresolvablePlaceholder { } @Configuration @Import(PostProcessorConfiguration.class) @EncryptablePropertySource(value = "classpath:${unresolvable:org/jasypt/spring31/annotation}/basic-encrypted-p1.properties", password = "s3cr3t") static class ConfigWithUnresolvablePlaceholderAndDefault { @Autowired Environment env; @Bean public TestBean testBean() { return new TestBean(env.getProperty("testbean.name")); } } @Configuration @Import(PostProcessorConfiguration.class) @EncryptablePropertySource(value = "classpath:${path.to.properties}/basic-encrypted-p1.properties", password = "s3cr3t") static class ConfigWithResolvablePlaceholder { @Autowired Environment env; @Bean public TestBean testBean() { return new TestBean(env.getProperty("testbean.name")); } } @Configuration @Import(PostProcessorConfiguration.class) @EncryptablePropertySource(value = "classpath:${path.to.properties}/basic-encrypted-p1.properties", password = "s3cr3t") static class ConfigWithResolvablePlaceholderAndFactoryBean { @Autowired Environment env; // @Bean // public EncryptablePropertySourcePostProcessor // encryptedPropertySourcePostProcessor() { // return new EncryptablePropertySourcePostProcessor(); // } @Bean public FactoryBean<TestBean> testBean() { final String name = env.getProperty("testbean.name"); return new FactoryBean<TestBean>() { @Override public TestBean getObject() { return new TestBean(name); } @Override public Class<?> getObjectType() { return TestBean.class; } @Override public boolean isSingleton() { return false; } }; } } @Configuration @Import(PostProcessorConfiguration.class) @EncryptablePropertySource(name = "p1", value = "classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties", password = "s3cr3t") static class ConfigWithExplicitName { @Autowired Environment env; @Bean public TestBean testBean() { return new TestBean(env.getProperty("testbean.name")); } } @Configuration @Import(PostProcessorConfiguration.class) @EncryptablePropertySource(value = "classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties", password = "s3cr3t") static class ConfigWithImplicitName { @Autowired Environment env; @Bean public TestBean testBean() { return new TestBean(env.getProperty("testbean.name")); } } @Configuration @Import(PostProcessorConfiguration.class) @EncryptablePropertySource(value = "classpath:org/jasypt/spring31/annotation/strong-encrypted-p2.properties", password = "s3cr3t", encryptionType = "strong") static class P2Config { } @Configuration @Import(PostProcessorConfiguration.class) @EncryptablePropertySource(name = "psName", value = { "classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties", "classpath:org/jasypt/spring31/annotation/basic-encrypted-p3.properties" }, password = "s3cr3t") static class ConfigWithNameAndMultipleResourceLocations { } @Configuration @Import(PostProcessorConfiguration.class) @EncryptablePropertySource(value = { "classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties", "classpath:org/jasypt/spring31/annotation/basic-encrypted-p3.properties" }, password = "s3cr3t") static class ConfigWithMultipleResourceLocations { } // @Configuration // @EncryptablePropertySources({ // @EncryptablePropertySource("classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties"), // @EncryptablePropertySource("classpath:${base.package}/strong-encrypted-p2.properties"), // }, // password="s3cr3t") // static class ConfigWithPropertySources { // } // @Configuration // @EncryptablePropertySources({ // @EncryptablePropertySource(name = "psName", value = // "classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties"), // @EncryptablePropertySource(name = "psName", value = // "classpath:org/jasypt/spring31/annotation/strong-encrypted-p2.properties"), // }) // static class ConfigWithNamedPropertySources { // } // @Configuration // @EncryptablePropertySources({ // @EncryptablePropertySource(name = "psName", value = // "classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties"), // @EncryptablePropertySource(name = "psName", value = // "classpath:org/jasypt/spring31/annotation/missing.properties"), // @EncryptablePropertySource(name = "psName", value = // "classpath:org/jasypt/spring31/annotation/strong-encrypted-p2.properties") // }) // static class ConfigWithMissingPropertySource { // } // // @Configuration // @EncryptablePropertySources({ // @EncryptablePropertySource(name = "psName", value = // "classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties"), // @EncryptablePropertySource(name = "psName", value = // "classpath:org/jasypt/spring31/annotation/missing.properties", // ignoreResourceNotFound = true), // @EncryptablePropertySource(name = "psName", value = // "classpath:${myPath}/missing.properties", ignoreResourceNotFound = true), // @EncryptablePropertySource(name = "psName", value = // "classpath:org/jasypt/spring31/annotation/strong-encrypted-p2.properties") // }) // static class ConfigWithIgnoredPropertySource { // } @Configuration @Import(PostProcessorConfiguration.class) @EncryptablePropertySource(value = {}, password = "s3cr3t") static class ConfigWithEmptyResourceLocations { } // @Import(ConfigImportedWithSameSourceImportedInDifferentOrder.class) // @EncryptablePropertySources({ // @EncryptablePropertySource("classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties"), // @EncryptablePropertySource("classpath:org/jasypt/spring31/annotation/strong-encrypted-p2.properties") // }) // @Configuration // public static class ConfigWithSameSourceImportedInDifferentOrder { // // } // @Configuration // @EncryptablePropertySources({ // @EncryptablePropertySource("classpath:org/jasypt/spring31/annotation/strong-encrypted-p2.properties"), // @EncryptablePropertySource("classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties") // }) // public static class ConfigImportedWithSameSourceImportedInDifferentOrder // { // } // @Configuration // @EncryptablePropertySource(value = { // "classpath:org/jasypt/spring31/annotation/basic-encrypted-p1.properties", // "classpath:org/jasypt/spring31/annotation/strong-encrypted-p2.properties", // "classpath:org/jasypt/spring31/annotation/basic-encrypted-p3.properties", // "classpath:org/jasypt/spring31/annotation/strong-encrypted-p4.properties" // }) // static class ConfigWithFourResourceLocations { // } }