Migrate file encoding initializer to listener

pull/194/merge
Dave Syer 11 years ago
parent 4d28e1b601
commit 73c2216732

@ -14,16 +14,16 @@
* limitations under the License.
*/
package org.springframework.boot.context.initializer;
package org.springframework.boot.context.listener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.ApplicationListener;
/**
* An {@link ApplicationContextInitializer} halts application startup if the system file
* An {@link ApplicationListener}that halts application startup if the system file
* encoding does not match an expected value set in the environment. By default has no
* effect, but if you set <code>spring.mandatory_file_encoding</code> (or some camelCase
* or UPPERCASE variant of that) to the name of a character encoding (e.g. "UTF-8") then
@ -41,16 +41,15 @@ import org.springframework.context.ConfigurableApplicationContext;
*
* @author Dave Syer
*/
public class FileEncodingApplicationContextInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
public class FileEncodingApplicationListener implements
ApplicationListener<SpringApplicationEnvironmentAvailableEvent> {
private static Log logger = LogFactory
.getLog(FileEncodingApplicationContextInitializer.class);
private static Log logger = LogFactory.getLog(FileEncodingApplicationListener.class);
@Override
public void initialize(final ConfigurableApplicationContext context) {
public void onApplicationEvent(SpringApplicationEnvironmentAvailableEvent event) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
context.getEnvironment(), "spring.");
event.getEnvironment(), "spring.");
if (resolver.containsProperty("mandatoryFileEncoding")) {
final String encoding = System.getProperty("file.encoding");
final String desired = resolver.getProperty("mandatoryFileEncoding");

@ -1,11 +1,11 @@
# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.initializer.FileEncodingApplicationContextInitializer,\
org.springframework.boot.context.initializer.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.initializer.EnvironmentDelegateApplicationContextInitializer
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.context.listener.FileEncodingApplicationListener,\
org.springframework.boot.context.listener.VcapApplicationListener,\
org.springframework.boot.context.listener.ConfigFileApplicationListener,\
org.springframework.boot.context.listener.LoggingApplicationListener,\

@ -20,6 +20,7 @@ import java.util.HashMap;
import java.util.Map;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
/**
@ -31,6 +32,10 @@ public abstract class TestUtils {
public static void addEnviroment(ConfigurableApplicationContext context,
String... pairs) {
addEnviroment(context.getEnvironment(), pairs);
}
public static void addEnviroment(ConfigurableEnvironment enviroment, String... pairs) {
Map<String, Object> map = new HashMap<String, Object>();
for (String pair : pairs) {
int index = pair.indexOf(":");
@ -38,8 +43,7 @@ public abstract class TestUtils {
String value = index > 0 ? pair.substring(index + 1) : "";
map.put(key.trim(), value.trim());
}
context.getEnvironment().getPropertySources()
.addFirst(new MapPropertySource("test", map));
enviroment.getPropertySources().addFirst(new MapPropertySource("test", map));
}
}

@ -176,7 +176,7 @@ public class SpringApplicationBuilderTests {
SpringApplicationBuilder application = new SpringApplicationBuilder(
ExampleConfig.class).web(false);
this.context = application.run();
assertEquals(3, application.application().getInitializers().size());
assertEquals(2, application.application().getInitializers().size());
}
@Test
@ -184,7 +184,7 @@ public class SpringApplicationBuilderTests {
SpringApplicationBuilder application = new SpringApplicationBuilder(
ExampleConfig.class).child(ChildConfig.class).web(false);
this.context = application.run();
assertEquals(4, application.application().getInitializers().size());
assertEquals(3, application.application().getInitializers().size());
}
@Test
@ -198,7 +198,7 @@ public class SpringApplicationBuilderTests {
}
});
this.context = application.run();
assertEquals(4, application.application().getInitializers().size());
assertEquals(3, application.application().getInitializers().size());
}
@Configuration

@ -1,58 +0,0 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.context.initializer;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.TestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
/**
* @author Dave Syer
*/
public class FileEncodingApplicationContextInitializerTests {
private FileEncodingApplicationContextInitializer initializer = new FileEncodingApplicationContextInitializer();
private ConfigurableApplicationContext context;
@Before
public void init() {
this.context = new StaticApplicationContext();
}
@Test(expected = IllegalStateException.class)
public void testIllegalState() {
TestUtils.addEnviroment(this.context, "spring.mandatory_file_encoding:FOO");
this.initializer.initialize(this.context);
}
@Test
public void testSunnyDayNothingMandated() {
this.initializer.initialize(this.context);
}
@Test
public void testSunnyDayMandated() {
Assume.assumeNotNull(System.getProperty("file.encoding"));
TestUtils.addEnviroment(this.context,
"spring.mandatory_file_encoding:" + System.getProperty("file.encoding"));
this.initializer.initialize(this.context);
}
}

@ -0,0 +1,56 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.context.listener;
import org.junit.Assume;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent;
import org.springframework.boot.TestUtils;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
/**
* @author Dave Syer
*/
public class FileEncodingApplicationListenerTests {
private FileEncodingApplicationListener initializer = new FileEncodingApplicationListener();
private ConfigurableEnvironment environment = new StandardEnvironment();
private SpringApplicationEnvironmentAvailableEvent event = new SpringApplicationEnvironmentAvailableEvent(
new SpringApplication(), this.environment, new String[0]);
@Test(expected = IllegalStateException.class)
public void testIllegalState() {
TestUtils.addEnviroment(this.environment, "spring.mandatory_file_encoding:FOO");
this.initializer.onApplicationEvent(this.event);
}
@Test
public void testSunnyDayNothingMandated() {
this.initializer.onApplicationEvent(this.event);
}
@Test
public void testSunnyDayMandated() {
Assume.assumeNotNull(System.getProperty("file.encoding"));
TestUtils.addEnviroment(this.environment, "spring.mandatory_file_encoding:"
+ System.getProperty("file.encoding"));
this.initializer.onApplicationEvent(this.event);
}
}
Loading…
Cancel
Save