Skip to content

Commit

Permalink
Use AssertionError(String, Throwable) instead of supplying the caus…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed May 29, 2024
1 parent f74135f commit c497a97
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -223,9 +221,7 @@ private static ImmutableList<URL> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ private static ImmutableList<URL> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ private static <X extends Throwable, V> Function<X, V> unexpectedFunction() {
return new Function<X, V>() {
@Override
public V apply(X t) {
throw newAssertionError("Unexpected fallback", t);
throw new AssertionError("Unexpected fallback", t);
}
};
}
Expand Down Expand Up @@ -946,18 +946,11 @@ private static <X extends Throwable, V> AsyncFunction<X, V> unexpectedAsyncFunct
return new AsyncFunction<X, V>() {
@Override
public ListenableFuture<V> 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 {
Expand Down Expand Up @@ -3191,7 +3184,7 @@ String smartToString(ImmutableSet<ListenableFuture<String>> inputs) {
void smartAssertTrue(
ImmutableSet<ListenableFuture<String>> inputs, Exception cause, boolean expression) {
if (!expression) {
throw failureWithCause(cause, smartToString(inputs));
throw new AssertionError(smartToString(inputs), cause);
}
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -42,7 +40,7 @@ static void verifyGetOnPendingFuture(Future<?> future) {
fail();
} catch (TimeoutException expected) {
} catch (ExecutionException e) {
throw failureWithCause(e, "");
throw new AssertionError(e);
}
}

Expand All @@ -52,7 +50,7 @@ static void verifyTimedGetOnPendingFuture(Future<?> future) {
fail();
} catch (TimeoutException expected) {
} catch (ExecutionException e) {
throw failureWithCause(e, "");
throw new AssertionError(e);
}
}

Expand All @@ -73,9 +71,7 @@ static <V> V getDoneFromTimeoutOverload(Future<V> 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);
}
}

Expand Down
6 changes: 1 addition & 5 deletions guava-tests/test/com/google/common/base/EnumsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -223,9 +221,7 @@ private static ImmutableList<URL> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ private static ImmutableList<URL> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ private static <X extends Throwable, V> Function<X, V> unexpectedFunction() {
return new Function<X, V>() {
@Override
public V apply(X t) {
throw newAssertionError("Unexpected fallback", t);
throw new AssertionError("Unexpected fallback", t);
}
};
}
Expand Down Expand Up @@ -946,18 +946,11 @@ private static <X extends Throwable, V> AsyncFunction<X, V> unexpectedAsyncFunct
return new AsyncFunction<X, V>() {
@Override
public ListenableFuture<V> 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 {
Expand Down Expand Up @@ -3191,7 +3184,7 @@ String smartToString(ImmutableSet<ListenableFuture<String>> inputs) {
void smartAssertTrue(
ImmutableSet<ListenableFuture<String>> inputs, Exception cause, boolean expression) {
if (!expression) {
throw failureWithCause(cause, smartToString(inputs));
throw new AssertionError(smartToString(inputs), cause);
}
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -42,7 +40,7 @@ static void verifyGetOnPendingFuture(Future<?> future) {
fail();
} catch (TimeoutException expected) {
} catch (ExecutionException e) {
throw failureWithCause(e, "");
throw new AssertionError(e);
}
}

Expand All @@ -52,7 +50,7 @@ static void verifyTimedGetOnPendingFuture(Future<?> future) {
fail();
} catch (TimeoutException expected) {
} catch (ExecutionException e) {
throw failureWithCause(e, "");
throw new AssertionError(e);
}
}

Expand All @@ -73,9 +71,7 @@ static <V> V getDoneFromTimeoutOverload(Future<V> 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);
}
}

Expand Down

0 comments on commit c497a97

Please sign in to comment.