From b4236bdd78bd6f421fb5a39543243e34154fa743 Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Tue, 31 May 2011 15:43:57 -0400 Subject: [PATCH] More tests for DATADOC-155 - Need to support plain POJOs with non-ObjectId-compatible ID properties --- .../mongodb/mapping/MappingTests.java | 69 ++++++++++++++++--- .../mongodb/mapping/PersonPojoIntId.java | 6 +- .../mongodb/mapping/PersonPojoLongId.java | 43 ++++++++++++ .../mongodb/mapping/PersonPojoStringId.java | 43 ++++++++++++ ...ersonPojo.java => PersonWithObjectId.java} | 4 +- 5 files changed, 152 insertions(+), 13 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoLongId.java create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoStringId.java rename spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/{PersonPojo.java => PersonWithObjectId.java} (87%) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java index ace04840d..dff70e4b7 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java @@ -35,6 +35,7 @@ import com.mongodb.MongoException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -55,7 +56,10 @@ public class MappingTests { private final String[] collectionsToDrop = new String[]{ MongoCollectionUtils.getPreferredCollectionName(Person.class), MongoCollectionUtils.getPreferredCollectionName(PersonMapProperty.class), - MongoCollectionUtils.getPreferredCollectionName(PersonPojo.class), + MongoCollectionUtils.getPreferredCollectionName(PersonWithObjectId.class), + MongoCollectionUtils.getPreferredCollectionName(PersonPojoIntId.class), + MongoCollectionUtils.getPreferredCollectionName(PersonPojoLongId.class), + //MongoCollectionUtils.getPreferredCollectionName(PersonPojoStringId.class), MongoCollectionUtils.getPreferredCollectionName(PersonCustomIdName.class), MongoCollectionUtils.getPreferredCollectionName(PersonMultiDimArrays.class), MongoCollectionUtils.getPreferredCollectionName(PersonMultiCollection.class), @@ -94,13 +98,13 @@ public class MappingTests { public void testPersonPojo() throws Exception { LOGGER.info("about to create new personpojo"); - PersonPojo p = new PersonPojo(12345, "Person", "Pojo"); + PersonWithObjectId p = new PersonWithObjectId(12345, "Person", "Pojo"); LOGGER.info("about to insert"); template.insert(p); LOGGER.info("done inserting"); assertNotNull(p.getId()); - List result = template.find(new Query(Criteria.where("ssn").is(12345)), PersonPojo.class); + List result = template.find(new Query(Criteria.where("ssn").is(12345)), PersonWithObjectId.class); assertThat(result.size(), is(1)); assertThat(result.get(0).getSsn(), is(12345)); } @@ -347,14 +351,14 @@ public class MappingTests { @Test public void testOrQuery() { - PersonPojo p1 = new PersonPojo(1, "first", ""); + PersonWithObjectId p1 = new PersonWithObjectId(1, "first", ""); template.save(p1); - PersonPojo p2 = new PersonPojo(2, "second", ""); + PersonWithObjectId p2 = new PersonWithObjectId(2, "second", ""); template.save(p2); Query one = query(where("ssn").is(1)); Query two = query(where("ssn").is(2)); - List results = template.find(new Query().or(one, two), PersonPojo.class); + List results = template.find(new Query().or(one, two), PersonWithObjectId.class); assertNotNull(results); assertThat(results.size(), is(2)); @@ -373,14 +377,63 @@ public class MappingTests { } @Test - public void testNoMappingAnnotations() { + public void testNoMappingAnnotationsUsingIntAsId() { PersonPojoIntId p = new PersonPojoIntId(1, "Text"); - template.save(p); + template.insert(p); template.updateFirst(PersonPojoIntId.class, query(where("id").is(1)), update("text", "New Text")); PersonPojoIntId p2 = template.findOne(query(where("id").is(1)), PersonPojoIntId.class); assertEquals("New Text", p2.getText()); + + p.setText("Different Text"); + template.save(p); + + PersonPojoIntId p3 = template.findOne(query(where("id").is(1)), PersonPojoIntId.class); + assertEquals("Different Text", p3.getText()); + } + + @Test + public void testNoMappingAnnotationsUsingLongAsId() { + PersonPojoLongId p = new PersonPojoLongId(1, "Text"); + template.insert(p); + template.updateFirst(PersonPojoLongId.class, query(where("id").is(1)), + update("text", "New Text")); + + PersonPojoLongId p2 = template.findOne(query(where("id").is(1)), + PersonPojoLongId.class); + assertEquals("New Text", p2.getText()); + + p.setText("Different Text"); + template.save(p); + + PersonPojoLongId p3 = template.findOne(query(where("id").is(1)), + PersonPojoLongId.class); + assertEquals("Different Text", p3.getText()); + + } + + @Test + @Ignore("DATADOC-155 - To be investigated") + public void testNoMappingAnnotationsUsingStringAsId() { + //Assign the String Id in code + PersonPojoStringId p = new PersonPojoStringId("1", "Text"); + template.insert(p); + template.updateFirst(PersonPojoLongId.class, query(where("id").is("1")), + update("text", "New Text")); + + PersonPojoStringId p2 = template.findOne(query(where("id").is("1")), + PersonPojoStringId.class); + assertEquals("New Text", p2.getText()); + + p.setText("Different Text"); + template.save(p); + + PersonPojoStringId p3 = template.findOne(query(where("id").is("1")), + PersonPojoStringId.class); + assertEquals("Different Text", p3.getText()); + + } // @Test // public void testThroughput() { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoIntId.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoIntId.java index a59d9525b..87a767782 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoIntId.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoIntId.java @@ -21,15 +21,15 @@ package org.springframework.data.document.mongodb.mapping; */ public class PersonPojoIntId { - private Integer id; + private int id; private String text; - public PersonPojoIntId(Integer id, String text) { + public PersonPojoIntId(int id, String text) { this.id = id; this.text = text; } - public Integer getId() { + public int getId() { return id; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoLongId.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoLongId.java new file mode 100644 index 000000000..61d173a6c --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoLongId.java @@ -0,0 +1,43 @@ +/* + * 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.document.mongodb.mapping; + +/** + * @author Jon Brisbin + */ +public class PersonPojoLongId { + + private long id; + private String text; + + public PersonPojoLongId(long id, String text) { + this.id = id; + this.text = text; + } + + public long getId() { + return id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoStringId.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoStringId.java new file mode 100644 index 000000000..0d92597f1 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoStringId.java @@ -0,0 +1,43 @@ +/* + * 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.document.mongodb.mapping; + +/** + * @author Jon Brisbin + */ +public class PersonPojoStringId { + + private String id; + private String text; + + public PersonPojoStringId(String id, String text) { + this.id = id; + this.text = text; + } + + public String getId() { + return id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojo.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonWithObjectId.java similarity index 87% rename from spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojo.java rename to spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonWithObjectId.java index dbf4d6b5f..5169bc941 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojo.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonWithObjectId.java @@ -21,11 +21,11 @@ import org.bson.types.ObjectId; /** * @author Jon Brisbin */ -public class PersonPojo extends BasePerson { +public class PersonWithObjectId extends BasePerson { private ObjectId id; - public PersonPojo(Integer ssn, String firstName, String lastName) { + public PersonWithObjectId(Integer ssn, String firstName, String lastName) { super(ssn, firstName, lastName); }