Java Code Examples for org.springframework.mock.web.MockServletContext#addInitParameter()

The following examples show how to use org.springframework.mock.web.MockServletContext#addInitParameter() . 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: DispatcherServletChannelInitializer.java    From nettyholdspringmvc with Apache License 2.0 6 votes vote down vote up
public DispatcherServletChannelInitializer() throws ServletException {

    	MockServletContext servletContext = new MockServletContext();
    	MockServletConfig servletConfig = new MockServletConfig(servletContext);
        servletConfig.addInitParameter("contextConfigLocation","classpath:/META-INF/spring/root-context.xml");
        servletContext.addInitParameter("contextConfigLocation","classpath:/META-INF/spring/root-context.xml");

    	//AnnotationConfigWebApplicationContext wac = new AnnotationConfigWebApplicationContext();
        XmlWebApplicationContext wac = new XmlWebApplicationContext();

        //ClassPathXmlApplicationContext wac = new ClassPathXmlApplicationContext();
		wac.setServletContext(servletContext);
		wac.setServletConfig(servletConfig);
        wac.setConfigLocation("classpath:/servlet-context.xml");
    	//wac.register(WebConfig.class);
    	wac.refresh();

    	this.dispatcherServlet = new DispatcherServlet(wac);
    	this.dispatcherServlet.init(servletConfig);
	}
 
Example 2
Source File: ModelManagerListenerIT.java    From oryx with Apache License 2.0 6 votes vote down vote up
@Test
public void testListener() throws Exception {
  Map<String,Object> overlayConfig = new HashMap<>();
  overlayConfig.put("oryx.serving.model-manager-class", MockServingModelManager.class.getName());
  Config config = ConfigUtils.overlayOn(overlayConfig, getConfig());
  String serializedConfig = ConfigUtils.serialize(config);

  MockServletContext mockContext = new MockServletContext();
  mockContext.addInitParameter(ConfigUtils.class.getName() + ".serialized", serializedConfig);

  startMessaging();

  try (ModelManagerListener<?,?,?> listener = new ModelManagerListener<>()) {
    listener.init(mockContext);
    try {
      listener.contextInitialized(new ServletContextEvent(mockContext));
      ServingModelManager<?> manager =
          (ServingModelManager<?>) mockContext.getAttribute(ModelManagerListener.MANAGER_KEY);
      assertNotNull(manager);
      assertFalse(manager.isReadOnly());
      assertNotNull(manager.getConfig());
    } finally {
      listener.contextDestroyed(new ServletContextEvent(mockContext));
    }
  }
}
 
Example 3
Source File: GuiceBindingSpringContextLoaderListenerTest.java    From attic-rave with Apache License 2.0 6 votes vote down vote up
@Test
public void initialize_test() {
    Injector injector = Guice.createInjector(createNiceMock(Module.class));

    MockServletContext mockServletContext = new MockServletContext();
    mockServletContext.addInitParameter("contextConfigLocation",
            "classpath:rave-shindig-test-applicationContext.xml, classpath:rave-shindig-test-dataContext.xml");
    mockServletContext.setAttribute(GuiceServletContextListener.INJECTOR_ATTRIBUTE, injector);

    GuiceBindingSpringContextLoaderListener listener = new GuiceBindingSpringContextLoaderListener();
    ServletContextEvent event = createNiceMock(ServletContextEvent.class);
    expect(event.getServletContext()).andReturn(mockServletContext).anyTimes();
    replay(event);

    listener.contextInitialized(event);
    assertThat((Injector)mockServletContext.getAttribute(GuiceServletContextListener.INJECTOR_ATTRIBUTE), is(not(sameInstance(injector))));

}
 
Example 4
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note 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 5
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 6
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 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 7
Source File: RaveContextLoaderListenerTest.java    From attic-rave with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    mockServletContext = new MockServletContext("test");
    mockServletContext.addInitParameter("contextConfigLocation", "../test-applicationContext.xml");
    servletContextEvent = new ServletContextEvent(mockServletContext);

    raveContextLoaderListener = new RaveContextLoaderListener();
}
 
Example 8
Source File: StartupListenerTest.java    From ankush with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @throws java.lang.Exception
 */
// @Before
public void setUp() throws Exception {
	sc = new MockServletContext("");

	// initialize Spring
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"classpath:/applicationContext-dao.xml, "
					+ "classpath:/applicationContext-service.xml");

	springListener = new ContextLoaderListener();
	springListener.contextInitialized(new ServletContextEvent(sc));
	listener = new StartupListener();

}
 
Example 9
Source File: DelegatingServletTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDelegateToTheGivenServlet() throws IOException, ServletException {
    MockServletContext ctx = new MockServletContext();
    ctx.addInitParameter(DelegatingListener.DELEGATE_SERVLET, DummyServlet.class.getCanonicalName());
    ServletContextEvent evt = new ServletContextEvent(ctx);
    DelegatingListener listener = new DelegatingListener();
    listener.contextInitialized(evt);
    assertThat((DummyServlet) ctx.getAttribute(DelegatingListener.DELEGATE_SERVLET), isA(DummyServlet.class));
    DelegatingServlet servlet = new DelegatingServlet();
    servlet.init(new MockServletConfig(ctx));

    servlet.service(httpServletRequest, new MockHttpServletResponse());
    verify(servletRequestWrapper).setRequestURI("/go/stuff/action");
}
 
Example 10
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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"));
}
 
Example 11
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void registerServletParamPropertySources_StaticWebApplicationContext() {
	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");

	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.setServletConfig(servletConfig);
	ctx.refresh();

	ConfigurableEnvironment environment = ctx.getEnvironment();
	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"));
}
 
Example 12
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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"));
}
 
Example 13
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void registerServletParamPropertySources_StaticWebApplicationContext() {
	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");

	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.setServletConfig(servletConfig);
	ctx.refresh();

	ConfigurableEnvironment environment = ctx.getEnvironment();
	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"));
}
 
Example 14
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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"));
}
 
Example 15
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void registerServletParamPropertySources_StaticWebApplicationContext() {
	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");

	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.setServletConfig(servletConfig);
	ctx.refresh();

	ConfigurableEnvironment environment = ctx.getEnvironment();
	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"));
}