/blueprints-neo4j-graph/src/test/java/com/tinkerpop/blueprints/pgm/impls/neo4j/Neo4jGraphTest.java

https://github.com/tor5/blueprints · Java · 201 lines · 170 code · 23 blank · 8 comment · 17 complexity · 1091f4718906704faba0972f0634825c MD5 · raw file

  1. package com.tinkerpop.blueprints.pgm.impls.neo4j;
  2. import com.tinkerpop.blueprints.pgm.AutomaticIndexTestSuite;
  3. import com.tinkerpop.blueprints.pgm.Edge;
  4. import com.tinkerpop.blueprints.pgm.EdgeTestSuite;
  5. import com.tinkerpop.blueprints.pgm.Graph;
  6. import com.tinkerpop.blueprints.pgm.GraphTestSuite;
  7. import com.tinkerpop.blueprints.pgm.Index;
  8. import com.tinkerpop.blueprints.pgm.IndexTestSuite;
  9. import com.tinkerpop.blueprints.pgm.IndexableGraph;
  10. import com.tinkerpop.blueprints.pgm.IndexableGraphTestSuite;
  11. import com.tinkerpop.blueprints.pgm.TestSuite;
  12. import com.tinkerpop.blueprints.pgm.TransactionalGraphTestSuite;
  13. import com.tinkerpop.blueprints.pgm.Vertex;
  14. import com.tinkerpop.blueprints.pgm.VertexTestSuite;
  15. import com.tinkerpop.blueprints.pgm.impls.GraphTest;
  16. import com.tinkerpop.blueprints.pgm.util.graphml.GraphMLReaderTestSuite;
  17. import java.io.File;
  18. import java.lang.reflect.Method;
  19. import java.util.Iterator;
  20. /**
  21. * @author Marko A. Rodriguez (http://markorodriguez.com)
  22. */
  23. public class Neo4jGraphTest extends GraphTest {
  24. public Neo4jGraphTest() {
  25. this.allowsDuplicateEdges = true;
  26. this.allowsSelfLoops = false;
  27. this.isPersistent = true;
  28. this.isRDFModel = false;
  29. this.supportsVertexIteration = true;
  30. this.supportsEdgeIteration = true;
  31. this.supportsVertexIndex = true;
  32. this.supportsEdgeIndex = true;
  33. this.ignoresSuppliedIds = true;
  34. this.supportsTransactions = true;
  35. }
  36. /*public void testNeo4jBenchmarkTestSuite() throws Exception {
  37. this.stopWatch();
  38. doTestSuite(new Neo4jBenchmarkTestSuite(this));
  39. printTestPerformance("Neo4jBenchmarkTestSuite", this.stopWatch());
  40. }*/
  41. public void testVertexTestSuite() throws Exception {
  42. this.stopWatch();
  43. doTestSuite(new VertexTestSuite(this));
  44. printTestPerformance("VertexTestSuite", this.stopWatch());
  45. }
  46. public void testEdgeTestSuite() throws Exception {
  47. this.stopWatch();
  48. doTestSuite(new EdgeTestSuite(this));
  49. printTestPerformance("EdgeTestSuite", this.stopWatch());
  50. }
  51. public void testGraphTestSuite() throws Exception {
  52. this.stopWatch();
  53. doTestSuite(new GraphTestSuite(this));
  54. printTestPerformance("GraphTestSuite", this.stopWatch());
  55. }
  56. public void testIndexableGraphTestSuite() throws Exception {
  57. this.stopWatch();
  58. doTestSuite(new IndexableGraphTestSuite(this));
  59. printTestPerformance("IndexableGraphTestSuite", this.stopWatch());
  60. }
  61. public void testIndexTestSuite() throws Exception {
  62. this.stopWatch();
  63. doTestSuite(new IndexTestSuite(this));
  64. printTestPerformance("IndexTestSuite", this.stopWatch());
  65. }
  66. public void testAutomaticIndexTestSuite() throws Exception {
  67. this.stopWatch();
  68. doTestSuite(new AutomaticIndexTestSuite(this));
  69. printTestPerformance("AutomaticIndexTestSuite", this.stopWatch());
  70. }
  71. public void testTransactionalGraphTestSuite() throws Exception {
  72. this.stopWatch();
  73. doTestSuite(new TransactionalGraphTestSuite(this));
  74. printTestPerformance("TransactionalGraphTestSuite", this.stopWatch());
  75. }
  76. public void testGraphMLReaderTestSuite() throws Exception {
  77. this.stopWatch();
  78. doTestSuite(new GraphMLReaderTestSuite(this));
  79. printTestPerformance("GraphMLReaderTestSuite", this.stopWatch());
  80. }
  81. public Graph getGraphInstance() {
  82. String directory = System.getProperty("neo4jGraphDirectory");
  83. if (directory == null)
  84. directory = this.getWorkingDirectory();
  85. return new Neo4jGraph(directory);
  86. }
  87. public void doTestSuite(final TestSuite testSuite) throws Exception {
  88. String doTest = System.getProperty("testNeo4jGraph");
  89. if (doTest == null || doTest.equals("true")) {
  90. String directory = System.getProperty("neo4jGraphDirectory");
  91. if (directory == null)
  92. directory = this.getWorkingDirectory();
  93. deleteDirectory(new File(directory));
  94. for (Method method : testSuite.getClass().getDeclaredMethods()) {
  95. if (method.getName().startsWith("test")) {
  96. System.out.println("Testing " + method.getName() + "...");
  97. method.invoke(testSuite);
  98. deleteDirectory(new File(directory));
  99. }
  100. }
  101. }
  102. }
  103. private String getWorkingDirectory() {
  104. String directory = System.getProperty("neo4jGraphDirectory");
  105. if (directory == null) {
  106. if (System.getProperty("os.name").toUpperCase().contains("WINDOWS"))
  107. directory = "C:/temp/blueprints_test";
  108. else
  109. directory = "/tmp/blueprints_test";
  110. }
  111. return directory;
  112. }
  113. public void testLongIdConversions() {
  114. String id1 = "100"; // good 100
  115. String id2 = "100.0"; // good 100
  116. String id3 = "100.1"; // good 100
  117. String id4 = "one"; // bad
  118. try {
  119. Double.valueOf(id1).longValue();
  120. assertTrue(true);
  121. } catch (NumberFormatException e) {
  122. assertFalse(true);
  123. }
  124. try {
  125. Double.valueOf(id2).longValue();
  126. assertTrue(true);
  127. } catch (NumberFormatException e) {
  128. assertFalse(true);
  129. }
  130. try {
  131. Double.valueOf(id3).longValue();
  132. assertTrue(true);
  133. } catch (NumberFormatException e) {
  134. assertFalse(true);
  135. }
  136. try {
  137. Double.valueOf(id4).longValue();
  138. assertTrue(false);
  139. } catch (NumberFormatException e) {
  140. assertFalse(false);
  141. }
  142. }
  143. public void testQueryIndex() throws Exception {
  144. String directory = System.getProperty("neo4jGraphDirectory");
  145. if (directory == null)
  146. directory = this.getWorkingDirectory();
  147. IndexableGraph graph = new Neo4jGraph(directory);
  148. Vertex a = graph.addVertex(null);
  149. a.setProperty("name", "marko");
  150. Iterator itty = graph.getIndex(Index.VERTICES, Vertex.class).get("name", Neo4jTokens.QUERY_HEADER + "*rko").iterator();
  151. int counter = 0;
  152. while (itty.hasNext()) {
  153. counter++;
  154. assertEquals(itty.next(), a);
  155. }
  156. assertEquals(counter, 1);
  157. Vertex b = graph.addVertex(null);
  158. Edge edge = graph.addEdge(null, a, b, "knows");
  159. edge.setProperty("weight", 0.75);
  160. itty = graph.getIndex(Index.EDGES, Edge.class).get("label", Neo4jTokens.QUERY_HEADER + "k?ows").iterator();
  161. counter = 0;
  162. while (itty.hasNext()) {
  163. counter++;
  164. assertEquals(itty.next(), edge);
  165. }
  166. assertEquals(counter, 1);
  167. itty = graph.getIndex(Index.EDGES, Edge.class).get("weight", Neo4jTokens.QUERY_HEADER + "[0.5 TO 1.0]").iterator();
  168. counter = 0;
  169. while (itty.hasNext()) {
  170. counter++;
  171. assertEquals(itty.next(), edge);
  172. }
  173. assertEquals(counter, 1);
  174. assertEquals(count(graph.getIndex(Index.EDGES, Edge.class).get("weight", Neo4jTokens.QUERY_HEADER + "[0.1 TO 0.5]")), 0);
  175. graph.shutdown();
  176. deleteDirectory(new File(directory));
  177. }
  178. }