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.
This commit is contained in:
@@ -296,7 +296,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
|
||||
|
||||
// Inform listeners
|
||||
if (null != applicationEventPublisher) {
|
||||
applicationEventPublisher.publishEvent(new MappingContextEvent<E, P>(entity));
|
||||
applicationEventPublisher.publishEvent(new MappingContextEvent<E, P>(this, entity));
|
||||
}
|
||||
|
||||
return entity;
|
||||
|
||||
@@ -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<E extends PersistentEntity<?, P>, 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<E extends PersistentEntity<?, P>, 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<E extends PersistentEntity<?, P>, P extends PersistentProperty<P>> {
|
||||
|
||||
@Mock
|
||||
E entity;
|
||||
|
||||
@Mock
|
||||
MappingContext<?, ?> mappingContext, otherMappingContext;
|
||||
|
||||
@Test
|
||||
public void returnsPersistentEntityHandedToTheEvent() {
|
||||
|
||||
MappingContextEvent<E, P> event = new MappingContextEvent<E, P>(mappingContext, entity);
|
||||
assertThat(event.getPersistentEntity(), is(entity));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesMappingContextAsEventSource() {
|
||||
|
||||
MappingContextEvent<E, P> event = new MappingContextEvent<E, P>(mappingContext, entity);
|
||||
assertThat(event.getSource(), is((Object) mappingContext));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsEmittingMappingContextCorrectly() {
|
||||
|
||||
MappingContextEvent<E, P> event = new MappingContextEvent<E, P>(mappingContext, entity);
|
||||
assertThat(event.wasEmittedBy(mappingContext), is(true));
|
||||
assertThat(event.wasEmittedBy(otherMappingContext), is(false));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user