Skip to content

Commit 86c11bc

Browse files
Thomas Darimontodrotbohm
authored andcommitted
DATAMONGO-790 - Ensure compatibility with Spring Framework 4.0.
The GenericApplicationContext doesn't refresh automatically in Spring 4.x we thus we have to call refresh manually. Also the test for the custom application listener registration fails on 4.0, so I adapted it to run on both versions.
1 parent be34b4e commit 86c11bc

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2012 the original author or authors.
2+
* Copyright 2010-2014 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.
@@ -21,18 +21,17 @@
2121
import static org.mockito.Mockito.*;
2222

2323
import java.math.BigInteger;
24-
import java.util.Collection;
2524
import java.util.Collections;
2625
import java.util.regex.Pattern;
2726

2827
import org.bson.types.ObjectId;
2928
import org.junit.Before;
3029
import org.junit.Test;
3130
import org.junit.runner.RunWith;
31+
import org.mockito.ArgumentMatcher;
3232
import org.mockito.Mock;
3333
import org.mockito.Mockito;
3434
import org.mockito.runners.MockitoJUnitRunner;
35-
import org.springframework.context.ApplicationListener;
3635
import org.springframework.context.support.GenericApplicationContext;
3736
import org.springframework.core.convert.converter.Converter;
3837
import org.springframework.dao.DataAccessException;
@@ -113,7 +112,10 @@ public void defaultsConverterToMappingMongoConverter() throws Exception {
113112

114113
@Test(expected = InvalidDataAccessApiUsageException.class)
115114
public void rejectsNotFoundMapReduceResource() {
116-
template.setApplicationContext(new GenericApplicationContext());
115+
116+
GenericApplicationContext ctx = new GenericApplicationContext();
117+
ctx.refresh();
118+
template.setApplicationContext(ctx);
117119
template.mapReduce("foo", "classpath:doesNotExist.js", "function() {}", Person.class);
118120
}
119121

@@ -212,18 +214,25 @@ public void registersDefaultEntityIndexCreatorIfApplicationContextHasOneForDiffe
212214
GenericApplicationContext applicationContext = new GenericApplicationContext();
213215
applicationContext.getBeanFactory().registerSingleton("foo",
214216
new MongoPersistentEntityIndexCreator(new MongoMappingContext(), factory));
217+
applicationContext.refresh();
218+
219+
GenericApplicationContext spy = spy(applicationContext);
215220

216221
MongoTemplate mongoTemplate = new MongoTemplate(factory, converter);
217-
mongoTemplate.setApplicationContext(applicationContext);
222+
mongoTemplate.setApplicationContext(spy);
223+
224+
verify(spy, times(1)).addApplicationListener(argThat(new ArgumentMatcher<MongoPersistentEntityIndexCreator>() {
218225

219-
Collection<ApplicationListener<?>> listeners = applicationContext.getApplicationListeners();
220-
assertThat(listeners, hasSize(1));
226+
@Override
227+
public boolean matches(Object argument) {
221228

222-
ApplicationListener<?> listener = listeners.iterator().next();
229+
if (!(argument instanceof MongoPersistentEntityIndexCreator)) {
230+
return false;
231+
}
223232

224-
assertThat(listener, is(instanceOf(MongoPersistentEntityIndexCreator.class)));
225-
MongoPersistentEntityIndexCreator creator = (MongoPersistentEntityIndexCreator) listener;
226-
assertThat(creator.isIndexCreatorFor(mappingContext), is(true));
233+
return ((MongoPersistentEntityIndexCreator) argument).isIndexCreatorFor(mappingContext);
234+
}
235+
}));
227236
}
228237

229238
class AutogenerateableId {

0 commit comments

Comments
 (0)