Refined packaging: moved all classes into package de.juplo.simplemapper
[simple-mapper] / src / test / java / de / juplo / simplemapper / SimpleMapperServiceAutoConfigurationTest.java
1 package de.juplo.simplemapper;
2
3
4 import com.fasterxml.jackson.core.JsonFactory;
5 import static org.junit.Assert.assertEquals;
6 import org.junit.Test;
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;
17
18
19
20 public class SimpleMapperServiceAutoConfigurationTest
21 {
22   private final Logger LOG =
23       LoggerFactory.getLogger(SimpleMapperServiceAutoConfigurationTest.class);
24
25
26   @Test
27   public void emptyConfiguration()
28   {
29     LOG.info("<-- Start Of New Test-Case!");
30
31     ConfigurableApplicationContext context = load(EmptyConfiguration.class);
32
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());
39
40     context.close();
41   }
42
43   @Test
44   public void factoryConfigured()
45   {
46     LOG.info("<-- Start Of New Test-Case!");
47
48     ConfigurableApplicationContext context = load(FactoryConfigured.class);
49
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());
57
58     context.close();
59   }
60
61   @Test
62   public void serviceConfigured()
63   {
64     LOG.info("<-- Start Of New Test-Case!");
65
66     ConfigurableApplicationContext context = load(ServiceConfigured.class);
67
68     SimpleMapperService service = context.getBean(SimpleMapperService.class);
69     assertNotNull(service);
70     assertNotNull(service.getFactory());
71     assertEquals(ServiceConfigured.factory, service.getFactory());
72     try
73     {
74       context.getBean(JsonFactory.class);
75       fail("A bean of type JsonFactory was found!");
76     }
77     catch(NoSuchBeanDefinitionException e)
78     {
79       LOG.trace("expected exception", e);
80     }
81
82     context.close();
83   }
84
85
86   @Configuration
87   static class EmptyConfiguration
88   {
89   }
90
91   @Configuration
92   static class FactoryConfigured
93   {
94     static JsonFactory factory = new JsonFactory();
95
96
97     @Bean
98     public JsonFactory factory()
99     {
100       return factory;
101     }
102   }
103
104   @Configuration
105   static class ServiceConfigured
106   {
107     static JsonFactory factory = new JsonFactory();
108     static SimpleMapperService service = new SimpleMapperService(factory);
109
110
111     @Bean
112     public SimpleMapperService service()
113     {
114       return service;
115     }
116   }
117
118
119   private ConfigurableApplicationContext load(Class<?> config, String... pairs)
120   {
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);
128     ctx.refresh();
129     return ctx;
130   }
131 }