Browse Source
The repository publishes events before and after inserting, updating and deleting entities, as well as after instantiation of entities. JdbcEvent ist the common super class of all events and makes the id and the entity available (if possible). Added issue id comments to tests from previous issues. Original pull request: #5.pull/6/merge
18 changed files with 742 additions and 62 deletions
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.mapping.event; |
||||
|
||||
import java.util.function.Function; |
||||
|
||||
/** |
||||
* gets published after instantiation and setting of all the properties of an entity. This allows to do some |
||||
* postprocessing of entities. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class AfterCreationEvent extends JdbcEvent{ |
||||
|
||||
/** |
||||
* @param instance the newly instantiated entity. |
||||
* @param idProvider a function providing the id, for the instance. |
||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
||||
*/ |
||||
public <T> AfterCreationEvent(T instance, Function<T, Object> idProvider) { |
||||
super(instance, idProvider); |
||||
} |
||||
} |
||||
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.mapping.event; |
||||
|
||||
import java.util.function.Function; |
||||
|
||||
/** |
||||
* get published after deletion of an entity. The source might contain the Id or the actual entity, depending on the |
||||
* {@code delete(...)} method used. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class AfterDeleteEvent extends JdbcEvent{ |
||||
|
||||
/** |
||||
* @param instance the deleted entity. |
||||
* @param idProvider a function providing the id, for the instance. |
||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
||||
*/ |
||||
public <T> AfterDeleteEvent(T instance, Function<T, Object> idProvider) { |
||||
super(instance, idProvider); |
||||
} |
||||
} |
||||
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.mapping.event; |
||||
|
||||
import java.util.function.Function; |
||||
|
||||
/** |
||||
* gets published after an entity got inserted into the database. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class AfterInsertEvent extends AfterSaveEvent { |
||||
|
||||
/** |
||||
* @param instance the newly inserted entity. |
||||
* @param idProvider a function providing the id, for the instance. |
||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
||||
*/ |
||||
public <T> AfterInsertEvent(T instance, Function<T, Object> idProvider) { |
||||
super(instance, idProvider); |
||||
} |
||||
} |
||||
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.mapping.event; |
||||
|
||||
import java.util.function.Function; |
||||
|
||||
/** |
||||
* subclasses of this get published after a new instance or a changed instance was saved in the database |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class AfterSaveEvent extends JdbcEvent{ |
||||
|
||||
/** |
||||
* @param instance the newly saved entity. |
||||
* @param idProvider a function providing the id, for the instance. |
||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
||||
*/ |
||||
<T> AfterSaveEvent(T instance, Function<T, Object> idProvider) { |
||||
super(instance, idProvider); |
||||
} |
||||
} |
||||
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.mapping.event; |
||||
|
||||
import java.util.function.Function; |
||||
|
||||
/** |
||||
* gets published after an entity was updated in the database. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class AfterUpdateEvent extends AfterSaveEvent { |
||||
|
||||
/** |
||||
* @param instance the updated entity. |
||||
* @param idProvider a function providing the id, for the instance. |
||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
||||
*/ |
||||
public <T> AfterUpdateEvent(T instance, Function<T, Object> idProvider) { |
||||
super(instance, idProvider); |
||||
} |
||||
} |
||||
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.mapping.event; |
||||
|
||||
import java.util.function.Function; |
||||
|
||||
import org.springframework.context.ApplicationEvent; |
||||
|
||||
/** |
||||
* gets published when an entity is about to get deleted. {@link ApplicationEvent#getSource()} might contain either the |
||||
* entity or the id of the entity, depending on which delete method was used. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class BeforeDeleteEvent extends JdbcEvent { |
||||
|
||||
/** |
||||
* @param instance the entity about to get deleted. Might be {@literal NULL} |
||||
* @param idProvider a function providing the id, for the instance. Must provide a not {@literal NULL} id, when called with {@link #instance} |
||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
||||
*/ |
||||
public <T> BeforeDeleteEvent(T instance, Function<T, Object> idProvider) { |
||||
super(instance, idProvider); |
||||
} |
||||
} |
||||
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.mapping.event; |
||||
|
||||
import java.util.function.Function; |
||||
|
||||
/** |
||||
* gets published before an entity gets inserted into the database. |
||||
* |
||||
* When the id-property of the entity must get set manually, an event listener for this event may do so. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class BeforeInsertEvent extends BeforeSaveEvent { |
||||
|
||||
/** |
||||
* @param instance the entity about to get inserted. |
||||
* @param idProvider a function providing the id, for the instance. |
||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
||||
*/ |
||||
public <T> BeforeInsertEvent(T instance, Function<T, Object> idProvider) { |
||||
super(instance, idProvider); |
||||
} |
||||
} |
||||
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.mapping.event; |
||||
|
||||
import java.util.function.Function; |
||||
|
||||
import org.springframework.context.ApplicationEvent; |
||||
|
||||
/** |
||||
* subclasses of this get published before an entity gets saved to the database. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class BeforeSaveEvent extends JdbcEvent { |
||||
|
||||
/** |
||||
* @param instance the entity about to get saved. |
||||
* @param idProvider a function providing the id, for the instance. |
||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
||||
*/ |
||||
<T> BeforeSaveEvent(T instance, Function<T, Object> idProvider) { |
||||
super(instance, idProvider); |
||||
} |
||||
} |
||||
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.mapping.event; |
||||
|
||||
import java.util.function.Function; |
||||
|
||||
/** |
||||
* gets published before an entity gets updated in the database. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class BeforeUpdateEvent extends BeforeSaveEvent { |
||||
|
||||
/** |
||||
* @param instance the entity about to get saved. |
||||
* @param idProvider a function providing the id, for the instance. |
||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
||||
*/ |
||||
public <T> BeforeUpdateEvent(T instance, Function<T, Object> idProvider) { |
||||
super(instance, idProvider); |
||||
} |
||||
} |
||||
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.mapping.event; |
||||
|
||||
import java.util.function.Function; |
||||
|
||||
import org.springframework.context.ApplicationEvent; |
||||
|
||||
/** |
||||
* is the common superclass for all events published by JDBC repositories. |
||||
* |
||||
* It is recommendet not to use the {@link #getSource()} since it may contain the entity if it was available, when the |
||||
* event was published, or in case of delete events only the Id. |
||||
* |
||||
* Use the dedicated methods {@link #getId()} or {@link #getInstance()} instead. Note that the later might be |
||||
* {@literal NULL} in the cases mentioned above. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class JdbcEvent extends ApplicationEvent { |
||||
|
||||
private final Object id; |
||||
private final Object instance; |
||||
|
||||
<T> JdbcEvent(T instance, Function<T, Object> idProvider) { |
||||
|
||||
super(instance == null ? idProvider.apply(instance) : instance); |
||||
this.instance = instance; |
||||
this.id = idProvider.apply(instance); |
||||
} |
||||
|
||||
/** |
||||
* the entity for which this event was publish. Might be {@literal NULL} in cases of delete events where only the id |
||||
* was provided to the delete method. |
||||
* |
||||
* @return instance of the entity triggering this event. |
||||
*/ |
||||
public Object getInstance() { |
||||
return instance; |
||||
} |
||||
|
||||
/** |
||||
* the id of the entity, triggering this event. Guaranteed not to be {@literal NULL}. |
||||
* @return |
||||
*/ |
||||
public Object getId() { |
||||
return id; |
||||
} |
||||
} |
||||
@ -0,0 +1,59 @@
@@ -0,0 +1,59 @@
|
||||
/* |
||||
* Copyright 2017 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.jdbc.repository; |
||||
|
||||
import java.sql.ResultSet; |
||||
import java.sql.SQLException; |
||||
|
||||
import org.springframework.context.ApplicationEventPublisher; |
||||
import org.springframework.data.jdbc.mapping.event.AfterCreationEvent; |
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity; |
||||
import org.springframework.jdbc.core.RowMapper; |
||||
|
||||
/** |
||||
* a RowMapper that publishes events after a delegate, did the actual work of mapping a {@link ResultSet} to an entity. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class EventPublishingEntityRowMapper<T> implements RowMapper<T> { |
||||
|
||||
private final RowMapper<T> delegate; |
||||
private final JdbcPersistentEntity<T> entity; |
||||
private final ApplicationEventPublisher publisher; |
||||
|
||||
/** |
||||
* |
||||
* @param delegate does the actuall mapping. |
||||
* @param entity provides functionality to create ids from entities |
||||
* @param publisher used for event publishing after the mapping. |
||||
*/ |
||||
EventPublishingEntityRowMapper(RowMapper<T> delegate,JdbcPersistentEntity<T> entity, ApplicationEventPublisher publisher) { |
||||
|
||||
this.delegate = delegate; |
||||
this.entity = entity; |
||||
this.publisher = publisher; |
||||
} |
||||
|
||||
@Override |
||||
public T mapRow(ResultSet resultSet, int i) throws SQLException { |
||||
|
||||
T instance = delegate.mapRow(resultSet, i); |
||||
|
||||
publisher.publishEvent(new AfterCreationEvent(instance, entity::getIdValue)); |
||||
|
||||
return instance; |
||||
} |
||||
} |
||||
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
package org.springframework.data.jdbc.repository; |
||||
|
||||
import static org.mockito.Mockito.*; |
||||
|
||||
import java.sql.ResultSet; |
||||
import java.sql.SQLException; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.context.ApplicationEventPublisher; |
||||
import org.springframework.data.annotation.Id; |
||||
import org.springframework.data.jdbc.mapping.event.AfterCreationEvent; |
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity; |
||||
import org.springframework.jdbc.core.RowMapper; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class EventPublishingEntityRowMapperTest { |
||||
|
||||
private RowMapper rowMapperDelegate = mock(RowMapper.class); |
||||
private JdbcPersistentEntity<DummyEntity> entity = mock(JdbcPersistentEntity.class); |
||||
private ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class); |
||||
|
||||
@Test // DATAJDBC-99
|
||||
public void eventGetsPublishedAfterInstantiation() throws SQLException { |
||||
|
||||
when(entity.getIdValue(any())).thenReturn(1L); |
||||
|
||||
EventPublishingEntityRowMapper<DummyEntity> rowMapper = new EventPublishingEntityRowMapper<>( |
||||
rowMapperDelegate, |
||||
entity, |
||||
publisher); |
||||
|
||||
ResultSet resultSet = mock(ResultSet.class); |
||||
rowMapper.mapRow(resultSet, 1); |
||||
|
||||
verify(publisher).publishEvent(isA(AfterCreationEvent.class)); |
||||
} |
||||
|
||||
@Data |
||||
private static class DummyEntity { |
||||
|
||||
@Id private final Long Id; |
||||
} |
||||
} |
||||
@ -0,0 +1,116 @@
@@ -0,0 +1,116 @@
|
||||
package org.springframework.data.jdbc.repository; |
||||
|
||||
import static java.util.Arrays.*; |
||||
import static org.junit.Assert.*; |
||||
import static org.mockito.Mockito.*; |
||||
import static org.springframework.util.Assert.*; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.context.ApplicationEventPublisher; |
||||
import org.springframework.data.annotation.Id; |
||||
import org.springframework.data.jdbc.mapping.event.AfterDeleteEvent; |
||||
import org.springframework.data.jdbc.mapping.event.AfterInsertEvent; |
||||
import org.springframework.data.jdbc.mapping.event.AfterUpdateEvent; |
||||
import org.springframework.data.jdbc.mapping.event.BeforeDeleteEvent; |
||||
import org.springframework.data.jdbc.mapping.event.BeforeInsertEvent; |
||||
import org.springframework.data.jdbc.mapping.event.BeforeUpdateEvent; |
||||
import org.springframework.data.jdbc.mapping.event.JdbcEvent; |
||||
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; |
||||
import org.springframework.data.repository.CrudRepository; |
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class SimpleJdbcRepositoryEventsUnitTests { |
||||
|
||||
private FakePublisher publisher = new FakePublisher(); |
||||
|
||||
private DummyEntityRepository repository; |
||||
|
||||
@Before |
||||
public void before() { |
||||
JdbcRepositoryFactory factory = new JdbcRepositoryFactory(publisher, mock(NamedParameterJdbcOperations.class)); |
||||
repository = factory.getRepository(DummyEntityRepository.class); |
||||
} |
||||
|
||||
@Test // DATAJDBC-99
|
||||
public void publishesEventsOnSave() { |
||||
|
||||
DummyEntity entity = new DummyEntity(23L); |
||||
|
||||
repository.save(entity); |
||||
|
||||
isInstanceOf(BeforeUpdateEvent.class, publisher.events.get(0)); |
||||
isInstanceOf(AfterUpdateEvent.class, publisher.events.get(1)); |
||||
} |
||||
|
||||
@Test // DATAJDBC-99
|
||||
public void publishesEventsOnSaveMany() { |
||||
|
||||
DummyEntity entity1 = new DummyEntity(null); |
||||
DummyEntity entity2 = new DummyEntity(23L); |
||||
|
||||
repository.save(asList(entity1, entity2)); |
||||
|
||||
isInstanceOf(BeforeInsertEvent.class, publisher.events.get(0)); |
||||
isInstanceOf(AfterInsertEvent.class, publisher.events.get(1)); |
||||
isInstanceOf(BeforeUpdateEvent.class, publisher.events.get(2)); |
||||
isInstanceOf(AfterUpdateEvent.class, publisher.events.get(3)); |
||||
} |
||||
|
||||
|
||||
@Test // DATAJDBC-99
|
||||
public void publishesEventsOnDelete() { |
||||
|
||||
DummyEntity entity = new DummyEntity(23L); |
||||
|
||||
repository.delete(entity); |
||||
|
||||
isInstanceOf(BeforeDeleteEvent.class, publisher.events.get(0)); |
||||
isInstanceOf(AfterDeleteEvent.class, publisher.events.get(1)); |
||||
|
||||
assertEquals(entity, publisher.events.get(0).getInstance()); |
||||
assertEquals(entity, publisher.events.get(1).getInstance()); |
||||
|
||||
assertEquals(23L, publisher.events.get(0).getId()); |
||||
assertEquals(23L, publisher.events.get(1).getId()); |
||||
} |
||||
|
||||
|
||||
@Test // DATAJDBC-99
|
||||
public void publishesEventsOnDeleteById() { |
||||
|
||||
repository.delete(23L); |
||||
|
||||
isInstanceOf(BeforeDeleteEvent.class, publisher.events.get(0)); |
||||
isInstanceOf(AfterDeleteEvent.class, publisher.events.get(1)); |
||||
} |
||||
|
||||
|
||||
@Data |
||||
private static class DummyEntity { |
||||
|
||||
@Id private final Long id; |
||||
} |
||||
|
||||
private interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> { |
||||
|
||||
} |
||||
|
||||
static class FakePublisher implements ApplicationEventPublisher { |
||||
|
||||
List<JdbcEvent> events = new ArrayList<>(); |
||||
|
||||
@Override |
||||
public void publishEvent(Object o) { |
||||
events.add((JdbcEvent) o); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue