Java Code Examples for org.springframework.web.context.support.StandardServletEnvironment

The following examples show how to use org.springframework.web.context.support.StandardServletEnvironment. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: spring-analysis-note   Source File: DispatcherServletTests.java    License: MIT License 6 votes vote down vote up
@Test
public void environmentOperations() {
	DispatcherServlet servlet = new DispatcherServlet();
	ConfigurableEnvironment defaultEnv = servlet.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	ConfigurableEnvironment env1 = new StandardServletEnvironment();
	servlet.setEnvironment(env1); // should succeed
	assertThat(servlet.getEnvironment(), sameInstance(env1));
	try {
		servlet.setEnvironment(new DummyEnvironment());
		fail("expected IllegalArgumentException for non-configurable Environment");
	}
	catch (IllegalArgumentException ex) {
	}
	class CustomServletEnvironment extends StandardServletEnvironment { }
	@SuppressWarnings("serial")
	DispatcherServlet custom = new DispatcherServlet() {
		@Override
		protected ConfigurableWebEnvironment createEnvironment() {
			return new CustomServletEnvironment();
		}
	};
	assertThat(custom.getEnvironment(), instanceOf(CustomServletEnvironment.class));
}
 
Example 2
Source Project: java-technology-stack   Source File: DispatcherServletTests.java    License: MIT License 6 votes vote down vote up
@Test
public void environmentOperations() {
	DispatcherServlet servlet = new DispatcherServlet();
	ConfigurableEnvironment defaultEnv = servlet.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	ConfigurableEnvironment env1 = new StandardServletEnvironment();
	servlet.setEnvironment(env1); // should succeed
	assertThat(servlet.getEnvironment(), sameInstance(env1));
	try {
		servlet.setEnvironment(new DummyEnvironment());
		fail("expected IllegalArgumentException for non-configurable Environment");
	}
	catch (IllegalArgumentException ex) {
	}
	class CustomServletEnvironment extends StandardServletEnvironment { }
	@SuppressWarnings("serial")
	DispatcherServlet custom = new DispatcherServlet() {
		@Override
		protected ConfigurableWebEnvironment createEnvironment() {
			return new CustomServletEnvironment();
		}
	};
	assertThat(custom.getEnvironment(), instanceOf(CustomServletEnvironment.class));
}
 
Example 3
@Test
public void environmentOperations() {
	DispatcherServlet servlet = new DispatcherServlet();
	ConfigurableEnvironment defaultEnv = servlet.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	ConfigurableEnvironment env1 = new StandardServletEnvironment();
	servlet.setEnvironment(env1); // should succeed
	assertThat(servlet.getEnvironment(), sameInstance(env1));
	try {
		servlet.setEnvironment(new DummyEnvironment());
		fail("expected IllegalArgumentException for non-configurable Environment");
	}
	catch (IllegalArgumentException ex) {
	}
	class CustomServletEnvironment extends StandardServletEnvironment { }
	@SuppressWarnings("serial")
	DispatcherServlet custom = new DispatcherServlet() {
		@Override
		protected ConfigurableWebEnvironment createEnvironment() {
			return new CustomServletEnvironment();
		}
	};
	assertThat(custom.getEnvironment(), instanceOf(CustomServletEnvironment.class));
}
 
Example 4
@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
@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 Project: ambari-logsearch   Source File: LogSearchConfigMapHolder.java    License: Apache License 2.0 5 votes vote down vote up
public Map<String, String> getLogsearchProperties() {
  if (logsearchProperties.isEmpty()) {
    PropertySource propertySource = ((StandardServletEnvironment) environment)
      .getPropertySources().get("class path resource [" + LOGSEARCH_PROPERTIES_FILE + "]");
    setLogsearchProperties(stringifyValues(((MapPropertySource) propertySource).getSource()));
  }
  return logsearchProperties;
}
 
Example 7
Source Project: spring-javaformat   Source File: SpringApplication.java    License: Apache License 2.0 5 votes vote down vote up
private ConfigurableEnvironment getOrCreateEnvironment() {
	if (this.environment != null) {
		return this.environment;
	}
	if (this.webApplicationType == WebApplicationType.SERVLET) {
		return new StandardServletEnvironment();
	}
	return new StandardEnvironment();
}
 
Example 8
@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 9
@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 10
@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 11
private void assertHasStandardServletEnvironment(WebApplicationContext ctx) {
	// ensure a default servlet environment exists
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardServletEnvironment.class));
}
 
Example 12
@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
@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
private void assertHasStandardServletEnvironment(WebApplicationContext ctx) {
	// ensure a default servlet environment exists
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardServletEnvironment.class));
}
 
Example 15
private static boolean noActiveProfiles(String[] args) {
    return new StandardServletEnvironment().getActiveProfiles().length == 0
            && Arrays.stream(args)
            .noneMatch(param -> param.startsWith("--spring.profiles.active"));
}
 
Example 16
Source Project: spring-boot-start-current   Source File: DemoTask.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void setEnvironment ( Environment environment ) {
    this.environment = ( StandardServletEnvironment ) environment;
}
 
Example 17
@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 18
@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 19
private void assertHasStandardServletEnvironment(WebApplicationContext ctx) {
	// ensure a default servlet environment exists
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardServletEnvironment.class));
}
 
Example 20
Source Project: cuba   Source File: CubaClassPathXmlApplicationContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected ConfigurableEnvironment createEnvironment() {
    StandardServletEnvironment standardServletEnvironment = new StandardServletEnvironment();
    standardServletEnvironment.initPropertySources(servletContext, null);
    return standardServletEnvironment;
}
 
Example 21
/**
 * Customize the set of property sources with those contributed by superclasses as
 * well as those appropriate for standard portlet-based environments:
 * <ul>
 * <li>{@value #PORTLET_CONFIG_PROPERTY_SOURCE_NAME}
 * <li>{@value #PORTLET_CONTEXT_PROPERTY_SOURCE_NAME}
 * <li>{@linkplain StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME "servletContextInitParams"}
 * <li>{@linkplain StandardServletEnvironment#JNDI_PROPERTY_SOURCE_NAME "jndiProperties"}
 * </ul>
 * <p>Properties present in {@value #PORTLET_CONFIG_PROPERTY_SOURCE_NAME} will
 * take precedence over those in {@value #PORTLET_CONTEXT_PROPERTY_SOURCE_NAME},
 * which takes precedence over those in {@linkplain
 * StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME "servletContextInitParams"}
 * and so on.
 * <p>Properties in any of the above will take precedence over system properties and
 * environment variables contributed by the {@link StandardEnvironment} superclass.
 * <p>The property sources are added as stubs for now, and will be
 * {@linkplain PortletApplicationContextUtils#initPortletPropertySources fully
 * initialized} once the actual {@link PortletConfig}, {@link PortletContext}, and
 * {@link ServletContext} objects are available.
 * @see StandardEnvironment#customizePropertySources
 * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources
 * @see PortletConfigPropertySource
 * @see PortletContextPropertySource
 * @see PortletApplicationContextUtils#initPortletPropertySources
 */
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
	propertySources.addLast(new StubPropertySource(PORTLET_CONFIG_PROPERTY_SOURCE_NAME));
	propertySources.addLast(new StubPropertySource(PORTLET_CONTEXT_PROPERTY_SOURCE_NAME));
	propertySources.addLast(new StubPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
	if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
		propertySources.addLast(new JndiPropertySource(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME));
	}
	super.customizePropertySources(propertySources);
}
 
Example 22
Source Project: spring-analysis-note   Source File: HttpServletBean.java    License: MIT License 2 votes vote down vote up
/**
 * Create and return a new {@link StandardServletEnvironment}.
 * <p>Subclasses may override this in order to configure the environment or
 * specialize the environment type returned.
 */
protected ConfigurableEnvironment createEnvironment() {
	return new StandardServletEnvironment();
}
 
Example 23
Source Project: spring-analysis-note   Source File: GenericFilterBean.java    License: MIT License 2 votes vote down vote up
/**
 * Create and return a new {@link StandardServletEnvironment}.
 * <p>Subclasses may override this in order to configure the environment or
 * specialize the environment type returned.
 * @since 4.3.9
 */
protected Environment createEnvironment() {
	return new StandardServletEnvironment();
}
 
Example 24
Source Project: java-technology-stack   Source File: HttpServletBean.java    License: MIT License 2 votes vote down vote up
/**
 * Create and return a new {@link StandardServletEnvironment}.
 * <p>Subclasses may override this in order to configure the environment or
 * specialize the environment type returned.
 */
protected ConfigurableEnvironment createEnvironment() {
	return new StandardServletEnvironment();
}
 
Example 25
Source Project: java-technology-stack   Source File: GenericFilterBean.java    License: MIT License 2 votes vote down vote up
/**
 * Create and return a new {@link StandardServletEnvironment}.
 * <p>Subclasses may override this in order to configure the environment or
 * specialize the environment type returned.
 * @since 4.3.9
 */
protected Environment createEnvironment() {
	return new StandardServletEnvironment();
}
 
Example 26
Source Project: lams   Source File: HttpServletBean.java    License: GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create and return a new {@link StandardServletEnvironment}.
 * <p>Subclasses may override this in order to configure the environment or
 * specialize the environment type returned.
 */
protected ConfigurableEnvironment createEnvironment() {
	return new StandardServletEnvironment();
}
 
Example 27
Source Project: lams   Source File: GenericFilterBean.java    License: GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create and return a new {@link StandardServletEnvironment}.
 * <p>Subclasses may override this in order to configure the environment or
 * specialize the environment type returned.
 * @since 4.3.9
 */
protected Environment createEnvironment() {
	return new StandardServletEnvironment();
}
 
Example 28
Source Project: spring4-understanding   Source File: HttpServletBean.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Create and return a new {@link StandardServletEnvironment}. Subclasses may override
 * in order to configure the environment or specialize the environment type returned.
 */
protected ConfigurableEnvironment createEnvironment() {
	return new StandardServletEnvironment();
}