1
1
/*
2
- * Copyright 2008-2012 the original author or authors.
2
+ * Copyright 2008-2013 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.
21
21
import static org .mockito .Mockito .*;
22
22
23
23
import java .io .Serializable ;
24
- import java .util .HashMap ;
25
- import java .util .Map ;
26
24
27
- import org .hamcrest .Description ;
28
- import org .hamcrest .TypeSafeMatcher ;
29
25
import org .junit .Before ;
30
26
import org .junit .Test ;
31
27
import org .junit .runner .RunWith ;
32
28
import org .mockito .Mock ;
33
29
import org .mockito .runners .MockitoJUnitRunner ;
30
+ import org .springframework .aop .framework .Advised ;
31
+ import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
32
+ import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
34
33
import org .springframework .context .ApplicationContext ;
34
+ import org .springframework .context .support .GenericApplicationContext ;
35
35
import org .springframework .core .convert .TypeDescriptor ;
36
36
import org .springframework .core .convert .support .DefaultConversionService ;
37
37
import org .springframework .data .repository .CrudRepository ;
38
38
import org .springframework .data .repository .core .EntityInformation ;
39
39
import org .springframework .data .repository .core .RepositoryInformation ;
40
40
import org .springframework .data .repository .core .support .DummyEntityInformation ;
41
- import org .springframework .data .repository .core .support .RepositoryFactoryInformation ;
41
+ import org .springframework .data .repository .core .support .DummyRepositoryFactoryBean ;
42
42
43
43
/**
44
44
* Unit test for {@link DomainClassConverter}.
@@ -56,17 +56,8 @@ public class DomainClassConverterUnitTests {
56
56
TypeDescriptor sourceDescriptor ;
57
57
TypeDescriptor targetDescriptor ;
58
58
59
- @ SuppressWarnings ("rawtypes" )
60
- Map <String , RepositoryFactoryInformation > providers ;
61
-
62
- @ Mock
63
- ApplicationContext context , parent ;
64
- @ Mock
65
- UserRepository repository ;
66
59
@ Mock
67
60
DefaultConversionService service ;
68
- @ Mock
69
- RepositoryFactoryInformation <User , Serializable > provider ;
70
61
71
62
@ Before
72
63
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
@@ -76,27 +67,22 @@ public void setUp() {
76
67
RepositoryInformation repositoryInformation = new DummyRepositoryInformation (UserRepository .class );
77
68
78
69
converter = new DomainClassConverter (service );
79
- providers = new HashMap <String , RepositoryFactoryInformation >();
80
70
81
71
sourceDescriptor = TypeDescriptor .valueOf (String .class );
82
72
targetDescriptor = TypeDescriptor .valueOf (User .class );
83
-
84
- when (provider .getEntityInformation ()).thenReturn (information );
85
- when (provider .getRepositoryInformation ()).thenReturn (repositoryInformation );
86
73
}
87
74
88
75
@ Test
89
76
public void matchFailsIfNoDaoAvailable () throws Exception {
90
77
91
- converter .setApplicationContext (context );
78
+ converter .setApplicationContext (new GenericApplicationContext () );
92
79
assertMatches (false );
93
80
}
94
81
95
82
@ Test
96
83
public void matchesIfConversionInBetweenIsPossible () throws Exception {
97
84
98
- letContextContain (context , provider );
99
- converter .setApplicationContext (context );
85
+ converter .setApplicationContext (initContextWithRepo ());
100
86
101
87
when (service .canConvert (String .class , Long .class )).thenReturn (true );
102
88
@@ -106,8 +92,7 @@ public void matchesIfConversionInBetweenIsPossible() throws Exception {
106
92
@ Test
107
93
public void matchFailsIfNoIntermediateConversionIsPossible () throws Exception {
108
94
109
- letContextContain (context , provider );
110
- converter .setApplicationContext (context );
95
+ converter .setApplicationContext (initContextWithRepo ());
111
96
112
97
when (service .canConvert (String .class , Long .class )).thenReturn (false );
113
98
@@ -136,16 +121,18 @@ private void assertMatches(boolean matchExpected) {
136
121
@ Test
137
122
public void convertsStringToUserCorrectly () throws Exception {
138
123
139
- letContextContain ( context , provider );
124
+ ApplicationContext context = initContextWithRepo ( );
140
125
converter .setApplicationContext (context );
141
126
142
127
when (service .canConvert (String .class , Long .class )).thenReturn (true );
143
128
when (service .convert (anyString (), eq (Long .class ))).thenReturn (1L );
144
- when (repository .findOne (1L )).thenReturn (USER );
145
129
146
- Object user = converter .convert ("1" , sourceDescriptor , targetDescriptor );
147
- assertThat (user , is (instanceOf (User .class )));
148
- assertThat (user , is ((Object ) USER ));
130
+ converter .convert ("1" , sourceDescriptor , targetDescriptor );
131
+
132
+ UserRepository bean = context .getBean (UserRepository .class );
133
+ UserRepository repo = (UserRepository ) ((Advised ) bean ).getTargetSource ().getTarget ();
134
+
135
+ verify (repo , times (1 )).findOne (1L );
149
136
}
150
137
151
138
/**
@@ -154,54 +141,24 @@ public void convertsStringToUserCorrectly() throws Exception {
154
141
@ Test
155
142
public void discoversFactoryAndRepoFromParentApplicationContext () {
156
143
157
- letContextContain (parent , provider );
158
- when (context .getParentBeanFactory ()).thenReturn (parent );
144
+ ApplicationContext parent = initContextWithRepo ();
145
+ ApplicationContext context = new GenericApplicationContext (parent );
146
+
159
147
when (service .canConvert (String .class , Long .class )).thenReturn (true );
160
148
161
149
converter .setApplicationContext (context );
162
150
assertThat (converter .matches (sourceDescriptor , targetDescriptor ), is (true ));
163
151
}
164
152
165
- private void letContextContain (ApplicationContext context , Object bean ) {
166
-
167
- configureContextToReturnBeans (context , repository , provider );
168
-
169
- Map <String , Object > beanMap = getBeanAsMap (bean );
170
- when (context .getBeansOfType (argThat (is (subtypeOf (bean .getClass ()))))).thenReturn (beanMap );
171
- }
172
-
173
- private void configureContextToReturnBeans (ApplicationContext context , UserRepository repository ,
174
- RepositoryFactoryInformation <User , Serializable > provider ) {
175
-
176
- Map <String , UserRepository > map = getBeanAsMap (repository );
177
- when (context .getBeansOfType (UserRepository .class )).thenReturn (map );
178
-
179
- providers .put ("provider" , provider );
180
- when (context .getBeansOfType (RepositoryFactoryInformation .class )).thenReturn (providers );
181
- }
182
-
183
- private <T > Map <String , T > getBeanAsMap (T bean ) {
184
-
185
- Map <String , T > beanMap = new HashMap <String , T >();
186
- beanMap .put (bean .getClass ().getName (), bean );
187
- return beanMap ;
188
- }
189
-
190
- private static <T > TypeSafeMatcher <Class <T >> subtypeOf (final Class <? extends T > type ) {
191
-
192
- return new TypeSafeMatcher <Class <T >>() {
193
-
194
- public void describeTo (Description arg0 ) {
153
+ private ApplicationContext initContextWithRepo () {
195
154
196
- arg0 . appendText ( "not a subtype of" );
197
- }
155
+ BeanDefinitionBuilder builder = BeanDefinitionBuilder . rootBeanDefinition ( DummyRepositoryFactoryBean . class );
156
+ builder . addPropertyValue ( "repositoryInterface" , UserRepository . class );
198
157
199
- @ Override
200
- public boolean matchesSafely ( Class < T > arg0 ) {
158
+ DefaultListableBeanFactory factory = new DefaultListableBeanFactory ();
159
+ factory . registerBeanDefinition ( "provider" , builder . getBeanDefinition ());
201
160
202
- return arg0 .isAssignableFrom (type );
203
- }
204
- };
161
+ return new GenericApplicationContext (factory );
205
162
}
206
163
207
164
private static class User {
0 commit comments