org.springframework.mock.env.MockPropertySource Java Examples

The following examples show how to use org.springframework.mock.env.MockPropertySource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PropertySourcesPlaceholderConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void explicitPropertySourcesExcludesEnvironment() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addLast(new MockPropertySource());

	PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
	ppc.setPropertySources(propertySources);
	ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "env"));
	ppc.setIgnoreUnresolvablePlaceholders(true);
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
	assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
 
Example #2
Source File: PropertySourcesPlaceholderConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void explicitPropertySources() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo"));

	PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
	ppc.setPropertySources(propertySources);
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo"));
	assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
 
Example #3
Source File: PropertySourcesPlaceholderConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void explicitPropertySourcesExcludesEnvironment() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addLast(new MockPropertySource());

	PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
	ppc.setPropertySources(propertySources);
	ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "env"));
	ppc.setIgnoreUnresolvablePlaceholders(true);
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
	assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
 
Example #4
Source File: PropertySourcesPlaceholderConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void explicitPropertySourcesExcludesLocalProperties() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addLast(new MockPropertySource());

	PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
	ppc.setPropertySources(propertySources);
	ppc.setProperties(new Properties() {{
		put("my.name", "local");
	}});
	ppc.setIgnoreUnresolvablePlaceholders(true);
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
}
 
Example #5
Source File: PropertySourcesPropertyResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void getPropertySources_replacePropertySource() {
	propertySources = new MutablePropertySources();
	propertyResolver = new PropertySourcesPropertyResolver(propertySources);
	propertySources.addLast(new MockPropertySource("local").withProperty("foo", "localValue"));
	propertySources.addLast(new MockPropertySource("system").withProperty("foo", "systemValue"));

	// 'local' was added first so has precedence
	assertThat(propertyResolver.getProperty("foo"), equalTo("localValue"));

	// replace 'local' with new property source
	propertySources.replace("local", new MockPropertySource("new").withProperty("foo", "newValue"));

	// 'system' now has precedence
	assertThat(propertyResolver.getProperty("foo"), equalTo("newValue"));

	assertThat(propertySources.size(), is(2));
}
 
Example #6
Source File: MutablePropertySourcesTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void iteratorContainsPropertySource() {
	MutablePropertySources sources = new MutablePropertySources();
	sources.addLast(new MockPropertySource("test"));

	Iterator<PropertySource<?>> it = sources.iterator();
	assertTrue(it.hasNext());
	assertEquals("test", it.next().getName());

	try {
		it.remove();
		fail("Should have thrown UnsupportedOperationException");
	}
	catch (UnsupportedOperationException ex) {
		// expected
	}
	assertFalse(it.hasNext());
}
 
Example #7
Source File: PropertySourcesPropertyResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void getPropertySources_replacePropertySource() {
	propertySources = new MutablePropertySources();
	propertyResolver = new PropertySourcesPropertyResolver(propertySources);
	propertySources.addLast(new MockPropertySource("local").withProperty("foo", "localValue"));
	propertySources.addLast(new MockPropertySource("system").withProperty("foo", "systemValue"));

	// 'local' was added first so has precedence
	assertThat(propertyResolver.getProperty("foo"), equalTo("localValue"));

	// replace 'local' with new property source
	propertySources.replace("local", new MockPropertySource("new").withProperty("foo", "newValue"));

	// 'system' now has precedence
	assertThat(propertyResolver.getProperty("foo"), equalTo("newValue"));

	assertThat(propertySources.size(), is(2));
}
 
Example #8
Source File: PropertySourcesPlaceholderConfigurerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void explicitPropertySourcesExcludesLocalProperties() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addLast(new MockPropertySource());

	PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
	pc.setPropertySources(propertySources);
	pc.setProperties(new Properties() {{
		put("my.name", "local");
	}});
	pc.setIgnoreUnresolvablePlaceholders(true);
	pc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
}
 
Example #9
Source File: PropertySourcesPlaceholderConfigurerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void explicitPropertySourcesExcludesEnvironment() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addLast(new MockPropertySource());

	PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
	pc.setPropertySources(propertySources);
	pc.setEnvironment(new MockEnvironment().withProperty("my.name", "env"));
	pc.setIgnoreUnresolvablePlaceholders(true);
	pc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
	assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
 
Example #10
Source File: PropertySourcesPlaceholderConfigurerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void explicitPropertySources() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo"));

	PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
	pc.setPropertySources(propertySources);
	pc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo"));
	assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
 
Example #11
Source File: OverrideProperties.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreResourceNotFound(true);
    configurer.setIgnoreUnresolvablePlaceholders(true);
    MutablePropertySources propertySources = new MutablePropertySources();
    MockPropertySource source = new MockPropertySource()
        .withProperty("rabbitmq.host", "192.168.10.10")
        .withProperty("rabbitmq.port", "5673")
        .withProperty("rabbitmq.username", "jfw")
        .withProperty("rabbitmq.password", "jfw")
        .withProperty("rabbitmq.channel-cache-size", 100);
    propertySources.addLast(source);
    configurer.setPropertySources(propertySources);
    return configurer;
}
 
Example #12
Source File: PropertyMockingApplicationContextInitializer.java    From entref-spring-boot with MIT License 6 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    // configure a net binding instance
    Net mongoNet = this.getMongoNet();

    // register some autowire-able dependencies, to make leveraging the configured instances in a test possible
    applicationContext.getBeanFactory().registerResolvableDependency(RestTemplateBuilder.class, this.getRestTemplateBuilder());
    applicationContext.getBeanFactory().registerResolvableDependency(Net.class, mongoNet);
    applicationContext.getBeanFactory().registerResolvableDependency(MongodExecutable.class, this.getMongo(mongoNet));

    // configure the property sources that will be used by the application
    MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
    MockPropertySource mockEnvVars = new MockPropertySource()
            .withProperty(Constants.ENV_DB_NAME, this.getDbName())
            .withProperty(Constants.ENV_DB_CONNSTR, "mongodb://localhost:" + mongoNet.getPort())
            .withProperty(Constants.ENV_ALLOWED_ORIGIN, this.getAllowedOrigin())
            .withProperty(Constants.ENV_EXCLUDE_FILTER, String.join(",", this.getExcludeList()));

    // inject the property sources into the application as environment variables
    propertySources.replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, mockEnvVars);
}
 
Example #13
Source File: PropertySourcesPlaceholderConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void explicitPropertySourcesExcludesLocalProperties() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addLast(new MockPropertySource());

	PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
	ppc.setPropertySources(propertySources);
	ppc.setProperties(new Properties() {{
		put("my.name", "local");
	}});
	ppc.setIgnoreUnresolvablePlaceholders(true);
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
}
 
Example #14
Source File: PropertySourcesPlaceholderConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void explicitPropertySources() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo"));

	PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
	ppc.setPropertySources(propertySources);
	ppc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo"));
	assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
 
Example #15
Source File: MutablePropertySourcesTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void iteratorContainsPropertySource() {
	MutablePropertySources sources = new MutablePropertySources();
	sources.addLast(new MockPropertySource("test"));

	Iterator<PropertySource<?>> it = sources.iterator();
	assertTrue(it.hasNext());
	assertEquals("test", it.next().getName());

	try {
		it.remove();
		fail("Should have thrown UnsupportedOperationException");
	}
	catch (UnsupportedOperationException ex) {
		// expected
	}
	assertFalse(it.hasNext());
}
 
Example #16
Source File: ServiceBaseTest.java    From data-prep with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    if (!environmentSet) {
        RestAssured.baseURI = RestAssured.DEFAULT_URI;
        RestAssured.port = port;

        // Overrides connection information with random port value
        String url = RestAssured.baseURI + ":" + port;
        MockPropertySource connectionInformation = new MockPropertySource()
                .withProperty("dataset.service.url", url)
                .withProperty("transformation.service.url", url)
                .withProperty("preparation.service.url", url)
                .withProperty("async_store.service.url", url)
                .withProperty("gateway.service.url", url)
                .withProperty("fullrun.service.url", url);
        environment.getPropertySources().addFirst(connectionInformation);
        environmentSet = true;
    }
}
 
Example #17
Source File: PropertySourcesPropertyResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void getPropertySources_replacePropertySource() {
	propertySources = new MutablePropertySources();
	propertyResolver = new PropertySourcesPropertyResolver(propertySources);
	propertySources.addLast(new MockPropertySource("local").withProperty("foo", "localValue"));
	propertySources.addLast(new MockPropertySource("system").withProperty("foo", "systemValue"));

	// 'local' was added first so has precedence
	assertThat(propertyResolver.getProperty("foo"), equalTo("localValue"));

	// replace 'local' with new property source
	propertySources.replace("local", new MockPropertySource("new").withProperty("foo", "newValue"));

	// 'system' now has precedence
	assertThat(propertyResolver.getProperty("foo"), equalTo("newValue"));

	assertThat(propertySources.size(), is(2));
}
 
Example #18
Source File: CustomConfiguredSessionCachingIntegrationTests.java    From spring-boot-data-geode with Apache License 2.0 6 votes vote down vote up
private Function<ConfigurableApplicationContext, ConfigurableApplicationContext> newSpringSessionGemFirePropertiesConfigurationFunction() {

		return applicationContext -> {

			PropertySource springSessionGemFireProperties = new MockPropertySource("TestSpringSessionGemFireProperties")
				.withProperty(springSessionPropertyName("cache.client.region.shortcut"), "LOCAL")
				.withProperty(springSessionPropertyName("session.attributes.indexable"), "one, two")
				.withProperty(springSessionPropertyName("session.expiration.max-inactive-interval-seconds"), "600")
				.withProperty(springSessionPropertyName("cache.client.pool.name"), "MockPool")
				.withProperty(springSessionPropertyName("session.region.name"), "MockRegion")
				.withProperty(springSessionPropertyName("cache.server.region.shortcut"), "REPLICATE")
				.withProperty(springSessionPropertyName("session.serializer.bean-name"), "MockSessionSerializer");

			applicationContext.getEnvironment().getPropertySources().addFirst(springSessionGemFireProperties);

			return applicationContext;
		};
	}
 
Example #19
Source File: EnvironmentAccessorIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("all")
public void braceAccess() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "#{environment['my.name']}")
				.getBeanDefinition());

	GenericApplicationContext ctx = new GenericApplicationContext(bf);
	ctx.getEnvironment().getPropertySources().addFirst(new MockPropertySource().withProperty("my.name", "myBean"));
	ctx.refresh();

	assertThat(ctx.getBean(TestBean.class).getName(), equalTo("myBean"));
}
 
Example #20
Source File: PropertySourcesPropertyResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void getProperty_propertySourceSearchOrderIsFIFO() {
	MutablePropertySources sources = new MutablePropertySources();
	PropertyResolver resolver = new PropertySourcesPropertyResolver(sources);
	sources.addFirst(new MockPropertySource("ps1").withProperty("pName", "ps1Value"));
	assertThat(resolver.getProperty("pName"), equalTo("ps1Value"));
	sources.addFirst(new MockPropertySource("ps2").withProperty("pName", "ps2Value"));
	assertThat(resolver.getProperty("pName"), equalTo("ps2Value"));
	sources.addFirst(new MockPropertySource("ps3").withProperty("pName", "ps3Value"));
	assertThat(resolver.getProperty("pName"), equalTo("ps3Value"));
}
 
Example #21
Source File: TestPropertySourceUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * @since 4.1.5
 */
@Test
@SuppressWarnings("rawtypes")
public void addInlinedPropertiesToEnvironmentWithEmptyProperty() {
	ConfigurableEnvironment environment = new MockEnvironment();
	MutablePropertySources propertySources = environment.getPropertySources();
	propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
	assertEquals(0, propertySources.size());
	addInlinedPropertiesToEnvironment(environment, new String[] { "  " });
	assertEquals(1, propertySources.size());
	assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
}
 
Example #22
Source File: TestPropertySourceUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void addInlinedPropertiesToEnvironmentWithEmptyProperty() {
	ConfigurableEnvironment environment = new MockEnvironment();
	MutablePropertySources propertySources = environment.getPropertySources();
	propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
	assertEquals(0, propertySources.size());
	addInlinedPropertiesToEnvironment(environment, asArray("  "));
	assertEquals(1, propertySources.size());
	assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
}
 
Example #23
Source File: BisqEnvironmentTests.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testPropertySourcePrecedence() {
    PropertySource commandlineProps = new MockPropertySource(BISQ_COMMANDLINE_PROPERTY_SOURCE_NAME)
            .withProperty("key.x", "x.commandline");

    PropertySource filesystemProps = new MockPropertySource(BISQ_APP_DIR_PROPERTY_SOURCE_NAME)
            .withProperty("key.x", "x.bisqEnvironment")
            .withProperty("key.y", "y.bisqEnvironment");

    ConfigurableEnvironment bisqEnvironment = new BisqEnvironment(commandlineProps) {
        @Override
        PropertySource<?> getAppDirProperties() {
            return filesystemProps;
        }
    };
    MutablePropertySources propertySources = bisqEnvironment.getPropertySources();

    assertThat(propertySources.precedenceOf(named(BISQ_COMMANDLINE_PROPERTY_SOURCE_NAME)), equalTo(0));
    assertThat(propertySources.precedenceOf(named(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(1));
    assertThat(propertySources.precedenceOf(named(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(2));
    assertThat(propertySources.precedenceOf(named(BISQ_DEFAULT_PROPERTY_SOURCE_NAME)), equalTo(4));

    // we removed support for the rest
  /*  assertThat(propertySources.precedenceOf(named(BISQ_APP_DIR_PROPERTY_SOURCE_NAME)), equalTo(3));
    assertThat(propertySources.precedenceOf(named(BISQ_HOME_DIR_PROPERTY_SOURCE_NAME)), equalTo(4));
    assertThat(propertySources.precedenceOf(named(BISQ_CLASSPATH_PROPERTY_SOURCE_NAME)), equalTo(5));*/
    assertThat(propertySources.size(), equalTo(5));

    assertThat(bisqEnvironment.getProperty("key.x"), equalTo("x.commandline")); // commandline value wins due to precedence

    //TODO check why it fails
    //assertThat(bisqEnvironment.getProperty("key.y"), equalTo("y.bisqEnvironment")); // bisqEnvironment value wins because it's the only one available
}
 
Example #24
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void registerServletParamPropertySources_GenericWebApplicationContext() {
	MockServletContext servletContext = new MockServletContext();
	servletContext.addInitParameter("pCommon", "pCommonContextValue");
	servletContext.addInitParameter("pContext1", "pContext1Value");

	GenericWebApplicationContext ctx = new GenericWebApplicationContext();
	ctx.setServletContext(servletContext);
	ctx.refresh();

	ConfigurableEnvironment environment = ctx.getEnvironment();
	assertThat(environment, instanceOf(StandardServletEnvironment.class));
	MutablePropertySources propertySources = environment.getPropertySources();
	assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));

	// ServletContext params are available
	assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
	assertThat(environment.getProperty("pContext1"), is("pContext1Value"));

	// Servlet* PropertySources have precedence over System* PropertySources
	assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)),
			lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));

	// Replace system properties with a mock property source for convenience
	MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
	mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue");
	mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value");
	propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);

	// assert that servletcontext init params resolve with higher precedence than sysprops
	assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
	assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
}
 
Example #25
Source File: StandardEnvironmentTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void merge() {
	ConfigurableEnvironment child = new StandardEnvironment();
	child.setActiveProfiles("c1", "c2");
	child.getPropertySources().addLast(
			new MockPropertySource("childMock")
					.withProperty("childKey", "childVal")
					.withProperty("bothKey", "childBothVal"));

	ConfigurableEnvironment parent = new StandardEnvironment();
	parent.setActiveProfiles("p1", "p2");
	parent.getPropertySources().addLast(
			new MockPropertySource("parentMock")
					.withProperty("parentKey", "parentVal")
					.withProperty("bothKey", "parentBothVal"));

	assertThat(child.getProperty("childKey"), is("childVal"));
	assertThat(child.getProperty("parentKey"), nullValue());
	assertThat(child.getProperty("bothKey"), is("childBothVal"));

	assertThat(parent.getProperty("childKey"), nullValue());
	assertThat(parent.getProperty("parentKey"), is("parentVal"));
	assertThat(parent.getProperty("bothKey"), is("parentBothVal"));

	assertThat(child.getActiveProfiles(), equalTo(new String[]{"c1","c2"}));
	assertThat(parent.getActiveProfiles(), equalTo(new String[]{"p1","p2"}));

	child.merge(parent);

	assertThat(child.getProperty("childKey"), is("childVal"));
	assertThat(child.getProperty("parentKey"), is("parentVal"));
	assertThat(child.getProperty("bothKey"), is("childBothVal"));

	assertThat(parent.getProperty("childKey"), nullValue());
	assertThat(parent.getProperty("parentKey"), is("parentVal"));
	assertThat(parent.getProperty("bothKey"), is("parentBothVal"));

	assertThat(child.getActiveProfiles(), equalTo(new String[]{"c1","c2","p1","p2"}));
	assertThat(parent.getActiveProfiles(), equalTo(new String[]{"p1","p2"}));
}
 
Example #26
Source File: PropertySourcesPropertyResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void getPropertyAsClass_withObjectForValue() {
	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addFirst(new MockPropertySource().withProperty("some.class", new SpecificType()));
	PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
	assertTrue(resolver.getPropertyAsClass("some.class", SomeType.class).equals(SpecificType.class));
}
 
Example #27
Source File: PropertySourcesPropertyResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test(expected=ConversionException.class)
public void getPropertyAsClass_withMismatchedObjectForValue() {
	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addFirst(new MockPropertySource().withProperty("some.class", new Integer(42)));
	PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
	resolver.getPropertyAsClass("some.class", SomeType.class);
}
 
Example #28
Source File: PropertySourcesPropertyResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void getPropertyAsClass_withRealClassForValue() {
	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addFirst(new MockPropertySource().withProperty("some.class", SpecificType.class));
	PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
	assertTrue(resolver.getPropertyAsClass("some.class", SomeType.class).equals(SpecificType.class));
}
 
Example #29
Source File: PropertySourcesPropertyResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test(expected=ConversionException.class)
public void getPropertyAsClass_withMismatchedRealClassForValue() {
	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addFirst(new MockPropertySource().withProperty("some.class", Integer.class));
	PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
	resolver.getPropertyAsClass("some.class", SomeType.class);
}
 
Example #30
Source File: StandardEnvironmentTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void getDefaultProfiles() {
	assertThat(environment.getDefaultProfiles(), equalTo(new String[] {RESERVED_DEFAULT_PROFILE_NAME}));
	environment.getPropertySources().addFirst(new MockPropertySource().withProperty(DEFAULT_PROFILES_PROPERTY_NAME, "pd1"));
	assertThat(environment.getDefaultProfiles().length, is(1));
	assertThat(Arrays.asList(environment.getDefaultProfiles()), hasItem("pd1"));
}