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

The following examples show how to use org.springframework.web.context.support.StandardServletEnvironment. 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: DispatcherServletTests.java    From spring-analysis-note with 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 File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 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
Source File: DispatcherServletTests.java    From java-technology-stack with 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 #4
Source File: LogSearchConfigMapHolder.java    From ambari-logsearch with 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 #5
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 #6
Source File: SpringApplication.java    From spring-javaformat with 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 #7
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 #8
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 #9
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void assertHasStandardServletEnvironment(WebApplicationContext ctx) {
	// ensure a default servlet environment exists
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardServletEnvironment.class));
}
 
Example #10
Source File: CubaClassPathXmlApplicationContext.java    From cuba with 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 #11
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
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
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"));
}
 
Example #13
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 #14
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 #15
Source File: DemoTask.java    From spring-boot-start-current with Apache License 2.0 4 votes vote down vote up
@Override
public void setEnvironment ( Environment environment ) {
    this.environment = ( StandardServletEnvironment ) environment;
}
 
Example #16
Source File: BookmarksApplication.java    From Microservices-with-Spring-Cloud with MIT License 4 votes vote down vote up
private static boolean noActiveProfiles(String[] args) {
    return new StandardServletEnvironment().getActiveProfiles().length == 0
            && Arrays.stream(args)
            .noneMatch(param -> param.startsWith("--spring.profiles.active"));
}
 
Example #17
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void assertHasStandardServletEnvironment(WebApplicationContext ctx) {
	// ensure a default servlet environment exists
	Environment defaultEnv = ctx.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	assertThat(defaultEnv, instanceOf(StandardServletEnvironment.class));
}
 
Example #18
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 #19
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 #20
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 #21
Source File: StandardPortletEnvironment.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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 File: HttpServletBean.java    From spring-analysis-note with 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 File: HttpServletBean.java    From lams with 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 #24
Source File: GenericFilterBean.java    From lams with 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 #25
Source File: HttpServletBean.java    From spring4-understanding with 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();
}
 
Example #26
Source File: GenericFilterBean.java    From spring-analysis-note with 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 #27
Source File: GenericFilterBean.java    From java-technology-stack with 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 #28
Source File: HttpServletBean.java    From java-technology-stack with 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();
}