From f98607f5dcbe3c20303cae73fbbbe1a64051c19f Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Mon, 29 Aug 2011 11:46:05 -0400 Subject: [PATCH] Add integration tests for mapping events --- .../index/MongoMappingEventPublisher.java | 6 + .../core/mapping/event/AfterSaveListener.java | 31 +++++ .../event/ApplicationContextEventTests.java | 118 ++++++++++++++++++ ...ApplicationContextEventTestsAppConfig.java | 53 ++++++++ .../event/PersonBeforeSaveListener.java | 32 +++++ .../event/SimpleMappingEventListener.java | 55 ++++++++ 6 files changed, 295 insertions(+) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AfterSaveListener.java create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTestsAppConfig.java create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/PersonBeforeSaveListener.java create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/SimpleMappingEventListener.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoMappingEventPublisher.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoMappingEventPublisher.java index 20f78323a..dea7f66b7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoMappingEventPublisher.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoMappingEventPublisher.java @@ -23,6 +23,12 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; /** + * An implementation of ApplicationEventPublisher that will only fire MappingContextEvents for use by the index creator when + * MongoTemplate is used 'stand-alone', that is not declared inside a Spring ApplicationContext. + * + * Declare MongoTemplate inside an ApplicationContext to enable the publishing of all persistence events such as + * {@link AfterLoadEvent}, {@link AfterSaveEvent}, etc. + * * @author Jon Brisbin */ public class MongoMappingEventPublisher implements ApplicationEventPublisher { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AfterSaveListener.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AfterSaveListener.java new file mode 100644 index 000000000..95b681a1a --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AfterSaveListener.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2011 by the original author(s). + * + * 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.mongodb.core.mapping.event; + +import java.util.ArrayList; + +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; + +public class AfterSaveListener implements ApplicationListener> { + + public final ArrayList seenEvents = new ArrayList(); + + public void onApplicationEvent(AfterSaveEvent event) { + this.seenEvents.add(event); + } + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java new file mode 100644 index 000000000..7cfdbf633 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2011 by the original author(s). + * + * 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.mongodb.core.mapping.event; + +import static org.junit.Assert.assertEquals; + +import java.net.UnknownHostException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.mapping.PersonPojoStringId; + +import com.mongodb.DB; +import com.mongodb.DBObject; +import com.mongodb.Mongo; +import com.mongodb.WriteConcern; + +/** + * Integration test for Mapping Events. + * + * @author Mark Pollack + */ +public class ApplicationContextEventTests { + + private final String[] collectionsToDrop = new String[] { "personPojoStringId" }; + + private ApplicationContext applicationContext; + private MongoTemplate template; + + @Before + public void setUp() throws Exception { + cleanDb(); + applicationContext = new AnnotationConfigApplicationContext(ApplicationContextEventTestsAppConfig.class); + template = applicationContext.getBean(MongoTemplate.class); + template.setWriteConcern(WriteConcern.FSYNC_SAFE); + } + + @After + public void cleanUp() throws Exception { + cleanDb(); + } + + private void cleanDb() throws UnknownHostException { + Mongo mongo = new Mongo(); + DB db = mongo.getDB("database"); + for (String coll : collectionsToDrop) { + db.getCollection(coll).drop(); + } + } + + @Test + @SuppressWarnings("unchecked") + public void beforeSaveEvent() { + PersonBeforeSaveListener personBeforeSaveListener = applicationContext.getBean(PersonBeforeSaveListener.class); + AfterSaveListener afterSaveListener = applicationContext.getBean(AfterSaveListener.class); + SimpleMappingEventListener simpleMappingEventListener = applicationContext.getBean(SimpleMappingEventListener.class); + + assertEquals(0, personBeforeSaveListener.seenEvents.size()); + assertEquals(0, afterSaveListener.seenEvents.size()); + + assertEquals(0, simpleMappingEventListener.onBeforeSaveEvents.size()); + assertEquals(0, simpleMappingEventListener.onAfterSaveEvents.size()); + + + PersonPojoStringId p = new PersonPojoStringId("1", "Text"); + template.insert(p); + + assertEquals(1, personBeforeSaveListener.seenEvents.size()); + assertEquals(1, afterSaveListener.seenEvents.size()); + + assertEquals(1, simpleMappingEventListener.onBeforeSaveEvents.size()); + assertEquals(1, simpleMappingEventListener.onAfterSaveEvents.size()); + + Assert.assertTrue(personBeforeSaveListener.seenEvents.get(0) instanceof BeforeSaveEvent); + Assert.assertTrue(afterSaveListener.seenEvents.get(0) instanceof AfterSaveEvent); + + BeforeSaveEvent beforeSaveEvent = (BeforeSaveEvent)personBeforeSaveListener.seenEvents.get(0); + PersonPojoStringId p2 = beforeSaveEvent.getSource(); + DBObject dbo = beforeSaveEvent.getDBObject(); + + comparePersonAndDbo(p, p2, dbo); + + AfterSaveEvent afterSaveEvent = (AfterSaveEvent)afterSaveListener.seenEvents.get(0); + Assert.assertTrue(afterSaveEvent.getSource() instanceof PersonPojoStringId); + p2 = (PersonPojoStringId)afterSaveEvent.getSource(); + dbo = beforeSaveEvent.getDBObject(); + + comparePersonAndDbo(p, p2, dbo); + + } + + private void comparePersonAndDbo(PersonPojoStringId p, PersonPojoStringId p2, DBObject dbo) { + assertEquals(p.getId(), p2.getId()); + assertEquals(p.getText(), p2.getText()); + + assertEquals("org.springframework.data.mongodb.core.mapping.PersonPojoStringId", dbo.get("_class")); + assertEquals("1", dbo.get("_id")); + assertEquals("Text", dbo.get("text")); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTestsAppConfig.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTestsAppConfig.java new file mode 100644 index 000000000..57811ad30 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTestsAppConfig.java @@ -0,0 +1,53 @@ +/* + * Copyright 2010-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.mongodb.core.mapping.event; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.config.AbstractMongoConfiguration; + +import com.mongodb.Mongo; + +@Configuration +public class ApplicationContextEventTestsAppConfig extends AbstractMongoConfiguration { + + @Override + public String getDatabaseName() { + return "database"; + } + + @Override + @Bean + public Mongo mongo() throws Exception { + return new Mongo("127.0.0.1"); + } + + @Bean + public PersonBeforeSaveListener personBeforeSaveListener() { + return new PersonBeforeSaveListener(); + } + + @Bean + public AfterSaveListener afterSaveListener() { + return new AfterSaveListener(); + } + + @Bean + public SimpleMappingEventListener simpleMappingEventListener() { + return new SimpleMappingEventListener(); + } + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/PersonBeforeSaveListener.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/PersonBeforeSaveListener.java new file mode 100644 index 000000000..6a234e275 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/PersonBeforeSaveListener.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2011 by the original author(s). + * + * 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.mongodb.core.mapping.event; + +import java.util.ArrayList; + +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.data.mongodb.core.mapping.PersonPojoStringId; + +public class PersonBeforeSaveListener implements ApplicationListener> { + + public final ArrayList seenEvents = new ArrayList(); + + public void onApplicationEvent(BeforeSaveEvent event) { + this.seenEvents.add(event); + } + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/SimpleMappingEventListener.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/SimpleMappingEventListener.java new file mode 100644 index 000000000..5483ea478 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/SimpleMappingEventListener.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2011 by the original author(s). + * + * 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.mongodb.core.mapping.event; + +import java.util.ArrayList; + +import com.mongodb.DBObject; + + +public class SimpleMappingEventListener extends AbstractMappingEventListener, Object> { + + public final ArrayList> onBeforeConvertEvents = new ArrayList>(); + public final ArrayList> onBeforeSaveEvents = new ArrayList>(); + public final ArrayList> onAfterSaveEvents = new ArrayList>(); + public final ArrayList> onAfterLoadEvents = new ArrayList>(); + public final ArrayList> onAfterConvertEvents = new ArrayList>(); + + @Override + public void onBeforeConvert(Object source) { + onBeforeConvertEvents.add(new BeforeConvertEvent(source)); + } + + @Override + public void onBeforeSave(Object source, DBObject dbo) { + onBeforeSaveEvents.add(new BeforeSaveEvent(source, dbo)); + } + + @Override + public void onAfterSave(Object source, DBObject dbo) { + onAfterSaveEvents.add(new AfterSaveEvent(source, dbo)); + } + + @Override + public void onAfterLoad(DBObject dbo) { + onAfterLoadEvents.add(new AfterLoadEvent(dbo)); + } + + @Override + public void onAfterConvert(DBObject dbo, Object source) { + onAfterConvertEvents.add(new AfterConvertEvent(dbo, source)); + } +}