@Test(expected = IllegalArgumentException.class) public void setDefaultProfiles_withNullProfileArray() { environment.setDefaultProfiles((String[]) null); }
@Test(expected = IllegalArgumentException.class) public void setDefaultProfiles_withNullProfile() { environment.setDefaultProfiles((String) null); }
@Test(expected = IllegalArgumentException.class) public void setDefaultProfiles_withEmptyProfile() { environment.setDefaultProfiles(""); }
@Test(expected = IllegalArgumentException.class) public void setDefaultProfiles_withNotOperator() { environment.setDefaultProfiles("d1", "!d2"); }
protected void applySpringProfiles(ConfigurableEnvironment environment, ServletContext servletContext) { String systemProfiles = System.getProperty("spring.profiles.active"); environment.setDefaultProfiles(new String[0]); if (environment.containsProperty("spring_profiles")) { String profiles = environment.getProperty("spring_profiles"); servletContext.log("Setting active profiles: " + profiles); environment.setActiveProfiles(StringUtils.tokenizeToStringArray(profiles, ",", true, true)); } else { if (isEmpty(systemProfiles)) { environment.setActiveProfiles("hsqldb"); } else { environment.setActiveProfiles(commaDelimitedListToStringArray(systemProfiles)); } } }
@Test public void setDefaultProfiles() { environment.setDefaultProfiles(); assertThat(environment.getDefaultProfiles().length, is(0)); environment.setDefaultProfiles("pd1"); assertThat(Arrays.asList(environment.getDefaultProfiles()), hasItem("pd1")); environment.setDefaultProfiles("pd2", "pd3"); assertThat(Arrays.asList(environment.getDefaultProfiles()), not(hasItem("pd1"))); assertThat(Arrays.asList(environment.getDefaultProfiles()), hasItems("pd2", "pd3")); }
@Test public void reservedDefaultProfile() { assertThat(environment.getDefaultProfiles(), equalTo(new String[]{RESERVED_DEFAULT_PROFILE_NAME})); System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "d0"); assertThat(environment.getDefaultProfiles(), equalTo(new String[]{"d0"})); environment.setDefaultProfiles("d1", "d2"); assertThat(environment.getDefaultProfiles(), equalTo(new String[]{"d1","d2"})); System.getProperties().remove(DEFAULT_PROFILES_PROPERTY_NAME); }
@Test public void testIntegrationWithAnnotationConfigApplicationContext_defaultProfile() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME); // no active profiles are set ctx.register(DefaultProfileAnnotatedComponent.class); ctx.refresh(); assertThat(ctx.containsBean(DefaultProfileAnnotatedComponent.BEAN_NAME), is(true)); }
@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)); }
LOG.warn("The usage of " + getDeprecatedDefaultProfileKey() + ".properties is deprecated and will be removed in a future release. Use Spring's default profile properties of 'default.properties'." + " Alternatively, set the 'spring.profiles.default' system property with -Dspring.profiles.default=development to change the default profile name that Spring runs in."); env.setDefaultProfiles(ArrayUtils.add(defaultProfiles, deprecatedDefaultProfile));
@Test public void testDefaultProfile() { { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); ConfigurableEnvironment env = new StandardEnvironment(); env.setDefaultProfiles("custom-default"); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_ELIGIBLE_XML, getClass())); assertThat(beanFactory, not(containsTargetBean())); } { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); ConfigurableEnvironment env = new StandardEnvironment(); env.setDefaultProfiles("custom-default"); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(CUSTOM_DEFAULT_ELIGIBLE_XML, getClass())); assertThat(beanFactory, containsTargetBean()); } }
@Test public void testIntegrationWithAnnotationConfigApplicationContext_defaultAndDevProfile() { Class<?> beanClass = DefaultAndDevProfileAnnotatedComponent.class; String beanName = DefaultAndDevProfileAnnotatedComponent.BEAN_NAME; { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME); // no active profiles are set ctx.register(beanClass); ctx.refresh(); assertThat(ctx.containsBean(beanName), is(true)); } { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME); ctx.getEnvironment().setActiveProfiles("dev"); ctx.register(beanClass); ctx.refresh(); assertThat(ctx.containsBean(beanName), is(true)); } { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME); ctx.getEnvironment().setActiveProfiles("other"); ctx.register(beanClass); ctx.refresh(); assertThat(ctx.containsBean(beanName), is(false)); } }
@Test public void testIntegrationWithAnnotationConfigApplicationContext_metaProfile() { Class<?> beanClass = MetaProfileAnnotatedComponent.class; String beanName = MetaProfileAnnotatedComponent.BEAN_NAME; { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME); // no active profiles are set ctx.register(beanClass); ctx.refresh(); assertThat(ctx.containsBean(beanName), is(true)); } { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME); ctx.getEnvironment().setActiveProfiles("dev"); ctx.register(beanClass); ctx.refresh(); assertThat(ctx.containsBean(beanName), is(true)); } { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setDefaultProfiles(TEST_DEFAULT_PROFILE_NAME); ctx.getEnvironment().setActiveProfiles("other"); ctx.register(beanClass); ctx.refresh(); assertThat(ctx.containsBean(beanName), is(false)); } }
@Test public void withMultiCustomReservedDefaultProfile() { class CustomEnvironment extends AbstractEnvironment { @Override @SuppressWarnings("serial") protected Set<String> getReservedDefaultProfiles() { return new HashSet<String>() {{ add("rd1"); add("rd2"); }}; } } ConfigurableEnvironment env = new CustomEnvironment(); assertThat(env.acceptsProfiles(defaultProfile()), is(false)); assertThat(env.acceptsProfiles(Profiles.of("rd1 | rd2")), is(true)); // finally, issue additional assertions to cover all combinations of calling these // methods, however unlikely. env.setDefaultProfiles("d1"); assertThat(env.acceptsProfiles(Profiles.of("rd1 | rd2")), is(false)); assertThat(env.acceptsProfiles(Profiles.of("d1")), is(true)); env.setActiveProfiles("a1", "a2"); assertThat(env.acceptsProfiles(Profiles.of("d1")), is(false)); assertThat(env.acceptsProfiles(Profiles.of("a1 | a2")), is(true)); env.setActiveProfiles(); assertThat(env.acceptsProfiles(Profiles.of("d1")), is(true)); assertThat(env.acceptsProfiles(Profiles.of("a1 | a2")), is(false)); env.setDefaultProfiles(); assertThat(env.acceptsProfiles(defaultProfile()), is(false)); assertThat(env.acceptsProfiles(Profiles.of("rd1 | rd2")), is(false)); assertThat(env.acceptsProfiles(Profiles.of("d1")), is(false)); assertThat(env.acceptsProfiles(Profiles.of("a1 | a2")), is(false)); }
@Override public void setDefaultProfiles(String... profiles) { delegate.setDefaultProfiles(profiles); }
ConfigurableEnvironment env = new StandardEnvironment(); env.setActiveProfiles(DEV_ACTIVE); env.setDefaultProfiles("default"); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass())); ConfigurableEnvironment env = new StandardEnvironment(); env.setDefaultProfiles("default"); reader.setEnvironment(env); reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass()));
@Override public void initialize(AbstractRefreshableConfigApplicationContext ctx) { ctx.setAllowBeanDefinitionOverriding(false); ConfigurableEnvironment env = ctx.getEnvironment(); Set<String> aps = new LinkedHashSet<>(); Collections.addAll(aps, env.getActiveProfiles()); Collections.addAll(aps, testConfig ? Constants.PROFILES_TEST : Constants.PROFILES_PROD); env.getPropertySources().addLast(new LabelPropertySource()); env.getPropertySources().addLast(new DomainPropertySource(ctx)); env.setDefaultProfiles(Constants.PROFILE_ROOT_DEFAULT); ctx.setConfigLocations(Constants.DEFAULT_LOCATIONS); env.setActiveProfiles(aps.toArray(new String[aps.size()])); }
protected void setDefaultProfiles(ConfigurableApplicationContext applicationContext, List<String> defaultProfiles) { if (!CollectionUtils.isEmpty(defaultProfiles)) { ConfigurableEnvironment env = applicationContext.getEnvironment(); env.setDefaultProfiles(CollectionUtils.toStringArray(defaultProfiles)); } }
private static WebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation(CONFIG_LOCATION); context.getEnvironment().setDefaultProfiles(DEFAULT_PROFILE); return context; }
public static void setupProfiles(ConfigurableApplicationContext ctx, List<String> activeProfiles, List<String> defaultProfiles) { if (!CollectionUtils.isEmpty(activeProfiles)) { ConfigurableEnvironment env = ctx.getEnvironment(); env.setActiveProfiles(CollectionUtils.toStringArray(activeProfiles)); } if (!CollectionUtils.isEmpty(defaultProfiles)) { ConfigurableEnvironment env = ctx.getEnvironment(); env.setDefaultProfiles(CollectionUtils.toStringArray(defaultProfiles)); } }