1 package de.juplo.autoconfigure;
4 import com.fasterxml.jackson.core.JsonFactory;
5 import de.juplo.jackson.SimpleMapperService;
6 import static org.junit.Assert.assertEquals;
8 import org.springframework.context.annotation.Configuration;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer;
12 import org.springframework.context.ConfigurableApplicationContext;
13 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.fail;
16 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
17 import org.springframework.context.annotation.Bean;
21 public class SimpleMapperServiceAutoConfigurationTest
23 private final Logger LOG =
24 LoggerFactory.getLogger(SimpleMapperServiceAutoConfigurationTest.class);
28 public void emptyConfiguration()
30 LOG.info("<-- Start Of New Test-Case!");
32 ConfigurableApplicationContext context = load(EmptyConfiguration.class);
34 SimpleMapperService service = context.getBean(SimpleMapperService.class);
35 assertNotNull(service);
36 assertNotNull(service.getFactory());
37 JsonFactory factory = context.getBean(JsonFactory.class);
38 assertNotNull(factory);
39 assertEquals(factory, service.getFactory());
45 public void factoryConfigured()
47 LOG.info("<-- Start Of New Test-Case!");
49 ConfigurableApplicationContext context = load(FactoryConfigured.class);
51 SimpleMapperService service = context.getBean(SimpleMapperService.class);
52 assertNotNull(service);
53 assertNotNull(service.getFactory());
54 JsonFactory factory = context.getBean(JsonFactory.class);
55 assertNotNull(factory);
56 assertEquals(FactoryConfigured.factory, factory);
57 assertEquals(factory, service.getFactory());
63 public void serviceConfigured()
65 LOG.info("<-- Start Of New Test-Case!");
67 ConfigurableApplicationContext context = load(ServiceConfigured.class);
69 SimpleMapperService service = context.getBean(SimpleMapperService.class);
70 assertNotNull(service);
71 assertNotNull(service.getFactory());
72 assertEquals(ServiceConfigured.factory, service.getFactory());
75 context.getBean(JsonFactory.class);
76 fail("A bean of type JsonFactory was found!");
78 catch(NoSuchBeanDefinitionException e)
80 LOG.trace("expected exception", e);
88 static class EmptyConfiguration
93 static class FactoryConfigured
95 static JsonFactory factory = new JsonFactory();
99 public JsonFactory factory()
106 static class ServiceConfigured
108 static JsonFactory factory = new JsonFactory();
109 static SimpleMapperService service = new SimpleMapperService(factory);
113 public SimpleMapperService service()
120 private ConfigurableApplicationContext load(Class<?> config, String... pairs)
122 AnnotationConfigApplicationContext ctx =
123 new AnnotationConfigApplicationContext();
124 ctx.register(config);
125 ctx.register(SimpleMapperServiceAutoConfiguration.class); // << Does not work as expected, if the autoconfiguration is registered before the configuration!
126 AutoConfigurationReportLoggingInitializer report =
127 new AutoConfigurationReportLoggingInitializer();
128 report.initialize(ctx);