diff options
Diffstat (limited to 'guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java')
-rw-r--r-- | guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java | 253 |
1 files changed, 104 insertions, 149 deletions
diff --git a/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java b/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java index 39d82ee87..bb90a6813 100644 --- a/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java +++ b/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java @@ -20,6 +20,7 @@ import static com.google.common.graph.GraphConstants.ENDPOINTS_MISMATCH; import static com.google.common.graph.TestUtil.assertEdgeNotInGraphErrorMessage; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.TruthJUnit.assume; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -65,13 +66,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor assume().that(graphIsMutable()).isTrue(); Set<Integer> nodes = network.nodes(); - try { - nodes.add(N2); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - addNode(N1); - assertThat(network.nodes()).containsExactlyElementsIn(nodes); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> nodes.add(N2)); + addNode(N1); + assertThat(network.nodes()).containsExactlyElementsIn(nodes); } @Override @@ -80,13 +78,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor assume().that(graphIsMutable()).isTrue(); Set<String> edges = network.edges(); - try { - edges.add(E12); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - addEdge(N1, N2, E12); - assertThat(network.edges()).containsExactlyElementsIn(edges); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> edges.add(E12)); + addEdge(N1, N2, E12); + assertThat(network.edges()).containsExactlyElementsIn(edges); } @Override @@ -96,13 +91,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor addNode(N1); Set<String> incidentEdges = network.incidentEdges(N1); - try { - incidentEdges.add(E12); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - addEdge(N1, N2, E12); - assertThat(network.incidentEdges(N1)).containsExactlyElementsIn(incidentEdges); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> incidentEdges.add(E12)); + addEdge(N1, N2, E12); + assertThat(network.incidentEdges(N1)).containsExactlyElementsIn(incidentEdges); } @Override @@ -112,13 +104,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor addNode(N1); Set<Integer> adjacentNodes = network.adjacentNodes(N1); - try { - adjacentNodes.add(N2); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - addEdge(N1, N2, E12); - assertThat(network.adjacentNodes(N1)).containsExactlyElementsIn(adjacentNodes); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> adjacentNodes.add(N2)); + addEdge(N1, N2, E12); + assertThat(network.adjacentNodes(N1)).containsExactlyElementsIn(adjacentNodes); } @Override @@ -144,13 +133,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor addNode(N1); addNode(N2); Set<String> edgesConnecting = network.edgesConnecting(N1, N2); - try { - edgesConnecting.add(E23); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - addEdge(N1, N2, E12); - assertThat(network.edgesConnecting(N1, N2)).containsExactlyElementsIn(edgesConnecting); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> edgesConnecting.add(E23)); + addEdge(N1, N2, E12); + assertThat(network.edgesConnecting(N1, N2)).containsExactlyElementsIn(edgesConnecting); } @Override @@ -160,13 +146,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor addNode(N2); Set<String> inEdges = network.inEdges(N2); - try { - inEdges.add(E12); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - addEdge(N1, N2, E12); - assertThat(network.inEdges(N2)).containsExactlyElementsIn(inEdges); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> inEdges.add(E12)); + addEdge(N1, N2, E12); + assertThat(network.inEdges(N2)).containsExactlyElementsIn(inEdges); } @Override @@ -176,13 +159,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor addNode(N1); Set<String> outEdges = network.outEdges(N1); - try { - outEdges.add(E12); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - addEdge(N1, N2, E12); - assertThat(network.outEdges(N1)).containsExactlyElementsIn(outEdges); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> outEdges.add(E12)); + addEdge(N1, N2, E12); + assertThat(network.outEdges(N1)).containsExactlyElementsIn(outEdges); } @Override @@ -192,13 +172,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor addNode(N2); Set<Integer> predecessors = network.predecessors(N2); - try { - predecessors.add(N1); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - addEdge(N1, N2, E12); - assertThat(network.predecessors(N2)).containsExactlyElementsIn(predecessors); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> predecessors.add(N1)); + addEdge(N1, N2, E12); + assertThat(network.predecessors(N2)).containsExactlyElementsIn(predecessors); } @Override @@ -208,13 +185,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor addNode(N1); Set<Integer> successors = network.successors(N1); - try { - successors.add(N2); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - addEdge(N1, N2, E12); - assertThat(successors).containsExactlyElementsIn(network.successors(N1)); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> successors.add(N2)); + addEdge(N1, N2, E12); + assertThat(successors).containsExactlyElementsIn(network.successors(N1)); } @Test @@ -229,34 +203,37 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor @Test public void edgesConnecting_orderMismatch() { addEdge(N1, N2, E12); - try { - Set<String> unused = network.edgesConnecting(EndpointPair.unordered(N1, N2)); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> { + Set<String> unused = network.edgesConnecting(EndpointPair.unordered(N1, N2)); + }); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test public void edgeConnecting_orderMismatch() { addEdge(N1, N2, E12); - try { - Optional<String> unused = network.edgeConnecting(EndpointPair.unordered(N1, N2)); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> { + Optional<String> unused = network.edgeConnecting(EndpointPair.unordered(N1, N2)); + }); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test public void edgeConnectingOrNull_orderMismatch() { addEdge(N1, N2, E12); - try { - String unused = network.edgeConnectingOrNull(EndpointPair.unordered(N1, N2)); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> { + String unused = network.edgeConnectingOrNull(EndpointPair.unordered(N1, N2)); + }); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Override @@ -316,12 +293,11 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor @Test public void source_edgeNotInGraph() { - try { - network.incidentNodes(EDGE_NOT_IN_GRAPH).source(); - fail(ERROR_EDGE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertEdgeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> network.incidentNodes(EDGE_NOT_IN_GRAPH).source()); + assertEdgeNotInGraphErrorMessage(e); } @Test @@ -332,12 +308,11 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor @Test public void target_edgeNotInGraph() { - try { - network.incidentNodes(EDGE_NOT_IN_GRAPH).target(); - fail(ERROR_EDGE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertEdgeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> network.incidentNodes(EDGE_NOT_IN_GRAPH).target()); + assertEdgeNotInGraphErrorMessage(e); } @Test @@ -528,20 +503,12 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor assume().that(graphIsMutable()).isTrue(); addEdge(N1, N2, E12); - try { - // Edge between totally different nodes - networkAsMutableNetwork.addEdge(N4, N5, E12); - fail(ERROR_ADDED_EXISTING_EDGE); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); - } - try { - // Edge between same nodes but in reverse direction - addEdge(N2, N1, E12); - fail(ERROR_ADDED_EXISTING_EDGE); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N4, N5, E12)); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); + e = assertThrows(IllegalArgumentException.class, () -> addEdge(N2, N1, E12)); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); } @Test @@ -550,12 +517,11 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor assume().that(network.allowsParallelEdges()).isFalse(); addEdge(N1, N2, E12); - try { - networkAsMutableNetwork.addEdge(N1, N2, EDGE_NOT_IN_GRAPH); - fail(ERROR_ADDED_PARALLEL_EDGE); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> networkAsMutableNetwork.addEdge(N1, N2, EDGE_NOT_IN_GRAPH)); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); } @Test @@ -573,12 +539,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor assume().that(graphIsMutable()).isTrue(); EndpointPair<Integer> endpoints = EndpointPair.unordered(N1, N2); - try { - networkAsMutableNetwork.addEdge(endpoints, E12); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(endpoints, E12)); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test @@ -586,12 +550,10 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor assume().that(graphIsMutable()).isTrue(); assume().that(network.allowsSelfLoops()).isFalse(); - try { - networkAsMutableNetwork.addEdge(N1, N1, E11); - fail(ERROR_ADDED_SELF_LOOP); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N1, E11)); + assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP); } /** @@ -644,25 +606,19 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor assume().that(network.allowsSelfLoops()).isTrue(); addEdge(N1, N1, E11); - try { - networkAsMutableNetwork.addEdge(N1, N2, E11); - fail("Reusing an existing self-loop edge to connect different nodes succeeded"); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); - } - try { - networkAsMutableNetwork.addEdge(N2, N2, E11); - fail("Reusing an existing self-loop edge to make a different self-loop edge succeeded"); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N2, E11)); + assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); + e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N2, N2, E11)); + assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); addEdge(N1, N2, E12); - try { - networkAsMutableNetwork.addEdge(N1, N1, E12); - fail("Reusing an existing edge to add a self-loop edge between different nodes succeeded"); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); - } + e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N1, E12)); + assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE); } @Test @@ -672,12 +628,11 @@ public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetwor assume().that(network.allowsParallelEdges()).isFalse(); addEdge(N1, N1, E11); - try { - networkAsMutableNetwork.addEdge(N1, N1, EDGE_NOT_IN_GRAPH); - fail("Adding a parallel self-loop edge succeeded"); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> networkAsMutableNetwork.addEdge(N1, N1, EDGE_NOT_IN_GRAPH)); + assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); } @Test |