19
19
import static org .junit .Assert .*;
20
20
21
21
import java .util .ArrayList ;
22
+ import java .util .Arrays ;
22
23
import java .util .List ;
24
+ import java .util .function .Consumer ;
23
25
24
26
import org .hamcrest .Matcher ;
25
27
import org .junit .Test ;
26
28
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
29
+ import org .springframework .context .annotation .Bean ;
30
+ import org .springframework .context .annotation .Configuration ;
27
31
import org .springframework .data .classloadersupport .HidingClassLoader ;
28
32
import org .springframework .data .web .ProjectingJackson2HttpMessageConverter ;
29
33
import org .springframework .data .web .XmlBeamHttpMessageConverter ;
30
34
import org .springframework .http .converter .HttpMessageConverter ;
35
+ import org .springframework .test .util .ReflectionTestUtils ;
31
36
import org .xmlbeam .XBProjector ;
32
37
33
38
import com .fasterxml .jackson .databind .ObjectMapper ;
@@ -47,7 +52,7 @@ public void shouldNotLoadJacksonConverterWhenJacksonNotPresent() {
47
52
48
53
List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?>>();
49
54
50
- createConfigWithClassLoader (HidingClassLoader .hide (ObjectMapper .class )). extendMessageConverters (converters );
55
+ createConfigWithClassLoader (HidingClassLoader .hide (ObjectMapper .class ), triggerExtendConverters (converters ) );
51
56
52
57
assertThat (converters , not (hasItem (instanceWithClassName (ProjectingJackson2HttpMessageConverter .class ))));
53
58
}
@@ -57,7 +62,7 @@ public void shouldNotLoadJacksonConverterWhenJaywayNotPresent() {
57
62
58
63
List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?>>();
59
64
60
- createConfigWithClassLoader (HidingClassLoader .hide (DocumentContext .class )). extendMessageConverters (converters );
65
+ createConfigWithClassLoader (HidingClassLoader .hide (DocumentContext .class ), triggerExtendConverters (converters ) );
61
66
62
67
assertThat (converters , not (hasItem (instanceWithClassName (ProjectingJackson2HttpMessageConverter .class ))));
63
68
}
@@ -67,8 +72,7 @@ public void shouldNotLoadXBeamConverterWhenXBeamNotPresent() throws Exception {
67
72
68
73
List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?>>();
69
74
70
- ClassLoader classLoader = HidingClassLoader .hide (XBProjector .class );
71
- createConfigWithClassLoader (classLoader ).extendMessageConverters (converters );
75
+ createConfigWithClassLoader (HidingClassLoader .hide (XBProjector .class ), triggerExtendConverters (converters ));
72
76
73
77
assertThat (converters , not (hasItem (instanceWithClassName (XmlBeamHttpMessageConverter .class ))));
74
78
}
@@ -78,21 +82,49 @@ public void shouldLoadAllConvertersWhenDependenciesArePresent() throws Exception
78
82
79
83
List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?>>();
80
84
81
- createConfigWithClassLoader (getClass ().getClassLoader ()). extendMessageConverters (converters );
85
+ createConfigWithClassLoader (getClass ().getClassLoader (), triggerExtendConverters (converters ) );
82
86
83
87
assertThat (converters , hasItem (instanceWithClassName (XmlBeamHttpMessageConverter .class )));
84
88
assertThat (converters , hasItem (instanceWithClassName (ProjectingJackson2HttpMessageConverter .class )));
85
89
}
86
90
87
- private SpringDataWebConfiguration createConfigWithClassLoader (ClassLoader classLoader ) {
91
+ @ Test // DATACMNS-1152
92
+ public void usesCustomObjectMapper () {
93
+
94
+ List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?>>();
95
+
96
+ createConfigWithClassLoader (getClass ().getClassLoader (), triggerExtendConverters (converters ),
97
+ SomeConfiguration .class );
98
+
99
+ boolean found = false ;
100
+
101
+ for (HttpMessageConverter <?> converter : converters ) {
102
+
103
+ if (converter instanceof ProjectingJackson2HttpMessageConverter ) {
104
+
105
+ found = true ;
106
+
107
+ assertThat (ReflectionTestUtils .getField (converter , "objectMapper" ),
108
+ is (sameInstance ((Object ) SomeConfiguration .MAPPER )));
109
+ }
110
+ }
111
+
112
+ assertThat (found , is (true ));
113
+ }
114
+
115
+ private void createConfigWithClassLoader (ClassLoader classLoader , Consumer <SpringDataWebConfiguration > callback ,
116
+ Class <?>... additionalConfigurationClasses ) {
117
+
118
+ List <Class <?>> configClasses = new ArrayList <Class <?>>(Arrays .asList (additionalConfigurationClasses ));
119
+ configClasses .add (SpringDataWebConfiguration .class );
88
120
89
121
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
90
- SpringDataWebConfiguration . class );
122
+ configClasses . toArray ( new Class <?>[ configClasses . size ()]) );
91
123
92
124
context .setClassLoader (classLoader );
93
125
94
126
try {
95
- return context .getBean (SpringDataWebConfiguration .class );
127
+ callback . accept ( context .getBean (SpringDataWebConfiguration .class ) );
96
128
} finally {
97
129
context .close ();
98
130
}
@@ -108,4 +140,27 @@ private SpringDataWebConfiguration createConfigWithClassLoader(ClassLoader class
108
140
private static <T > Matcher <? super T > instanceWithClassName (Class <T > expectedClass ) {
109
141
return hasProperty ("class" , hasProperty ("name" , equalTo (expectedClass .getName ())));
110
142
}
143
+
144
+ private static Consumer <SpringDataWebConfiguration > triggerExtendConverters (
145
+ final List <HttpMessageConverter <?>> converters ) {
146
+
147
+ return new Consumer <SpringDataWebConfiguration >() {
148
+
149
+ @ Override
150
+ public void accept (SpringDataWebConfiguration t ) {
151
+ t .extendMessageConverters (converters );
152
+ }
153
+ };
154
+ }
155
+
156
+ @ Configuration
157
+ static class SomeConfiguration {
158
+
159
+ static ObjectMapper MAPPER = new ObjectMapper ();
160
+
161
+ @ Bean
162
+ ObjectMapper mapper () {
163
+ return MAPPER ;
164
+ }
165
+ }
111
166
}
0 commit comments