Search in sources :

Example 16 with MockPropertySource

use of org.springframework.mock.env.MockPropertySource in project spring-framework by spring-projects.

the class MutablePropertySourcesTests method test.

@Test
public void test() {
    MutablePropertySources sources = new MutablePropertySources();
    sources.addLast(new MockPropertySource("b").withProperty("p1", "bValue"));
    sources.addLast(new MockPropertySource("d").withProperty("p1", "dValue"));
    sources.addLast(new MockPropertySource("f").withProperty("p1", "fValue"));
    assertThat(sources.size(), equalTo(3));
    assertThat(sources.contains("a"), is(false));
    assertThat(sources.contains("b"), is(true));
    assertThat(sources.contains("c"), is(false));
    assertThat(sources.contains("d"), is(true));
    assertThat(sources.contains("e"), is(false));
    assertThat(sources.contains("f"), is(true));
    assertThat(sources.contains("g"), is(false));
    assertThat(sources.get("b"), not(nullValue()));
    assertThat(sources.get("b").getProperty("p1"), equalTo((Object) "bValue"));
    assertThat(sources.get("d"), not(nullValue()));
    assertThat(sources.get("d").getProperty("p1"), equalTo((Object) "dValue"));
    sources.addBefore("b", new MockPropertySource("a"));
    sources.addAfter("b", new MockPropertySource("c"));
    assertThat(sources.size(), equalTo(5));
    assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
    assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
    assertThat(sources.precedenceOf(PropertySource.named("f")), is(4));
    sources.addBefore("f", new MockPropertySource("e"));
    sources.addAfter("f", new MockPropertySource("g"));
    assertThat(sources.size(), equalTo(7));
    assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
    assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
    assertThat(sources.precedenceOf(PropertySource.named("e")), is(4));
    assertThat(sources.precedenceOf(PropertySource.named("f")), is(5));
    assertThat(sources.precedenceOf(PropertySource.named("g")), is(6));
    sources.addLast(new MockPropertySource("a"));
    assertThat(sources.size(), equalTo(7));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("d")), is(2));
    assertThat(sources.precedenceOf(PropertySource.named("e")), is(3));
    assertThat(sources.precedenceOf(PropertySource.named("f")), is(4));
    assertThat(sources.precedenceOf(PropertySource.named("g")), is(5));
    assertThat(sources.precedenceOf(PropertySource.named("a")), is(6));
    sources.addFirst(new MockPropertySource("a"));
    assertThat(sources.size(), equalTo(7));
    assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
    assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
    assertThat(sources.precedenceOf(PropertySource.named("e")), is(4));
    assertThat(sources.precedenceOf(PropertySource.named("f")), is(5));
    assertThat(sources.precedenceOf(PropertySource.named("g")), is(6));
    assertEquals(sources.remove("a"), PropertySource.named("a"));
    assertThat(sources.size(), equalTo(6));
    assertThat(sources.contains("a"), is(false));
    assertEquals(sources.remove("a"), null);
    assertThat(sources.size(), equalTo(6));
    String bogusPS = "bogus";
    try {
        sources.addAfter(bogusPS, new MockPropertySource("h"));
        fail("expected non-existent PropertySource exception");
    } catch (IllegalArgumentException ex) {
        assertTrue(ex.getMessage().contains("does not exist"));
    }
    sources.addFirst(new MockPropertySource("a"));
    assertThat(sources.size(), equalTo(7));
    assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
    sources.replace("a", new MockPropertySource("a-replaced"));
    assertThat(sources.size(), equalTo(7));
    assertThat(sources.precedenceOf(PropertySource.named("a-replaced")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
    sources.replace("a-replaced", new MockPropertySource("a"));
    try {
        sources.replace(bogusPS, new MockPropertySource("bogus-replaced"));
        fail("expected non-existent PropertySource exception");
    } catch (IllegalArgumentException ex) {
        assertTrue(ex.getMessage().contains("does not exist"));
    }
    try {
        sources.addBefore("b", new MockPropertySource("b"));
        fail("expected exception");
    } catch (IllegalArgumentException ex) {
        assertTrue(ex.getMessage().contains("cannot be added relative to itself"));
    }
    try {
        sources.addAfter("b", new MockPropertySource("b"));
        fail("expected exception");
    } catch (IllegalArgumentException ex) {
        assertTrue(ex.getMessage().contains("cannot be added relative to itself"));
    }
}
Also used : MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 17 with MockPropertySource

use of org.springframework.mock.env.MockPropertySource in project spring-framework by spring-projects.

the class PropertySourcesPropertyResolverTests method ignoreUnresolvableNestedPlaceholdersIsConfigurable.

@Test
public void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
    MutablePropertySources ps = new MutablePropertySources();
    ps.addFirst(new MockPropertySource().withProperty("p1", "v1").withProperty("p2", "v2").withProperty("p3", // unresolvable w/ default
    "${p1}:${p2}:${bogus:def}").withProperty("p4", // unresolvable placeholder
    "${p1}:${p2}:${bogus}"));
    ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps);
    assertThat(pr.getProperty("p1"), equalTo("v1"));
    assertThat(pr.getProperty("p2"), equalTo("v2"));
    assertThat(pr.getProperty("p3"), equalTo("v1:v2:def"));
    // exception by default
    try {
        pr.getProperty("p4");
    } catch (IllegalArgumentException ex) {
        assertThat(ex.getMessage(), containsString("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""));
    }
    // relax the treatment of unresolvable nested placeholders
    pr.setIgnoreUnresolvableNestedPlaceholders(true);
    // and observe they now pass through unresolved
    assertThat(pr.getProperty("p4"), equalTo("v1:v2:${bogus}"));
    // resolve[Nested]Placeholders methods behave as usual regardless the value of
    // ignoreUnresolvableNestedPlaceholders
    assertThat(pr.resolvePlaceholders("${p1}:${p2}:${bogus}"), equalTo("v1:v2:${bogus}"));
    try {
        pr.resolveRequiredPlaceholders("${p1}:${p2}:${bogus}");
    } catch (IllegalArgumentException ex) {
        assertThat(ex.getMessage(), containsString("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""));
    }
}
Also used : MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 18 with MockPropertySource

use of org.springframework.mock.env.MockPropertySource in project spring-framework by spring-projects.

the class EnvironmentAccessorIntegrationTests method braceAccess.

@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"));
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 19 with MockPropertySource

use of org.springframework.mock.env.MockPropertySource in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurerTests method explicitPropertySourcesExcludesLocalProperties.

@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}"));
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

MockPropertySource (org.springframework.mock.env.MockPropertySource)19 Test (org.junit.Test)16 MutablePropertySources (org.springframework.core.env.MutablePropertySources)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 TestBean (org.springframework.tests.sample.beans.TestBean)4 MockServletContext (org.springframework.mock.web.MockServletContext)3 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)2 MockServletConfig (org.springframework.mock.web.MockServletConfig)2 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)2 StandardServletEnvironment (org.springframework.web.context.support.StandardServletEnvironment)2 BitsquareEnvironment (io.bitsquare.app.BitsquareEnvironment)1 Properties (java.util.Properties)1 Connector (org.eclipse.jetty.server.Connector)1 Server (org.eclipse.jetty.server.Server)1 SelectChannelConnector (org.eclipse.jetty.server.nio.SelectChannelConnector)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1