DATAGRAPH-365 : before and after delete events

This commit is contained in:
Nicki Watt
2013-09-02 14:41:30 +01:00
parent 4f7759270f
commit 9208a49b8c
7 changed files with 303 additions and 15 deletions

View File

@@ -0,0 +1,22 @@
/**
* Copyright 2011 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.neo4j.lifecycle;
public class AfterDeleteEvent<T> extends DeleteEvent<T> {
public AfterDeleteEvent(Object source, T entity) {
super(source, entity);
}
}

View File

@@ -0,0 +1,22 @@
/**
* Copyright 2011 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.neo4j.lifecycle;
public class BeforeDeleteEvent<T> extends Neo4jLifecycleEvent<T> {
public BeforeDeleteEvent(Object source, T entity) {
super(source, entity);
}
}

View File

@@ -5,7 +5,7 @@
* 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
* 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,
@@ -15,8 +15,14 @@
*/
package org.springframework.data.neo4j.lifecycle;
/**
* @deprecated Rather use the AfterDeleteEvent
* @param <T>
*/
@Deprecated
public class DeleteEvent<T> extends Neo4jLifecycleEvent<T> {
public DeleteEvent(Object source, T entity) {
super(source, entity);
}
}

View File

@@ -39,9 +39,10 @@ import org.springframework.data.neo4j.core.GraphDatabase;
import org.springframework.data.neo4j.core.TypeRepresentationStrategy;
import org.springframework.data.neo4j.core.UncategorizedGraphStoreException;
import org.springframework.data.neo4j.fieldaccess.GraphBackedEntityIterableWrapper;
import org.springframework.data.neo4j.lifecycle.AfterDeleteEvent;
import org.springframework.data.neo4j.lifecycle.AfterSaveEvent;
import org.springframework.data.neo4j.lifecycle.BeforeDeleteEvent;
import org.springframework.data.neo4j.lifecycle.BeforeSaveEvent;
import org.springframework.data.neo4j.lifecycle.DeleteEvent;
import org.springframework.data.neo4j.mapping.IndexInfo;
import org.springframework.data.neo4j.mapping.MappingPolicy;
import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty;
@@ -219,9 +220,9 @@ public class Neo4jTemplate implements Neo4jOperations, ApplicationContextAware {
@Override
public void delete(final Object entity) {
infrastructure.getEntityRemover().remove(entity);
if (applicationContext != null) applicationContext.publishEvent(new DeleteEvent<Object>(this, entity));
if (applicationContext != null) applicationContext.publishEvent(new BeforeDeleteEvent<Object>(this, entity));
infrastructure.getEntityRemover().remove(entity);
if (applicationContext != null) applicationContext.publishEvent(new AfterDeleteEvent<Object>(this, entity));
}
/**

View File

@@ -0,0 +1,107 @@
/**
* Copyright 2011 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.neo4j.lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.neo4j.support.node.Neo4jHelper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
public class AfterDeleteEventTests {
@Configuration
@EnableNeo4jRepositories
static class TestConfig extends Neo4jConfiguration {
@Bean
GraphDatabaseService graphDatabaseService() {
return new TestGraphDatabaseFactory().newImpermanentDatabase();
}
@Bean
ApplicationListener<AfterDeleteEvent<Program>> afterDeleteEventApplicationListener() {
return new ApplicationListener<AfterDeleteEvent<Program>>() {
@Override
public void onApplicationEvent(AfterDeleteEvent<Program> event) {
afterProgramDeleteEvents.add(event.getEntity());
lastEvent = Event.AFTER_DELETE;
}
};
}
}
@Autowired
Neo4jTemplate template;
@Autowired
GraphDatabaseService graphDatabaseService;
enum Event { NONE, BEFORE_DELETE, AFTER_DELETE }
static Event lastEvent = Event.NONE;
static List<Program> afterProgramDeleteEvents = new ArrayList<Program>();
@BeforeTransaction
public void beforeTransaction() {
Neo4jHelper.cleanDb(template);
}
@Before
public void before() {
lastEvent = Event.NONE;
afterProgramDeleteEvents.clear();
}
@Test
public void shouldFireAfterEntityIsDeleted() throws Exception {
assertEquals(Event.NONE, lastEvent);
assertThat(afterProgramDeleteEvents, hasSize(0));
Program sark = template.save(new Program("Sark"));
template.delete(sark);
assertThat(afterProgramDeleteEvents, hasSize(1));
assertEquals(Event.AFTER_DELETE, lastEvent);
}
}

View File

@@ -0,0 +1,124 @@
/**
* Copyright 2011 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.neo4j.lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.neo4j.support.node.Neo4jHelper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
public class BeforeDeleteEventTests {
@Configuration
@EnableNeo4jRepositories
static class TestConfig extends Neo4jConfiguration {
@Bean
GraphDatabaseService graphDatabaseService() {
return new TestGraphDatabaseFactory().newImpermanentDatabase();
}
@Bean
ApplicationListener<BeforeDeleteEvent<Program>> beforeDeleteEventApplicationListener() {
return new ApplicationListener<BeforeDeleteEvent<Program>>() {
@Override
public void onApplicationEvent(BeforeDeleteEvent<Program> event) {
beforeProgramDeleteEvents.add(event.getEntity());
lastEvent = Event.BEFORE_DELETE;
}
};
}
}
@Autowired
Neo4jTemplate template;
@Autowired
GraphDatabaseService graphDatabaseService;
enum Event { NONE, BEFORE_DELETE, AFTER_DELETE }
static Event lastEvent = Event.NONE;
static List<Program> beforeProgramDeleteEvents = new ArrayList<Program>();
@BeforeTransaction
public void beforeTransaction() {
Neo4jHelper.cleanDb(template);
}
@Before
public void before() {
lastEvent = Event.NONE;
beforeProgramDeleteEvents.clear();
}
@Test
public void shouldFireBeforeEntityIsDeleted() throws Exception {
assertEquals(Event.NONE, lastEvent);
assertThat(beforeProgramDeleteEvents, hasSize(0));
Program sark = template.save(new Program("Sark"));
template.delete(sark);
assertThat(beforeProgramDeleteEvents, hasSize(1));
assertEquals(Event.BEFORE_DELETE, lastEvent);
}
}
@NodeEntity
class Program {
@GraphId
Long id;
String name;
Program() {
}
public Program(String name) {
this.name = name;
}
}

View File

@@ -5,7 +5,7 @@
* 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
* 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,
@@ -40,21 +40,27 @@ import java.util.LinkedList;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.hasItem;
@Deprecated
@NodeEntity
class Program {
class DeprecatedProgram {
@GraphId
Long id;
String name;
Program() {
DeprecatedProgram() {
}
public Program(String name) {
public DeprecatedProgram(String name) {
this.name = name;
}
}
// This test should be deprecated moving forward as it is replaced by
// BeforeAndAfterDeleteEventTests, however as we are leaving the
// DeleteEvent class for backwards compatibility, we leave this test
// here too to ensure we don't break anything.
@Deprecated
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
@@ -68,10 +74,10 @@ public class DeleteEventTests {
}
@Bean
ApplicationListener<DeleteEvent<Program>> deleteEventApplicationListener() {
return new ApplicationListener<DeleteEvent<Program>>() {
ApplicationListener<DeleteEvent<DeprecatedProgram>> deleteEventApplicationListener() {
return new ApplicationListener<DeleteEvent<DeprecatedProgram>>() {
@Override
public void onApplicationEvent(DeleteEvent<Program> event) {
public void onApplicationEvent(DeleteEvent<DeprecatedProgram> event) {
deletions.add(event.getEntity());
}
};
@@ -84,7 +90,7 @@ public class DeleteEventTests {
@Autowired
GraphDatabaseService graphDatabaseService;
static final LinkedList<Program> deletions = new LinkedList<Program>();
static final LinkedList<DeprecatedProgram> deletions = new LinkedList<DeprecatedProgram>();
@BeforeTransaction
public void beforeTransaction() {
@@ -98,10 +104,10 @@ public class DeleteEventTests {
@Test
public void shouldFireEventOnNodeDeletion() throws Exception {
Program sark = template.save(new Program("Sark"));
DeprecatedProgram sark = template.save(new DeprecatedProgram("Sark"));
template.delete(sark);
assertThat(deletions, hasItem(sark));
}
}
}