More tests for DATADOC-155 - Need to support plain POJOs with non-ObjectId-compatible ID properties
This commit is contained in:
@@ -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<PersonPojo> result = template.find(new Query(Criteria.where("ssn").is(12345)), PersonPojo.class);
|
||||
List<PersonWithObjectId> 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<PersonPojo> results = template.find(new Query().or(one, two), PersonPojo.class);
|
||||
List<PersonWithObjectId> 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() {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 <jbrisbin@vmware.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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 <jbrisbin@vmware.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,11 @@ import org.bson.types.ObjectId;
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user