Make sure list saving/loading works.

This commit is contained in:
Michael Nitschinger
2013-05-28 13:43:00 +02:00
parent a97908e30b
commit 6e04c9edc3
3 changed files with 69 additions and 14 deletions

24
pom.xml
View File

@@ -2,7 +2,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>couchbase</groupId>
<groupId>com.couchbase</groupId>
<artifactId>spring-data-couchbase</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
@@ -21,8 +22,11 @@
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<couchbase>1.1.6</couchbase>
<spring>3.2.3.RELEASE</spring>
</properties>
<repositories>
@@ -31,9 +35,6 @@
<name>Couchbase Maven Repository</name>
<layout>default</layout>
<url>http://files.couchbase.com/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
@@ -41,13 +42,12 @@
<dependency>
<groupId>couchbase</groupId>
<artifactId>couchbase-client</artifactId>
<version>1.1.5</version>
<version>${couchbase}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.2.RELEASE</version>
<type>jar</type>
<version>${spring}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
@@ -63,13 +63,13 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.2.RELEASE</version>
<version>${spring}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>1.4.1.RELEASE</version>
<artifactId>spring-data-commons</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
@@ -79,8 +79,8 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.2.RELEASE</version>
<version>${spring}</version>
</dependency>
</dependencies>
<artifactId>spring-data-couchbase</artifactId>
</project>

View File

@@ -32,6 +32,7 @@ import java.io.OutputStream;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@@ -168,6 +169,7 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter
OutputStream jsonStream = new ByteArrayOutputStream();
final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(
jsonStream, JsonEncoding.UTF8);
jsonGenerator.setCodec(new ObjectMapper());
jsonGenerator.writeStartObject();
entity.doWithProperties(new PropertyHandler<CouchbasePersistentProperty>() {

View File

@@ -35,6 +35,10 @@ import org.springframework.data.annotation.Id;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestApplicationConfig.class)
public class CouchbaseTemplateTest {
@@ -121,12 +125,32 @@ public class CouchbaseTemplateTest {
result = client.get(id);
assertNull(result);
}
@Test
public void storeLists() {
String id ="persons:lots-of-names";
List<String> names = new ArrayList<String>();
names.add("Michael");
names.add("Thomas");
List<Integer> votes = new LinkedList<Integer>();
ComplexPerson complex = new ComplexPerson(id, names, votes);
template.save(complex);
String expected = "{\"votes\":[],\"firstnames\":[\"Michael\",\"Thomas\"]}";
assertEquals(expected, client.get(id));
ComplexPerson response = template.findById(id, ComplexPerson.class);
assertEquals(names, response.getFirstnames());
assertEquals(votes, response.getVotes());
assertEquals(id, response.getId());
}
/**
* A sample document with just an id and property.
*/
@Document
class SimplePerson {
static class SimplePerson {
@Id
private final String id;
@Field
@@ -142,7 +166,7 @@ public class CouchbaseTemplateTest {
* A sample document that expires in 2 seconds.
*/
@Document(expiry=2)
class DocumentWithExpiry {
static class DocumentWithExpiry {
@Id
private final String id;
@@ -150,4 +174,33 @@ public class CouchbaseTemplateTest {
this.id = id;
}
}
@Document
static class ComplexPerson {
@Id
private final String id;
@Field
private final List<String> firstnames;
@Field
private final List<Integer> votes;
public ComplexPerson(String id, List<String> firstnames,
List<Integer> votes) {
this.id = id;
this.firstnames = firstnames;
this.votes = votes;
}
List<String> getFirstnames() {
return firstnames;
}
List<Integer> getVotes() {
return votes;
}
String getId() {
return id;
}
}
}