Skip to content

Commit ca12c33

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
And use `assertThrows` in some more places where it is now obvious that that is possible. PiperOrigin-RevId: 686721569
1 parent 702e4b2 commit ca12c33

File tree

9 files changed

+80
-125
lines changed

9 files changed

+80
-125
lines changed

android/guava-testlib/test/com/google/common/collect/testing/features/FeatureEnumTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ private static void assertGoodTesterAnnotation(Class<? extends Annotation> annot
5252
try {
5353
method = annotationClass.getMethod(propertyName);
5454
} catch (NoSuchMethodException e) {
55-
fail(
56-
rootLocaleFormat("%s must have a property named '%s'.", annotationClass, propertyName));
55+
throw new AssertionError("Annotation is missing required method", e);
5756
}
5857
final Class<?> returnType = method.getReturnType();
5958
assertTrue(

android/guava-tests/test/com/google/common/cache/CacheLoadingTest.java

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static com.google.common.cache.TestingCacheLoaders.identityLoader;
2222
import static com.google.common.cache.TestingRemovalListeners.countingRemovalListener;
2323
import static com.google.common.truth.Truth.assertThat;
24-
import static java.lang.Thread.currentThread;
2524
import static java.util.Arrays.asList;
2625
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2726
import static org.junit.Assert.assertThrows;
@@ -75,7 +74,7 @@ public void setUp() throws Exception {
7574
public void tearDown() throws Exception {
7675
super.tearDown();
7776
// TODO(cpovirk): run tests in other thread instead of messing with main thread interrupt status
78-
currentThread().interrupted();
77+
Thread.interrupted();
7978
LocalCache.logger.removeHandler(logHandler);
8079
}
8180

@@ -1148,11 +1147,11 @@ public void testLoadInterruptedException() {
11481147
assertEquals(0, stats.hitCount());
11491148

11501149
// Sanity check:
1151-
assertFalse(currentThread().interrupted());
1150+
assertFalse(Thread.interrupted());
11521151

11531152
Exception expected = assertThrows(ExecutionException.class, () -> cache.get(new Object()));
11541153
assertThat(expected).hasCauseThat().isSameInstanceAs(e);
1155-
assertTrue(currentThread().interrupted());
1154+
assertTrue(Thread.interrupted());
11561155
stats = cache.stats();
11571156
assertEquals(1, stats.missCount());
11581157
assertEquals(0, stats.loadSuccessCount());
@@ -1162,15 +1161,15 @@ public void testLoadInterruptedException() {
11621161
expected =
11631162
assertThrows(UncheckedExecutionException.class, () -> cache.getUnchecked(new Object()));
11641163
assertThat(expected).hasCauseThat().isSameInstanceAs(e);
1165-
assertTrue(currentThread().interrupted());
1164+
assertTrue(Thread.interrupted());
11661165
stats = cache.stats();
11671166
assertEquals(2, stats.missCount());
11681167
assertEquals(0, stats.loadSuccessCount());
11691168
assertEquals(2, stats.loadExceptionCount());
11701169
assertEquals(0, stats.hitCount());
11711170

11721171
cache.refresh(new Object());
1173-
assertTrue(currentThread().interrupted());
1172+
assertTrue(Thread.interrupted());
11741173
checkLoggedCause(e);
11751174
stats = cache.stats();
11761175
assertEquals(2, stats.missCount());
@@ -1183,7 +1182,7 @@ public void testLoadInterruptedException() {
11831182
assertThrows(
11841183
ExecutionException.class, () -> cache.get(new Object(), throwing(callableException)));
11851184
assertThat(expected).hasCauseThat().isSameInstanceAs(callableException);
1186-
assertTrue(currentThread().interrupted());
1185+
assertTrue(Thread.interrupted());
11871186
stats = cache.stats();
11881187
assertEquals(3, stats.missCount());
11891188
assertEquals(0, stats.loadSuccessCount());
@@ -1192,7 +1191,7 @@ public void testLoadInterruptedException() {
11921191

11931192
expected = assertThrows(ExecutionException.class, () -> cache.getAll(asList(new Object())));
11941193
assertThat(expected).hasCauseThat().isSameInstanceAs(e);
1195-
assertTrue(currentThread().interrupted());
1194+
assertTrue(Thread.interrupted());
11961195
stats = cache.stats();
11971196
assertEquals(4, stats.missCount());
11981197
assertEquals(0, stats.loadSuccessCount());
@@ -1392,7 +1391,7 @@ public void testBulkLoadInterruptedException() {
13921391
ExecutionException expected =
13931392
assertThrows(ExecutionException.class, () -> cache.getAll(asList(new Object())));
13941393
assertThat(expected).hasCauseThat().isSameInstanceAs(e);
1395-
assertTrue(currentThread().interrupted());
1394+
assertTrue(Thread.interrupted());
13961395
stats = cache.stats();
13971396
assertEquals(1, stats.missCount());
13981397
assertEquals(0, stats.loadSuccessCount());
@@ -1765,31 +1764,22 @@ public void testLoadingExceptionWithCause() {
17651764
LoadingCache<Object, Object> cacheChecked =
17661765
CacheBuilder.newBuilder().build(exceptionLoader(ee));
17671766

1768-
try {
1769-
cacheUnchecked.get(new Object());
1770-
fail();
1771-
} catch (ExecutionException e) {
1772-
fail();
1773-
} catch (UncheckedExecutionException caughtEe) {
1774-
assertThat(caughtEe).hasCauseThat().isSameInstanceAs(uee);
1775-
}
1776-
17771767
UncheckedExecutionException caughtUee =
1768+
assertThrows(UncheckedExecutionException.class, () -> cacheUnchecked.get(new Object()));
1769+
assertThat(caughtUee).hasCauseThat().isSameInstanceAs(uee);
1770+
1771+
caughtUee =
17781772
assertThrows(
17791773
UncheckedExecutionException.class, () -> cacheUnchecked.getUnchecked(new Object()));
17801774
assertThat(caughtUee).hasCauseThat().isSameInstanceAs(uee);
17811775

17821776
cacheUnchecked.refresh(new Object());
17831777
checkLoggedCause(uee);
17841778

1785-
try {
1786-
cacheUnchecked.getAll(asList(new Object()));
1787-
fail();
1788-
} catch (ExecutionException e) {
1789-
fail();
1790-
} catch (UncheckedExecutionException caughtEe) {
1791-
assertThat(caughtEe).hasCauseThat().isSameInstanceAs(uee);
1792-
}
1779+
caughtUee =
1780+
assertThrows(
1781+
UncheckedExecutionException.class, () -> cacheUnchecked.getAll(asList(new Object())));
1782+
assertThat(caughtUee).hasCauseThat().isSameInstanceAs(uee);
17931783

17941784
ExecutionException caughtEe =
17951785
assertThrows(ExecutionException.class, () -> cacheChecked.get(new Object()));
@@ -1818,14 +1808,10 @@ public void testBulkLoadingExceptionWithCause() {
18181808
LoadingCache<Object, Object> cacheChecked =
18191809
CacheBuilder.newBuilder().build(bulkLoader(exceptionLoader(ee)));
18201810

1821-
try {
1822-
cacheUnchecked.getAll(asList(new Object()));
1823-
fail();
1824-
} catch (ExecutionException e) {
1825-
fail();
1826-
} catch (UncheckedExecutionException caughtEe) {
1827-
assertThat(caughtEe).hasCauseThat().isSameInstanceAs(uee);
1828-
}
1811+
UncheckedExecutionException caughtUee =
1812+
assertThrows(
1813+
UncheckedExecutionException.class, () -> cacheUnchecked.getAll(asList(new Object())));
1814+
assertThat(caughtUee).hasCauseThat().isSameInstanceAs(uee);
18291815

18301816
ExecutionException caughtEe =
18311817
assertThrows(ExecutionException.class, () -> cacheChecked.getAll(asList(new Object())));
@@ -1897,6 +1883,7 @@ private static void testConcurrentLoadingNull(CacheBuilder<Object, Object> build
18971883
builder.build(
18981884
new CacheLoader<String, String>() {
18991885
@Override
1886+
@SuppressWarnings("CacheLoaderNull") // test of broken user implementation
19001887
public String load(String key) throws InterruptedException {
19011888
callCount.incrementAndGet();
19021889
startSignal.await();

android/guava-tests/test/com/google/common/eventbus/EventBusTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,7 @@ public void throwExceptionOn(String message) {
159159
}
160160
};
161161
eventBus.register(subscriber);
162-
try {
163-
eventBus.post(EVENT);
164-
} catch (RuntimeException e) {
165-
fail("Exception should not be thrown.");
166-
}
162+
eventBus.post(EVENT);
167163
}
168164

169165
public void testDeadEventForwarding() {

android/guava-tests/test/com/google/common/util/concurrent/JSR166TestCase.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
* tests.
100100
* </ul>
101101
*/
102+
// We call threadUnexpectedException, which does the right thing for errors.
103+
@SuppressWarnings("AssertionFailureIgnored")
102104
abstract class JSR166TestCase extends TestCase {
103105
private static final boolean useSecurityManager = Boolean.getBoolean("jsr166.useSecurityManager");
104106

@@ -449,54 +451,46 @@ static void delay(long millis) throws InterruptedException {
449451
}
450452

451453
/** Waits out termination of a thread pool or fails doing so. */
452-
void joinPool(ExecutorService exec) {
454+
void joinPool(ExecutorService exec) throws InterruptedException {
453455
try {
454456
exec.shutdown();
455457
assertTrue(
456458
"ExecutorService did not terminate in a timely manner",
457459
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS));
458460
} catch (SecurityException ok) {
459461
// Allowed in case test doesn't have privs
460-
} catch (InterruptedException ie) {
461-
fail("Unexpected InterruptedException");
462462
}
463463
}
464464

465465
/**
466466
* Checks that thread does not terminate within the default millisecond delay of {@code
467467
* timeoutMillis()}.
468468
*/
469-
void assertThreadStaysAlive(Thread thread) {
469+
void assertThreadStaysAlive(Thread thread) throws InterruptedException {
470470
assertThreadStaysAlive(thread, timeoutMillis());
471471
}
472472

473473
/** Checks that thread does not terminate within the given millisecond delay. */
474-
void assertThreadStaysAlive(Thread thread, long millis) {
475-
try {
476-
// No need to optimize the failing case via Thread.join.
477-
delay(millis);
478-
assertTrue(thread.isAlive());
479-
} catch (InterruptedException ie) {
480-
fail("Unexpected InterruptedException");
481-
}
474+
void assertThreadStaysAlive(Thread thread, long millis) throws InterruptedException {
475+
// No need to optimize the failing case via Thread.join.
476+
delay(millis);
477+
assertTrue(thread.isAlive());
482478
}
483479

484480
/**
485481
* Checks that the threads do not terminate within the default millisecond delay of {@code
486482
* timeoutMillis()}.
487483
*/
488-
void assertThreadsStayAlive(Thread... threads) {
484+
void assertThreadsStayAlive(Thread... threads) throws InterruptedException {
489485
assertThreadsStayAlive(timeoutMillis(), threads);
490486
}
491487

492488
/** Checks that the threads do not terminate within the given millisecond delay. */
493-
void assertThreadsStayAlive(long millis, Thread... threads) {
494-
try {
495-
// No need to optimize the failing case via Thread.join.
496-
delay(millis);
497-
for (Thread thread : threads) assertTrue(thread.isAlive());
498-
} catch (InterruptedException ie) {
499-
fail("Unexpected InterruptedException");
489+
void assertThreadsStayAlive(long millis, Thread... threads) throws InterruptedException {
490+
// No need to optimize the failing case via Thread.join.
491+
delay(millis);
492+
for (Thread thread : threads) {
493+
assertTrue(thread.isAlive());
500494
}
501495
}
502496

guava-testlib/test/com/google/common/collect/testing/features/FeatureEnumTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ private static void assertGoodTesterAnnotation(Class<? extends Annotation> annot
5252
try {
5353
method = annotationClass.getMethod(propertyName);
5454
} catch (NoSuchMethodException e) {
55-
fail(
56-
rootLocaleFormat("%s must have a property named '%s'.", annotationClass, propertyName));
55+
throw new AssertionError("Annotation is missing required method", e);
5756
}
5857
final Class<?> returnType = method.getReturnType();
5958
assertTrue(

guava-testlib/test/com/google/common/testing/NullPointerTesterTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
* @author Kevin Bourrillion
5757
* @author Mick Killianey
5858
*/
59-
@SuppressWarnings("CheckReturnValue")
59+
@SuppressWarnings({
60+
"CheckReturnValue",
61+
"unused", // many methods tested reflectively -- maybe prefer local @Keep annotations?
62+
})
6063
public class NullPointerTesterTest extends TestCase {
6164

6265
/** Non-NPE RuntimeException. */
@@ -268,7 +271,7 @@ public void testStaticOneArgMethodsThatShouldPass() throws Exception {
268271
try {
269272
new NullPointerTester().testMethodParameter(new OneArg(), method, 0);
270273
} catch (AssertionError unexpected) {
271-
fail("Should not have flagged method " + methodName);
274+
throw new AssertionError("Should not have flagged method " + methodName, unexpected);
272275
}
273276
}
274277
}
@@ -293,7 +296,7 @@ public void testNonStaticOneArgMethodsThatShouldPass() throws Exception {
293296
try {
294297
new NullPointerTester().testMethodParameter(foo, method, 0);
295298
} catch (AssertionError unexpected) {
296-
fail("Should not have flagged method " + methodName);
299+
throw new AssertionError("Should not have flagged method " + methodName, unexpected);
297300
}
298301
}
299302
}

0 commit comments

Comments
 (0)