DATADOC-48 some more cross-store tests

This commit is contained in:
Thomas Risberg
2011-03-03 10:16:54 -05:00
parent 716875db03
commit 8f39f6616d
8 changed files with 107 additions and 7 deletions

View File

@@ -2,7 +2,7 @@
<classpath>
<classpathentry including="**/*.aj|**/*.java" kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry including="**/*.aj|**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>

View File

@@ -36,11 +36,12 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
@Override
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id, ChangeSet changeSet)
throws DataAccessException, NotFoundException {
String collection = ClassUtils.getQualifiedName(entityClass);
String collection = ClassUtils.getShortName(entityClass).toLowerCase();
DBObject q = new BasicDBObject();
q.put("_id", id);
try {
DBObject dbo = mongoTemplate.getCollection(collection).findOne(q);
log.debug("Found DBObject: " + dbo);
if (dbo == null) {
throw new NotFoundException();
}
@@ -90,7 +91,7 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
if (id == null) {
log.info("Flush: entity make persistent; data store will assign id");
cs.set("_class", entityClass.getName());
String collection = entityClass.getName();
String collection = entityClass.getSimpleName().toLowerCase();
DBCollection dbc = mongoTemplate.getCollection(collection);
DBObject dbo = mapChangeSetToDbObject(cs);
if (dbc == null) {

View File

@@ -4,12 +4,18 @@ import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.persistence.document.test.MongoPerson;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext.xml")
@@ -18,6 +24,17 @@ public class CrossStoreMongoTests {
@Autowired
private Mongo mongo;
@Autowired
private MongoTemplate mongoTemplate;
@BeforeTransaction
public void setUp() {
DBCollection col = this.mongoTemplate.getCollection(MongoPerson.class.getSimpleName().toLowerCase());
if (col != null) {
this.mongoTemplate.dropCollection(MongoPerson.class.getName());
}
}
@Test
@Transactional
@Rollback(false)
@@ -29,4 +46,17 @@ public class CrossStoreMongoTests {
Assert.assertEquals(1 + age, p.getAge());
}
@Test
@Transactional
public void testInstantiatedFinder() throws MongoException {
String key = MongoPerson.class.getSimpleName().toLowerCase();
DBCollection col = this.mongoTemplate.getCollection(key);
DBObject dbo = col.findOne();
Object id1 = dbo.get("_id");
MongoPerson found = MongoPerson.findPerson(id1);
Assert.assertNotNull(found);
Assert.assertEquals(id1, found.getId());
System.out.println("Loaded MongoPerson data: " + found);
}
}

View File

@@ -1,4 +1,4 @@
package org.springframework.data.document.persistence;
package org.springframework.persistence.document.test;
import javax.persistence.Entity;
import javax.persistence.Id;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.document.persistence;
package org.springframework.persistence.document.test;
import org.springframework.persistence.RelatedEntity;
import org.springframework.persistence.document.DocumentEntity;

View File

@@ -0,0 +1,60 @@
package org.springframework.persistence.document.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.persistence.support.ChangeSet;
import org.springframework.persistence.support.ChangeSetBacked;
import org.springframework.persistence.support.ChangeSetConfiguration;
import org.springframework.persistence.support.ChangeSetPersister;
import org.springframework.persistence.support.ChangeSetSynchronizer;
import org.springframework.persistence.support.HashMapChangeSet;
import org.springframework.persistence.support.ChangeSetPersister.NotFoundException;
/**
* EXAMPLE OF CODE THAT SHOULD BE GENERATED BY ROO BESIDES EACH MONGOENTITY CLASS
*
* Note: Combines X_Roo_Entity with X_Roo_Finder, as
* we need only a single aspect for entities.
*
* @author Thomas Risberg
*
*/
privileged aspect MongoPerson_Roo_Mongo_Entity {
private static ChangeSetPersister<Object> changeSetPersister() {
return new MongoConfigurationHolder().changeSetConfig.getChangeSetPersister();
}
private static ChangeSetSynchronizer<ChangeSetBacked> changeSetManager() {
return new MongoConfigurationHolder().changeSetConfig.getChangeSetManager();
}
@Configurable
public static class MongoConfigurationHolder {
@Autowired
@Qualifier("mongoChangeSetConfiguration")
public ChangeSetConfiguration<Object> changeSetConfig;
}
/**
* Add constructor that takes ChangeSet.
* @param ChangeSet
*/
public MongoPerson.new(ChangeSet cs) {
super();
setChangeSet(cs);
}
public static MongoPerson MongoPerson.findPerson(Object id) {
ChangeSet rv = new HashMapChangeSet();
try {
changeSetPersister().getPersistentState(MongoPerson.class, id, rv);
return new MongoPerson(rv);
}
catch (NotFoundException ex) {
return null;
}
}
}

View File

@@ -2,7 +2,7 @@
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.springframework.data.document.persistence.Account</class>
<class>org.springframework.persistence.document.test.Account</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<!--value='create' to build a new database on each run; value='update' to modify an existing database; value='create-drop' means the same as 'create' but also drops tables when Hibernate closes; value='validate' makes no changes to the database-->

View File

@@ -3,10 +3,20 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:spring-configured/>
<context:component-scan base-package="org.springframework.persistence.test">
<context:exclude-filter expression=".*_Roo_.*" type="regex"/>
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<!-- Mongo config -->
<bean id="mongo" class="org.springframework.data.document.mongodb.MongoFactoryBean">
<property name="host" value="localhost" />
<property name="port" value="27017" />
@@ -15,7 +25,6 @@
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="test" />
<constructor-arg name="defaultCollectionName" value="cross-store" />
</bean>
<bean class="org.springframework.data.document.mongodb.MongoExceptionTranslator" />