DATAMONGO-1823 - Emit ApplicationEvents using projecting find methods.

We now again emit application events when using finder methods that apply projection.

Original pull request: #517.
This commit is contained in:
Christoph Strobl
2017-11-20 09:15:12 +01:00
committed by Mark Paluch
parent eb51828d4d
commit e411831e76
2 changed files with 27 additions and 4 deletions

View File

@@ -2813,10 +2813,15 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
Class<?> typeToRead = targetType.isInterface() || targetType.isAssignableFrom(entityType) ? entityType
: targetType;
if (null != object) {
maybeEmitEvent(new AfterLoadEvent<T>(object, targetType, collectionName));
}
Object source = reader.read(typeToRead, object);
Object result = targetType.isInterface() ? projectionFactory.createProjection(targetType, source) : source;
if (result == null) {
if (result != null) {
maybeEmitEvent(new AfterConvertEvent<>(object, result, collectionName));
}

View File

@@ -19,10 +19,12 @@ import static org.hamcrest.collection.IsCollectionWithSize.*;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsEqual.*;
import static org.junit.Assert.*;
import static org.springframework.data.mongodb.core.DocumentTestUtils.assertTypeHint;
import static org.springframework.data.mongodb.core.DocumentTestUtils.*;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import lombok.Data;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.LinkedHashMap;
@@ -46,8 +48,6 @@ import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;
import lombok.Data;
/**
* Integration test for Mapping Events.
*
@@ -390,6 +390,24 @@ public class ApplicationContextEventTests {
is(equalTo(RELATED_COLLECTION_NAME)));
}
@Test // DATAMONGO-1823
public void publishesAfterConvertEventForFindQueriesUsingProjections() {
PersonPojoStringId entity = new PersonPojoStringId("1", "Text");
template.insert(entity);
template.query(PersonPojoStringId.class).matching(query(where("id").is(entity.getId()))).all();
assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(1));
assertThat(simpleMappingEventListener.onAfterLoadEvents.get(0).getCollectionName(), is(COLLECTION_NAME));
assertThat(simpleMappingEventListener.onBeforeConvertEvents.size(), is(1));
assertThat(simpleMappingEventListener.onBeforeConvertEvents.get(0).getCollectionName(), is(COLLECTION_NAME));
assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(1));
assertThat(simpleMappingEventListener.onAfterConvertEvents.get(0).getCollectionName(), is(COLLECTION_NAME));
}
private void comparePersonAndDocument(PersonPojoStringId p, PersonPojoStringId p2, org.bson.Document document) {
assertEquals(p.getId(), p2.getId());