|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 The Guava Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.common.testing; |
| 16 | + |
| 17 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | + |
| 19 | +import com.google.common.annotations.GwtCompatible; |
| 20 | +import com.google.common.annotations.GwtIncompatible; |
| 21 | +import com.google.common.annotations.J2ktIncompatible; |
| 22 | +import com.google.common.base.Predicate; |
| 23 | +import com.google.common.base.VerifyException; |
| 24 | +import com.google.common.collect.ImmutableMap; |
| 25 | +import com.google.common.util.concurrent.ExecutionError; |
| 26 | +import com.google.common.util.concurrent.UncheckedExecutionException; |
| 27 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 28 | +import java.lang.reflect.InvocationTargetException; |
| 29 | +import java.nio.charset.UnsupportedCharsetException; |
| 30 | +import java.util.ConcurrentModificationException; |
| 31 | +import java.util.NoSuchElementException; |
| 32 | +import java.util.concurrent.CancellationException; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | +import java.util.concurrent.TimeoutException; |
| 35 | +import junit.framework.AssertionFailedError; |
| 36 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 37 | + |
| 38 | +/** Replacements for JUnit's {@code assertThrows} that work under GWT/J2CL. */ |
| 39 | +@GwtCompatible(emulated = true) |
| 40 | +@ElementTypesAreNonnullByDefault |
| 41 | +final class ReflectionFreeAssertThrows { |
| 42 | + interface ThrowingRunnable { |
| 43 | + void run() throws Throwable; |
| 44 | + } |
| 45 | + |
| 46 | + interface ThrowingSupplier { |
| 47 | + @Nullable Object get() throws Throwable; |
| 48 | + } |
| 49 | + |
| 50 | + @CanIgnoreReturnValue |
| 51 | + static <T extends Throwable> T assertThrows( |
| 52 | + Class<T> expectedThrowable, ThrowingSupplier supplier) { |
| 53 | + return doAssertThrows(expectedThrowable, supplier, /* userPassedSupplier= */ true); |
| 54 | + } |
| 55 | + |
| 56 | + @CanIgnoreReturnValue |
| 57 | + static <T extends Throwable> T assertThrows( |
| 58 | + Class<T> expectedThrowable, ThrowingRunnable runnable) { |
| 59 | + return doAssertThrows( |
| 60 | + expectedThrowable, |
| 61 | + () -> { |
| 62 | + runnable.run(); |
| 63 | + return null; |
| 64 | + }, |
| 65 | + /* userPassedSupplier= */ false); |
| 66 | + } |
| 67 | + |
| 68 | + private static <T extends Throwable> T doAssertThrows( |
| 69 | + Class<T> expectedThrowable, ThrowingSupplier supplier, boolean userPassedSupplier) { |
| 70 | + checkNotNull(expectedThrowable); |
| 71 | + checkNotNull(supplier); |
| 72 | + Predicate<Throwable> predicate = INSTANCE_OF.get(expectedThrowable); |
| 73 | + if (predicate == null) { |
| 74 | + throw new IllegalArgumentException( |
| 75 | + expectedThrowable |
| 76 | + + " is not yet supported by ReflectionFreeAssertThrows. Add an entry for it in the" |
| 77 | + + " map in that class."); |
| 78 | + } |
| 79 | + Object result; |
| 80 | + try { |
| 81 | + result = supplier.get(); |
| 82 | + } catch (Throwable t) { |
| 83 | + if (predicate.apply(t)) { |
| 84 | + // We are careful to set up INSTANCE_OF to match each Predicate to its target Class. |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + T caught = (T) t; |
| 87 | + return caught; |
| 88 | + } |
| 89 | + throw new AssertionError( |
| 90 | + "expected to throw " + expectedThrowable.getSimpleName() + " but threw " + t, t); |
| 91 | + } |
| 92 | + if (userPassedSupplier) { |
| 93 | + throw new AssertionError( |
| 94 | + "expected to throw " |
| 95 | + + expectedThrowable.getSimpleName() |
| 96 | + + " but returned result: " |
| 97 | + + result); |
| 98 | + } else { |
| 99 | + throw new AssertionError("expected to throw " + expectedThrowable.getSimpleName()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private enum PlatformSpecificExceptionBatch { |
| 104 | + PLATFORM { |
| 105 | + @GwtIncompatible |
| 106 | + @J2ktIncompatible |
| 107 | + @Override |
| 108 | + // returns the types available in "normal" environments |
| 109 | + ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> exceptions() { |
| 110 | + return ImmutableMap.of( |
| 111 | + InvocationTargetException.class, |
| 112 | + e -> e instanceof InvocationTargetException, |
| 113 | + StackOverflowError.class, |
| 114 | + e -> e instanceof StackOverflowError); |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + // used under GWT, etc., since the override of this method does not exist there |
| 119 | + ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> exceptions() { |
| 120 | + return ImmutableMap.of(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static final ImmutableMap<Class<? extends Throwable>, Predicate<Throwable>> INSTANCE_OF = |
| 125 | + ImmutableMap.<Class<? extends Throwable>, Predicate<Throwable>>builder() |
| 126 | + .put(ArithmeticException.class, e -> e instanceof ArithmeticException) |
| 127 | + .put( |
| 128 | + ArrayIndexOutOfBoundsException.class, |
| 129 | + e -> e instanceof ArrayIndexOutOfBoundsException) |
| 130 | + .put(ArrayStoreException.class, e -> e instanceof ArrayStoreException) |
| 131 | + .put(AssertionFailedError.class, e -> e instanceof AssertionFailedError) |
| 132 | + .put(CancellationException.class, e -> e instanceof CancellationException) |
| 133 | + .put(ClassCastException.class, e -> e instanceof ClassCastException) |
| 134 | + .put( |
| 135 | + ConcurrentModificationException.class, |
| 136 | + e -> e instanceof ConcurrentModificationException) |
| 137 | + .put(ExecutionError.class, e -> e instanceof ExecutionError) |
| 138 | + .put(ExecutionException.class, e -> e instanceof ExecutionException) |
| 139 | + .put(IllegalArgumentException.class, e -> e instanceof IllegalArgumentException) |
| 140 | + .put(IllegalStateException.class, e -> e instanceof IllegalStateException) |
| 141 | + .put(IndexOutOfBoundsException.class, e -> e instanceof IndexOutOfBoundsException) |
| 142 | + .put(NoSuchElementException.class, e -> e instanceof NoSuchElementException) |
| 143 | + .put(NullPointerException.class, e -> e instanceof NullPointerException) |
| 144 | + .put(NumberFormatException.class, e -> e instanceof NumberFormatException) |
| 145 | + .put(RuntimeException.class, e -> e instanceof RuntimeException) |
| 146 | + .put(TimeoutException.class, e -> e instanceof TimeoutException) |
| 147 | + .put(UncheckedExecutionException.class, e -> e instanceof UncheckedExecutionException) |
| 148 | + .put(UnsupportedCharsetException.class, e -> e instanceof UnsupportedCharsetException) |
| 149 | + .put(UnsupportedOperationException.class, e -> e instanceof UnsupportedOperationException) |
| 150 | + .put(VerifyException.class, e -> e instanceof VerifyException) |
| 151 | + .putAll(PlatformSpecificExceptionBatch.PLATFORM.exceptions()) |
| 152 | + .buildOrThrow(); |
| 153 | + |
| 154 | + private ReflectionFreeAssertThrows() {} |
| 155 | +} |
0 commit comments