Trying to fix merge conflicts.
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,6 @@
|
||||
.DS_Store
|
||||
*.iml
|
||||
*.ipr
|
||||
target
|
||||
.springBeans
|
||||
.ant-targets-build.xml
|
||||
|
||||
@@ -1,161 +1,166 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-document-parent</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../spring-data-document-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
<artifactId>spring-data-couchdb</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Spring Data CouchDB Support</name>
|
||||
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<id>tareq.abedrabbo</id>
|
||||
<name>Tareq Abedrabbo</name>
|
||||
<email>tareq.abedrabbo@opencredo.com</email>
|
||||
<organization>OpenCredo</organization>
|
||||
<organizationUrl>http://www.opencredo.org</organizationUrl>
|
||||
<roles>
|
||||
<role>Project Admin</role>
|
||||
<role>Developer</role>
|
||||
</roles>
|
||||
<timezone>+0</timezone>
|
||||
</developer>
|
||||
<developer>
|
||||
<id>tomas.lukosius</id>
|
||||
<name>Tomas Lukosius</name>
|
||||
<email>tomas.lukosius@opencredo.com</email>
|
||||
<organization>OpenCredo</organization>
|
||||
<organizationUrl>http://www.opencredo.org</organizationUrl>
|
||||
<roles>
|
||||
<role>Project Admin</role>
|
||||
<role>Developer</role>
|
||||
</roles>
|
||||
<timezone>+0</timezone>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<dependencies>
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-document-parent</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../spring-data-document-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
<artifactId>spring-data-couchdb</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Spring Data CouchDB Support</name>
|
||||
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<!-- Spring Data -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-document-core</artifactId>
|
||||
</dependency>
|
||||
<developers>
|
||||
<developer>
|
||||
<id>tareq.abedrabbo</id>
|
||||
<name>Tareq Abedrabbo</name>
|
||||
<email>tareq.abedrabbo@opencredo.com</email>
|
||||
<organization>OpenCredo</organization>
|
||||
<organizationUrl>http://www.opencredo.org</organizationUrl>
|
||||
<roles>
|
||||
<role>Project Admin</role>
|
||||
<role>Developer</role>
|
||||
</roles>
|
||||
<timezone>+0</timezone>
|
||||
</developer>
|
||||
<developer>
|
||||
<id>tomas.lukosius</id>
|
||||
<name>Tomas Lukosius</name>
|
||||
<email>tomas.lukosius@opencredo.com</email>
|
||||
<organization>OpenCredo</organization>
|
||||
<organizationUrl>http://www.opencredo.org</organizationUrl>
|
||||
<roles>
|
||||
<role>Project Admin</role>
|
||||
<role>Developer</role>
|
||||
</roles>
|
||||
<timezone>+0</timezone>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<!-- Jackson JSON -->
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jackson</groupId>
|
||||
<artifactId>jackson-core-asl</artifactId>
|
||||
<version>1.6.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jackson</groupId>
|
||||
<artifactId>jackson-mapper-asl</artifactId>
|
||||
<version>1.6.1</version>
|
||||
</dependency>
|
||||
<dependencies>
|
||||
|
||||
<!-- Logging -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>mail</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>javax.jms</groupId>
|
||||
<artifactId>jms</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>com.sun.jdmk</groupId>
|
||||
<artifactId>jmxtools</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>com.sun.jmx</groupId>
|
||||
<artifactId>jmxri</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.annotation</groupId>
|
||||
<artifactId>jsr250-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>1.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- Jackson JSON -->
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jackson</groupId>
|
||||
<artifactId>jackson-core-asl</artifactId>
|
||||
<version>1.6.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jackson</groupId>
|
||||
<artifactId>jackson-mapper-asl</artifactId>
|
||||
<version>1.6.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Logging -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>mail</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>javax.jms</groupId>
|
||||
<artifactId>jms</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>com.sun.jdmk</groupId>
|
||||
<artifactId>jmxtools</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>com.sun.jmx</groupId>
|
||||
<artifactId>jmxri</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.annotation</groupId>
|
||||
<artifactId>jsr250-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>1.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Couch DB -->
|
||||
<dependency>
|
||||
<groupId>com.google.code.jcouchdb</groupId>
|
||||
<artifactId>jcouchdb</artifactId>
|
||||
<version>0.11.0-1</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.springsource.bundlor</groupId>
|
||||
<artifactId>com.springsource.bundlor.maven</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</dependencies>
|
||||
<!--
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.springsource.bundlor</groupId>
|
||||
<artifactId>com.springsource.bundlor.maven</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
-->
|
||||
</project>
|
||||
|
||||
@@ -1,351 +1,350 @@
|
||||
/*
|
||||
* 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.document.mongodb;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.document.mongodb.SomeEnumTest.NumberEnum;
|
||||
import org.springframework.data.document.mongodb.SomeEnumTest.StringEnum;
|
||||
import org.springframework.data.document.mongodb.convert.SimpleMongoConverter;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.util.JSON;
|
||||
|
||||
public class SimpleMongoConverterTests {
|
||||
|
||||
static final String SIMPLE_JSON = "{ \"map\" : { \"foo\" : 3 , \"bar\" : 4}, \"number\" : 15 }";
|
||||
static final String COMPLEX_JSON = "{ \"map\" : { \"trade\" : { \"orderType\" : \"BUY\" , \"price\" : 90.5 , \"quantity\" : 0 , \"ticker\" : \"VMW\"}}}";
|
||||
|
||||
SimpleMongoConverter converter;
|
||||
DBObject object;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
converter = new SimpleMongoConverter();
|
||||
object = new BasicDBObject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notNestedObject() {
|
||||
User user = new User();
|
||||
user.setAccountName("My Account");
|
||||
user.setUserName("Mark");
|
||||
converter.write(user, object);
|
||||
assertEquals("My Account", object.get("accountName"));
|
||||
assertEquals("Mark", object.get("userName"));
|
||||
|
||||
User u = converter.read(User.class, object);
|
||||
|
||||
assertEquals("My Account", u.getAccountName());
|
||||
assertEquals("Mark", u.getUserName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedObject() {
|
||||
Portfolio p = createPortfolioWithNoTrades();
|
||||
converter.write(p, object);
|
||||
|
||||
assertEquals("High Risk Trading Account", object.get("portfolioName"));
|
||||
assertTrue(object.containsField("user"));
|
||||
|
||||
Portfolio cp = converter.read(Portfolio.class, object);
|
||||
|
||||
assertEquals("High Risk Trading Account", cp.getPortfolioName());
|
||||
assertEquals("Joe Trader", cp.getUser().getUserName());
|
||||
assertEquals("ACCT-123", cp.getUser().getAccountName());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectWithMap() {
|
||||
Portfolio p = createPortfolioWithPositions();
|
||||
converter.write(p, object);
|
||||
|
||||
Portfolio cp = converter.read(Portfolio.class, object);
|
||||
assertEquals("High Risk Trading Account", cp.getPortfolioName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectWithMapContainingNonPrimitiveTypeAsValue() {
|
||||
Portfolio p = createPortfolioWithManagers();
|
||||
converter.write(p, object);
|
||||
|
||||
Portfolio cp = converter.read(Portfolio.class, object);
|
||||
assertEquals("High Risk Trading Account", cp.getPortfolioName());
|
||||
}
|
||||
|
||||
protected Portfolio createPortfolioWithPositions() {
|
||||
|
||||
Portfolio portfolio = new Portfolio();
|
||||
portfolio.setPortfolioName("High Risk Trading Account");
|
||||
Map<String, Integer> positions = new HashMap<String, Integer>();
|
||||
positions.put("CSCO", 1);
|
||||
portfolio.setPositions(positions);
|
||||
return portfolio;
|
||||
}
|
||||
|
||||
protected Portfolio createPortfolioWithManagers() {
|
||||
|
||||
Portfolio portfolio = new Portfolio();
|
||||
portfolio.setPortfolioName("High Risk Trading Account");
|
||||
Map<String, Person> managers = new HashMap<String, Person>();
|
||||
Person p1 = new Person();
|
||||
p1.setFirstName("Mark");
|
||||
managers.put("CSCO", p1);
|
||||
portfolio.setPortfolioManagers(managers);
|
||||
return portfolio;
|
||||
}
|
||||
|
||||
protected Portfolio createPortfolioWithNoTrades() {
|
||||
Portfolio portfolio = new Portfolio();
|
||||
User user = new User();
|
||||
user.setUserName("Joe Trader");
|
||||
user.setAccountName("ACCT-123");
|
||||
portfolio.setUser(user);
|
||||
portfolio.setPortfolioName("High Risk Trading Account");
|
||||
return portfolio;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectWithArrayContainingNonPrimitiveType() {
|
||||
TradeBatch b = createTradeBatch();
|
||||
converter.write(b, object);
|
||||
|
||||
TradeBatch b2 = converter.read(TradeBatch.class, object);
|
||||
assertEquals(b.getBatchId(), b2.getBatchId());
|
||||
assertNotNull(b2.getTradeList());
|
||||
assertEquals(b.getTradeList().size(), b2.getTradeList().size());
|
||||
assertEquals(b.getTradeList().get(1).getTicker(), b2.getTradeList().get(1).getTicker());
|
||||
assertEquals(b.getTrades().length, b2.getTrades().length);
|
||||
assertEquals(b.getTrades()[1].getTicker(), b2.getTrades()[1].getTicker());
|
||||
}
|
||||
|
||||
private TradeBatch createTradeBatch() {
|
||||
TradeBatch tb = new TradeBatch();
|
||||
tb.setBatchId("123456");
|
||||
Trade t1 = new Trade();
|
||||
t1.setOrderType("BUY");
|
||||
t1.setTicker("AAPL");
|
||||
t1.setQuantity(1000);
|
||||
t1.setPrice(320.77D);
|
||||
Trade t2 = new Trade();
|
||||
t2.setOrderType("SELL");
|
||||
t2.setTicker("MSFT");
|
||||
t2.setQuantity(100);
|
||||
t2.setPrice(27.92D);
|
||||
tb.setTrades(new Trade[] { t2, t1 });
|
||||
tb.setTradeList(Arrays.asList(new Trade[] { t1, t2 }));
|
||||
return tb;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectWithEnumTypes() {
|
||||
SomeEnumTest test = new SomeEnumTest();
|
||||
test.setId("123AAA");
|
||||
test.setName("Sven");
|
||||
test.setStringEnum(StringEnum.ONE);
|
||||
test.setNumberEnum(NumberEnum.FIVE);
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(test, dbo);
|
||||
|
||||
SomeEnumTest results = converter.read(SomeEnumTest.class, dbo);
|
||||
assertNotNull(results);
|
||||
assertEquals(test.getId(), results.getId());
|
||||
assertEquals(test.getName(), results.getName());
|
||||
assertEquals(test.getStringEnum(), results.getStringEnum());
|
||||
assertEquals(test.getNumberEnum(), results.getNumberEnum());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializesClassWithFinalObjectIdCorrectly() throws Exception {
|
||||
|
||||
BasicDBObject object = new BasicDBObject();
|
||||
Person person = new Person("Oliver");
|
||||
converter.write(person, object);
|
||||
|
||||
assertThat(object.get("class"), is(nullValue()));
|
||||
assertThat(object.get("_id"), is((Object) person.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversGenericsForType() throws Exception {
|
||||
|
||||
Field field = ReflectionUtils.findField(Sample.class, "map");
|
||||
assertListOfStringAndLong(converter.getGenericParameters(field.getGenericType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writesSimpleMapCorrectly() throws Exception {
|
||||
|
||||
Map<String, Long> map = new HashMap<String, Long>();
|
||||
map.put("foo", 1L);
|
||||
map.put("bar", 2L);
|
||||
|
||||
Sample sample = new Sample();
|
||||
sample.setMap(map);
|
||||
sample.setNumber(15L);
|
||||
|
||||
converter.write(sample, object);
|
||||
|
||||
assertThat(object.get("number"), is((Object) 15L));
|
||||
|
||||
Object result = object.get("map");
|
||||
assertTrue(result instanceof Map);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Long> mapResult = (Map<String, Long>) result;
|
||||
assertThat(mapResult.size(), is(2));
|
||||
assertThat(mapResult.get("foo"), is(1L));
|
||||
assertThat(mapResult.get("bar"), is(2L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writesComplexMapCorrectly() throws Exception {
|
||||
|
||||
Trade trade = new Trade();
|
||||
trade.setOrderType("BUY");
|
||||
trade.setTicker("VMW");
|
||||
trade.setPrice(90.50d);
|
||||
|
||||
Map<String, Trade> map = new HashMap<String, Trade>();
|
||||
map.put("trade", trade);
|
||||
|
||||
converter.write(new Sample2(map), object);
|
||||
DBObject tradeDbObject = new BasicDBObject();
|
||||
converter.write(trade, tradeDbObject);
|
||||
|
||||
Object result = object.get("map");
|
||||
assertTrue(result instanceof Map);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, DBObject> mapResult = (Map<String, DBObject>) result;
|
||||
assertThat(mapResult.size(), is(1));
|
||||
assertThat(mapResult.get("trade"), is(tradeDbObject));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readsMapWithSetterCorrectly() throws Exception {
|
||||
|
||||
DBObject input = (DBObject) JSON.parse(SIMPLE_JSON);
|
||||
Sample result = converter.read(Sample.class, input);
|
||||
assertThat(result.getNumber(), is(15L));
|
||||
|
||||
Map<String, Long> map = result.getMap();
|
||||
assertThat(map, is(notNullValue()));
|
||||
assertThat(map.size(), is(2));
|
||||
assertThat(map.get("foo"), is(3L));
|
||||
assertThat(map.get("bar"), is(4L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readsMapWithFieldOnlyCorrectly() throws Exception {
|
||||
|
||||
DBObject input = (DBObject) JSON.parse(COMPLEX_JSON);
|
||||
Sample2 result = converter.read(Sample2.class, input);
|
||||
|
||||
Map<String, Trade> map = result.getMap();
|
||||
|
||||
Trade trade = new Trade();
|
||||
trade.setOrderType("BUY");
|
||||
trade.setTicker("VMW");
|
||||
trade.setPrice(90.50d);
|
||||
|
||||
assertThat(map.size(), is(1));
|
||||
assertThat(map.get("trade").getTicker(), is("VMW"));
|
||||
assertThat(map.get("trade").getOrderType(), is("BUY"));
|
||||
assertThat(map.get("trade").getPrice(), is(90.50d));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsBigIntegerAsIdProperty() throws Exception {
|
||||
|
||||
Sample3 sample3 = new Sample3();
|
||||
sample3.id = new BigInteger("4d24809660413b687f5d323e", 16);
|
||||
converter.write(sample3, object);
|
||||
assertThat(object.get("_id"), is(notNullValue()));
|
||||
|
||||
Sample3 result = converter.read(Sample3.class,
|
||||
(DBObject) JSON.parse("{\"_id\" : {\"$oid\" : \"4d24809660413b687f5d323e\" }}"));
|
||||
assertThat(result.getId().toString(16), is("4d24809660413b687f5d323e"));
|
||||
}
|
||||
|
||||
private void assertListOfStringAndLong(List<Class<?>> types) {
|
||||
|
||||
assertThat(types.size(), is(2));
|
||||
assertEquals(String.class, types.get(0));
|
||||
assertEquals(Long.class, types.get(1));
|
||||
}
|
||||
|
||||
public static class Sample {
|
||||
|
||||
private Map<String, Long> map;
|
||||
private Long number;
|
||||
|
||||
public void setMap(Map<String, Long> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public Map<String, Long> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setNumber(Long number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public Long getNumber() {
|
||||
return number;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Sample2 {
|
||||
|
||||
private final Map<String, Trade> map;
|
||||
|
||||
protected Sample2() {
|
||||
this.map = null;
|
||||
}
|
||||
|
||||
public Sample2(Map<String, Trade> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public Map<String, Trade> getMap() {
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Sample3 {
|
||||
|
||||
private BigInteger id;
|
||||
|
||||
public BigInteger getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.document.mongodb;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.util.JSON;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.document.mongodb.SomeEnumTest.NumberEnum;
|
||||
import org.springframework.data.document.mongodb.SomeEnumTest.StringEnum;
|
||||
import org.springframework.data.document.mongodb.convert.SimpleMongoConverter;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SimpleMongoConverterTests {
|
||||
|
||||
static final String SIMPLE_JSON = "{ \"map\" : { \"foo\" : 3 , \"bar\" : 4}, \"number\" : 15 }";
|
||||
static final String COMPLEX_JSON = "{ \"map\" : { \"trade\" : { \"orderType\" : \"BUY\" , \"price\" : 90.5 , \"quantity\" : 0 , \"ticker\" : \"VMW\"}}}";
|
||||
|
||||
SimpleMongoConverter converter;
|
||||
DBObject object;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
converter = new SimpleMongoConverter();
|
||||
object = new BasicDBObject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notNestedObject() {
|
||||
User user = new User();
|
||||
user.setAccountName("My Account");
|
||||
user.setUserName("Mark");
|
||||
converter.write(user, object);
|
||||
assertEquals("My Account", object.get("accountName"));
|
||||
assertEquals("Mark", object.get("userName"));
|
||||
|
||||
User u = converter.read(User.class, object);
|
||||
|
||||
assertEquals("My Account", u.getAccountName());
|
||||
assertEquals("Mark", u.getUserName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedObject() {
|
||||
Portfolio p = createPortfolioWithNoTrades();
|
||||
converter.write(p, object);
|
||||
|
||||
assertEquals("High Risk Trading Account", object.get("portfolioName"));
|
||||
assertTrue(object.containsField("user"));
|
||||
|
||||
Portfolio cp = converter.read(Portfolio.class, object);
|
||||
|
||||
assertEquals("High Risk Trading Account", cp.getPortfolioName());
|
||||
assertEquals("Joe Trader", cp.getUser().getUserName());
|
||||
assertEquals("ACCT-123", cp.getUser().getAccountName());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectWithMap() {
|
||||
Portfolio p = createPortfolioWithPositions();
|
||||
converter.write(p, object);
|
||||
|
||||
Portfolio cp = converter.read(Portfolio.class, object);
|
||||
assertEquals("High Risk Trading Account", cp.getPortfolioName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectWithMapContainingNonPrimitiveTypeAsValue() {
|
||||
Portfolio p = createPortfolioWithManagers();
|
||||
converter.write(p, object);
|
||||
|
||||
Portfolio cp = converter.read(Portfolio.class, object);
|
||||
assertEquals("High Risk Trading Account", cp.getPortfolioName());
|
||||
}
|
||||
|
||||
protected Portfolio createPortfolioWithPositions() {
|
||||
|
||||
Portfolio portfolio = new Portfolio();
|
||||
portfolio.setPortfolioName("High Risk Trading Account");
|
||||
Map<String, Integer> positions = new HashMap<String, Integer>();
|
||||
positions.put("CSCO", 1);
|
||||
portfolio.setPositions(positions);
|
||||
return portfolio;
|
||||
}
|
||||
|
||||
protected Portfolio createPortfolioWithManagers() {
|
||||
|
||||
Portfolio portfolio = new Portfolio();
|
||||
portfolio.setPortfolioName("High Risk Trading Account");
|
||||
Map<String, Person> managers = new HashMap<String, Person>();
|
||||
Person p1 = new Person();
|
||||
p1.setFirstName("Mark");
|
||||
managers.put("CSCO", p1);
|
||||
portfolio.setPortfolioManagers(managers);
|
||||
return portfolio;
|
||||
}
|
||||
|
||||
protected Portfolio createPortfolioWithNoTrades() {
|
||||
Portfolio portfolio = new Portfolio();
|
||||
User user = new User();
|
||||
user.setUserName("Joe Trader");
|
||||
user.setAccountName("ACCT-123");
|
||||
portfolio.setUser(user);
|
||||
portfolio.setPortfolioName("High Risk Trading Account");
|
||||
return portfolio;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectWithArrayContainingNonPrimitiveType() {
|
||||
TradeBatch b = createTradeBatch();
|
||||
converter.write(b, object);
|
||||
|
||||
TradeBatch b2 = converter.read(TradeBatch.class, object);
|
||||
assertEquals(b.getBatchId(), b2.getBatchId());
|
||||
assertNotNull(b2.getTradeList());
|
||||
assertEquals(b.getTradeList().size(), b2.getTradeList().size());
|
||||
assertEquals(b.getTradeList().get(1).getTicker(), b2.getTradeList().get(1).getTicker());
|
||||
assertEquals(b.getTrades().length, b2.getTrades().length);
|
||||
assertEquals(b.getTrades()[1].getTicker(), b2.getTrades()[1].getTicker());
|
||||
}
|
||||
|
||||
private TradeBatch createTradeBatch() {
|
||||
TradeBatch tb = new TradeBatch();
|
||||
tb.setBatchId("123456");
|
||||
Trade t1 = new Trade();
|
||||
t1.setOrderType("BUY");
|
||||
t1.setTicker("AAPL");
|
||||
t1.setQuantity(1000);
|
||||
t1.setPrice(320.77D);
|
||||
Trade t2 = new Trade();
|
||||
t2.setOrderType("SELL");
|
||||
t2.setTicker("MSFT");
|
||||
t2.setQuantity(100);
|
||||
t2.setPrice(27.92D);
|
||||
tb.setTrades(new Trade[]{t2, t1});
|
||||
tb.setTradeList(Arrays.asList(new Trade[]{t1, t2}));
|
||||
return tb;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objectWithEnumTypes() {
|
||||
SomeEnumTest test = new SomeEnumTest();
|
||||
test.setId("123AAA");
|
||||
test.setName("Sven");
|
||||
test.setStringEnum(StringEnum.ONE);
|
||||
test.setNumberEnum(NumberEnum.FIVE);
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(test, dbo);
|
||||
|
||||
SomeEnumTest results = converter.read(SomeEnumTest.class, dbo);
|
||||
assertNotNull(results);
|
||||
assertEquals(test.getId(), results.getId());
|
||||
assertEquals(test.getName(), results.getName());
|
||||
assertEquals(test.getStringEnum(), results.getStringEnum());
|
||||
assertEquals(test.getNumberEnum(), results.getNumberEnum());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializesClassWithFinalObjectIdCorrectly() throws Exception {
|
||||
|
||||
BasicDBObject object = new BasicDBObject();
|
||||
Person person = new Person("Oliver");
|
||||
converter.write(person, object);
|
||||
|
||||
assertThat(object.get("class"), is(nullValue()));
|
||||
assertThat(object.get("_id"), is((Object) person.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversGenericsForType() throws Exception {
|
||||
|
||||
Field field = ReflectionUtils.findField(Sample.class, "map");
|
||||
assertListOfStringAndLong(converter.getGenericParameters(field.getGenericType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writesSimpleMapCorrectly() throws Exception {
|
||||
|
||||
Map<String, Long> map = new HashMap<String, Long>();
|
||||
map.put("foo", 1L);
|
||||
map.put("bar", 2L);
|
||||
|
||||
Sample sample = new Sample();
|
||||
sample.setMap(map);
|
||||
sample.setNumber(15L);
|
||||
|
||||
converter.write(sample, object);
|
||||
|
||||
assertThat(object.get("number"), is((Object) 15L));
|
||||
|
||||
Object result = object.get("map");
|
||||
assertTrue(result instanceof Map);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Long> mapResult = (Map<String, Long>) result;
|
||||
assertThat(mapResult.size(), is(2));
|
||||
assertThat(mapResult.get("foo"), is(1L));
|
||||
assertThat(mapResult.get("bar"), is(2L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writesComplexMapCorrectly() throws Exception {
|
||||
|
||||
Trade trade = new Trade();
|
||||
trade.setOrderType("BUY");
|
||||
trade.setTicker("VMW");
|
||||
trade.setPrice(90.50d);
|
||||
|
||||
Map<String, Trade> map = new HashMap<String, Trade>();
|
||||
map.put("trade", trade);
|
||||
|
||||
converter.write(new Sample2(map), object);
|
||||
DBObject tradeDbObject = new BasicDBObject();
|
||||
converter.write(trade, tradeDbObject);
|
||||
|
||||
Object result = object.get("map");
|
||||
assertTrue(result instanceof Map);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, DBObject> mapResult = (Map<String, DBObject>) result;
|
||||
assertThat(mapResult.size(), is(1));
|
||||
assertThat(mapResult.get("trade"), is(tradeDbObject));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readsMapWithSetterCorrectly() throws Exception {
|
||||
|
||||
DBObject input = (DBObject) JSON.parse(SIMPLE_JSON);
|
||||
Sample result = converter.read(Sample.class, input);
|
||||
assertThat(result.getNumber(), is(15L));
|
||||
|
||||
Map<String, Long> map = result.getMap();
|
||||
assertThat(map, is(notNullValue()));
|
||||
assertThat(map.size(), is(2));
|
||||
assertThat(map.get("foo"), is(3L));
|
||||
assertThat(map.get("bar"), is(4L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readsMapWithFieldOnlyCorrectly() throws Exception {
|
||||
|
||||
DBObject input = (DBObject) JSON.parse(COMPLEX_JSON);
|
||||
Sample2 result = converter.read(Sample2.class, input);
|
||||
|
||||
Map<String, Trade> map = result.getMap();
|
||||
|
||||
Trade trade = new Trade();
|
||||
trade.setOrderType("BUY");
|
||||
trade.setTicker("VMW");
|
||||
trade.setPrice(90.50d);
|
||||
|
||||
assertThat(map.size(), is(1));
|
||||
assertThat(map.get("trade").getTicker(), is("VMW"));
|
||||
assertThat(map.get("trade").getOrderType(), is("BUY"));
|
||||
assertThat(map.get("trade").getPrice(), is(90.50d));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsBigIntegerAsIdProperty() throws Exception {
|
||||
|
||||
Sample3 sample3 = new Sample3();
|
||||
sample3.id = new BigInteger("4d24809660413b687f5d323e", 16);
|
||||
converter.write(sample3, object);
|
||||
assertThat(object.get("_id"), is(notNullValue()));
|
||||
|
||||
Sample3 result = converter.read(Sample3.class,
|
||||
(DBObject) JSON.parse("{\"_id\" : {\"$oid\" : \"4d24809660413b687f5d323e\" }}"));
|
||||
assertThat(result.getId().toString(16), is("4d24809660413b687f5d323e"));
|
||||
}
|
||||
|
||||
private void assertListOfStringAndLong(List<Class<?>> types) {
|
||||
|
||||
assertThat(types.size(), is(2));
|
||||
assertEquals(String.class, types.get(0));
|
||||
assertEquals(Long.class, types.get(1));
|
||||
}
|
||||
|
||||
public static class Sample {
|
||||
|
||||
private Map<String, Long> map;
|
||||
private Long number;
|
||||
|
||||
public void setMap(Map<String, Long> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public Map<String, Long> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setNumber(Long number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public Long getNumber() {
|
||||
return number;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Sample2 {
|
||||
|
||||
private final Map<String, Trade> map;
|
||||
|
||||
protected Sample2() {
|
||||
this.map = null;
|
||||
}
|
||||
|
||||
public Sample2(Map<String, Trade> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public Map<String, Trade> getMap() {
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Sample3 {
|
||||
|
||||
private BigInteger id;
|
||||
|
||||
public BigInteger getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user