diff options
Diffstat (limited to 'android/guava-tests/test/com/google/common/graph')
12 files changed, 476 insertions, 726 deletions
diff --git a/android/guava-tests/test/com/google/common/graph/AbstractGraphTest.java b/android/guava-tests/test/com/google/common/graph/AbstractGraphTest.java index 3a489a11c..a8209244c 100644 --- a/android/guava-tests/test/com/google/common/graph/AbstractGraphTest.java +++ b/android/guava-tests/test/com/google/common/graph/AbstractGraphTest.java @@ -16,13 +16,12 @@ package com.google.common.graph; -import static com.google.common.graph.TestUtil.ERROR_NODE_NOT_IN_GRAPH; import static com.google.common.graph.TestUtil.assertNodeNotInGraphErrorMessage; import static com.google.common.graph.TestUtil.assertStronglyEquivalent; import static com.google.common.graph.TestUtil.sanityCheckSet; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.TruthJUnit.assume; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableSet; import java.util.HashSet; @@ -235,12 +234,9 @@ public abstract class AbstractGraphTest { @Test public void adjacentNodes_nodeNotInGraph() { - try { - graph.adjacentNodes(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> graph.adjacentNodes(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -251,12 +247,9 @@ public abstract class AbstractGraphTest { @Test public void predecessors_nodeNotInGraph() { - try { - graph.predecessors(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> graph.predecessors(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -267,12 +260,9 @@ public abstract class AbstractGraphTest { @Test public void successors_nodeNotInGraph() { - try { - graph.successors(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> graph.successors(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -283,12 +273,9 @@ public abstract class AbstractGraphTest { @Test public void incidentEdges_nodeNotInGraph() { - try { - graph.incidentEdges(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> graph.incidentEdges(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -306,12 +293,9 @@ public abstract class AbstractGraphTest { @Test public void degree_nodeNotInGraph() { - try { - graph.degree(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> graph.degree(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -322,12 +306,9 @@ public abstract class AbstractGraphTest { @Test public void inDegree_nodeNotInGraph() { - try { - graph.inDegree(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> graph.inDegree(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -338,12 +319,9 @@ public abstract class AbstractGraphTest { @Test public void outDegree_nodeNotInGraph() { - try { - graph.outDegree(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> graph.outDegree(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -407,16 +385,16 @@ public abstract class AbstractGraphTest { public void removeNode_queryAfterRemoval() { assume().that(graphIsMutable()).isTrue(); - addNode(N1); - @SuppressWarnings("unused") - Set<Integer> unused = graph.adjacentNodes(N1); // ensure cache (if any) is populated + putEdge(N1, N2); + putEdge(N2, N1); + Set<Integer> n1AdjacentNodes = graph.adjacentNodes(N1); + Set<Integer> n2AdjacentNodes = graph.adjacentNodes(N2); assertThat(graphAsMutableGraph.removeNode(N1)).isTrue(); - try { - graph.adjacentNodes(N1); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + assertThat(n1AdjacentNodes).isEmpty(); + assertThat(n2AdjacentNodes).isEmpty(); + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> graph.adjacentNodes(N1)); + assertNodeNotInGraphErrorMessage(e); } @Test diff --git a/android/guava-tests/test/com/google/common/graph/AbstractNetworkTest.java b/android/guava-tests/test/com/google/common/graph/AbstractNetworkTest.java index a02783f61..b9558f706 100644 --- a/android/guava-tests/test/com/google/common/graph/AbstractNetworkTest.java +++ b/android/guava-tests/test/com/google/common/graph/AbstractNetworkTest.java @@ -16,7 +16,6 @@ package com.google.common.graph; -import static com.google.common.graph.TestUtil.ERROR_NODE_NOT_IN_GRAPH; import static com.google.common.graph.TestUtil.assertEdgeNotInGraphErrorMessage; import static com.google.common.graph.TestUtil.assertNodeNotInGraphErrorMessage; import static com.google.common.graph.TestUtil.assertStronglyEquivalent; @@ -25,6 +24,7 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.TruthJUnit.assume; import static java.util.concurrent.Executors.newFixedThreadPool; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -416,12 +416,10 @@ public abstract class AbstractNetworkTest { @Test public void incidentEdges_nodeNotInGraph() { - try { - network.incidentEdges(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> network.incidentEdges(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -432,12 +430,10 @@ public abstract class AbstractNetworkTest { @Test public void incidentNodes_edgeNotInGraph() { - try { - network.incidentNodes(EDGE_NOT_IN_GRAPH); - fail(ERROR_EDGE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertEdgeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> network.incidentNodes(EDGE_NOT_IN_GRAPH)); + assertEdgeNotInGraphErrorMessage(e); } @Test @@ -455,12 +451,10 @@ public abstract class AbstractNetworkTest { @Test public void adjacentNodes_nodeNotInGraph() { - try { - network.adjacentNodes(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> network.adjacentNodes(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -481,12 +475,10 @@ public abstract class AbstractNetworkTest { @Test public void adjacentEdges_edgeNotInGraph() { - try { - network.adjacentEdges(EDGE_NOT_IN_GRAPH); - fail(ERROR_EDGE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertEdgeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> network.adjacentEdges(EDGE_NOT_IN_GRAPH)); + assertEdgeNotInGraphErrorMessage(e); } @Test @@ -512,24 +504,19 @@ public abstract class AbstractNetworkTest { public void edgesConnecting_nodesNotInGraph() { addNode(N1); addNode(N2); - try { - network.edgesConnecting(N1, NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } - try { - network.edgesConnecting(NODE_NOT_IN_GRAPH, N2); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } - try { - network.edgesConnecting(NODE_NOT_IN_GRAPH, NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> network.edgesConnecting(N1, NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); + e = + assertThrows( + IllegalArgumentException.class, () -> network.edgesConnecting(NODE_NOT_IN_GRAPH, N2)); + assertNodeNotInGraphErrorMessage(e); + e = + assertThrows( + IllegalArgumentException.class, + () -> network.edgesConnecting(NODE_NOT_IN_GRAPH, NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -594,12 +581,9 @@ public abstract class AbstractNetworkTest { @Test public void inEdges_nodeNotInGraph() { - try { - network.inEdges(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> network.inEdges(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -610,12 +594,9 @@ public abstract class AbstractNetworkTest { @Test public void outEdges_nodeNotInGraph() { - try { - network.outEdges(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> network.outEdges(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -626,12 +607,9 @@ public abstract class AbstractNetworkTest { @Test public void predecessors_nodeNotInGraph() { - try { - network.predecessors(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> network.predecessors(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -642,12 +620,9 @@ public abstract class AbstractNetworkTest { @Test public void successors_nodeNotInGraph() { - try { - network.successors(NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> network.successors(NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -695,17 +670,16 @@ public abstract class AbstractNetworkTest { public void removeNode_queryAfterRemoval() { assume().that(graphIsMutable()).isTrue(); - addNode(N1); - @SuppressWarnings("unused") - Set<Integer> unused = - networkAsMutableNetwork.adjacentNodes(N1); // ensure cache (if any) is populated + addEdge(N1, N2, E12); + Set<Integer> n1AdjacentNodes = networkAsMutableNetwork.adjacentNodes(N1); + Set<Integer> n2AdjacentNodes = networkAsMutableNetwork.adjacentNodes(N2); assertTrue(networkAsMutableNetwork.removeNode(N1)); - try { - networkAsMutableNetwork.adjacentNodes(N1); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + assertThat(n1AdjacentNodes).isEmpty(); + assertThat(n2AdjacentNodes).isEmpty(); + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.adjacentNodes(N1)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -750,12 +724,10 @@ public abstract class AbstractNetworkTest { EndpointPair<Integer> unused = networkAsMutableNetwork.incidentNodes(E12); // ensure cache (if any) is populated assertTrue(networkAsMutableNetwork.removeEdge(E12)); - try { - networkAsMutableNetwork.incidentNodes(E12); - fail(ERROR_EDGE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertEdgeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.incidentNodes(E12)); + assertEdgeNotInGraphErrorMessage(e); } @Test diff --git a/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedGraphTest.java b/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedGraphTest.java index c50a7da67..b1a69de2f 100644 --- a/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedGraphTest.java +++ b/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedGraphTest.java @@ -19,8 +19,8 @@ package com.google.common.graph; import static com.google.common.graph.GraphConstants.ENDPOINTS_MISMATCH; 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; import java.util.Set; import org.junit.Test; @@ -36,13 +36,10 @@ public abstract class AbstractStandardDirectedGraphTest extends AbstractGraphTes assume().that(graphIsMutable()).isTrue(); Set<Integer> nodes = graph.nodes(); - try { - nodes.add(N2); - fail(ERROR_MODIFIABLE_SET); - } catch (UnsupportedOperationException e) { - addNode(N1); - assertThat(graph.nodes()).containsExactlyElementsIn(nodes); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> nodes.add(N2)); + addNode(N1); + assertThat(graph.nodes()).containsExactlyElementsIn(nodes); } @Override @@ -52,13 +49,10 @@ public abstract class AbstractStandardDirectedGraphTest extends AbstractGraphTes 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); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> adjacentNodes.add(N2)); + putEdge(N1, N2); + assertThat(graph.adjacentNodes(N1)).containsExactlyElementsIn(adjacentNodes); } @Override @@ -68,13 +62,10 @@ public abstract class AbstractStandardDirectedGraphTest extends AbstractGraphTes addNode(N2); Set<Integer> predecessors = graph.predecessors(N2); - try { - predecessors.add(N1); - fail(ERROR_MODIFIABLE_SET); - } catch (UnsupportedOperationException e) { - putEdge(N1, N2); - assertThat(graph.predecessors(N2)).containsExactlyElementsIn(predecessors); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> predecessors.add(N1)); + putEdge(N1, N2); + assertThat(graph.predecessors(N2)).containsExactlyElementsIn(predecessors); } @Override @@ -84,13 +75,10 @@ public abstract class AbstractStandardDirectedGraphTest extends AbstractGraphTes addNode(N1); Set<Integer> successors = graph.successors(N1); - try { - successors.add(N2); - fail(ERROR_MODIFIABLE_SET); - } catch (UnsupportedOperationException e) { - putEdge(N1, N2); - assertThat(successors).containsExactlyElementsIn(graph.successors(N1)); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> successors.add(N2)); + putEdge(N1, N2); + assertThat(successors).containsExactlyElementsIn(graph.successors(N1)); } @Override @@ -100,13 +88,12 @@ public abstract class AbstractStandardDirectedGraphTest extends AbstractGraphTes addNode(N1); Set<EndpointPair<Integer>> incidentEdges = graph.incidentEdges(N1); - try { - incidentEdges.add(EndpointPair.ordered(N1, N2)); - fail(ERROR_MODIFIABLE_SET); - } catch (UnsupportedOperationException e) { - putEdge(N1, N2); - assertThat(incidentEdges).containsExactlyElementsIn(graph.incidentEdges(N1)); - } + UnsupportedOperationException e = + assertThrows( + UnsupportedOperationException.class, + () -> incidentEdges.add(EndpointPair.ordered(N1, N2))); + putEdge(N1, N2); + assertThat(incidentEdges).containsExactlyElementsIn(graph.incidentEdges(N1)); } @Test @@ -364,12 +351,9 @@ public abstract class AbstractStandardDirectedGraphTest extends AbstractGraphTes assume().that(graphIsMutable()).isTrue(); EndpointPair<Integer> endpoints = EndpointPair.unordered(N1, N2); - try { - graphAsMutableGraph.putEdge(endpoints); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> graphAsMutableGraph.putEdge(endpoints)); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } /** @@ -398,12 +382,9 @@ public abstract class AbstractStandardDirectedGraphTest extends AbstractGraphTes assume().that(graphIsMutable()).isTrue(); assume().that(graph.allowsSelfLoops()).isFalse(); - try { - graphAsMutableGraph.putEdge(N1, N1); - fail(ERROR_ADDED_SELF_LOOP); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> graphAsMutableGraph.putEdge(N1, N1)); + assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP); } @Test @@ -449,12 +430,10 @@ public abstract class AbstractStandardDirectedGraphTest extends AbstractGraphTes putEdge(N1, N2); EndpointPair<Integer> endpoints = EndpointPair.unordered(N1, N2); - try { - graphAsMutableGraph.removeEdge(endpoints); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> graphAsMutableGraph.removeEdge(endpoints)); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test diff --git a/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java b/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java index 98bc2fd65..1e6d22863 100644 --- a/android/guava-tests/test/com/google/common/graph/AbstractStandardDirectedNetworkTest.java +++ b/android/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; @@ -64,13 +65,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 @@ -79,13 +77,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 @@ -95,13 +90,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 @@ -111,13 +103,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 @@ -143,13 +132,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 @@ -159,13 +145,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 @@ -175,13 +158,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 @@ -191,13 +171,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 @@ -207,13 +184,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 @@ -228,23 +202,25 @@ 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 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 @@ -304,12 +280,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 @@ -320,12 +295,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 @@ -516,20 +490,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 @@ -538,12 +504,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 @@ -561,12 +526,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 @@ -574,12 +537,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); } /** @@ -632,25 +593,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 @@ -660,12 +615,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 diff --git a/android/guava-tests/test/com/google/common/graph/AbstractStandardUndirectedGraphTest.java b/android/guava-tests/test/com/google/common/graph/AbstractStandardUndirectedGraphTest.java index da9226c31..0acc786a4 100644 --- a/android/guava-tests/test/com/google/common/graph/AbstractStandardUndirectedGraphTest.java +++ b/android/guava-tests/test/com/google/common/graph/AbstractStandardUndirectedGraphTest.java @@ -18,8 +18,8 @@ package com.google.common.graph; 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; import com.google.common.testing.EqualsTester; import java.util.Set; @@ -47,13 +47,10 @@ public abstract class AbstractStandardUndirectedGraphTest extends AbstractGraphT assume().that(graphIsMutable()).isTrue(); Set<Integer> nodes = graph.nodes(); - try { - nodes.add(N2); - fail(ERROR_MODIFIABLE_SET); - } catch (UnsupportedOperationException e) { - addNode(N1); - assertThat(graph.nodes()).containsExactlyElementsIn(nodes); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> nodes.add(N2)); + addNode(N1); + assertThat(graph.nodes()).containsExactlyElementsIn(nodes); } @Override @@ -63,13 +60,10 @@ public abstract class AbstractStandardUndirectedGraphTest extends AbstractGraphT 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); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> adjacentNodes.add(N2)); + putEdge(N1, N2); + assertThat(graph.adjacentNodes(N1)).containsExactlyElementsIn(adjacentNodes); } @Override @@ -79,13 +73,10 @@ public abstract class AbstractStandardUndirectedGraphTest extends AbstractGraphT addNode(N2); Set<Integer> predecessors = graph.predecessors(N2); - try { - predecessors.add(N1); - fail(ERROR_MODIFIABLE_SET); - } catch (UnsupportedOperationException e) { - putEdge(N1, N2); - assertThat(graph.predecessors(N2)).containsExactlyElementsIn(predecessors); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> predecessors.add(N1)); + putEdge(N1, N2); + assertThat(graph.predecessors(N2)).containsExactlyElementsIn(predecessors); } @Override @@ -95,13 +86,10 @@ public abstract class AbstractStandardUndirectedGraphTest extends AbstractGraphT addNode(N1); Set<Integer> successors = graph.successors(N1); - try { - successors.add(N2); - fail(ERROR_MODIFIABLE_SET); - } catch (UnsupportedOperationException e) { - putEdge(N1, N2); - assertThat(graph.successors(N1)).containsExactlyElementsIn(successors); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> successors.add(N2)); + putEdge(N1, N2); + assertThat(graph.successors(N1)).containsExactlyElementsIn(successors); } @Override @@ -111,13 +99,12 @@ public abstract class AbstractStandardUndirectedGraphTest extends AbstractGraphT addNode(N1); Set<EndpointPair<Integer>> incidentEdges = graph.incidentEdges(N1); - try { - incidentEdges.add(EndpointPair.unordered(N1, N2)); - fail(ERROR_MODIFIABLE_SET); - } catch (UnsupportedOperationException e) { - putEdge(N1, N2); - assertThat(incidentEdges).containsExactlyElementsIn(graph.incidentEdges(N1)); - } + UnsupportedOperationException e = + assertThrows( + UnsupportedOperationException.class, + () -> incidentEdges.add(EndpointPair.unordered(N1, N2))); + putEdge(N1, N2); + assertThat(incidentEdges).containsExactlyElementsIn(graph.incidentEdges(N1)); } @Test @@ -380,12 +367,9 @@ public abstract class AbstractStandardUndirectedGraphTest extends AbstractGraphT assume().that(graphIsMutable()).isTrue(); assume().that(graph.allowsSelfLoops()).isFalse(); - try { - putEdge(N1, N1); - fail(ERROR_ADDED_SELF_LOOP); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> putEdge(N1, N1)); + assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP); } @Test diff --git a/android/guava-tests/test/com/google/common/graph/AbstractStandardUndirectedNetworkTest.java b/android/guava-tests/test/com/google/common/graph/AbstractStandardUndirectedNetworkTest.java index beadaf92d..52f65577a 100644 --- a/android/guava-tests/test/com/google/common/graph/AbstractStandardUndirectedNetworkTest.java +++ b/android/guava-tests/test/com/google/common/graph/AbstractStandardUndirectedNetworkTest.java @@ -19,6 +19,7 @@ package com.google.common.graph; import static com.google.common.graph.GraphConstants.ENDPOINTS_MISMATCH; 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; @@ -59,26 +60,20 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw @Test public void nodes_checkReturnedSetMutability() { 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 @Test public void edges_checkReturnedSetMutability() { 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 @@ -86,13 +81,10 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw public void incidentEdges_checkReturnedSetMutability() { 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 @@ -100,13 +92,10 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw public void adjacentNodes_checkReturnedSetMutability() { 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 @@ -128,13 +117,10 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw 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 @@ -142,13 +128,10 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw public void inEdges_checkReturnedSetMutability() { 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 @@ -156,13 +139,10 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw public void outEdges_checkReturnedSetMutability() { 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 @@ -170,13 +150,10 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw public void predecessors_checkReturnedSetMutability() { 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 @@ -184,13 +161,10 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw public void successors_checkReturnedSetMutability() { addNode(N1); Set<Integer> successors = network.successors(N1); - try { - successors.add(N2); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - addEdge(N1, N2, E12); - assertThat(network.successors(N1)).containsExactlyElementsIn(successors); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> successors.add(N2)); + addEdge(N1, N2, E12); + assertThat(network.successors(N1)).containsExactlyElementsIn(successors); } @Test @@ -203,23 +177,25 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw @Test public void edgesConnecting_orderMismatch() { addEdge(N1, N2, E12); - try { - Set<String> unused = network.edgesConnecting(ENDPOINTS_N1N2); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> { + Set<String> unused = network.edgesConnecting(ENDPOINTS_N1N2); + }); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test public void edgeConnectingOrNull_orderMismatch() { addEdge(N1, N2, E12); - try { - String unused = network.edgeConnectingOrNull(ENDPOINTS_N1N2); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> { + String unused = network.edgeConnectingOrNull(ENDPOINTS_N1N2); + }); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test @@ -429,13 +405,10 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw 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); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N4, N5, E12)); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); } @Test @@ -444,18 +417,16 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw 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); - } - try { - networkAsMutableNetwork.addEdge(N2, N1, 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); + e = + assertThrows( + IllegalArgumentException.class, + () -> networkAsMutableNetwork.addEdge(N2, N1, EDGE_NOT_IN_GRAPH)); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); } @Test @@ -474,12 +445,10 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw assume().that(graphIsMutable()).isTrue(); EndpointPair<Integer> endpoints = EndpointPair.ordered(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 @@ -487,12 +456,10 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw 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); } /** @@ -544,25 +511,19 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw 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).hasMessageThat().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).hasMessageThat().contains(ERROR_REUSE_EDGE); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N2, E11)); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); + e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N2, N2, E11)); + assertThat(e).hasMessageThat().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).hasMessageThat().contains(ERROR_REUSE_EDGE); - } + e = + assertThrows( + IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N1, E12)); + assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE); } @Test @@ -572,12 +533,11 @@ public abstract class AbstractStandardUndirectedNetworkTest extends AbstractNetw 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).hasMessageThat().contains(ERROR_PARALLEL_EDGE); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> networkAsMutableNetwork.addEdge(N1, N1, EDGE_NOT_IN_GRAPH)); + assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE); } @Test diff --git a/android/guava-tests/test/com/google/common/graph/DefaultNetworkImplementationsTest.java b/android/guava-tests/test/com/google/common/graph/DefaultNetworkImplementationsTest.java index eaddb949c..b773a36e6 100644 --- a/android/guava-tests/test/com/google/common/graph/DefaultNetworkImplementationsTest.java +++ b/android/guava-tests/test/com/google/common/graph/DefaultNetworkImplementationsTest.java @@ -16,13 +16,11 @@ package com.google.common.graph; -import static com.google.common.graph.AbstractNetworkTest.ERROR_MODIFIABLE_COLLECTION; -import static com.google.common.graph.TestUtil.ERROR_NODE_NOT_IN_GRAPH; import static com.google.common.graph.TestUtil.EdgeType.DIRECTED; import static com.google.common.graph.TestUtil.EdgeType.UNDIRECTED; import static com.google.common.graph.TestUtil.assertNodeNotInGraphErrorMessage; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import com.google.common.graph.TestUtil.EdgeType; import java.util.Arrays; @@ -89,24 +87,21 @@ public final class DefaultNetworkImplementationsTest { public void edgesConnecting_nodesNotInGraph() { network.addNode(N1); network.addNode(N2); - try { - networkForTest.edgesConnecting(N1, NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } - try { - networkForTest.edgesConnecting(NODE_NOT_IN_GRAPH, N2); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } - try { - networkForTest.edgesConnecting(NODE_NOT_IN_GRAPH, NODE_NOT_IN_GRAPH); - fail(ERROR_NODE_NOT_IN_GRAPH); - } catch (IllegalArgumentException e) { - assertNodeNotInGraphErrorMessage(e); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> networkForTest.edgesConnecting(N1, NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); + e = + assertThrows( + IllegalArgumentException.class, + () -> networkForTest.edgesConnecting(NODE_NOT_IN_GRAPH, N2)); + assertNodeNotInGraphErrorMessage(e); + e = + assertThrows( + IllegalArgumentException.class, + () -> networkForTest.edgesConnecting(NODE_NOT_IN_GRAPH, NODE_NOT_IN_GRAPH)); + assertNodeNotInGraphErrorMessage(e); } @Test @@ -114,13 +109,10 @@ public final class DefaultNetworkImplementationsTest { network.addNode(N1); network.addNode(N2); Set<String> edgesConnecting = network.edgesConnecting(N1, N2); - try { - edgesConnecting.add(E23); - fail(ERROR_MODIFIABLE_COLLECTION); - } catch (UnsupportedOperationException e) { - network.addEdge(N1, N2, E12); - assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactlyElementsIn(edgesConnecting); - } + UnsupportedOperationException e = + assertThrows(UnsupportedOperationException.class, () -> edgesConnecting.add(E23)); + network.addEdge(N1, N2, E12); + assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactlyElementsIn(edgesConnecting); } @Test diff --git a/android/guava-tests/test/com/google/common/graph/EndpointPairTest.java b/android/guava-tests/test/com/google/common/graph/EndpointPairTest.java index c67693a71..391f4c838 100644 --- a/android/guava-tests/test/com/google/common/graph/EndpointPairTest.java +++ b/android/guava-tests/test/com/google/common/graph/EndpointPairTest.java @@ -17,7 +17,7 @@ package com.google.common.graph; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -91,11 +91,7 @@ public final class EndpointPairTest { for (MutableNetwork<Integer, String> network : testNetworks) { network.addEdge(1, 2, "1-2"); EndpointPair<Integer> endpointPair = network.incidentNodes("1-2"); - try { - endpointPair.adjacentNode(3); - fail("Should have rejected adjacentNode() called with a node not incident to edge."); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> endpointPair.adjacentNode(3)); } } @@ -195,11 +191,8 @@ public final class EndpointPairTest { directedGraph.removeEdge(N2, N1); containsExactlySanityCheck(edges); - try { - edges.add(EndpointPair.ordered(N1, N2)); - fail("Set returned by edges() should be unmodifiable"); - } catch (UnsupportedOperationException expected) { - } + assertThrows( + UnsupportedOperationException.class, () -> edges.add(EndpointPair.ordered(N1, N2))); } @Test diff --git a/android/guava-tests/test/com/google/common/graph/GraphsTest.java b/android/guava-tests/test/com/google/common/graph/GraphsTest.java index a07def019..b31121da4 100644 --- a/android/guava-tests/test/com/google/common/graph/GraphsTest.java +++ b/android/guava-tests/test/com/google/common/graph/GraphsTest.java @@ -22,7 +22,7 @@ import static com.google.common.graph.Graphs.reachableNodes; import static com.google.common.graph.Graphs.transitiveClosure; import static com.google.common.graph.Graphs.transpose; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableSet; import java.util.Set; @@ -399,20 +399,14 @@ public class GraphsTest { public void inducedSubgraph_nodeNotInGraph() { MutableNetwork<Integer, String> undirectedGraph = NetworkBuilder.undirected().build(); - try { - inducedSubgraph(undirectedGraph, ImmutableSet.of(N1)); - fail("Should have rejected getting induced subgraph with node not in original graph."); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> inducedSubgraph(undirectedGraph, ImmutableSet.of(N1))); } @Test public void copyOf_nullArgument() { - try { - copyOf((Graph<?>) null); - fail("Should have rejected a null graph."); - } catch (NullPointerException expected) { - } + assertThrows(NullPointerException.class, () -> copyOf((Graph<?>) null)); } @Test @@ -475,20 +469,13 @@ public class GraphsTest { assertThat(directedGraph.edgesConnecting(N2, N1)).isEmpty(); // By default, parallel edges are not allowed. - try { - directedGraph.addEdge(N1, N2, E12_A); - fail(ERROR_ADDED_PARALLEL_EDGE); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> directedGraph.addEdge(N1, N2, E12_A)); + assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); // By default, self-loop edges are not allowed. - try { - directedGraph.addEdge(N1, N1, E11); - fail(ERROR_ADDED_SELF_LOOP); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP); - } + e = assertThrows(IllegalArgumentException.class, () -> directedGraph.addEdge(N1, N1, E11)); + assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP); } @Test @@ -501,26 +488,15 @@ public class GraphsTest { assertThat(undirectedGraph.edgesConnecting(N2, N1)).isEqualTo(ImmutableSet.of(E12)); // By default, parallel edges are not allowed. - try { - undirectedGraph.addEdge(N1, N2, E12_A); - fail(ERROR_ADDED_PARALLEL_EDGE); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); - } - try { - undirectedGraph.addEdge(N2, N1, E21); - fail(ERROR_ADDED_PARALLEL_EDGE); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); - } + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N1, N2, E12_A)); + assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); + e = assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N2, N1, E21)); + assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); // By default, self-loop edges are not allowed. - try { - undirectedGraph.addEdge(N1, N1, E11); - fail(ERROR_ADDED_SELF_LOOP); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP); - } + e = assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N1, N1, E11)); + assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP); } @Test @@ -564,12 +540,10 @@ public class GraphsTest { @Test public void builder_expectedNodeCount_negative() { - try { - NetworkBuilder.directed().expectedNodeCount(-1); - fail("Should have rejected negative expected node count."); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage()).contains(ERROR_NEGATIVE_COUNT); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> NetworkBuilder.directed().expectedNodeCount(-1)); + assertThat(e.getMessage()).contains(ERROR_NEGATIVE_COUNT); } @Test @@ -592,12 +566,10 @@ public class GraphsTest { @Test public void builder_expectedEdgeCount_negative() { - try { - NetworkBuilder.directed().expectedEdgeCount(-1); - fail("Should have rejected negative expected edge count."); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage()).contains(ERROR_NEGATIVE_COUNT); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> NetworkBuilder.directed().expectedEdgeCount(-1)); + assertThat(e.getMessage()).contains(ERROR_NEGATIVE_COUNT); } private static <N> void checkTransitiveClosure(Graph<N> originalGraph, Graph<N> expectedClosure) { diff --git a/android/guava-tests/test/com/google/common/graph/PackageSanityTests.java b/android/guava-tests/test/com/google/common/graph/PackageSanityTests.java index f1526c235..4c863020e 100644 --- a/android/guava-tests/test/com/google/common/graph/PackageSanityTests.java +++ b/android/guava-tests/test/com/google/common/graph/PackageSanityTests.java @@ -51,8 +51,14 @@ public class PackageSanityTests extends AbstractPackageSanityTests { NetworkBuilder.directed().<String, String>immutable().addNode("B").build(); public PackageSanityTests() { + MutableNetwork<String, String> mutableNetworkA = NetworkBuilder.directed().build(); + mutableNetworkA.addNode("a"); + MutableNetwork<String, String> mutableNetworkB = NetworkBuilder.directed().build(); + mutableNetworkB.addNode("b"); + setDistinctValues(AbstractGraphBuilder.class, GRAPH_BUILDER_A, GRAPH_BUILDER_B); setDistinctValues(Graph.class, IMMUTABLE_GRAPH_A, IMMUTABLE_GRAPH_B); + setDistinctValues(MutableNetwork.class, mutableNetworkA, mutableNetworkB); setDistinctValues(NetworkBuilder.class, NETWORK_BUILDER_A, NETWORK_BUILDER_B); setDistinctValues(Network.class, IMMUTABLE_NETWORK_A, IMMUTABLE_NETWORK_B); setDefault(EndpointPair.class, EndpointPair.ordered("A", "B")); diff --git a/android/guava-tests/test/com/google/common/graph/TraverserTest.java b/android/guava-tests/test/com/google/common/graph/TraverserTest.java index d4c8cf752..a941b9529 100644 --- a/android/guava-tests/test/com/google/common/graph/TraverserTest.java +++ b/android/guava-tests/test/com/google/common/graph/TraverserTest.java @@ -21,7 +21,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.collect.Lists.charactersOf; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import com.google.common.collect.HashMultiset; import com.google.common.collect.ImmutableList; @@ -311,11 +311,9 @@ public class TraverserTest { @Test public void forGraph_breadthFirst_emptyGraph() { - try { - Traverser.forGraph(createDirectedGraph()).breadthFirst('a'); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forGraph(createDirectedGraph()).breadthFirst('a')); } /** @@ -326,11 +324,9 @@ public class TraverserTest { public void forGraph_breadthFirstIterable_emptyGraph() { assertEqualCharNodes( Traverser.forGraph(createDirectedGraph()).breadthFirst(charactersOf("")), ""); - try { - Traverser.forGraph(createDirectedGraph()).breadthFirst(charactersOf("a")); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forGraph(createDirectedGraph()).breadthFirst(charactersOf("a"))); } /** @@ -509,22 +505,18 @@ public class TraverserTest { @Test public void forGraph_depthFirstPreOrder_emptyGraph() { - try { - Traverser.forGraph(createDirectedGraph()).depthFirstPreOrder('a'); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forGraph(createDirectedGraph()).depthFirstPreOrder('a')); } @Test public void forGraph_depthFirstPreOrderIterable_emptyGraph() { assertEqualCharNodes( Traverser.forGraph(createDirectedGraph()).depthFirstPreOrder(charactersOf("")), ""); - try { - Traverser.forGraph(createDirectedGraph()).depthFirstPreOrder(charactersOf("a")); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forGraph(createDirectedGraph()).depthFirstPreOrder(charactersOf("a"))); } @Test @@ -691,22 +683,18 @@ public class TraverserTest { @Test public void forGraph_depthFirstPostOrder_emptyGraph() { - try { - Traverser.forGraph(createDirectedGraph()).depthFirstPostOrder('a'); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forGraph(createDirectedGraph()).depthFirstPostOrder('a')); } @Test public void forGraph_depthFirstPostOrderIterable_emptyGraph() { assertEqualCharNodes( Traverser.forGraph(createDirectedGraph()).depthFirstPostOrder(charactersOf("")), ""); - try { - Traverser.forGraph(createDirectedGraph()).depthFirstPostOrder(charactersOf("a")); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forGraph(createDirectedGraph()).depthFirstPostOrder(charactersOf("a"))); } @Test @@ -749,11 +737,7 @@ public class TraverserTest { MutableGraph<String> graph = GraphBuilder.undirected().build(); graph.putEdge("a", "b"); - try { - Traverser.forTree(graph); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Traverser.forTree(graph)); } @Test @@ -770,11 +754,7 @@ public class TraverserTest { MutableValueGraph<String, Integer> valueGraph = ValueGraphBuilder.undirected().build(); valueGraph.putEdgeValue("a", "b", 11); - try { - Traverser.forTree(valueGraph); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Traverser.forTree(valueGraph)); } @Test @@ -791,11 +771,7 @@ public class TraverserTest { MutableNetwork<String, Integer> network = NetworkBuilder.undirected().build(); network.addEdge("a", "b", 11); - try { - Traverser.forTree(network); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> Traverser.forTree(network)); } @Test @@ -889,22 +865,18 @@ public class TraverserTest { @Test public void forTree_breadthFirst_emptyGraph() { - try { - Traverser.forTree(createDirectedGraph()).breadthFirst('a'); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forTree(createDirectedGraph()).breadthFirst('a')); } @Test public void forTree_breadthFirstIterable_emptyGraph() { assertEqualCharNodes( Traverser.forTree(createDirectedGraph()).breadthFirst(charactersOf("")), ""); - try { - Traverser.forTree(createDirectedGraph()).breadthFirst(charactersOf("a")); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forTree(createDirectedGraph()).breadthFirst(charactersOf("a"))); } @Test @@ -1026,22 +998,18 @@ public class TraverserTest { @Test public void forTree_depthFirstPreOrder_emptyGraph() { - try { - Traverser.forTree(createDirectedGraph()).depthFirstPreOrder('a'); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forTree(createDirectedGraph()).depthFirstPreOrder('a')); } @Test public void forTree_depthFirstPreOrderIterable_emptyGraph() { assertEqualCharNodes( Traverser.forTree(createDirectedGraph()).depthFirstPreOrder(charactersOf("")), ""); - try { - Traverser.forTree(createDirectedGraph()).depthFirstPreOrder(charactersOf("a")); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forTree(createDirectedGraph()).depthFirstPreOrder(charactersOf("a"))); } @Test @@ -1156,22 +1124,18 @@ public class TraverserTest { @Test public void forTree_depthFirstPostOrder_emptyGraph() { - try { - Traverser.forTree(createDirectedGraph()).depthFirstPostOrder('a'); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forTree(createDirectedGraph()).depthFirstPostOrder('a')); } @Test public void forTree_depthFirstPostOrderIterable_emptyGraph() { assertEqualCharNodes( Traverser.forTree(createDirectedGraph()).depthFirstPostOrder(charactersOf("")), ""); - try { - Traverser.forTree(createDirectedGraph()).depthFirstPostOrder(charactersOf("a")); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> Traverser.forTree(createDirectedGraph()).depthFirstPostOrder(charactersOf("a"))); } @Test @@ -1201,11 +1165,11 @@ public class TraverserTest { } private static SuccessorsFunction<Character> createDirectedGraph(String... edges) { - return createGraph(/* directed = */ true, edges); + return createGraph(/* directed= */ true, edges); } private static SuccessorsFunction<Character> createUndirectedGraph(String... edges) { - return createGraph(/* directed = */ false, edges); + return createGraph(/* directed= */ false, edges); } /** diff --git a/android/guava-tests/test/com/google/common/graph/ValueGraphTest.java b/android/guava-tests/test/com/google/common/graph/ValueGraphTest.java index 24323ca00..caf7ed984 100644 --- a/android/guava-tests/test/com/google/common/graph/ValueGraphTest.java +++ b/android/guava-tests/test/com/google/common/graph/ValueGraphTest.java @@ -20,7 +20,7 @@ import static com.google.common.graph.GraphConstants.ENDPOINTS_MISMATCH; import static com.google.common.graph.TestUtil.assertStronglyEquivalent; import static com.google.common.truth.Truth.assertThat; import static java.util.concurrent.Executors.newFixedThreadPool; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; import java.util.Set; @@ -197,13 +197,16 @@ public final class ValueGraphTest { public void edgeValueOrDefault_directed_mismatch() { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "A"); - try { - String unused = graph.edgeValueOrDefault(EndpointPair.unordered(1, 2), "default"); - unused = graph.edgeValueOrDefault(EndpointPair.unordered(2, 1), "default"); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> graph.edgeValueOrDefault(EndpointPair.unordered(1, 2), "default")); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); + e = + assertThrows( + IllegalArgumentException.class, + () -> graph.edgeValueOrDefault(EndpointPair.unordered(2, 1), "default")); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test @@ -225,18 +228,16 @@ public final class ValueGraphTest { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "A"); // Check that edgeValueOrDefault() throws on each possible ordering of an ordered EndpointPair - try { - String unused = graph.edgeValueOrDefault(EndpointPair.ordered(1, 2), "default"); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } - try { - String unused = graph.edgeValueOrDefault(EndpointPair.ordered(2, 1), "default"); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> graph.edgeValueOrDefault(EndpointPair.ordered(1, 2), "default")); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); + e = + assertThrows( + IllegalArgumentException.class, + () -> graph.edgeValueOrDefault(EndpointPair.ordered(2, 1), "default")); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test @@ -252,23 +253,21 @@ public final class ValueGraphTest { @Test public void putEdgeValue_directed_orderMismatch() { graph = ValueGraphBuilder.directed().build(); - try { - graph.putEdgeValue(EndpointPair.unordered(1, 2), "irrelevant"); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> graph.putEdgeValue(EndpointPair.unordered(1, 2), "irrelevant")); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test public void putEdgeValue_undirected_orderMismatch() { graph = ValueGraphBuilder.undirected().build(); - try { - graph.putEdgeValue(EndpointPair.ordered(1, 2), "irrelevant"); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> graph.putEdgeValue(EndpointPair.ordered(1, 2), "irrelevant")); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test @@ -315,13 +314,14 @@ public final class ValueGraphTest { graph = ValueGraphBuilder.directed().build(); graph.putEdgeValue(1, 2, "1->2"); graph.putEdgeValue(2, 1, "2->1"); - try { - graph.removeEdge(EndpointPair.unordered(1, 2)); - graph.removeEdge(EndpointPair.unordered(2, 1)); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> graph.removeEdge(EndpointPair.unordered(1, 2))); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); + e = + assertThrows( + IllegalArgumentException.class, () -> graph.removeEdge(EndpointPair.unordered(2, 1))); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test @@ -329,18 +329,14 @@ public final class ValueGraphTest { graph = ValueGraphBuilder.undirected().build(); graph.putEdgeValue(1, 2, "1-2"); // Check that removeEdge() throws on each possible ordering of an ordered EndpointPair - try { - graph.removeEdge(EndpointPair.ordered(1, 2)); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } - try { - graph.removeEdge(EndpointPair.ordered(2, 1)); - fail("Expected IllegalArgumentException: " + ENDPOINTS_MISMATCH); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); - } + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> graph.removeEdge(EndpointPair.ordered(1, 2))); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); + e = + assertThrows( + IllegalArgumentException.class, () -> graph.removeEdge(EndpointPair.ordered(2, 1))); + assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH); } @Test |