From 85e5387eb140d58a141d82fbc8c8ab615dcbdea9 Mon Sep 17 00:00:00 2001 From: trisberg Date: Tue, 27 Jul 2010 20:50:19 -0400 Subject: [PATCH] removed old dummy test --- .classpath | 1 + .settings/org.eclipse.wst.common.component | 1 + pom.xml | 2 +- ...uctorBypassingGraphEntityInstantiator.java | 48 +++++++ .../Neo4jConstructorEntityInstantiator.java | 19 +++ .../persistence/graph/neo4j/NodeBacked.java | 2 + .../datastore/graph/GraphUtilsTest.java | 15 --- .../neo4j/Neo4jGraphPersistenceTest.java | 25 ++++ .../META-INF/spring/database.properties | 6 + .../META-INF/spring/neo4j.properties | 1 + src/test/resources/log4j.properties | 22 ++++ .../Neo4jGraphPersistenceTest-context.xml | 121 ++++++++++++++++++ 12 files changed, 247 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/springframework/persistence/graph/neo4j/ConstructorBypassingGraphEntityInstantiator.java create mode 100644 src/main/java/org/springframework/persistence/graph/neo4j/Neo4jConstructorEntityInstantiator.java delete mode 100644 src/test/java/org/springframework/datastore/graph/GraphUtilsTest.java create mode 100644 src/test/java/org/springframework/persistence/graph/neo4j/Neo4jGraphPersistenceTest.java create mode 100644 src/test/resources/META-INF/spring/database.properties create mode 100644 src/test/resources/META-INF/spring/neo4j.properties create mode 100644 src/test/resources/log4j.properties create mode 100644 src/test/resources/org/springframework/persistence/graph/neo4j/Neo4jGraphPersistenceTest-context.xml diff --git a/.classpath b/.classpath index 320153234..0db60442f 100644 --- a/.classpath +++ b/.classpath @@ -2,6 +2,7 @@ + diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index caf0f5ae0..015e47d6e 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,5 +4,6 @@ + diff --git a/pom.xml b/pom.xml index d4db32ec5..7e0279a0b 100644 --- a/pom.xml +++ b/pom.xml @@ -185,7 +185,7 @@ maven-surefire-plugin 2.4.3 - org.junit:com.springsource.org.junit + junit:junit **/*_Roo_* diff --git a/src/main/java/org/springframework/persistence/graph/neo4j/ConstructorBypassingGraphEntityInstantiator.java b/src/main/java/org/springframework/persistence/graph/neo4j/ConstructorBypassingGraphEntityInstantiator.java new file mode 100644 index 000000000..913133348 --- /dev/null +++ b/src/main/java/org/springframework/persistence/graph/neo4j/ConstructorBypassingGraphEntityInstantiator.java @@ -0,0 +1,48 @@ +package org.springframework.persistence.graph.neo4j; + +import java.lang.reflect.Constructor; + +import org.neo4j.graphdb.Node; +import org.springframework.persistence.support.EntityInstantiator; + +import sun.reflect.ReflectionFactory; + +/** + * Uses Sun internal libraries used in deserializations to instantiate + * objects bypassing constructor. + * + * Code based on this http://www.javaspecialists.eu/archive/Issue175.html + * TODO check license implications + * + * @author rodjohnson + * + */ +public class ConstructorBypassingGraphEntityInstantiator implements EntityInstantiator { + + private static T createWithoutConstructorInvocation(Class clazz) { + return createWithoutConstructorInvocation(clazz, Object.class); + } + + @SuppressWarnings("unchecked") + private static T createWithoutConstructorInvocation(Class clazz, Class parent) { + try { + ReflectionFactory rf = ReflectionFactory.getReflectionFactory(); + Constructor objDef = parent.getDeclaredConstructor(); + Constructor intConstr = rf.newConstructorForSerialization(clazz, + objDef); + return clazz.cast(intConstr.newInstance()); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new IllegalStateException("Cannot create object", e); + } + } + + @Override + public T createEntityFromState(Node n, Class c) { + T t = createWithoutConstructorInvocation(c); + t.setUnderlyingNode(n); + return t; + } + +} diff --git a/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jConstructorEntityInstantiator.java b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jConstructorEntityInstantiator.java new file mode 100644 index 000000000..34949c443 --- /dev/null +++ b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jConstructorEntityInstantiator.java @@ -0,0 +1,19 @@ +package org.springframework.persistence.graph.neo4j; + +import org.neo4j.graphdb.Node; +import org.springframework.persistence.support.AbstractConstructorEntityInstantiator; + +/** + * Try for a constructor taking a Neo4j Node: failing that, try a no-arg + * constructor and then setUnderlyingNode(). + * + * @author Rod Johnson + */ +public class Neo4jConstructorEntityInstantiator extends AbstractConstructorEntityInstantiator{ + + @Override + protected void setState(NodeBacked entity, Node s) { + entity.setUnderlyingNode(s); + } + +} diff --git a/src/main/java/org/springframework/persistence/graph/neo4j/NodeBacked.java b/src/main/java/org/springframework/persistence/graph/neo4j/NodeBacked.java index 1dba34744..5b51fa841 100644 --- a/src/main/java/org/springframework/persistence/graph/neo4j/NodeBacked.java +++ b/src/main/java/org/springframework/persistence/graph/neo4j/NodeBacked.java @@ -10,5 +10,7 @@ import org.neo4j.graphdb.Node; public interface NodeBacked { Node getUnderlyingNode(); + + void setUnderlyingNode(Node n); } diff --git a/src/test/java/org/springframework/datastore/graph/GraphUtilsTest.java b/src/test/java/org/springframework/datastore/graph/GraphUtilsTest.java deleted file mode 100644 index e252d01d4..000000000 --- a/src/test/java/org/springframework/datastore/graph/GraphUtilsTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.springframework.datastore.graph; - -import junit.framework.Assert; - -import org.junit.Test; - - -public class GraphUtilsTest { - - @Test - public void testSomething() { - Assert.assertTrue( true ); - } - -} diff --git a/src/test/java/org/springframework/persistence/graph/neo4j/Neo4jGraphPersistenceTest.java b/src/test/java/org/springframework/persistence/graph/neo4j/Neo4jGraphPersistenceTest.java new file mode 100644 index 000000000..ef66ac966 --- /dev/null +++ b/src/test/java/org/springframework/persistence/graph/neo4j/Neo4jGraphPersistenceTest.java @@ -0,0 +1,25 @@ +package org.springframework.persistence.graph.neo4j; + +import junit.framework.Assert; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.neo4j.graphdb.GraphDatabaseService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class Neo4jGraphPersistenceTest { + + @Autowired + protected GraphDatabaseService graphDatabaseService; + + @Test + public void testGraphDatabaseServiceCreatedAndAutowired() { + Assert.assertNotNull( graphDatabaseService ); + } + +} diff --git a/src/test/resources/META-INF/spring/database.properties b/src/test/resources/META-INF/spring/database.properties new file mode 100644 index 000000000..6dda8e5d5 --- /dev/null +++ b/src/test/resources/META-INF/spring/database.properties @@ -0,0 +1,6 @@ +#Updated at Tue Mar 23 14:26:10 PDT 2010 +#Tue Mar 23 14:26:10 PDT 2010 +database.password= +database.url=jdbc\:hsqldb\:mem\:test +database.username=sa +database.driverClassName=org.hsqldb.jdbcDriver diff --git a/src/test/resources/META-INF/spring/neo4j.properties b/src/test/resources/META-INF/spring/neo4j.properties new file mode 100644 index 000000000..f2ca3696a --- /dev/null +++ b/src/test/resources/META-INF/spring/neo4j.properties @@ -0,0 +1 @@ +neo4j.databaseDirectory=target/data/test \ No newline at end of file diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties new file mode 100644 index 000000000..035716f13 --- /dev/null +++ b/src/test/resources/log4j.properties @@ -0,0 +1,22 @@ +log4j.rootLogger=info, stdout, R + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout + +# Print the date in ISO 8601 format +log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n + +log4j.appender.R=org.apache.log4j.RollingFileAppender +log4j.appender.R.File=application.log + +log4j.appender.R.MaxFileSize=100KB +# Keep one backup file +log4j.appender.R.MaxBackupIndex=1 + +log4j.appender.R.layout=org.apache.log4j.PatternLayout +log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n + +log4j.category.org.springframework=DEBUG +log4j.category.org.springframework.data=TRACE +log4j.category.org.springframework.datastore=TRACE +log4j.category.org.springframework.persistence=TRACE diff --git a/src/test/resources/org/springframework/persistence/graph/neo4j/Neo4jGraphPersistenceTest-context.xml b/src/test/resources/org/springframework/persistence/graph/neo4j/Neo4jGraphPersistenceTest-context.xml new file mode 100644 index 000000000..60e118aa8 --- /dev/null +++ b/src/test/resources/org/springframework/persistence/graph/neo4j/Neo4jGraphPersistenceTest-context.xml @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +