From fc57f602e9d8d68ba1afb3336f73303673160aa6 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 31 Jul 2012 15:21:14 +0200 Subject: [PATCH] DATACMNS-209 - MappingContextEvent now carries its emitting MappingContext. MappingContextEvent now has an wasEmittedBy(MappingContext context) method to allow identifying whether the event was emitted by the given MappingContext. Useful if multiple MappingContexts are in place and one wants to react on the events emitted by a particular one only. --- .../context/AbstractMappingContext.java | 2 +- .../mapping/event/MappingContextEvent.java | 24 +++++-- .../event/MappingContextEventUnitTests.java | 64 +++++++++++++++++++ 3 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 spring-data-commons-core/src/test/java/org/springframework/data/mapping/event/MappingContextEventUnitTests.java diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index ffdb7d7fc..965730bf2 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -296,7 +296,7 @@ public abstract class AbstractMappingContext(entity)); + applicationEventPublisher.publishEvent(new MappingContextEvent(this, entity)); } return entity; diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/event/MappingContextEvent.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/event/MappingContextEvent.java index 5ae4b5dba..8b40cc514 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/event/MappingContextEvent.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/event/MappingContextEvent.java @@ -18,6 +18,7 @@ package org.springframework.data.mapping.event; import org.springframework.context.ApplicationEvent; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; +import org.springframework.data.mapping.context.MappingContext; import org.springframework.util.Assert; /** @@ -33,19 +34,24 @@ public class MappingContextEvent, P extends Per private static final long serialVersionUID = 1336466833846092490L; - private final E source; + private final MappingContext source; + private final E entity; /** - * Creates a new {@link MappingContextEvent} for the given {@link PersistentEntity}. + * Creates a new {@link MappingContextEvent} for the given {@link MappingContext} and {@link PersistentEntity}. * * @param source must not be {@literal null}. + * @param entity must not be {@literal null}. */ - public MappingContextEvent(E source) { + public MappingContextEvent(MappingContext source, E entity) { super(source); Assert.notNull(source); + Assert.notNull(entity); + this.source = source; + this.entity = entity; } /** @@ -54,6 +60,16 @@ public class MappingContextEvent, P extends Per * @return */ public E getPersistentEntity() { - return source; + return entity; + } + + /** + * Returns whether the {@link MappingContextEvent} was triggered by the given {@link MappingContext}. + * + * @param context the {@link MappingContext} that potentially created the event. + * @return + */ + public boolean wasEmittedBy(MappingContext context) { + return this.source.equals(context); } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/event/MappingContextEventUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/event/MappingContextEventUnitTests.java new file mode 100644 index 000000000..5e9334f5f --- /dev/null +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/event/MappingContextEventUnitTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mapping.event; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.PersistentProperty; +import org.springframework.data.mapping.context.MappingContext; + +/** + * Unit tests for {@link MappingContextEvent}. + * + * @author Oliver Gierke + */ +@RunWith(MockitoJUnitRunner.class) +public class MappingContextEventUnitTests, P extends PersistentProperty

> { + + @Mock + E entity; + + @Mock + MappingContext mappingContext, otherMappingContext; + + @Test + public void returnsPersistentEntityHandedToTheEvent() { + + MappingContextEvent event = new MappingContextEvent(mappingContext, entity); + assertThat(event.getPersistentEntity(), is(entity)); + } + + @Test + public void usesMappingContextAsEventSource() { + + MappingContextEvent event = new MappingContextEvent(mappingContext, entity); + assertThat(event.getSource(), is((Object) mappingContext)); + } + + @Test + public void detectsEmittingMappingContextCorrectly() { + + MappingContextEvent event = new MappingContextEvent(mappingContext, entity); + assertThat(event.wasEmittedBy(mappingContext), is(true)); + assertThat(event.wasEmittedBy(otherMappingContext), is(false)); + } +}