From c497a975ba2845d553bb1c1ae76d7118320b32a5 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Wed, 29 May 2024 09:27:01 -0700 Subject: [PATCH] Use `AssertionError(String, Throwable)` instead of supplying the cause later. `AssertionError(String, Throwable)` was [added in API Level 19](https://developer.android.com/reference/java/lang/AssertionError#AssertionError(java.lang.String,%20java.lang.Throwable)), so we can depend on it now. (I also included one change from `AssertionFailedError` to `AssertionError` (in `FuturesTest.failureWithCause`, also used in `TestPlatform`), again motivated by the latter's `Throwable`-accepting constructor. I'll make more such changes in a separate CL.) (And I removed an unused class in `EnumsTest`. It was added in cl/22887913, and it's been unused since cl/69856875.) RELNOTES=n/a PiperOrigin-RevId: 638311654 --- .../com/google/common/base/EnumsTest.java | 6 +----- ...eferenceQueueClassLoaderUnloadingTest.java | 4 +--- .../common/util/concurrent/FuturesTest.java | 21 ++++--------------- .../util/concurrent/GeneratedMonitorTest.java | 11 ++-------- .../common/util/concurrent/TestPlatform.java | 10 +++------ .../com/google/common/base/EnumsTest.java | 6 +----- ...eferenceQueueClassLoaderUnloadingTest.java | 4 +--- .../common/util/concurrent/FuturesTest.java | 21 ++++--------------- .../util/concurrent/GeneratedMonitorTest.java | 11 ++-------- .../common/util/concurrent/TestPlatform.java | 10 +++------ 10 files changed, 22 insertions(+), 82 deletions(-) diff --git a/android/guava-tests/test/com/google/common/base/EnumsTest.java b/android/guava-tests/test/com/google/common/base/EnumsTest.java index df821d36175b..26b3a52a376d 100644 --- a/android/guava-tests/test/com/google/common/base/EnumsTest.java +++ b/android/guava-tests/test/com/google/common/base/EnumsTest.java @@ -55,8 +55,6 @@ private enum TestEnum { POODLE, } - private enum OtherEnum {} - public void testGetIfPresent() { assertThat(Enums.getIfPresent(TestEnum.class, "CHEETO")).hasValue(TestEnum.CHEETO); assertThat(Enums.getIfPresent(TestEnum.class, "HONDA")).hasValue(TestEnum.HONDA); @@ -223,9 +221,7 @@ private static ImmutableList parseJavaClassPath() { urls.add(new URL("file", null, new File(entry).getAbsolutePath())); } } catch (MalformedURLException e) { - AssertionError error = new AssertionError("malformed class path entry: " + entry); - error.initCause(e); - throw error; + throw new AssertionError("malformed class path entry: " + entry, e); } } return urls.build(); diff --git a/android/guava-tests/test/com/google/common/base/FinalizableReferenceQueueClassLoaderUnloadingTest.java b/android/guava-tests/test/com/google/common/base/FinalizableReferenceQueueClassLoaderUnloadingTest.java index ae35c16f6482..9c6f18b4261c 100644 --- a/android/guava-tests/test/com/google/common/base/FinalizableReferenceQueueClassLoaderUnloadingTest.java +++ b/android/guava-tests/test/com/google/common/base/FinalizableReferenceQueueClassLoaderUnloadingTest.java @@ -281,9 +281,7 @@ private static ImmutableList parseJavaClassPath() { urls.add(new URL("file", null, new File(entry).getAbsolutePath())); } } catch (MalformedURLException e) { - AssertionError error = new AssertionError("malformed class path entry: " + entry); - error.initCause(e); - throw error; + throw new AssertionError("malformed class path entry: " + entry, e); } } return urls.build(); diff --git a/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java b/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java index abedb4e527b2..fcaf35d00826 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java @@ -910,7 +910,7 @@ private static Function unexpectedFunction() { return new Function() { @Override public V apply(X t) { - throw newAssertionError("Unexpected fallback", t); + throw new AssertionError("Unexpected fallback", t); } }; } @@ -946,18 +946,11 @@ private static AsyncFunction unexpectedAsyncFunct return new AsyncFunction() { @Override public ListenableFuture apply(X t) { - throw newAssertionError("Unexpected fallback", t); + throw new AssertionError("Unexpected fallback", t); } }; } - /** Alternative to AssertionError(String, Throwable), which doesn't exist in GWT 2.6.1. */ - private static AssertionError newAssertionError(String message, Throwable cause) { - AssertionError e = new AssertionError(message); - e.initCause(cause); - return e; - } - // catchingAsync tests cloned from the old withFallback tests: public void testCatchingAsync_inputDoesNotRaiseException() throws Exception { @@ -3191,7 +3184,7 @@ String smartToString(ImmutableSet> inputs) { void smartAssertTrue( ImmutableSet> inputs, Exception cause, boolean expression) { if (!expression) { - throw failureWithCause(cause, smartToString(inputs)); + throw new AssertionError(smartToString(inputs), cause); } } @@ -3294,7 +3287,7 @@ public V call() throws Exception { } catch (ExecutionException e) { propagateIfInstanceOf(e.getCause(), ExecutionException.class); propagateIfInstanceOf(e.getCause(), CancellationException.class); - throw failureWithCause(e, "Unexpected exception"); + throw new AssertionError("Unexpected exception", e); } finally { executor.shutdownNow(); // TODO(cpovirk): assertTrue(awaitTerminationUninterruptibly(executor, 10, SECONDS)); @@ -3932,12 +3925,6 @@ public void testFutures_nullChecks() throws Exception { .testNulls(); } - static AssertionFailedError failureWithCause(Throwable cause, String message) { - AssertionFailedError failure = new AssertionFailedError(message); - failure.initCause(cause); - return failure; - } - // This test covers a bug where an Error thrown from a callback could cause the TimeoutFuture to // never complete when timing out. Notably, nothing would get logged since the Error would get // stuck in the ScheduledFuture inside of TimeoutFuture and nothing ever calls get on it. diff --git a/android/guava-tests/test/com/google/common/util/concurrent/GeneratedMonitorTest.java b/android/guava-tests/test/com/google/common/util/concurrent/GeneratedMonitorTest.java index 9054ea9752c8..d0988efd7f82 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/GeneratedMonitorTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/GeneratedMonitorTest.java @@ -653,10 +653,10 @@ private Outcome doCall() { if (actualException instanceof InterruptedException) { return Outcome.INTERRUPT; } else { - throw newAssertionError("unexpected exception", targetException); + throw new AssertionError("unexpected exception", targetException); } } catch (IllegalAccessException e) { - throw newAssertionError("unexpected exception", e); + throw new AssertionError("unexpected exception", e); } } @@ -772,11 +772,4 @@ protected void runTest() throws Throwable { } }; } - - /** Alternative to AssertionError(String, Throwable), which doesn't exist in Java 1.6 */ - private static AssertionError newAssertionError(String message, Throwable cause) { - AssertionError e = new AssertionError(message); - e.initCause(cause); - return e; - } } diff --git a/android/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java b/android/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java index 5c87fe552a05..501e52d828d9 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; -import static com.google.common.util.concurrent.FuturesTest.failureWithCause; import static com.google.common.util.concurrent.FuturesTest.pseudoTimedGetUninterruptibly; import static com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly; import static java.util.concurrent.TimeUnit.MILLISECONDS; @@ -30,7 +29,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeoutException; -import junit.framework.AssertionFailedError; /** Methods factored out so that they can be emulated differently in GWT. */ @GwtCompatible(emulated = true) @@ -42,7 +40,7 @@ static void verifyGetOnPendingFuture(Future future) { fail(); } catch (TimeoutException expected) { } catch (ExecutionException e) { - throw failureWithCause(e, ""); + throw new AssertionError(e); } } @@ -52,7 +50,7 @@ static void verifyTimedGetOnPendingFuture(Future future) { fail(); } catch (TimeoutException expected) { } catch (ExecutionException e) { - throw failureWithCause(e, ""); + throw new AssertionError(e); } } @@ -73,9 +71,7 @@ static V getDoneFromTimeoutOverload(Future future) throws ExecutionExcept try { return getUninterruptibly(future, 0, SECONDS); } catch (TimeoutException e) { - AssertionFailedError error = new AssertionFailedError(e.getMessage()); - error.initCause(e); - throw error; + throw new AssertionError(e); } } diff --git a/guava-tests/test/com/google/common/base/EnumsTest.java b/guava-tests/test/com/google/common/base/EnumsTest.java index df821d36175b..26b3a52a376d 100644 --- a/guava-tests/test/com/google/common/base/EnumsTest.java +++ b/guava-tests/test/com/google/common/base/EnumsTest.java @@ -55,8 +55,6 @@ private enum TestEnum { POODLE, } - private enum OtherEnum {} - public void testGetIfPresent() { assertThat(Enums.getIfPresent(TestEnum.class, "CHEETO")).hasValue(TestEnum.CHEETO); assertThat(Enums.getIfPresent(TestEnum.class, "HONDA")).hasValue(TestEnum.HONDA); @@ -223,9 +221,7 @@ private static ImmutableList parseJavaClassPath() { urls.add(new URL("file", null, new File(entry).getAbsolutePath())); } } catch (MalformedURLException e) { - AssertionError error = new AssertionError("malformed class path entry: " + entry); - error.initCause(e); - throw error; + throw new AssertionError("malformed class path entry: " + entry, e); } } return urls.build(); diff --git a/guava-tests/test/com/google/common/base/FinalizableReferenceQueueClassLoaderUnloadingTest.java b/guava-tests/test/com/google/common/base/FinalizableReferenceQueueClassLoaderUnloadingTest.java index ae35c16f6482..9c6f18b4261c 100644 --- a/guava-tests/test/com/google/common/base/FinalizableReferenceQueueClassLoaderUnloadingTest.java +++ b/guava-tests/test/com/google/common/base/FinalizableReferenceQueueClassLoaderUnloadingTest.java @@ -281,9 +281,7 @@ private static ImmutableList parseJavaClassPath() { urls.add(new URL("file", null, new File(entry).getAbsolutePath())); } } catch (MalformedURLException e) { - AssertionError error = new AssertionError("malformed class path entry: " + entry); - error.initCause(e); - throw error; + throw new AssertionError("malformed class path entry: " + entry, e); } } return urls.build(); diff --git a/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java b/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java index abedb4e527b2..fcaf35d00826 100644 --- a/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java @@ -910,7 +910,7 @@ private static Function unexpectedFunction() { return new Function() { @Override public V apply(X t) { - throw newAssertionError("Unexpected fallback", t); + throw new AssertionError("Unexpected fallback", t); } }; } @@ -946,18 +946,11 @@ private static AsyncFunction unexpectedAsyncFunct return new AsyncFunction() { @Override public ListenableFuture apply(X t) { - throw newAssertionError("Unexpected fallback", t); + throw new AssertionError("Unexpected fallback", t); } }; } - /** Alternative to AssertionError(String, Throwable), which doesn't exist in GWT 2.6.1. */ - private static AssertionError newAssertionError(String message, Throwable cause) { - AssertionError e = new AssertionError(message); - e.initCause(cause); - return e; - } - // catchingAsync tests cloned from the old withFallback tests: public void testCatchingAsync_inputDoesNotRaiseException() throws Exception { @@ -3191,7 +3184,7 @@ String smartToString(ImmutableSet> inputs) { void smartAssertTrue( ImmutableSet> inputs, Exception cause, boolean expression) { if (!expression) { - throw failureWithCause(cause, smartToString(inputs)); + throw new AssertionError(smartToString(inputs), cause); } } @@ -3294,7 +3287,7 @@ public V call() throws Exception { } catch (ExecutionException e) { propagateIfInstanceOf(e.getCause(), ExecutionException.class); propagateIfInstanceOf(e.getCause(), CancellationException.class); - throw failureWithCause(e, "Unexpected exception"); + throw new AssertionError("Unexpected exception", e); } finally { executor.shutdownNow(); // TODO(cpovirk): assertTrue(awaitTerminationUninterruptibly(executor, 10, SECONDS)); @@ -3932,12 +3925,6 @@ public void testFutures_nullChecks() throws Exception { .testNulls(); } - static AssertionFailedError failureWithCause(Throwable cause, String message) { - AssertionFailedError failure = new AssertionFailedError(message); - failure.initCause(cause); - return failure; - } - // This test covers a bug where an Error thrown from a callback could cause the TimeoutFuture to // never complete when timing out. Notably, nothing would get logged since the Error would get // stuck in the ScheduledFuture inside of TimeoutFuture and nothing ever calls get on it. diff --git a/guava-tests/test/com/google/common/util/concurrent/GeneratedMonitorTest.java b/guava-tests/test/com/google/common/util/concurrent/GeneratedMonitorTest.java index 9e33d1f57f05..458d6e0a7e43 100644 --- a/guava-tests/test/com/google/common/util/concurrent/GeneratedMonitorTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/GeneratedMonitorTest.java @@ -679,10 +679,10 @@ private Outcome doCall() { if (actualException instanceof InterruptedException) { return Outcome.INTERRUPT; } else { - throw newAssertionError("unexpected exception", targetException); + throw new AssertionError("unexpected exception", targetException); } } catch (IllegalAccessException e) { - throw newAssertionError("unexpected exception", e); + throw new AssertionError("unexpected exception", e); } } @@ -812,11 +812,4 @@ protected void runTest() throws Throwable { } }; } - - /** Alternative to AssertionError(String, Throwable), which doesn't exist in Java 1.6 */ - private static AssertionError newAssertionError(String message, Throwable cause) { - AssertionError e = new AssertionError(message); - e.initCause(cause); - return e; - } } diff --git a/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java b/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java index 5c87fe552a05..501e52d828d9 100644 --- a/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java +++ b/guava-tests/test/com/google/common/util/concurrent/TestPlatform.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; -import static com.google.common.util.concurrent.FuturesTest.failureWithCause; import static com.google.common.util.concurrent.FuturesTest.pseudoTimedGetUninterruptibly; import static com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly; import static java.util.concurrent.TimeUnit.MILLISECONDS; @@ -30,7 +29,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeoutException; -import junit.framework.AssertionFailedError; /** Methods factored out so that they can be emulated differently in GWT. */ @GwtCompatible(emulated = true) @@ -42,7 +40,7 @@ static void verifyGetOnPendingFuture(Future future) { fail(); } catch (TimeoutException expected) { } catch (ExecutionException e) { - throw failureWithCause(e, ""); + throw new AssertionError(e); } } @@ -52,7 +50,7 @@ static void verifyTimedGetOnPendingFuture(Future future) { fail(); } catch (TimeoutException expected) { } catch (ExecutionException e) { - throw failureWithCause(e, ""); + throw new AssertionError(e); } } @@ -73,9 +71,7 @@ static V getDoneFromTimeoutOverload(Future future) throws ExecutionExcept try { return getUninterruptibly(future, 0, SECONDS); } catch (TimeoutException e) { - AssertionFailedError error = new AssertionFailedError(e.getMessage()); - error.initCause(e); - throw error; + throw new AssertionError(e); } }