DATAJDBC-197 - Polishing.
Rename AfterCreation event to AfterLoadEvent to align with other Spring Data modules. Add missing generics. Remove since tags pointing to version 2.0. Slightly tweak tests. Original pull request: #58.
This commit is contained in:
@@ -23,8 +23,8 @@ import org.springframework.data.jdbc.core.conversion.AggregateChange.Kind;
|
||||
import org.springframework.data.jdbc.core.conversion.Interpreter;
|
||||
import org.springframework.data.jdbc.core.conversion.JdbcEntityDeleteWriter;
|
||||
import org.springframework.data.jdbc.core.conversion.JdbcEntityWriter;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterCreation;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterDelete;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterLoadEvent;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterSave;
|
||||
import org.springframework.data.jdbc.mapping.event.BeforeDelete;
|
||||
import org.springframework.data.jdbc.mapping.event.BeforeSave;
|
||||
@@ -37,6 +37,7 @@ import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformati
|
||||
* {@link JdbcEntityOperations} implementation, storing aggregates in and obtaining them from a JDBC data store.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class JdbcEntityTemplate implements JdbcEntityOperations {
|
||||
|
||||
@@ -94,7 +95,7 @@ public class JdbcEntityTemplate implements JdbcEntityOperations {
|
||||
|
||||
T entity = accessStrategy.findById(id, domainType);
|
||||
if (entity != null) {
|
||||
publishAfterCreation(id, entity);
|
||||
publishAfterLoad(id, entity);
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
@@ -108,7 +109,7 @@ public class JdbcEntityTemplate implements JdbcEntityOperations {
|
||||
public <T> Iterable<T> findAll(Class<T> domainType) {
|
||||
|
||||
Iterable<T> all = accessStrategy.findAll(domainType);
|
||||
publishAfterCreation(all);
|
||||
publishAfterLoad(all);
|
||||
return all;
|
||||
}
|
||||
|
||||
@@ -116,7 +117,7 @@ public class JdbcEntityTemplate implements JdbcEntityOperations {
|
||||
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
|
||||
|
||||
Iterable<T> allById = accessStrategy.findAllById(ids, domainType);
|
||||
publishAfterCreation(allById);
|
||||
publishAfterLoad(allById);
|
||||
return allById;
|
||||
}
|
||||
|
||||
@@ -153,35 +154,38 @@ public class JdbcEntityTemplate implements JdbcEntityOperations {
|
||||
publisher.publishEvent(new AfterDelete(specifiedId, optionalEntity, change));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> AggregateChange createChange(T instance) {
|
||||
|
||||
AggregateChange aggregateChange = new AggregateChange(Kind.SAVE, instance.getClass(), instance);
|
||||
AggregateChange<?> aggregateChange = new AggregateChange(Kind.SAVE, instance.getClass(), instance);
|
||||
jdbcEntityWriter.write(instance, aggregateChange);
|
||||
return aggregateChange;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private AggregateChange createDeletingChange(Object id, Object entity, Class<?> domainType) {
|
||||
|
||||
AggregateChange aggregateChange = new AggregateChange(Kind.DELETE, domainType, entity);
|
||||
AggregateChange<?> aggregateChange = new AggregateChange(Kind.DELETE, domainType, entity);
|
||||
jdbcEntityDeleteWriter.write(id, aggregateChange);
|
||||
return aggregateChange;
|
||||
}
|
||||
|
||||
private AggregateChange createDeletingChange(Class<?> domainType) {
|
||||
|
||||
AggregateChange aggregateChange = new AggregateChange(Kind.DELETE, domainType, null);
|
||||
AggregateChange<?> aggregateChange = new AggregateChange<>(Kind.DELETE, domainType, null);
|
||||
jdbcEntityDeleteWriter.write(null, aggregateChange);
|
||||
return aggregateChange;
|
||||
}
|
||||
|
||||
private <T> void publishAfterCreation(Iterable<T> all) {
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> void publishAfterLoad(Iterable<T> all) {
|
||||
|
||||
all.forEach(e -> {
|
||||
publishAfterCreation(context.getRequiredPersistentEntityInformation((Class<T>) e.getClass()).getRequiredId(e), e);
|
||||
});
|
||||
for (T e : all) {
|
||||
publishAfterLoad(context.getRequiredPersistentEntityInformation((Class<T>) e.getClass()).getRequiredId(e), e);
|
||||
}
|
||||
}
|
||||
|
||||
private <T> void publishAfterCreation(Object id, T entity) {
|
||||
publisher.publishEvent(new AfterCreation(Identifier.of(id), entity));
|
||||
private <T> void publishAfterLoad(Object id, T entity) {
|
||||
publisher.publishEvent(new AfterLoadEvent(Identifier.of(id), entity));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,8 @@ import org.springframework.data.jdbc.mapping.event.Identifier.Specified;
|
||||
* postprocessing of entities.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @since 2.0
|
||||
*/
|
||||
public class AfterCreation extends JdbcEventWithIdAndEntity {
|
||||
public class AfterLoadEvent extends JdbcEventWithIdAndEntity {
|
||||
|
||||
private static final long serialVersionUID = -4185777271143436728L;
|
||||
|
||||
@@ -32,7 +31,7 @@ public class AfterCreation extends JdbcEventWithIdAndEntity {
|
||||
* @param id of the entity
|
||||
* @param entity the newly instantiated entity.
|
||||
*/
|
||||
public AfterCreation(Specified id, Object entity) {
|
||||
public AfterLoadEvent(Specified id, Object entity) {
|
||||
super(id, entity, null);
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,12 @@ import static java.util.Arrays.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -21,8 +23,8 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
|
||||
import org.springframework.data.jdbc.core.SqlGeneratorSource;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterCreation;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterDelete;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterLoadEvent;
|
||||
import org.springframework.data.jdbc.mapping.event.AfterSave;
|
||||
import org.springframework.data.jdbc.mapping.event.BeforeDelete;
|
||||
import org.springframework.data.jdbc.mapping.event.BeforeSave;
|
||||
@@ -36,11 +38,14 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
import org.springframework.jdbc.support.KeyHolder;
|
||||
|
||||
/**
|
||||
* Unit tests for application events via {@link SimpleJdbcRepository}.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class SimpleJdbcRepositoryEventsUnitTests {
|
||||
|
||||
FakePublisher publisher = new FakePublisher();
|
||||
CollectingEventPublisher publisher = new CollectingEventPublisher();
|
||||
|
||||
DummyEntityRepository repository;
|
||||
DefaultDataAccessStrategy dataAccessStrategy;
|
||||
@@ -48,7 +53,7 @@ public class SimpleJdbcRepositoryEventsUnitTests {
|
||||
@Before
|
||||
public void before() {
|
||||
|
||||
final JdbcMappingContext context = new JdbcMappingContext(createIdGeneratingOperations());
|
||||
JdbcMappingContext context = new JdbcMappingContext(createIdGeneratingOperations());
|
||||
|
||||
dataAccessStrategy = spy(new DefaultDataAccessStrategy( //
|
||||
new SqlGeneratorSource(context), //
|
||||
@@ -105,7 +110,7 @@ public class SimpleJdbcRepositoryEventsUnitTests {
|
||||
repository.delete(entity);
|
||||
|
||||
assertThat(publisher.events).extracting( //
|
||||
e -> (Class) e.getClass(), //
|
||||
JdbcEvent::getClass, //
|
||||
e -> e.getOptionalEntity().orElseGet(AssertionFailedError::new), //
|
||||
JdbcEvent::getId //
|
||||
).containsExactly( //
|
||||
@@ -140,8 +145,8 @@ public class SimpleJdbcRepositoryEventsUnitTests {
|
||||
assertThat(publisher.events) //
|
||||
.extracting(e -> (Class) e.getClass()) //
|
||||
.containsExactly( //
|
||||
AfterCreation.class, //
|
||||
AfterCreation.class //
|
||||
AfterLoadEvent.class, //
|
||||
AfterLoadEvent.class //
|
||||
);
|
||||
}
|
||||
|
||||
@@ -158,8 +163,8 @@ public class SimpleJdbcRepositoryEventsUnitTests {
|
||||
assertThat(publisher.events) //
|
||||
.extracting(e -> (Class) e.getClass()) //
|
||||
.containsExactly( //
|
||||
AfterCreation.class, //
|
||||
AfterCreation.class //
|
||||
AfterLoadEvent.class, //
|
||||
AfterLoadEvent.class //
|
||||
);
|
||||
}
|
||||
|
||||
@@ -174,7 +179,7 @@ public class SimpleJdbcRepositoryEventsUnitTests {
|
||||
assertThat(publisher.events) //
|
||||
.extracting(e -> (Class) e.getClass()) //
|
||||
.containsExactly( //
|
||||
AfterCreation.class //
|
||||
AfterLoadEvent.class //
|
||||
);
|
||||
}
|
||||
|
||||
@@ -198,12 +203,13 @@ public class SimpleJdbcRepositoryEventsUnitTests {
|
||||
|
||||
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {}
|
||||
|
||||
@Data
|
||||
@Value
|
||||
@RequiredArgsConstructor
|
||||
static class DummyEntity {
|
||||
private final @Id Long id;
|
||||
@Id Long id;
|
||||
}
|
||||
|
||||
static class FakePublisher implements ApplicationEventPublisher {
|
||||
static class CollectingEventPublisher implements ApplicationEventPublisher {
|
||||
|
||||
List<JdbcEvent> events = new ArrayList<>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user