1
1
/*
2
- * Copyright 2008-2014 the original author or authors.
2
+ * Copyright 2008-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import static org .mockito .Matchers .*;
21
21
import static org .mockito .Mockito .*;
22
22
23
+ import java .lang .reflect .Method ;
24
+
23
25
import org .junit .Before ;
24
26
import org .junit .Test ;
25
27
import org .junit .runner .RunWith ;
30
32
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
31
33
import org .springframework .context .ApplicationContext ;
32
34
import org .springframework .context .support .GenericApplicationContext ;
35
+ import org .springframework .core .MethodParameter ;
33
36
import org .springframework .core .convert .TypeDescriptor ;
34
37
import org .springframework .core .convert .support .DefaultConversionService ;
35
38
import org .springframework .data .repository .CrudRepository ;
36
39
import org .springframework .data .repository .core .support .DummyRepositoryFactoryBean ;
40
+ import org .springframework .data .repository .support .DomainClassConverter .ToIdConverter ;
41
+ import org .springframework .test .util .ReflectionTestUtils ;
42
+ import org .springframework .web .bind .annotation .ModelAttribute ;
37
43
38
44
/**
39
45
* Unit test for {@link DomainClassConverter}.
@@ -47,6 +53,7 @@ public class DomainClassConverterUnitTests {
47
53
static final User USER = new User ();
48
54
static final TypeDescriptor STRING_TYPE = TypeDescriptor .valueOf (String .class );
49
55
static final TypeDescriptor USER_TYPE = TypeDescriptor .valueOf (User .class );
56
+ static final TypeDescriptor SUB_USER_TYPE = TypeDescriptor .valueOf (SubUser .class );
50
57
static final TypeDescriptor LONG_TYPE = TypeDescriptor .valueOf (Long .class );
51
58
52
59
@ SuppressWarnings ("rawtypes" ) DomainClassConverter converter ;
@@ -144,11 +151,11 @@ public void discoversFactoryAndRepoFromParentApplicationContext() {
144
151
* @see DATACMNS-583
145
152
*/
146
153
@ Test
147
- public void shouldReturnSourceObjectIfSourceAndTargetTypesAreTheSame () {
154
+ public void converterDoesntMatchIfTargetTypeIsAssignableFromSource () {
148
155
149
156
converter .setApplicationContext (initContextWithRepo ());
150
157
151
- assertThat (converter .matches (USER_TYPE , USER_TYPE ), is (true ));
158
+ assertThat (converter .matches (SUB_USER_TYPE , USER_TYPE ), is (false ));
152
159
assertThat ((User ) converter .convert (USER , USER_TYPE , USER_TYPE ), is (USER ));
153
160
}
154
161
@@ -186,6 +193,24 @@ public void supportsConversionFromEntityToString() {
186
193
assertThat (converter .matches (USER_TYPE , STRING_TYPE ), is (true ));
187
194
}
188
195
196
+ /**
197
+ * @see DATACMNS-683
198
+ */
199
+ @ Test
200
+ public void toIdConverterDoesNotMatchIfTargetTypeIsAssignableFromSource () throws Exception {
201
+
202
+ converter .setApplicationContext (initContextWithRepo ());
203
+
204
+ @ SuppressWarnings ("rawtypes" )
205
+ DomainClassConverter .ToIdConverter toIdConverter = (ToIdConverter ) ReflectionTestUtils .getField (converter ,
206
+ "toIdConverter" );
207
+
208
+ Method method = Wrapper .class .getMethod ("foo" , User .class );
209
+ TypeDescriptor target = TypeDescriptor .nested (new MethodParameter (method , 0 ), 0 );
210
+
211
+ assertThat (toIdConverter .matches (SUB_USER_TYPE , target ), is (false ));
212
+ }
213
+
189
214
private ApplicationContext initContextWithRepo () {
190
215
191
216
BeanDefinitionBuilder builder = BeanDefinitionBuilder .rootBeanDefinition (DummyRepositoryFactoryBean .class );
@@ -199,10 +224,17 @@ private ApplicationContext initContextWithRepo() {
199
224
return ctx ;
200
225
}
201
226
227
+ static interface Wrapper {
228
+
229
+ void foo (@ ModelAttribute User user );
230
+ }
231
+
202
232
private static class User {
203
233
204
234
}
205
235
236
+ private static class SubUser extends User {}
237
+
206
238
private static interface UserRepository extends CrudRepository <User , Long > {
207
239
208
240
}
0 commit comments