1 package de.juplo.simplemapper;
4 import com.fasterxml.jackson.core.JsonFactory;
5 import static org.junit.Assert.assertEquals;
7 import org.springframework.context.annotation.Configuration;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer;
11 import org.springframework.context.ConfigurableApplicationContext;
12 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.fail;
15 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
16 import org.springframework.context.annotation.Bean;
20 public class SimpleMapperServiceAutoConfigurationTest
22 private final Logger LOG =
23 LoggerFactory.getLogger(SimpleMapperServiceAutoConfigurationTest.class);
27 public void emptyConfiguration()
29 LOG.info("<-- Start Of New Test-Case!");
31 ConfigurableApplicationContext context = load(EmptyConfiguration.class);
33 SimpleMapperService service = context.getBean(SimpleMapperService.class);
34 assertNotNull(service);
35 assertNotNull(service.getFactory());
36 JsonFactory factory = context.getBean(JsonFactory.class);
37 assertNotNull(factory);
38 assertEquals(factory, service.getFactory());
44 public void factoryConfigured()
46 LOG.info("<-- Start Of New Test-Case!");
48 ConfigurableApplicationContext context = load(FactoryConfigured.class);
50 SimpleMapperService service = context.getBean(SimpleMapperService.class);
51 assertNotNull(service);
52 assertNotNull(service.getFactory());
53 JsonFactory factory = context.getBean(JsonFactory.class);
54 assertNotNull(factory);
55 assertEquals(FactoryConfigured.factory, factory);
56 assertEquals(factory, service.getFactory());
62 public void serviceConfigured()
64 LOG.info("<-- Start Of New Test-Case!");
66 ConfigurableApplicationContext context = load(ServiceConfigured.class);
68 SimpleMapperService service = context.getBean(SimpleMapperService.class);
69 assertNotNull(service);
70 assertNotNull(service.getFactory());
71 assertEquals(ServiceConfigured.factory, service.getFactory());
74 context.getBean(JsonFactory.class);
75 fail("A bean of type JsonFactory was found!");
77 catch(NoSuchBeanDefinitionException e)
79 LOG.trace("expected exception", e);
87 static class EmptyConfiguration
92 static class FactoryConfigured
94 static JsonFactory factory = new JsonFactory();
98 public JsonFactory factory()
105 static class ServiceConfigured
107 static JsonFactory factory = new JsonFactory();
108 static SimpleMapperService service = new SimpleMapperService(factory);
112 public SimpleMapperService service()
119 private ConfigurableApplicationContext load(Class<?> config, String... pairs)
121 AnnotationConfigApplicationContext ctx =
122 new AnnotationConfigApplicationContext();
123 ctx.register(config);
124 ctx.register(SimpleMapperServiceAutoConfiguration.class); // << Does not work as expected, if the autoconfiguration is registered before the configuration!
125 AutoConfigurationReportLoggingInitializer report =
126 new AutoConfigurationReportLoggingInitializer();
127 report.initialize(ctx);