DATADOC-48 some more cross-store tests
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
<classpath>
|
<classpath>
|
||||||
<classpathentry including="**/*.aj|**/*.java" kind="src" output="target/classes" path="src/main/java"/>
|
<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 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="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.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"/>
|
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
|
||||||
|
|||||||
@@ -36,11 +36,12 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
|
|||||||
@Override
|
@Override
|
||||||
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id, ChangeSet changeSet)
|
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id, ChangeSet changeSet)
|
||||||
throws DataAccessException, NotFoundException {
|
throws DataAccessException, NotFoundException {
|
||||||
String collection = ClassUtils.getQualifiedName(entityClass);
|
String collection = ClassUtils.getShortName(entityClass).toLowerCase();
|
||||||
DBObject q = new BasicDBObject();
|
DBObject q = new BasicDBObject();
|
||||||
q.put("_id", id);
|
q.put("_id", id);
|
||||||
try {
|
try {
|
||||||
DBObject dbo = mongoTemplate.getCollection(collection).findOne(q);
|
DBObject dbo = mongoTemplate.getCollection(collection).findOne(q);
|
||||||
|
log.debug("Found DBObject: " + dbo);
|
||||||
if (dbo == null) {
|
if (dbo == null) {
|
||||||
throw new NotFoundException();
|
throw new NotFoundException();
|
||||||
}
|
}
|
||||||
@@ -90,7 +91,7 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
|
|||||||
if (id == null) {
|
if (id == null) {
|
||||||
log.info("Flush: entity make persistent; data store will assign id");
|
log.info("Flush: entity make persistent; data store will assign id");
|
||||||
cs.set("_class", entityClass.getName());
|
cs.set("_class", entityClass.getName());
|
||||||
String collection = entityClass.getName();
|
String collection = entityClass.getSimpleName().toLowerCase();
|
||||||
DBCollection dbc = mongoTemplate.getCollection(collection);
|
DBCollection dbc = mongoTemplate.getCollection(collection);
|
||||||
DBObject dbo = mapChangeSetToDbObject(cs);
|
DBObject dbo = mapChangeSetToDbObject(cs);
|
||||||
if (dbc == null) {
|
if (dbc == null) {
|
||||||
|
|||||||
@@ -4,12 +4,18 @@ import org.junit.Assert;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.annotation.Rollback;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import com.mongodb.DBCollection;
|
||||||
|
import com.mongodb.DBObject;
|
||||||
import com.mongodb.Mongo;
|
import com.mongodb.Mongo;
|
||||||
|
import com.mongodb.MongoException;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext.xml")
|
@ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext.xml")
|
||||||
@@ -18,6 +24,17 @@ public class CrossStoreMongoTests {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private Mongo mongo;
|
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
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
@Rollback(false)
|
@Rollback(false)
|
||||||
@@ -29,4 +46,17 @@ public class CrossStoreMongoTests {
|
|||||||
Assert.assertEquals(1 + age, p.getAge());
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.springframework.data.document.persistence;
|
package org.springframework.persistence.document.test;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
@@ -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.RelatedEntity;
|
||||||
import org.springframework.persistence.document.DocumentEntity;
|
import org.springframework.persistence.document.DocumentEntity;
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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 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">
|
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
|
||||||
<provider>org.hibernate.ejb.HibernatePersistence</provider>
|
<provider>org.hibernate.ejb.HibernatePersistence</provider>
|
||||||
<class>org.springframework.data.document.persistence.Account</class>
|
<class>org.springframework.persistence.document.test.Account</class>
|
||||||
<properties>
|
<properties>
|
||||||
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
|
<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-->
|
<!--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-->
|
||||||
|
|||||||
@@ -3,10 +3,20 @@
|
|||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
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
|
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/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">
|
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">
|
<bean id="mongo" class="org.springframework.data.document.mongodb.MongoFactoryBean">
|
||||||
<property name="host" value="localhost" />
|
<property name="host" value="localhost" />
|
||||||
<property name="port" value="27017" />
|
<property name="port" value="27017" />
|
||||||
@@ -15,7 +25,6 @@
|
|||||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
||||||
<constructor-arg name="mongo" ref="mongo" />
|
<constructor-arg name="mongo" ref="mongo" />
|
||||||
<constructor-arg name="databaseName" value="test" />
|
<constructor-arg name="databaseName" value="test" />
|
||||||
<constructor-arg name="defaultCollectionName" value="cross-store" />
|
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<bean class="org.springframework.data.document.mongodb.MongoExceptionTranslator" />
|
<bean class="org.springframework.data.document.mongodb.MongoExceptionTranslator" />
|
||||||
|
|||||||
Reference in New Issue
Block a user