11/*
2- * Copyright 2008-2012 the original author or authors.
2+ * Copyright 2008-2013 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2121import static org .mockito .Mockito .*;
2222
2323import java .io .Serializable ;
24- import java .util .HashMap ;
25- import java .util .Map ;
2624
27- import org .hamcrest .Description ;
28- import org .hamcrest .TypeSafeMatcher ;
2925import org .junit .Before ;
3026import org .junit .Test ;
3127import org .junit .runner .RunWith ;
3228import org .mockito .Mock ;
3329import 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 ;
3433import org .springframework .context .ApplicationContext ;
34+ import org .springframework .context .support .GenericApplicationContext ;
3535import org .springframework .core .convert .TypeDescriptor ;
3636import org .springframework .core .convert .support .DefaultConversionService ;
3737import org .springframework .data .repository .CrudRepository ;
3838import org .springframework .data .repository .core .EntityInformation ;
3939import org .springframework .data .repository .core .RepositoryInformation ;
4040import 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 ;
4242
4343/**
4444 * Unit test for {@link DomainClassConverter}.
@@ -56,17 +56,8 @@ public class DomainClassConverterUnitTests {
5656 TypeDescriptor sourceDescriptor ;
5757 TypeDescriptor targetDescriptor ;
5858
59- @ SuppressWarnings ("rawtypes" )
60- Map <String , RepositoryFactoryInformation > providers ;
61-
62- @ Mock
63- ApplicationContext context , parent ;
64- @ Mock
65- UserRepository repository ;
6659 @ Mock
6760 DefaultConversionService service ;
68- @ Mock
69- RepositoryFactoryInformation <User , Serializable > provider ;
7061
7162 @ Before
7263 @ SuppressWarnings ({ "unchecked" , "rawtypes" })
@@ -76,27 +67,22 @@ public void setUp() {
7667 RepositoryInformation repositoryInformation = new DummyRepositoryInformation (UserRepository .class );
7768
7869 converter = new DomainClassConverter (service );
79- providers = new HashMap <String , RepositoryFactoryInformation >();
8070
8171 sourceDescriptor = TypeDescriptor .valueOf (String .class );
8272 targetDescriptor = TypeDescriptor .valueOf (User .class );
83-
84- when (provider .getEntityInformation ()).thenReturn (information );
85- when (provider .getRepositoryInformation ()).thenReturn (repositoryInformation );
8673 }
8774
8875 @ Test
8976 public void matchFailsIfNoDaoAvailable () throws Exception {
9077
91- converter .setApplicationContext (context );
78+ converter .setApplicationContext (new GenericApplicationContext () );
9279 assertMatches (false );
9380 }
9481
9582 @ Test
9683 public void matchesIfConversionInBetweenIsPossible () throws Exception {
9784
98- letContextContain (context , provider );
99- converter .setApplicationContext (context );
85+ converter .setApplicationContext (initContextWithRepo ());
10086
10187 when (service .canConvert (String .class , Long .class )).thenReturn (true );
10288
@@ -106,8 +92,7 @@ public void matchesIfConversionInBetweenIsPossible() throws Exception {
10692 @ Test
10793 public void matchFailsIfNoIntermediateConversionIsPossible () throws Exception {
10894
109- letContextContain (context , provider );
110- converter .setApplicationContext (context );
95+ converter .setApplicationContext (initContextWithRepo ());
11196
11297 when (service .canConvert (String .class , Long .class )).thenReturn (false );
11398
@@ -136,16 +121,18 @@ private void assertMatches(boolean matchExpected) {
136121 @ Test
137122 public void convertsStringToUserCorrectly () throws Exception {
138123
139- letContextContain ( context , provider );
124+ ApplicationContext context = initContextWithRepo ( );
140125 converter .setApplicationContext (context );
141126
142127 when (service .canConvert (String .class , Long .class )).thenReturn (true );
143128 when (service .convert (anyString (), eq (Long .class ))).thenReturn (1L );
144- when (repository .findOne (1L )).thenReturn (USER );
145129
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 );
149136 }
150137
151138 /**
@@ -154,54 +141,24 @@ public void convertsStringToUserCorrectly() throws Exception {
154141 @ Test
155142 public void discoversFactoryAndRepoFromParentApplicationContext () {
156143
157- letContextContain (parent , provider );
158- when (context .getParentBeanFactory ()).thenReturn (parent );
144+ ApplicationContext parent = initContextWithRepo ();
145+ ApplicationContext context = new GenericApplicationContext (parent );
146+
159147 when (service .canConvert (String .class , Long .class )).thenReturn (true );
160148
161149 converter .setApplicationContext (context );
162150 assertThat (converter .matches (sourceDescriptor , targetDescriptor ), is (true ));
163151 }
164152
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 () {
195154
196- arg0 . appendText ( "not a subtype of" );
197- }
155+ BeanDefinitionBuilder builder = BeanDefinitionBuilder . rootBeanDefinition ( DummyRepositoryFactoryBean . class );
156+ builder . addPropertyValue ( "repositoryInterface" , UserRepository . class );
198157
199- @ Override
200- public boolean matchesSafely ( Class < T > arg0 ) {
158+ DefaultListableBeanFactory factory = new DefaultListableBeanFactory ();
159+ factory . registerBeanDefinition ( "provider" , builder . getBeanDefinition ());
201160
202- return arg0 .isAssignableFrom (type );
203- }
204- };
161+ return new GenericApplicationContext (factory );
205162 }
206163
207164 private static class User {
0 commit comments