Search in sources :

Example 6 with MockPropertySource

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

the class PropertySourcesPropertyResolverTests method resolveNestedPropertyPlaceholders.

@Test
public void resolveNestedPropertyPlaceholders() {
    MutablePropertySources ps = new MutablePropertySources();
    ps.addFirst(new MockPropertySource().withProperty("p1", "v1").withProperty("p2", "v2").withProperty("p3", // nested placeholders
    "${p1}:${p2}").withProperty("p4", // deeply nested placeholders
    "${p3}").withProperty("p5", // unresolvable placeholder
    "${p1}:${p2}:${bogus}").withProperty("p6", // unresolvable w/ default
    "${p1}:${p2}:${bogus:def}").withProperty("pL", // cyclic reference left
    "${pR}").withProperty("pR", // cyclic reference right
    "${pL}"));
    ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps);
    assertThat(pr.getProperty("p1"), equalTo("v1"));
    assertThat(pr.getProperty("p2"), equalTo("v2"));
    assertThat(pr.getProperty("p3"), equalTo("v1:v2"));
    assertThat(pr.getProperty("p4"), equalTo("v1:v2"));
    try {
        pr.getProperty("p5");
    } catch (IllegalArgumentException ex) {
        assertThat(ex.getMessage(), containsString("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""));
    }
    assertThat(pr.getProperty("p6"), equalTo("v1:v2:def"));
    try {
        pr.getProperty("pL");
    } catch (IllegalArgumentException ex) {
        assertTrue(ex.getMessage().toLowerCase().contains("circular"));
    }
}
Also used : MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 7 with MockPropertySource

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

the class StandardEnvironmentTests method merge.

@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" }));
}
Also used : MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 8 with MockPropertySource

use of org.springframework.mock.env.MockPropertySource in project webservices-axiom by apache.

the class ScenarioTestCase method setUp.

@Override
protected void setUp() throws Exception {
    server = new Server();
    // Set up a custom thread pool to improve thread names (for logging purposes)
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setName("jetty");
    server.setThreadPool(threadPool);
    Connector connector = new SelectChannelConnector();
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    ServletContextHandler handler = new ServletContextHandler(server, "/");
    MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    servlet.setContextClass(GenericWebApplicationContext.class);
    servlet.setContextInitializers(new ApplicationContextInitializer<ConfigurableApplicationContext>() {

        public void initialize(ConfigurableApplicationContext applicationContext) {
            configureContext((GenericWebApplicationContext) applicationContext, config.getServerMessageFactoryConfigurator(), new ClassPathResource("server.xml", ScenarioTestCase.this.getClass()));
        }
    });
    ServletHolder servletHolder = new ServletHolder(servlet);
    servletHolder.setName("spring-ws");
    servletHolder.setInitOrder(1);
    handler.addServlet(servletHolder, "/*");
    server.start();
    context = new GenericXmlApplicationContext();
    MockPropertySource propertySource = new MockPropertySource("client-properties");
    propertySource.setProperty("port", connector.getLocalPort());
    context.getEnvironment().getPropertySources().replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, propertySource);
    configureContext(context, config.getClientMessageFactoryConfigurator(), new ClassPathResource("client.xml", getClass()));
    context.refresh();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Connector(org.eclipse.jetty.server.Connector) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) MessageDispatcherServlet(org.springframework.ws.transport.http.MessageDispatcherServlet) ClassPathResource(org.springframework.core.io.ClassPathResource) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) MockPropertySource(org.springframework.mock.env.MockPropertySource) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 9 with MockPropertySource

use of org.springframework.mock.env.MockPropertySource in project bitsquare by bitsquare.

the class BitsquareEnvironmentTests method testPropertySourcePrecedence.

@Test
public void testPropertySourcePrecedence() {
    PropertySource commandlineProps = new MockPropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME).withProperty("key.x", "x.commandline");
    PropertySource filesystemProps = new MockPropertySource(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME).withProperty("key.x", "x.env").withProperty("key.y", "y.env");
    ConfigurableEnvironment env = new BitsquareEnvironment(commandlineProps) {

        @Override
        PropertySource<?> appDirProperties() {
            return filesystemProps;
        }
    };
    MutablePropertySources propertySources = env.getPropertySources();
    assertThat(propertySources.precedenceOf(named(BITSQUARE_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(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME)), equalTo(3));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_HOME_DIR_PROPERTY_SOURCE_NAME)), equalTo(4));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME)), equalTo(5));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME)), equalTo(6));
    assertThat(propertySources.size(), equalTo(7));
    // commandline value wins due to precedence
    assertThat(env.getProperty("key.x"), equalTo("x.commandline"));
    // env value wins because it's the only one available
    assertThat(env.getProperty("key.y"), equalTo("y.env"));
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) BitsquareEnvironment(io.bitsquare.app.BitsquareEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) PropertySource(org.springframework.core.env.PropertySource) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 10 with MockPropertySource

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

the class EnvironmentSystemIntegrationTests method registerServletParamPropertySources_AbstractRefreshableWebApplicationContext.

@Test
public void registerServletParamPropertySources_AbstractRefreshableWebApplicationContext() {
    MockServletContext servletContext = new MockServletContext();
    servletContext.addInitParameter("pCommon", "pCommonContextValue");
    servletContext.addInitParameter("pContext1", "pContext1Value");
    MockServletConfig servletConfig = new MockServletConfig(servletContext);
    servletConfig.addInitParameter("pCommon", "pCommonConfigValue");
    servletConfig.addInitParameter("pConfig1", "pConfig1Value");
    AbstractRefreshableWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.setConfigLocation(EnvironmentAwareBean.class.getName());
    ctx.setServletConfig(servletConfig);
    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));
    assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true));
    // ServletConfig gets precedence
    assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
    assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
    // but all params are available
    assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
    assertThat(environment.getProperty("pConfig1"), is("pConfig1Value"));
    // 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"));
}
Also used : MockServletConfig(org.springframework.mock.web.MockServletConfig) AbstractRefreshableWebApplicationContext(org.springframework.web.context.support.AbstractRefreshableWebApplicationContext) MockPropertySource(org.springframework.mock.env.MockPropertySource) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) MockServletContext(org.springframework.mock.web.MockServletContext) 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