public Node removeNode(SimpleUri nodeUri) { Preconditions.checkNotNull(nodeUri, "nodeUri cannot be null!"); Node node = findNode(nodeUri); if (node == null) { throw new RuntimeException("Node removal failure: there is no '" + nodeUri + "' in the render graph!"); } if (graph.adjacentNodes(node).size() != 0) { throw new RuntimeException("Node removal failure: node '" + nodeUri + "' is still connected to other nodes in the render graph!"); } nodeMap.remove(nodeUri); return nodeMap.remove(nodeUri); }
@Test public void adjacentNodes_oneEdge() { putEdge(N1, N2); assertThat(graph.adjacentNodes(N1)).containsExactly(N2); assertThat(graph.adjacentNodes(N2)).containsExactly(N1); }
@Override @Test public void adjacentNodes_checkReturnedSetMutability() { addNode(N1); Set<Integer> adjacentNodes = graph.adjacentNodes(N1); try { adjacentNodes.add(N2); fail(ERROR_MODIFIABLE_SET); } catch (UnsupportedOperationException e) { putEdge(N1, N2); assertThat(graph.adjacentNodes(N1)).containsExactlyElementsIn(adjacentNodes); } }
@Override @Test public void adjacentNodes_checkReturnedSetMutability() { addNode(N1); Set<Integer> adjacentNodes = graph.adjacentNodes(N1); try { adjacentNodes.add(N2); fail(ERROR_MODIFIABLE_SET); } catch (UnsupportedOperationException e) { putEdge(N1, N2); assertThat(graph.adjacentNodes(N1)).containsExactlyElementsIn(adjacentNodes); } }
@Test public void adjacentNodes_noAdjacentNodes() { addNode(N1); assertThat(graph.adjacentNodes(N1)).isEmpty(); }
@Override @Test public void addEdge_selfLoop() { assertThat(putEdge(N1, N1)).isTrue(); assertThat(graph.adjacentNodes(N1)).containsExactly(N1); }
@Test public void removeNode_queryAfterRemoval() { addNode(N1); @SuppressWarnings("unused") Set<Integer> unused = graph.adjacentNodes(N1); // ensure cache (if any) is populated assertThat(graph.removeNode(N1)).isTrue(); try { graph.adjacentNodes(N1); fail(ERROR_NODE_NOT_IN_GRAPH); } catch (IllegalArgumentException e) { assertNodeNotInGraphErrorMessage(e); } }
@Test public void adjacentNodes_selfLoop() { putEdge(N1, N1); putEdge(N1, N2); assertThat(graph.adjacentNodes(N1)).containsExactly(N1, N2); }
@Test public void adjacentNodes_nodeNotInGraph() { try { graph.adjacentNodes(NODE_NOT_IN_GRAPH); fail(ERROR_NODE_NOT_IN_GRAPH); } catch (IllegalArgumentException e) { assertNodeNotInGraphErrorMessage(e); } }
@Test public void adjacentNodes_selfLoop() { putEdge(N1, N1); putEdge(N1, N2); assertThat(graph.adjacentNodes(N1)).containsExactly(N1, N2); }
@Test public void removeNode_existingNode() { putEdge(N1, N2); putEdge(N4, N1); assertThat(graph.removeNode(N1)).isTrue(); assertThat(graph.removeNode(N1)).isFalse(); assertThat(graph.nodes()).containsExactly(N2, N4); assertThat(graph.adjacentNodes(N2)).isEmpty(); assertThat(graph.adjacentNodes(N4)).isEmpty(); }
/** * This test checks an implementation dependent feature. It tests that the method {@code addEdge} * will silently add the missing nodes to the graph, then add the edge connecting them. We are not * using the proxy methods here as we want to test {@code addEdge} when the end-points are not * elements of the graph. */ @Test public void addEdge_nodesNotInGraph() { graph.addNode(N1); assertTrue(graph.putEdge(N1, N5)); assertTrue(graph.putEdge(N4, N1)); assertTrue(graph.putEdge(N2, N3)); assertThat(graph.nodes()).containsExactly(N1, N5, N4, N2, N3).inOrder(); assertThat(graph.adjacentNodes(N1)).containsExactly(N4, N5); assertThat(graph.adjacentNodes(N2)).containsExactly(N3); assertThat(graph.adjacentNodes(N3)).containsExactly(N2); assertThat(graph.adjacentNodes(N4)).containsExactly(N1); assertThat(graph.adjacentNodes(N5)).containsExactly(N1); } }
@Test public void removeEdge_existingSelfLoopEdge() { putEdge(N1, N1); assertThat(graph.removeEdge(N1, N1)).isTrue(); assertThat(graph.nodes()).containsExactly(N1); assertThat(graph.adjacentNodes(N1)).isEmpty(); } }
@Test public void removeEdge_oneOfMany() { putEdge(N1, N2); putEdge(N1, N3); putEdge(N1, N4); assertThat(graph.removeEdge(N1, N3)).isTrue(); assertThat(graph.adjacentNodes(N1)).containsExactly(N2, N4); }
@Test public void removeEdge_antiparallelEdges() { putEdge(N1, N2); putEdge(N2, N1); // no-op assertThat(graph.removeEdge(N1, N2)).isTrue(); assertThat(graph.adjacentNodes(N1)).isEmpty(); assertThat(graph.edges()).isEmpty(); assertThat(graph.removeEdge(N2, N1)).isFalse(); } }
@After public void validateUndirectedEdges() { for (Integer node : graph.nodes()) { new EqualsTester() .addEqualityGroup( graph.predecessors(node), graph.successors(node), graph.adjacentNodes(node)) .testEquals(); } }
@Override public Set<N> adjacentNodes(N node) { checkNotNull(node, "node"); checkArgument(delegate.nodes().contains(node), NODE_NOT_IN_TREE, node); return delegate.adjacentNodes(node); }
@Test public void adjacentNodes_selfLoop() { putEdge(N1, N1); putEdge(N1, N2); assertThat(graph.adjacentNodes(N1)).containsExactly(N1, N2); }
@Test public void adjacentNodes_nodeNotInGraph() { try { graph.adjacentNodes(NODE_NOT_IN_GRAPH); fail(ERROR_NODE_NOT_IN_GRAPH); } catch (IllegalArgumentException e) { assertNodeNotInGraphErrorMessage(e); } }
@After public void validateUndirectedEdges() { for (Integer node : graph.nodes()) { new EqualsTester() .addEqualityGroup( graph.predecessors(node), graph.successors(node), graph.adjacentNodes(node)) .testEquals(); } }