Package org.springframework.mock.env

Examples of org.springframework.mock.env.MockPropertySource


public class PropertyMockingApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
        MockPropertySource mockEnvVars = new MockPropertySource().withProperty("bookService.port", PortFactory.findFreePort());
        propertySources.replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, mockEnvVars);
    }
View Full Code Here


        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"));
  }
View Full Code Here

    // Servlet* PropertySources have precedence over System* PropertySources
    assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_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 servletconfig params resolve with higher precedence than sysprops
    assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
    assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
View Full Code Here

    // 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"));
View Full Code Here

    // Servlet* PropertySources have precedence over System* PropertySources
    assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_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 servletconfig params resolve with higher precedence than sysprops
    assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
    assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
View Full Code Here

        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"));
View Full Code Here

        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);
View Full Code Here

        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");
View Full Code Here

public class MutablePropertySourcesTests {

  @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) {
      assertThat(ex.getMessage(),
          equalTo(format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS)));
    }

    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) {
      assertThat(ex.getMessage(),
          equalTo(format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS)));
    }

    try {
      sources.addBefore("b", new MockPropertySource("b"));
      fail("expected exception");
    } catch (IllegalArgumentException ex) {
      assertThat(ex.getMessage(),
          equalTo(format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b")));
    }

    try {
      sources.addAfter("b", new MockPropertySource("b"));
      fail("expected exception");
    } catch (IllegalArgumentException ex) {
      assertThat(ex.getMessage(),
          equalTo(format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b")));
    }
View Full Code Here

  @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"));
  }
View Full Code Here

TOP

Related Classes of org.springframework.mock.env.MockPropertySource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.