public void testSemanticallyCorrectIterables() { Graph graph = graphTest.generateGraph(); for (int i = 0; i < 15; i++) { graph.addEdge(null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); } if (graph.getFeatures().supportsVertexIteration) { Iterable<Vertex> vertices = graph.getVertices(); assertEquals(count(vertices), 30); assertEquals(count(vertices), 30); Iterator<Vertex> itty = vertices.iterator(); int counter = 0; while (itty.hasNext()) { assertTrue(itty.hasNext()); itty.next(); counter++; } assertEquals(counter, 30); } if (graph.getFeatures().supportsEdgeIteration) { Iterable<Edge> edges = graph.getEdges(); assertEquals(count(edges), 15); assertEquals(count(edges), 15); Iterator<Edge> itty = edges.iterator(); int counter = 0; while (itty.hasNext()) { assertTrue(itty.hasNext()); itty.next(); counter++; } assertEquals(counter, 15); } graph.shutdown(); }
public void testAddingDuplicateEdges() { Graph graph = graphTest.generateGraph(); Vertex v1 = graph.addVertex(convertId(graph, "1")); Vertex v2 = graph.addVertex(convertId(graph, "2")); Vertex v3 = graph.addVertex(convertId(graph, "3")); graph.addEdge(null, v1, v2, convertId(graph, "knows")); graph.addEdge(null, v2, v3, convertId(graph, "pets")); graph.addEdge(null, v2, v3, convertId(graph, "pets")); graph.addEdge(null, v2, v3, convertId(graph, "pets")); graph.addEdge(null, v2, v3, convertId(graph, "pets")); if (graph.getFeatures().supportsDuplicateEdges) { if (graph.getFeatures().supportsVertexIteration) assertEquals(3, count(graph.getVertices())); if (graph.getFeatures().supportsEdgeIteration) assertEquals(5, count(graph.getEdges())); assertEquals(0, count(v1.getEdges(Direction.IN))); assertEquals(1, count(v1.getEdges(Direction.OUT))); assertEquals(1, count(v2.getEdges(Direction.IN))); assertEquals(4, count(v2.getEdges(Direction.OUT))); assertEquals(4, count(v3.getEdges(Direction.IN))); assertEquals(0, count(v3.getEdges(Direction.OUT))); } else { if (graph.getFeatures().supportsVertexIteration) assertEquals(count(graph.getVertices()), 3); if (graph.getFeatures().supportsEdgeIteration) assertEquals(count(graph.getEdges()), 2); assertEquals(0, count(v1.getEdges(Direction.IN))); assertEquals(1, count(v1.getEdges(Direction.OUT))); assertEquals(1, count(v2.getEdges(Direction.IN))); assertEquals(1, count(v2.getEdges(Direction.OUT))); assertEquals(1, count(v3.getEdges(Direction.IN))); assertEquals(0, count(v3.getEdges(Direction.OUT))); } graph.shutdown(); }
public void testAddingRemovingEdgeProperties() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsEdgeProperties) { Vertex a = graph.addVertex(convertId(graph, "1")); Vertex b = graph.addVertex(convertId(graph, "2")); Edge edge = graph.addEdge(convertId(graph, "3"), a, b, "knows"); assertEquals(edge.getPropertyKeys().size(), 0); assertNull(edge.getProperty("weight")); if (graph.getFeatures().supportsDoubleProperty) { edge.setProperty("weight", 0.5); assertEquals(edge.getPropertyKeys().size(), 1); assertEquals(edge.getProperty("weight"), 0.5); edge.setProperty("weight", 0.6); assertEquals(edge.getPropertyKeys().size(), 1); assertEquals(edge.getProperty("weight"), 0.6); assertEquals(edge.removeProperty("weight"), 0.6); assertNull(edge.getProperty("weight")); assertEquals(edge.getPropertyKeys().size(), 0); } if (graph.getFeatures().supportsStringProperty) { edge.setProperty("blah", "marko"); edge.setProperty("blah2", "josh"); assertEquals(edge.getPropertyKeys().size(), 2); } } graph.shutdown(); }
public void testConcurrentModification() { Graph graph = graphTest.generateGraph(); Vertex a = graph.addVertex(null); graph.addVertex(null); graph.addVertex(null); if (graph.getFeatures().supportsVertexIteration) { for (Vertex vertex : graph.getVertices()) { graph.addEdge(null, vertex, a, convertId(graph, "x")); graph.addEdge(null, vertex, a, convertId(graph, "y")); } for (Vertex vertex : graph.getVertices()) { assertEquals(BaseTest.count(vertex.getEdges(Direction.OUT)), 2); for (Edge edge : vertex.getEdges(Direction.OUT)) { graph.removeEdge(edge); } } for (Vertex vertex : graph.getVertices()) { graph.removeVertex(vertex); } } else if (graph.getFeatures().supportsEdgeIteration) { for (int i = 0; i < 10; i++) { graph.addEdge(null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "test")); } for (Edge edge : graph.getEdges()) { graph.removeEdge(edge); } } graph.shutdown(); }
public void testEdgeCentricRemoving() { final Graph graph = graphTest.generateGraph(); final Edge a = graph.addEdge( null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); final Edge b = graph.addEdge( null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); final Edge c = graph.addEdge( null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); Object cId = c.getId(); if (graph.getFeatures().supportsEdgeIteration) assertEquals(count(graph.getEdges()), 3); a.remove(); b.remove(); if (graph.getFeatures().supportsEdgeRetrieval) assertNotNull(graph.getEdge(cId)); if (graph.getFeatures().supportsEdgeIteration) assertEquals(count(graph.getEdges()), 1); graph.shutdown(); }
public void testRemoveManyEdges() { Graph graph = graphTest.generateGraph(); long counter = 200000l; int edgeCount = 10; Set<Edge> edges = new HashSet<Edge>(); for (int i = 0; i < edgeCount; i++) { Vertex out = graph.addVertex(convertId(graph, "" + counter++)); Vertex in = graph.addVertex(convertId(graph, "" + counter++)); edges.add(graph.addEdge(null, out, in, convertId(graph, "a" + UUID.randomUUID().toString()))); } assertEquals(edgeCount, edges.size()); if (graph.getFeatures().supportsVertexIteration) { this.stopWatch(); assertEquals(edgeCount * 2, count(graph.getVertices())); printPerformance(graph.toString(), edgeCount * 2, "vertices counted", this.stopWatch()); } if (graph.getFeatures().supportsEdgeIteration) { this.stopWatch(); assertEquals(edgeCount, count(graph.getEdges())); printPerformance(graph.toString(), edgeCount, "edges counted", this.stopWatch()); int i = edgeCount; this.stopWatch(); for (Edge edge : edges) { graph.removeEdge(edge); i--; assertEquals(i, count(graph.getEdges())); if (graph.getFeatures().supportsVertexIteration) { int x = 0; for (Vertex vertex : graph.getVertices()) { if (count(vertex.getEdges(Direction.OUT)) > 0) { assertEquals(1, count(vertex.getEdges(Direction.OUT))); assertFalse(count(vertex.getEdges(Direction.IN)) > 0); } else if (count(vertex.getEdges(Direction.IN)) > 0) { assertEquals(1, count(vertex.getEdges(Direction.IN))); assertFalse(count(vertex.getEdges(Direction.OUT)) > 0); } else { x++; } } assertEquals((edgeCount - i) * 2, x); } } printPerformance( graph.toString(), edgeCount, "edges removed and graph checked", this.stopWatch()); } graph.shutdown(); }
public void testRemoveEdgesByRemovingVertex() { Graph graph = graphTest.generateGraph(); Vertex v1 = graph.addVertex(convertId(graph, "1")); Vertex v2 = graph.addVertex(convertId(graph, "2")); Vertex v3 = graph.addVertex(convertId(graph, "3")); graph.addEdge(null, v1, v2, convertId(graph, "knows")); graph.addEdge(null, v2, v3, convertId(graph, "pets")); graph.addEdge(null, v2, v3, convertId(graph, "pets")); assertEquals(0, count(v1.getEdges(Direction.IN))); assertEquals(1, count(v1.getEdges(Direction.OUT))); assertEquals(1, count(v2.getEdges(Direction.IN))); assertEquals(0, count(v3.getEdges(Direction.OUT))); if (!graph.getFeatures().ignoresSuppliedIds) { v1 = graph.getVertex(convertId(graph, "1")); v2 = graph.getVertex(convertId(graph, "2")); v3 = graph.getVertex(convertId(graph, "3")); assertEquals(0, count(v1.getEdges(Direction.IN))); assertEquals(1, count(v1.getEdges(Direction.OUT))); assertEquals(1, count(v2.getEdges(Direction.IN))); assertEquals(0, count(v3.getEdges(Direction.OUT))); } if (graph.getFeatures().supportsVertexIteration) assertEquals(3, count(graph.getVertices())); graph.removeVertex(v1); if (graph.getFeatures().supportsVertexIteration) assertEquals(2, count(graph.getVertices())); if (graph.getFeatures().supportsDuplicateEdges) assertEquals(2, count(v2.getEdges(Direction.OUT))); else assertEquals(1, count(v2.getEdges(Direction.OUT))); assertEquals(0, count(v3.getEdges(Direction.OUT))); assertEquals(0, count(v2.getEdges(Direction.IN))); if (graph.getFeatures().supportsDuplicateEdges) assertEquals(2, count(v3.getEdges(Direction.IN))); else assertEquals(1, count(v3.getEdges(Direction.IN))); graph.shutdown(); }
public void testNoConcurrentModificationException() { Graph graph = graphTest.generateGraph(); for (int i = 0; i < 25; i++) { graph.addEdge(null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "test")); } if (graph.getFeatures().supportsVertexIteration) assertEquals(BaseTest.count(graph.getVertices()), 50); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(BaseTest.count(graph.getEdges()), 25); for (final Edge edge : graph.getEdges()) { graph.removeEdge(edge); } assertEquals(BaseTest.count(graph.getEdges()), 0); } if (graph.getFeatures().supportsVertexIteration) assertEquals(BaseTest.count(graph.getVertices()), 50); graph.shutdown(); }
public void testRemoveSelfLoops() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsSelfLoops) { Vertex v1 = graph.addVertex(convertId(graph, "1")); Vertex v2 = graph.addVertex(convertId(graph, "2")); Vertex v3 = graph.addVertex(convertId(graph, "3")); Edge e1 = graph.addEdge(null, v1, v1, convertId(graph, "is_self")); Edge e2 = graph.addEdge(null, v2, v2, convertId(graph, "is_self")); Edge e3 = graph.addEdge(null, v3, v3, convertId(graph, "is_self")); if (graph.getFeatures().supportsVertexIteration) assertEquals(3, count(graph.getVertices())); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(3, count(graph.getEdges())); for (Edge edge : graph.getEdges()) { assertEquals(edge.getVertex(Direction.IN), edge.getVertex(Direction.OUT)); assertEquals(edge.getVertex(Direction.IN).getId(), edge.getVertex(Direction.OUT).getId()); } } graph.removeVertex(v1); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(2, count(graph.getEdges())); for (Edge edge : graph.getEdges()) { assertEquals(edge.getVertex(Direction.IN), edge.getVertex(Direction.OUT)); assertEquals(edge.getVertex(Direction.IN).getId(), edge.getVertex(Direction.OUT).getId()); } } assertEquals(1, count(v2.getEdges(Direction.OUT))); assertEquals(1, count(v2.getEdges(Direction.IN))); graph.removeEdge(e2); assertEquals(0, count(v2.getEdges(Direction.OUT))); assertEquals(0, count(v2.getEdges(Direction.IN))); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(count(graph.getEdges()), 1); for (Edge edge : graph.getEdges()) { assertEquals(edge.getVertex(Direction.IN), edge.getVertex(Direction.OUT)); assertEquals(edge.getVertex(Direction.IN).getId(), edge.getVertex(Direction.OUT).getId()); } } } graph.shutdown(); }
public void testRemovingVertices() { Graph graph = graphTest.generateGraph(); int vertexCount = 500; List<Vertex> vertices = new ArrayList<Vertex>(); List<Edge> edges = new ArrayList<Edge>(); this.stopWatch(); for (int i = 0; i < vertexCount; i++) { vertices.add(graph.addVertex(null)); } printPerformance(graph.toString(), vertexCount, "vertices added", this.stopWatch()); this.stopWatch(); for (int i = 0; i < vertexCount; i = i + 2) { Vertex a = vertices.get(i); Vertex b = vertices.get(i + 1); edges.add(graph.addEdge(null, a, b, convertId(graph, "a" + UUID.randomUUID()))); } printPerformance(graph.toString(), vertexCount / 2, "edges added", this.stopWatch()); this.stopWatch(); int counter = 0; for (Vertex v : vertices) { counter = counter + 1; graph.removeVertex(v); if ((counter + 1) % 2 == 0) { if (graph.getFeatures().supportsEdgeIteration) { assertEquals(edges.size() - ((counter + 1) / 2), count(graph.getEdges())); } } if (graph.getFeatures().supportsVertexIteration) { assertEquals(vertices.size() - counter, count(graph.getVertices())); } } printPerformance( graph.toString(), vertexCount, "vertices deleted (with size check on each delete)", this.stopWatch()); graph.shutdown(); }
public void testEdgeIterator() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsEdgeIteration) { Vertex v1 = graph.addVertex(convertId(graph, "1")); Vertex v2 = graph.addVertex(convertId(graph, "2")); Vertex v3 = graph.addVertex(convertId(graph, "3")); Edge e1 = graph.addEdge(null, v1, v2, convertId(graph, "test")); Edge e2 = graph.addEdge(null, v2, v3, convertId(graph, "test")); Edge e3 = graph.addEdge(null, v3, v1, convertId(graph, "test")); if (graph.getFeatures().supportsVertexIteration) assertEquals(3, count(graph.getVertices())); if (graph.getFeatures().supportsEdgeIteration) assertEquals(3, count(graph.getEdges())); Set<String> edgeIds = new HashSet<String>(); int count = 0; for (Edge e : graph.getEdges()) { count++; edgeIds.add(e.getId().toString()); assertEquals(convertId(graph, "test"), e.getLabel()); if (e.getId().toString().equals(e1.getId().toString())) { assertEquals(v1, e.getVertex(Direction.OUT)); assertEquals(v2, e.getVertex(Direction.IN)); } else if (e.getId().toString().equals(e2.getId().toString())) { assertEquals(v2, e.getVertex(Direction.OUT)); assertEquals(v3, e.getVertex(Direction.IN)); } else if (e.getId().toString().equals(e3.getId().toString())) { assertEquals(v3, e.getVertex(Direction.OUT)); assertEquals(v1, e.getVertex(Direction.IN)); } else { assertTrue(false); } // System.out.println(e); } assertEquals(3, count); assertEquals(3, edgeIds.size()); assertTrue(edgeIds.contains(e1.getId().toString())); assertTrue(edgeIds.contains(e2.getId().toString())); assertTrue(edgeIds.contains(e3.getId().toString())); } graph.shutdown(); }
public void testGraphDataPersists() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().isPersistent) { Vertex v = graph.addVertex(null); Vertex u = graph.addVertex(null); if (graph.getFeatures().supportsVertexProperties) { v.setProperty("name", "marko"); u.setProperty("name", "pavel"); } Edge e = graph.addEdge(null, v, u, convertId(graph, "collaborator")); if (graph.getFeatures().supportsEdgeProperties) e.setProperty("location", "internet"); if (graph.getFeatures().supportsVertexIteration) { assertEquals(count(graph.getVertices()), 2); } if (graph.getFeatures().supportsEdgeIteration) { assertEquals(count(graph.getEdges()), 1); } graph.shutdown(); this.stopWatch(); graph = graphTest.generateGraph(); printPerformance(graph.toString(), 1, "graph loaded", this.stopWatch()); if (graph.getFeatures().supportsVertexIteration) { assertEquals(count(graph.getVertices()), 2); if (graph.getFeatures().supportsVertexProperties) { for (Vertex vertex : graph.getVertices()) { assertTrue( vertex.getProperty("name").equals("marko") || vertex.getProperty("name").equals("pavel")); } } } if (graph.getFeatures().supportsEdgeIteration) { assertEquals(count(graph.getEdges()), 1); for (Edge edge : graph.getEdges()) { assertEquals(edge.getLabel(), convertId(graph, "collaborator")); if (graph.getFeatures().supportsEdgeProperties) assertEquals(edge.getProperty("location"), "internet"); } } } graph.shutdown(); }
public void testGettingVerticesAndEdgesWithKeyValue() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsVertexProperties) { Vertex v1 = graph.addVertex(null); v1.setProperty("name", "marko"); v1.setProperty("location", "everywhere"); Vertex v2 = graph.addVertex(null); v2.setProperty("name", "stephen"); v2.setProperty("location", "everywhere"); if (graph.getFeatures().supportsVertexIteration) { assertEquals(count(graph.getVertices("location", "everywhere")), 2); assertEquals(count(graph.getVertices("name", "marko")), 1); assertEquals(count(graph.getVertices("name", "stephen")), 1); assertEquals(getOnlyElement(graph.getVertices("name", "marko")), v1); assertEquals(getOnlyElement(graph.getVertices("name", "stephen")), v2); } } if (graph.getFeatures().supportsEdgeProperties) { Edge e1 = graph.addEdge( null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); e1.setProperty("name", "marko"); e1.setProperty("location", "everywhere"); Edge e2 = graph.addEdge( null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); e2.setProperty("name", "stephen"); e2.setProperty("location", "everywhere"); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(count(graph.getEdges("location", "everywhere")), 2); assertEquals(count(graph.getEdges("name", "marko")), 1); assertEquals(count(graph.getEdges("name", "stephen")), 1); assertEquals(graph.getEdges("name", "marko").iterator().next(), e1); assertEquals(graph.getEdges("name", "stephen").iterator().next(), e2); } } graph.shutdown(); }
public void testRemovingEdges() { Graph graph = graphTest.generateGraph(); int vertexCount = 100; int edgeCount = 200; List<Vertex> vertices = new ArrayList<Vertex>(); List<Edge> edges = new ArrayList<Edge>(); Random random = new Random(); this.stopWatch(); for (int i = 0; i < vertexCount; i++) { vertices.add(graph.addVertex(null)); } printPerformance(graph.toString(), vertexCount, "vertices added", this.stopWatch()); this.stopWatch(); for (int i = 0; i < edgeCount; i++) { Vertex a = vertices.get(random.nextInt(vertices.size())); Vertex b = vertices.get(random.nextInt(vertices.size())); if (a != b) { edges.add(graph.addEdge(null, a, b, convertId(graph, "a" + UUID.randomUUID()))); } } printPerformance(graph.toString(), edgeCount, "edges added", this.stopWatch()); this.stopWatch(); int counter = 0; for (Edge e : edges) { counter = counter + 1; graph.removeEdge(e); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(edges.size() - counter, count(graph.getEdges())); } if (graph.getFeatures().supportsVertexIteration) { assertEquals(vertices.size(), count(graph.getVertices())); } } printPerformance( graph.toString(), edgeCount, "edges deleted (with size check on each delete)", this.stopWatch()); graph.shutdown(); }
public void testAddingVerticesAndEdges() { Graph graph = graphTest.generateGraph(); Vertex a = graph.addVertex(null); Vertex b = graph.addVertex(null); Edge edge = graph.addEdge(null, a, b, convertId(graph, "knows")); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(1, count(graph.getEdges())); } if (graph.getFeatures().supportsVertexIteration) { assertEquals(2, count(graph.getVertices())); } graph.removeVertex(a); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(0, count(graph.getEdges())); } if (graph.getFeatures().supportsVertexIteration) { assertEquals(1, count(graph.getVertices())); } try { graph.removeEdge(edge); // TODO: doesn't work with wrapper graphs assertTrue(false); } catch (Exception e) { assertTrue(true); } if (graph.getFeatures().supportsEdgeIteration) { assertEquals(0, count(graph.getEdges())); } if (graph.getFeatures().supportsVertexIteration) { assertEquals(1, count(graph.getVertices())); } graph.shutdown(); }
public void testSettingProperties() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsEdgeProperties) { Vertex a = graph.addVertex(null); Vertex b = graph.addVertex(null); graph.addEdge(null, a, b, convertId(graph, "knows")); graph.addEdge(null, a, b, convertId(graph, "knows")); for (Edge edge : b.getEdges(Direction.IN)) { edge.setProperty("key", "value"); } } graph.shutdown(); }
public void testSimpleRemovingVerticesEdges() { Graph graph = graphTest.generateGraph(); Vertex v = graph.addVertex(null); Vertex u = graph.addVertex(null); Edge e = graph.addEdge(null, v, u, convertId(graph, "knows")); if (graph.getFeatures().supportsVertexIteration) assertEquals(count(graph.getVertices()), 2); if (graph.getFeatures().supportsEdgeIteration) assertEquals(count(graph.getEdges()), 1); assertEquals(v.getEdges(Direction.OUT).iterator().next().getVertex(Direction.IN), u); assertEquals(u.getEdges(Direction.IN).iterator().next().getVertex(Direction.OUT), v); assertEquals(v.getEdges(Direction.OUT).iterator().next(), e); assertEquals(u.getEdges(Direction.IN).iterator().next(), e); graph.removeVertex(v); // TODO: DEX // assertFalse(v.getEdges(Direction.OUT).iterator().hasNext()); if (graph.getFeatures().supportsVertexIteration) assertEquals(count(graph.getVertices()), 1); if (graph.getFeatures().supportsEdgeIteration) assertEquals(count(graph.getEdges()), 0); graph.shutdown(); }
public void testAddingSelfLoops() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsSelfLoops) { Vertex v1 = graph.addVertex(convertId(graph, "1")); Vertex v2 = graph.addVertex(convertId(graph, "2")); Vertex v3 = graph.addVertex(convertId(graph, "3")); graph.addEdge(null, v1, v1, convertId(graph, "is_self")); graph.addEdge(null, v2, v2, convertId(graph, "is_self")); graph.addEdge(null, v3, v3, convertId(graph, "is_self")); if (graph.getFeatures().supportsVertexIteration) assertEquals(3, count(graph.getVertices())); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(3, count(graph.getEdges())); int counter = 0; for (Edge edge : graph.getEdges()) { counter++; assertEquals(edge.getVertex(Direction.IN), edge.getVertex(Direction.OUT)); assertEquals(edge.getVertex(Direction.IN).getId(), edge.getVertex(Direction.OUT).getId()); } assertEquals(counter, 3); } } graph.shutdown(); }
public void testAddManyEdges() { Graph graph = graphTest.generateGraph(); int edgeCount = 100; int vertexCount = 200; long counter = 0l; this.stopWatch(); for (int i = 0; i < edgeCount; i++) { Vertex out = graph.addVertex(convertId(graph, "" + counter++)); Vertex in = graph.addVertex(convertId(graph, "" + counter++)); graph.addEdge(null, out, in, convertId(graph, UUID.randomUUID().toString())); } printPerformance(graph.toString(), vertexCount + edgeCount, "elements added", this.stopWatch()); if (graph.getFeatures().supportsEdgeIteration) { this.stopWatch(); assertEquals(edgeCount, count(graph.getEdges())); printPerformance(graph.toString(), edgeCount, "edges counted", this.stopWatch()); } if (graph.getFeatures().supportsVertexIteration) { this.stopWatch(); assertEquals(vertexCount, count(graph.getVertices())); printPerformance(graph.toString(), vertexCount, "vertices counted", this.stopWatch()); this.stopWatch(); for (Vertex vertex : graph.getVertices()) { if (count(vertex.getEdges(Direction.OUT)) > 0) { assertEquals(1, count(vertex.getEdges(Direction.OUT))); assertFalse(count(vertex.getEdges(Direction.IN)) > 0); } else { assertEquals(1, count(vertex.getEdges(Direction.IN))); assertFalse(count(vertex.getEdges(Direction.OUT)) > 0); } } printPerformance(graph.toString(), vertexCount, "vertices checked", this.stopWatch()); } graph.shutdown(); }
public void testEmptyKeyProperty() { final Graph graph = graphTest.generateGraph(); // no point in testing graph features for setting string properties because the intent is for it // to // fail based on the empty key. if (graph.getFeatures().supportsEdgeProperties) { final Edge e = graph.addEdge(null, graph.addVertex(null), graph.addVertex(null), "friend"); try { e.setProperty("", "value"); fail(); } catch (IllegalArgumentException e1) { assertTrue(true); } } graph.shutdown(); }
public void testGetNonExistantEdges() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsEdgeRetrieval) { try { graph.getEdge(null); fail("Getting an element with a null identifier must throw IllegalArgumentException"); } catch (IllegalArgumentException iae) { assertTrue(true); } assertNull(graph.getEdge("asbv")); assertNull(graph.getEdge(12.0d)); } graph.shutdown(); }
public void testGetEdges() { Graph graph = graphTest.generateGraph(); Vertex v1 = graph.addVertex(null); Vertex v2 = graph.addVertex(null); Vertex v3 = graph.addVertex(null); Edge e1 = graph.addEdge(null, v1, v2, convertId(graph, "test1")); Edge e2 = graph.addEdge(null, v2, v3, convertId(graph, "test2")); Edge e3 = graph.addEdge(null, v3, v1, convertId(graph, "test3")); if (graph.getFeatures().supportsEdgeRetrieval) { this.stopWatch(); assertEquals(graph.getEdge(e1.getId()), e1); assertEquals(graph.getEdge(e1.getId()).getVertex(Direction.IN), v2); assertEquals(graph.getEdge(e1.getId()).getVertex(Direction.OUT), v1); assertEquals(graph.getEdge(e2.getId()), e2); assertEquals(graph.getEdge(e2.getId()).getVertex(Direction.IN), v3); assertEquals(graph.getEdge(e2.getId()).getVertex(Direction.OUT), v2); assertEquals(graph.getEdge(e3.getId()), e3); assertEquals(graph.getEdge(e3.getId()).getVertex(Direction.IN), v1); assertEquals(graph.getEdge(e3.getId()).getVertex(Direction.OUT), v3); printPerformance(graph.toString(), 3, "edges retrieved", this.stopWatch()); } assertEquals(getOnlyElement(v1.getEdges(Direction.OUT)), e1); assertEquals(getOnlyElement(v1.getEdges(Direction.OUT)).getVertex(Direction.IN), v2); assertEquals(getOnlyElement(v1.getEdges(Direction.OUT)).getVertex(Direction.OUT), v1); assertEquals(getOnlyElement(v2.getEdges(Direction.OUT)), e2); assertEquals(getOnlyElement(v2.getEdges(Direction.OUT)).getVertex(Direction.IN), v3); assertEquals(getOnlyElement(v2.getEdges(Direction.OUT)).getVertex(Direction.OUT), v2); assertEquals(getOnlyElement(v3.getEdges(Direction.OUT)), e3); assertEquals(getOnlyElement(v3.getEdges(Direction.OUT)).getVertex(Direction.IN), v1); assertEquals(getOnlyElement(v3.getEdges(Direction.OUT)).getVertex(Direction.OUT), v3); graph.shutdown(); }
public void testAddingLabelAndIdProperty() { Graph graph = graphTest.generateGraph(); // no point in testing graph features for setting string properties because the intent is for it // to // fail based on the id or label properties. if (graph.getFeatures().supportsEdgeProperties) { Edge edge = graph.addEdge(null, graph.addVertex(null), graph.addVertex(null), "knows"); try { edge.setProperty("id", "123"); fail(); } catch (RuntimeException e) { } try { edge.setProperty("label", "hates"); fail(); } catch (RuntimeException e) { } } graph.shutdown(); }
public void testEdgeEquality() { Graph graph = graphTest.generateGraph(); Vertex v = graph.addVertex(convertId(graph, "1")); Vertex u = graph.addVertex(convertId(graph, "2")); Edge e = graph.addEdge(null, v, u, convertId(graph, "knows")); assertEquals(e.getLabel(), convertId(graph, "knows")); assertEquals(e.getVertex(Direction.IN), u); assertEquals(e.getVertex(Direction.OUT), v); assertEquals(e, v.getEdges(Direction.OUT).iterator().next()); assertEquals(e, u.getEdges(Direction.IN).iterator().next()); assertEquals( v.getEdges(Direction.OUT).iterator().next(), u.getEdges(Direction.IN).iterator().next()); Set<Edge> set = new HashSet<Edge>(); set.add(e); set.add(e); set.add(v.getEdges(Direction.OUT).iterator().next()); set.add(v.getEdges(Direction.OUT).iterator().next()); set.add(u.getEdges(Direction.IN).iterator().next()); set.add(u.getEdges(Direction.IN).iterator().next()); if (graph.getFeatures().supportsEdgeIteration) set.add(graph.getEdges().iterator().next()); assertEquals(set.size(), 1); graph.shutdown(); }
public void testTreeConnectivity() { Graph graph = graphTest.generateGraph(); this.stopWatch(); int branchSize = 11; Vertex start = graph.addVertex(null); for (int i = 0; i < branchSize; i++) { Vertex a = graph.addVertex(null); graph.addEdge(null, start, a, convertId(graph, "test1")); for (int j = 0; j < branchSize; j++) { Vertex b = graph.addVertex(null); graph.addEdge(null, a, b, convertId(graph, "test2")); for (int k = 0; k < branchSize; k++) { Vertex c = graph.addVertex(null); graph.addEdge(null, b, c, convertId(graph, "test3")); } } } assertEquals(0, count(start.getEdges(Direction.IN))); assertEquals(branchSize, count(start.getEdges(Direction.OUT))); for (Edge e : start.getEdges(Direction.OUT)) { assertEquals(convertId(graph, "test1"), e.getLabel()); assertEquals(branchSize, count(e.getVertex(Direction.IN).getEdges(Direction.OUT))); assertEquals(1, count(e.getVertex(Direction.IN).getEdges(Direction.IN))); for (Edge f : e.getVertex(Direction.IN).getEdges(Direction.OUT)) { assertEquals(convertId(graph, "test2"), f.getLabel()); assertEquals(branchSize, count(f.getVertex(Direction.IN).getEdges(Direction.OUT))); assertEquals(1, count(f.getVertex(Direction.IN).getEdges(Direction.IN))); for (Edge g : f.getVertex(Direction.IN).getEdges(Direction.OUT)) { assertEquals(convertId(graph, "test3"), g.getLabel()); assertEquals(0, count(g.getVertex(Direction.IN).getEdges(Direction.OUT))); assertEquals(1, count(g.getVertex(Direction.IN).getEdges(Direction.IN))); } } } int totalVertices = 0; for (int i = 0; i < 4; i++) { totalVertices = totalVertices + (int) Math.pow(branchSize, i); } printPerformance( graph.toString(), totalVertices, "vertices added in a tree structure", this.stopWatch()); if (graph.getFeatures().supportsVertexIteration) { this.stopWatch(); Set<Vertex> vertices = new HashSet<Vertex>(); for (Vertex v : graph.getVertices()) { vertices.add(v); } assertEquals(totalVertices, vertices.size()); printPerformance(graph.toString(), totalVertices, "vertices iterated", this.stopWatch()); } if (graph.getFeatures().supportsEdgeIteration) { this.stopWatch(); Set<Edge> edges = new HashSet<Edge>(); for (Edge e : graph.getEdges()) { edges.add(e); } assertEquals(totalVertices - 1, edges.size()); printPerformance(graph.toString(), totalVertices - 1, "edges iterated", this.stopWatch()); } graph.shutdown(); }
public void testConnectivityPatterns() { Graph graph = graphTest.generateGraph(); Vertex a = graph.addVertex(convertId(graph, "1")); Vertex b = graph.addVertex(convertId(graph, "2")); Vertex c = graph.addVertex(convertId(graph, "3")); Vertex d = graph.addVertex(convertId(graph, "4")); if (graph.getFeatures().supportsVertexIteration) assertEquals(4, count(graph.getVertices())); Edge e = graph.addEdge(null, a, b, convertId(graph, "knows")); Edge f = graph.addEdge(null, b, c, convertId(graph, "knows")); Edge g = graph.addEdge(null, c, d, convertId(graph, "knows")); Edge h = graph.addEdge(null, d, a, convertId(graph, "knows")); if (graph.getFeatures().supportsEdgeIteration) assertEquals(4, count(graph.getEdges())); if (graph.getFeatures().supportsVertexIteration) { for (Vertex v : graph.getVertices()) { assertEquals(1, count(v.getEdges(Direction.OUT))); assertEquals(1, count(v.getEdges(Direction.IN))); } } if (graph.getFeatures().supportsEdgeIteration) { for (Edge x : graph.getEdges()) { assertEquals(convertId(graph, "knows"), x.getLabel()); } } if (!graph.getFeatures().ignoresSuppliedIds) { a = graph.getVertex(convertId(graph, "1")); b = graph.getVertex(convertId(graph, "2")); c = graph.getVertex(convertId(graph, "3")); d = graph.getVertex(convertId(graph, "4")); assertEquals(1, count(a.getEdges(Direction.IN))); assertEquals(1, count(a.getEdges(Direction.OUT))); assertEquals(1, count(b.getEdges(Direction.IN))); assertEquals(1, count(b.getEdges(Direction.OUT))); assertEquals(1, count(c.getEdges(Direction.IN))); assertEquals(1, count(c.getEdges(Direction.OUT))); assertEquals(1, count(d.getEdges(Direction.IN))); assertEquals(1, count(d.getEdges(Direction.OUT))); Edge i = graph.addEdge(null, a, b, convertId(graph, "hates")); assertEquals(1, count(a.getEdges(Direction.IN))); assertEquals(2, count(a.getEdges(Direction.OUT))); assertEquals(2, count(b.getEdges(Direction.IN))); assertEquals(1, count(b.getEdges(Direction.OUT))); assertEquals(1, count(c.getEdges(Direction.IN))); assertEquals(1, count(c.getEdges(Direction.OUT))); assertEquals(1, count(d.getEdges(Direction.IN))); assertEquals(1, count(d.getEdges(Direction.OUT))); assertEquals(1, count(a.getEdges(Direction.IN))); assertEquals(2, count(a.getEdges(Direction.OUT))); for (Edge x : a.getEdges(Direction.OUT)) { assertTrue( x.getLabel().equals(convertId(graph, "knows")) || x.getLabel().equals(convertId(graph, "hates"))); } assertEquals(convertId(graph, "hates"), i.getLabel()); assertEquals(i.getVertex(Direction.IN).getId().toString(), convertId(graph, "2")); assertEquals(i.getVertex(Direction.OUT).getId().toString(), convertId(graph, "1")); } Set<Object> vertexIds = new HashSet<Object>(); vertexIds.add(a.getId()); vertexIds.add(a.getId()); vertexIds.add(b.getId()); vertexIds.add(b.getId()); vertexIds.add(c.getId()); vertexIds.add(d.getId()); vertexIds.add(d.getId()); vertexIds.add(d.getId()); assertEquals(4, vertexIds.size()); graph.shutdown(); }
public void testDataTypeValidationOnProperties() { final Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsElementProperties() && !graph.getFeatures().isWrapper) { final Vertex vertexA = graph.addVertex(null); final Vertex vertexB = graph.addVertex(null); final Edge edge = graph.addEdge(null, vertexA, vertexB, convertId(graph, "knows")); trySetProperty(vertexA, "keyString", "value", graph.getFeatures().supportsStringProperty); trySetProperty(edge, "keyString", "value", graph.getFeatures().supportsStringProperty); trySetProperty(vertexA, "keyInteger", 100, graph.getFeatures().supportsIntegerProperty); trySetProperty(edge, "keyInteger", 100, graph.getFeatures().supportsIntegerProperty); trySetProperty(vertexA, "keyLong", 10000L, graph.getFeatures().supportsLongProperty); trySetProperty(edge, "keyLong", 10000L, graph.getFeatures().supportsLongProperty); trySetProperty(vertexA, "keyDouble", 100.321d, graph.getFeatures().supportsDoubleProperty); trySetProperty(edge, "keyDouble", 100.321d, graph.getFeatures().supportsDoubleProperty); trySetProperty(vertexA, "keyFloat", 100.321f, graph.getFeatures().supportsFloatProperty); trySetProperty(edge, "keyFloat", 100.321f, graph.getFeatures().supportsFloatProperty); trySetProperty(vertexA, "keyBoolean", true, graph.getFeatures().supportsBooleanProperty); trySetProperty(edge, "keyBoolean", true, graph.getFeatures().supportsBooleanProperty); trySetProperty( vertexA, "keyDate", new Date(), graph.getFeatures().supportsSerializableObjectProperty); trySetProperty( edge, "keyDate", new Date(), graph.getFeatures().supportsSerializableObjectProperty); final ArrayList<String> listA = new ArrayList<String>(); listA.add("try1"); listA.add("try2"); trySetProperty( vertexA, "keyListString", listA, graph.getFeatures().supportsUniformListProperty); trySetProperty(edge, "keyListString", listA, graph.getFeatures().supportsUniformListProperty); final ArrayList listB = new ArrayList(); listB.add("try1"); listB.add(2); trySetProperty(vertexA, "keyListMixed", listB, graph.getFeatures().supportsMixedListProperty); trySetProperty(edge, "keyListMixed", listB, graph.getFeatures().supportsMixedListProperty); trySetProperty( vertexA, "keyArrayString", new String[] {"try1", "try2"}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( edge, "keyArrayString", new String[] {"try1", "try2"}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( vertexA, "keyArrayInteger", new int[] {1, 2}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( edge, "keyArrayInteger", new int[] {1, 2}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( vertexA, "keyArrayLong", new long[] {1000l, 2000l}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( edge, "keyArrayLong", new long[] {1000l, 2000l}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( vertexA, "keyArrayFloat", new float[] {1000.321f, 2000.321f}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( edge, "keyArrayFloat", new float[] {1000.321f, 2000.321f}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( vertexA, "keyArrayDouble", new double[] {1000.321d, 2000.321d}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( edge, "keyArrayDouble", new double[] {1000.321d, 2000.321d}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( vertexA, "keyArrayBoolean", new boolean[] {false, true}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( edge, "keyArrayBoolean", new boolean[] {false, true}, graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( vertexA, "keyArrayEmpty", new int[0], graph.getFeatures().supportsPrimitiveArrayProperty); trySetProperty( edge, "keyArrayEmpty", new int[0], graph.getFeatures().supportsPrimitiveArrayProperty); final Map map = new HashMap(); map.put("testString", "try"); map.put("testInteger", "string"); trySetProperty(vertexA, "keyMap", map, graph.getFeatures().supportsMapProperty); trySetProperty(edge, "keyMap", map, graph.getFeatures().supportsMapProperty); final MockSerializable mockSerializable = new MockSerializable(); mockSerializable.setTestField("test"); trySetProperty( vertexA, "keySerializable", mockSerializable, graph.getFeatures().supportsSerializableObjectProperty); trySetProperty( edge, "keySerializable", mockSerializable, graph.getFeatures().supportsSerializableObjectProperty); } graph.shutdown(); }
public void testFeatureCompliance() { Graph graph = graphTest.generateGraph(); graph.getFeatures().checkCompliance(); System.out.println(graph.getFeatures()); graph.shutdown(); }
public void testEmptyOnConstruction() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsVertexIteration) assertEquals(0, count(graph.getVertices())); if (graph.getFeatures().supportsEdgeIteration) assertEquals(0, count(graph.getEdges())); graph.shutdown(); }