Skip to content

Commit

Permalink
Use hasMessageThat and hasCauseThat.
Browse files Browse the repository at this point in the history
I notice a few more `try`-`fail` tests that should use `assertThrows` but were missed by our automation for whatever reason....

PiperOrigin-RevId: 696867864
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Nov 15, 2024
1 parent ed2c12a commit fb64bda
Show file tree
Hide file tree
Showing 32 changed files with 157 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public void testTest_symmetric() {
try {
tester.addEquivalenceGroup(group1Item1, group1Item2).test();
} catch (AssertionFailedError expected) {
assertThat(expected.getMessage())
assertThat(expected)
.hasMessageThat()
.contains(
"TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
+ "TestObject{group=1, item=1} [group 1, item 1]");
Expand Down Expand Up @@ -131,7 +132,8 @@ public void testTest_transitive() {
try {
tester.addEquivalenceGroup(group1Item1, group1Item2, group1Item3).test();
} catch (AssertionFailedError expected) {
assertThat(expected.getMessage())
assertThat(expected)
.hasMessageThat()
.contains(
"TestObject{group=1, item=2} [group 1, item 2] must be equivalent to "
+ "TestObject{group=1, item=3} [group 1, item 3]");
Expand All @@ -155,7 +157,8 @@ public void testTest_inequivalence() {
try {
tester.addEquivalenceGroup(group1Item1).addEquivalenceGroup(group2Item1).test();
} catch (AssertionFailedError expected) {
assertThat(expected.getMessage())
assertThat(expected)
.hasMessageThat()
.contains(
"TestObject{group=1, item=1} [group 1, item 1] must not be equivalent to "
+ "TestObject{group=2, item=1} [group 2, item 1]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private <T> void assertFailure(
tester.testForwarding(interfaceType, wrapperFunction);
} catch (AssertionFailedError expected) {
for (String message : expectedMessages) {
assertThat(expected.getMessage()).contains(message);
assertThat(expected).hasMessageThat().contains(message);
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void testPuttingTheSameKeyTwiceThrowsOnBuild() {

IllegalArgumentException expected =
assertThrows(IllegalArgumentException.class, () -> builder.build());
assertThat(expected.getMessage()).contains("one");
assertThat(expected).hasMessageThat().contains("one");
}

public void testOf() {
Expand Down Expand Up @@ -454,7 +454,7 @@ public void testOfNullValue() {
public void testOfWithDuplicateKey() {
IllegalArgumentException expected =
assertThrows(IllegalArgumentException.class, () -> ImmutableBiMap.of("one", 1, "one", 1));
assertThat(expected.getMessage()).contains("one");
assertThat(expected).hasMessageThat().contains("one");
}

public void testOfEntries() {
Expand Down Expand Up @@ -542,7 +542,7 @@ public void testDuplicateValues() {

IllegalArgumentException expected =
assertThrows(IllegalArgumentException.class, () -> ImmutableBiMap.copyOf(map));
assertThat(expected.getMessage()).containsMatch("1|2");
assertThat(expected).hasMessageThat().containsMatch("1|2");
}

// TODO(b/172823566): Use mainline testToImmutableBiMap once CollectorTester is usable to java7.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ public void testUniqueIndexDuplicates() {
assertThrows(
IllegalArgumentException.class,
() -> Maps.uniqueIndex(ImmutableSet.of("one", "uno"), Functions.constant(1)));
assertThat(expected.getMessage()).contains("Multimaps.index");
assertThat(expected).hasMessageThat().contains("Multimaps.index");
}

/** Null values are not allowed. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,16 +587,16 @@ public void addEdge_existingEdgeBetweenDifferentNodes_selfLoops() {
IllegalArgumentException e =
assertThrows(
IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N2, E11));
assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE);
assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE);
e =
assertThrows(
IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N2, N2, E11));
assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE);
assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE);
addEdge(N1, N2, E12);
e =
assertThrows(
IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N1, E12));
assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE);
assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE);
}

@Test
Expand All @@ -610,7 +610,7 @@ public void addEdge_parallelSelfLoopEdge_notAllowed() {
assertThrows(
IllegalArgumentException.class,
() -> networkAsMutableNetwork.addEdge(N1, N1, EDGE_NOT_IN_GRAPH));
assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE);
assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE);
}

@Test
Expand Down
10 changes: 5 additions & 5 deletions android/guava-tests/test/com/google/common/graph/GraphsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public void createDirected() {
// By default, parallel edges are not allowed.
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> directedGraph.addEdge(N1, N2, E12_A));
assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE);
assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE);

// By default, self-loop edges are not allowed.
e = assertThrows(IllegalArgumentException.class, () -> directedGraph.addEdge(N1, N1, E11));
Expand All @@ -486,9 +486,9 @@ public void createUndirected() {
// By default, parallel edges are not allowed.
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N1, N2, E12_A));
assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE);
assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE);
e = assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N2, N1, E21));
assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE);
assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE);

// By default, self-loop edges are not allowed.
e = assertThrows(IllegalArgumentException.class, () -> undirectedGraph.addEdge(N1, N1, E11));
Expand Down Expand Up @@ -539,7 +539,7 @@ public void builder_expectedNodeCount_negative() {
IllegalArgumentException e =
assertThrows(
IllegalArgumentException.class, () -> NetworkBuilder.directed().expectedNodeCount(-1));
assertThat(e.getMessage()).contains(ERROR_NEGATIVE_COUNT);
assertThat(e).hasMessageThat().contains(ERROR_NEGATIVE_COUNT);
}

@Test
Expand All @@ -565,7 +565,7 @@ public void builder_expectedEdgeCount_negative() {
IllegalArgumentException e =
assertThrows(
IllegalArgumentException.class, () -> NetworkBuilder.directed().expectedEdgeCount(-1));
assertThat(e.getMessage()).contains(ERROR_NEGATIVE_COUNT);
assertThat(e).hasMessageThat().contains(ERROR_NEGATIVE_COUNT);
}

private static <N> void checkTransitiveClosure(Graph<N> originalGraph, Graph<N> expectedClosure) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,15 +812,19 @@ public void testFromIpv4BigIntegerThrowsLessThanZero() {
assertThrows(
IllegalArgumentException.class,
() -> InetAddresses.fromIPv4BigInteger(BigInteger.valueOf(-1L)));
assertEquals("BigInteger must be greater than or equal to 0", expected.getMessage());
assertThat(expected)
.hasMessageThat()
.isEqualTo("BigInteger must be greater than or equal to 0");
}

public void testFromIpv6BigIntegerThrowsLessThanZero() {
IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> InetAddresses.fromIPv6BigInteger(BigInteger.valueOf(-1L)));
assertEquals("BigInteger must be greater than or equal to 0", expected.getMessage());
assertThat(expected)
.hasMessageThat()
.isEqualTo("BigInteger must be greater than or equal to 0");
}

public void testFromIpv4BigIntegerValid() {
Expand Down Expand Up @@ -851,10 +855,11 @@ public void testFromIpv4BigIntegerInputTooLarge() {
IllegalArgumentException.class,
() ->
InetAddresses.fromIPv4BigInteger(BigInteger.ONE.shiftLeft(32).add(BigInteger.ONE)));
assertEquals(
"BigInteger cannot be converted to InetAddress because it has more than 4 bytes:"
+ " 4294967297",
expected.getMessage());
assertThat(expected)
.hasMessageThat()
.isEqualTo(
"BigInteger cannot be converted to InetAddress because it has more than 4 bytes:"
+ " 4294967297");
}

public void testFromIpv6BigIntegerInputTooLarge() {
Expand All @@ -864,10 +869,11 @@ public void testFromIpv6BigIntegerInputTooLarge() {
() ->
InetAddresses.fromIPv6BigInteger(
BigInteger.ONE.shiftLeft(128).add(BigInteger.ONE)));
assertEquals(
"BigInteger cannot be converted to InetAddress because it has more than 16 bytes:"
+ " 340282366920938463463374607431768211457",
expected.getMessage());
assertThat(expected)
.hasMessageThat()
.isEqualTo(
"BigInteger cannot be converted to InetAddress because it has more than 16 bytes:"
+ " 340282366920938463463374607431768211457");
}

// see https://github.com/google/guava/issues/2587
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ private static void assertFailed(AbstractFuture<Integer> future, Throwable expec
getDone(future);
fail();
} catch (ExecutionException e) {
assertThat(e.getCause()).isSameInstanceAs(expectedException);
assertThat(e).hasCauseThat().isSameInstanceAs(expectedException);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public void testServiceTimeoutOnStartUp() throws Exception {
TimeoutException e =
assertThrows(
TimeoutException.class, () -> service.startAsync().awaitRunning(1, MILLISECONDS));
assertThat(e.getMessage()).contains(Service.State.STARTING.toString());
assertThat(e).hasMessageThat().contains(Service.State.STARTING.toString());
}

private class TimeoutOnStartUp extends AbstractExecutionThreadService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ public String pendingToString() {
.matches(
"[^\\[]+\\[status=PENDING, info=\\[cause=\\[Because this test isn't done\\]\\]\\]");
TimeoutException e = assertThrows(TimeoutException.class, () -> testFuture.get(1, NANOSECONDS));
assertThat(e.getMessage()).contains("1 nanoseconds");
assertThat(e.getMessage()).contains("Because this test isn't done");
assertThat(e).hasMessageThat().contains("1 nanoseconds");
assertThat(e).hasMessageThat().contains("Because this test isn't done");
}

public void testToString_completesDuringToString() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testCallWithTimeout_wrapsCheckedException() throws Exception {
assertThrows(
ExecutionException.class,
() -> timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, MILLISECONDS));
assertThat(e.getCause()).isEqualTo(exception);
assertThat(e).hasCauseThat().isEqualTo(exception);
}

public void testCallWithTimeout_wrapsUncheckedException() throws Exception {
Expand All @@ -64,7 +64,7 @@ public void testCallWithTimeout_wrapsUncheckedException() throws Exception {
assertThrows(
UncheckedExecutionException.class,
() -> timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, MILLISECONDS));
assertThat(e.getCause()).isEqualTo(exception);
assertThat(e).hasCauseThat().isEqualTo(exception);
}

public void testCallUninterruptiblyWithTimeout_propagatesReturnValue() throws Exception {
Expand All @@ -85,7 +85,7 @@ public void testRunWithTimeout_wrapsUncheckedException() throws Exception {
assertThrows(
UncheckedExecutionException.class,
() -> timeLimiter.runWithTimeout(runnableThrowing(exception), DELAY_MS, MILLISECONDS));
assertThat(e.getCause()).isEqualTo(exception);
assertThat(e).hasCauseThat().isEqualTo(exception);
}

public void testRunUninterruptiblyWithTimeout_wrapsUncheckedException() throws Exception {
Expand All @@ -96,7 +96,7 @@ public void testRunUninterruptiblyWithTimeout_wrapsUncheckedException() throws E
() ->
timeLimiter.runUninterruptiblyWithTimeout(
runnableThrowing(exception), DELAY_MS, MILLISECONDS));
assertThat(e.getCause()).isEqualTo(exception);
assertThat(e).hasCauseThat().isEqualTo(exception);
}

public static <T> Callable<T> callableThrowing(final Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.common.util.concurrent;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.util.concurrent.Futures.getUnchecked;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static com.google.common.util.concurrent.FuturesGetCheckedInputs.CHECKED_EXCEPTION;
Expand Down Expand Up @@ -65,28 +66,28 @@ public void testGetUnchecked_executionExceptionChecked() {
UncheckedExecutionException expected =
assertThrows(
UncheckedExecutionException.class, () -> getUnchecked(FAILED_FUTURE_CHECKED_EXCEPTION));
assertEquals(CHECKED_EXCEPTION, expected.getCause());
assertThat(expected).hasCauseThat().isEqualTo(CHECKED_EXCEPTION);
}

public void testGetUnchecked_executionExceptionUnchecked() {
UncheckedExecutionException expected =
assertThrows(
UncheckedExecutionException.class,
() -> getUnchecked(FAILED_FUTURE_UNCHECKED_EXCEPTION));
assertEquals(UNCHECKED_EXCEPTION, expected.getCause());
assertThat(expected).hasCauseThat().isEqualTo(UNCHECKED_EXCEPTION);
}

public void testGetUnchecked_executionExceptionError() {
ExecutionError expected =
assertThrows(ExecutionError.class, () -> getUnchecked(FAILED_FUTURE_ERROR));
assertEquals(ERROR, expected.getCause());
assertThat(expected).hasCauseThat().isEqualTo(ERROR);
}

public void testGetUnchecked_executionExceptionOtherThrowable() {
UncheckedExecutionException expected =
assertThrows(
UncheckedExecutionException.class, () -> getUnchecked(FAILED_FUTURE_OTHER_THROWABLE));
assertEquals(OTHER_THROWABLE, expected.getCause());
assertThat(expected).hasCauseThat().isEqualTo(OTHER_THROWABLE);
}

public void testGetUnchecked_runtimeException() {
Expand Down
Loading

0 comments on commit fb64bda

Please sign in to comment.