@Before public void setUp() { prodEnv.setActiveProfiles(PROD_ENV_NAME); devEnv.setActiveProfiles(DEV_ENV_NAME); prodWebEnv.setActiveProfiles(PROD_ENV_NAME); }
@Override public void initialize(GenericApplicationContext applicationContext) { applicationContext.getEnvironment().setActiveProfiles(PROFILE_TWO); } }
@Test(expected = IllegalArgumentException.class) public void setActiveProfiles_withNullProfileArray() { environment.setActiveProfiles((String[]) null); }
@Test(expected = IllegalArgumentException.class) public void setActiveProfiles_withNullProfile() { environment.setActiveProfiles((String) null); }
@Override public void initialize(GenericApplicationContext applicationContext) { applicationContext.getEnvironment().setActiveProfiles("dev"); } }
@Override public void initialize(GenericApplicationContext applicationContext) { applicationContext.getEnvironment().setActiveProfiles(PROFILE_ONE); }
@Test(expected = IllegalArgumentException.class) public void setActiveProfiles_withNotOperator() { environment.setActiveProfiles("p1", "!p2"); }
@Test(expected = IllegalArgumentException.class) public void setActiveProfiles_withEmptyProfile() { environment.setActiveProfiles(""); }
@Test public void acceptsProfiles_activeProfileSetProgrammatically() { assertThat(environment.acceptsProfiles("p1", "p2"), is(false)); environment.setActiveProfiles("p1"); assertThat(environment.acceptsProfiles("p1", "p2"), is(true)); environment.setActiveProfiles("p2"); assertThat(environment.acceptsProfiles("p1", "p2"), is(true)); environment.setActiveProfiles("p1", "p2"); assertThat(environment.acceptsProfiles("p1", "p2"), is(true)); }
@Test public void setActiveProfiles() { environment.setActiveProfiles("local", "embedded"); String[] activeProfiles = environment.getActiveProfiles(); assertThat(Arrays.asList(activeProfiles), hasItems("local", "embedded")); assertThat(activeProfiles.length, is(2)); }
@Test public void testIntegrationWithAnnotationConfigApplicationContext_validProfile() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); ctx.register(ProfileAnnotatedComponent.class); ctx.refresh(); assertThat(ctx.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); }
@Test public void testIntegrationWithAnnotationConfigApplicationContext_validMetaAnnotatedProfile() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles(DevComponent.PROFILE_NAME); ctx.register(ProfileMetaAnnotatedComponent.class); ctx.refresh(); assertThat(ctx.containsBean(ProfileMetaAnnotatedComponent.BEAN_NAME), is(true)); }
@Test public void testIntegrationWithAnnotationConfigApplicationContext_invalidProfile() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles("other"); ctx.register(ProfileAnnotatedComponent.class); ctx.refresh(); assertThat(ctx.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false)); }
@Test public void testIntegrationWithAnnotationConfigApplicationContext_invalidMetaAnnotatedProfile() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles("other"); ctx.register(ProfileMetaAnnotatedComponent.class); ctx.refresh(); assertThat(ctx.containsBean(ProfileMetaAnnotatedComponent.BEAN_NAME), is(false)); }
@Test public void acceptsProfiles_defaultProfile() { assertThat(environment.acceptsProfiles("pd"), is(false)); environment.setDefaultProfiles("pd"); assertThat(environment.acceptsProfiles("pd"), is(true)); environment.setActiveProfiles("p1"); assertThat(environment.acceptsProfiles("pd"), is(false)); assertThat(environment.acceptsProfiles("p1"), is(true)); }
@Test public void testWithInactiveProfile() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); ConfigurableEnvironment env = new StandardEnvironment(); env.setActiveProfiles("other"); provider.setEnvironment(env); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_PROFILE_PACKAGE); assertThat(containsBeanClass(candidates, ProfileAnnotatedComponent.class), is(false)); }
@Test public void testWithActiveProfile() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); ConfigurableEnvironment env = new StandardEnvironment(); env.setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); provider.setEnvironment(env); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_PROFILE_PACKAGE); assertThat(containsBeanClass(candidates, ProfileAnnotatedComponent.class), is(true)); }
@Test public void addActiveProfile() { assertThat(environment.getActiveProfiles().length, is(0)); environment.setActiveProfiles("local", "embedded"); assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("local", "embedded")); assertThat(environment.getActiveProfiles().length, is(2)); environment.addActiveProfile("p1"); assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("p1")); assertThat(environment.getActiveProfiles().length, is(3)); environment.addActiveProfile("p2"); environment.addActiveProfile("p3"); assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("p2", "p3")); assertThat(environment.getActiveProfiles().length, is(5)); }
@Test public void componentScanRespectsProfileAnnotation() { String xmlLocation = "org/springframework/context/annotation/componentScanRespectsProfileAnnotationTests.xml"; { // should exclude the profile-annotated bean if active profiles remains unset GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load(xmlLocation); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false)); context.close(); } { // should include the profile-annotated bean with active profiles set GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); context.load(xmlLocation); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); context.close(); } { // ensure the same works for AbstractRefreshableApplicationContext impls too ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xmlLocation }, false); context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); context.close(); } }
@Test public void getBean_withActiveProfile() { ConfigurableEnvironment env = new StandardEnvironment(); env.setActiveProfiles("dev"); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf); reader.setEnvironment(env); reader.loadBeanDefinitions(XML); bf.getBean("devOnlyBean"); // should not throw NSBDE Object foo = bf.getBean("foo"); assertThat(foo, instanceOf(Integer.class)); bf.getBean("devOnlyBean"); }