|
1 | 1 | /*
|
2 |
| - * Copyright 2010-2012 the original author or authors. |
| 2 | + * Copyright 2010-2014 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.math.BigInteger;
|
24 |
| -import java.util.Collection; |
25 | 24 | import java.util.Collections;
|
26 | 25 | import java.util.regex.Pattern;
|
27 | 26 |
|
28 | 27 | import org.bson.types.ObjectId;
|
29 | 28 | import org.junit.Before;
|
30 | 29 | import org.junit.Test;
|
31 | 30 | import org.junit.runner.RunWith;
|
| 31 | +import org.mockito.ArgumentMatcher; |
32 | 32 | import org.mockito.Mock;
|
33 | 33 | import org.mockito.Mockito;
|
34 | 34 | import org.mockito.runners.MockitoJUnitRunner;
|
35 |
| -import org.springframework.context.ApplicationListener; |
36 | 35 | import org.springframework.context.support.GenericApplicationContext;
|
37 | 36 | import org.springframework.core.convert.converter.Converter;
|
38 | 37 | import org.springframework.dao.DataAccessException;
|
@@ -113,7 +112,10 @@ public void defaultsConverterToMappingMongoConverter() throws Exception {
|
113 | 112 |
|
114 | 113 | @Test(expected = InvalidDataAccessApiUsageException.class)
|
115 | 114 | public void rejectsNotFoundMapReduceResource() {
|
116 |
| - template.setApplicationContext(new GenericApplicationContext()); |
| 115 | + |
| 116 | + GenericApplicationContext ctx = new GenericApplicationContext(); |
| 117 | + ctx.refresh(); |
| 118 | + template.setApplicationContext(ctx); |
117 | 119 | template.mapReduce("foo", "classpath:doesNotExist.js", "function() {}", Person.class);
|
118 | 120 | }
|
119 | 121 |
|
@@ -212,18 +214,25 @@ public void registersDefaultEntityIndexCreatorIfApplicationContextHasOneForDiffe
|
212 | 214 | GenericApplicationContext applicationContext = new GenericApplicationContext();
|
213 | 215 | applicationContext.getBeanFactory().registerSingleton("foo",
|
214 | 216 | new MongoPersistentEntityIndexCreator(new MongoMappingContext(), factory));
|
| 217 | + applicationContext.refresh(); |
| 218 | + |
| 219 | + GenericApplicationContext spy = spy(applicationContext); |
215 | 220 |
|
216 | 221 | 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>() { |
218 | 225 |
|
219 |
| - Collection<ApplicationListener<?>> listeners = applicationContext.getApplicationListeners(); |
220 |
| - assertThat(listeners, hasSize(1)); |
| 226 | + @Override |
| 227 | + public boolean matches(Object argument) { |
221 | 228 |
|
222 |
| - ApplicationListener<?> listener = listeners.iterator().next(); |
| 229 | + if (!(argument instanceof MongoPersistentEntityIndexCreator)) { |
| 230 | + return false; |
| 231 | + } |
223 | 232 |
|
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 | + })); |
227 | 236 | }
|
228 | 237 |
|
229 | 238 | class AutogenerateableId {
|
|
0 commit comments