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