Java Code Examples for org.springframework.mock.env.MockPropertySource

The following examples show how to use org.springframework.mock.env.MockPropertySource. These examples are extracted from open source projects. 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
@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 2
@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 3
@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 4
Source Project: spring-analysis-note   Source File: MutablePropertySourcesTests.java    License: 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 5
@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
@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 7
@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 8
@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 9
@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 10
@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 11
Source Project: sinavi-jfw   Source File: OverrideProperties.java    License: 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 Project: data-prep   Source File: ServiceBaseTest.java    License: 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 13
@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 14
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 15
@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 16
@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 17
@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 18
@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 19
@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 Project: spring-analysis-note   Source File: MutablePropertySourcesTests.java    License: MIT License 5 votes vote down vote up
@Test
public void streamContainsPropertySource() {
	MutablePropertySources sources = new MutablePropertySources();
	sources.addLast(new MockPropertySource("test"));

	assertThat(sources.stream(), notNullValue());
	assertThat(sources.stream().count(), is(1L));
	assertThat(sources.stream().anyMatch(source -> "test".equals(source.getName())), is(true));
	assertThat(sources.stream().anyMatch(source -> "bogus".equals(source.getName())), is(false));
}
 
Example 21
@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 22
@Test
public void resolvePlaceholders() {
	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
	PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
	assertThat(resolver.resolvePlaceholders("Replace this ${key}"), equalTo("Replace this value"));
}
 
Example 23
@Test
public void resolvePlaceholders_withUnresolvable() {
	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
	PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
	assertThat(resolver.resolvePlaceholders("Replace this ${key} plus ${unknown}"),
			equalTo("Replace this value plus ${unknown}"));
}
 
Example 24
@Test
public void resolvePlaceholders_withDefaultValue() {
	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
	PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
	assertThat(resolver.resolvePlaceholders("Replace this ${key} plus ${unknown:defaultValue}"),
			equalTo("Replace this value plus defaultValue"));
}
 
Example 25
@Test
public void resolveRequiredPlaceholders() {
	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
	PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
	assertThat(resolver.resolveRequiredPlaceholders("Replace this ${key}"), equalTo("Replace this value"));
}
 
Example 26
@Test(expected = IllegalArgumentException.class)
public void resolveRequiredPlaceholders_withUnresolvable() {
	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
	PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
	resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown}");
}
 
Example 27
@Test
public void resolveRequiredPlaceholders_withDefaultValue() {
	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
	PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
	assertThat(resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown:defaultValue}"),
			equalTo("Replace this value plus defaultValue"));
}
 
Example 28
Source Project: spring-analysis-note   Source File: StandardEnvironmentTests.java    License: MIT License 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 29
Source Project: spring-analysis-note   Source File: StandardEnvironmentTests.java    License: MIT License 5 votes vote down vote up
@Test
public void addActiveProfile_whenActiveProfilesPropertyIsAlreadySet() {
	ConfigurableEnvironment env = new StandardEnvironment();
	assertThat(env.getProperty(ACTIVE_PROFILES_PROPERTY_NAME), nullValue());
	env.getPropertySources().addFirst(new MockPropertySource().withProperty(ACTIVE_PROFILES_PROPERTY_NAME, "p1"));
	assertThat(env.getProperty(ACTIVE_PROFILES_PROPERTY_NAME), equalTo("p1"));
	env.addActiveProfile("p2");
	assertThat(env.getActiveProfiles(), arrayContaining("p1", "p2"));
}
 
Example 30
Source Project: spring-analysis-note   Source File: StandardEnvironmentTests.java    License: MIT License 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"));
}