Skip to content

Instantly share code, notes, and snippets.

@stevenschlansker
Last active July 5, 2016 17:48
Show Gist options
  • Save stevenschlansker/c432a6360fe8897a9cd9af6cd16e4719 to your computer and use it in GitHub Desktop.
Save stevenschlansker/c432a6360fe8897a9cd9af6cd16e4719 to your computer and use it in GitHub Desktop.
package hack;
import java.time.Duration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=TestCustomConversionService.Config.class)
@TestPropertySource(properties="dur=PT10s")
public class TestCustomConversionService {
@Autowired
Config config;
@Autowired
BeanFactory bf;
@Autowired
Environment env;
@Autowired
PropertyResolver pr;
// Works, uses our custom conversion service
@Test
public void testInject() {
bf.getBean(Duration.class);
}
// No go, doesn't use our conversionService
@Test
public void testEnvironment() {
env.getProperty("dur", Duration.class);
}
// No go, doesn't use our conversionService
@Test
public void testPropertyResolver() {
pr.getProperty("dur", Duration.class);
}
// But this works
@Test
public void testEnvironmentHackFix() {
((ConfigurableEnvironment) env).setConversionService((ConfigurableConversionService) Config.conversionService());
env.getProperty("dur", Duration.class);
}
// And so does this
@Test
public void testPropertyResolverHackFix() {
((ConfigurableEnvironment) env).setConversionService((ConfigurableConversionService) Config.conversionService());
pr.getProperty("dur", Duration.class);
}
@Configuration
static class Config {
@Bean
@Lazy
Duration convertDuration(@Value("PT20s") Duration duration) {
return duration;
}
@Bean
static ConversionService conversionService() {
DefaultFormattingConversionService cs = new DefaultFormattingConversionService();
new DateTimeFormatterRegistrar().registerFormatters(cs);
return cs;
}
}
}
package hack;
import java.time.Duration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=TestDefaultConversionService.Config.class)
@TestPropertySource(properties="dur=PT10s")
public class TestDefaultConversionService {
@Autowired
Config config;
@Autowired
BeanFactory bf;
@Autowired
Environment env;
@Autowired
PropertyResolver pr;
// All three of these fail due to not knowing Duration type
@Test
public void testInject() {
bf.getBean(Duration.class);
}
@Test
public void testEnvironment() {
env.getProperty("dur", Duration.class);
}
@Test
public void testPropertyResolver() {
pr.getProperty("dur", Duration.class);
}
@Configuration
static class Config {
@Bean
@Lazy
Duration convertDuration(@Value("PT20s") Duration duration) {
return duration;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment