diff options
Diffstat (limited to 'guava-tests/test/com/google/common/cache')
9 files changed, 302 insertions, 545 deletions
diff --git a/guava-tests/test/com/google/common/cache/AbstractLoadingCacheTest.java b/guava-tests/test/com/google/common/cache/AbstractLoadingCacheTest.java index 84e004a8b..c029ff3d0 100644 --- a/guava-tests/test/com/google/common/cache/AbstractLoadingCacheTest.java +++ b/guava-tests/test/com/google/common/cache/AbstractLoadingCacheTest.java @@ -17,6 +17,7 @@ package com.google.common.cache; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import com.google.common.util.concurrent.ExecutionError; import com.google.common.util.concurrent.UncheckedExecutionException; @@ -52,12 +53,9 @@ public class AbstractLoadingCacheTest extends TestCase { } }; - try { - cache.getUnchecked(new Object()); - fail(); - } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isEqualTo(cause); - } + UncheckedExecutionException expected = + assertThrows(UncheckedExecutionException.class, () -> cache.getUnchecked(new Object())); + assertThat(expected).hasCauseThat().isEqualTo(cause); Object newValue = new Object(); valueRef.set(newValue); @@ -84,12 +82,9 @@ public class AbstractLoadingCacheTest extends TestCase { } }; - try { - cache.getUnchecked(new Object()); - fail(); - } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isEqualTo(cause); - } + UncheckedExecutionException expected = + assertThrows(UncheckedExecutionException.class, () -> cache.getUnchecked(new Object())); + assertThat(expected).hasCauseThat().isEqualTo(cause); Object newValue = new Object(); valueRef.set(newValue); @@ -116,12 +111,9 @@ public class AbstractLoadingCacheTest extends TestCase { } }; - try { - cache.getUnchecked(new Object()); - fail(); - } catch (ExecutionError expected) { - assertThat(expected).hasCauseThat().isEqualTo(cause); - } + ExecutionError expected = + assertThrows(ExecutionError.class, () -> cache.getUnchecked(new Object())); + assertThat(expected).hasCauseThat().isEqualTo(cause); Object newValue = new Object(); valueRef.set(newValue); @@ -148,12 +140,9 @@ public class AbstractLoadingCacheTest extends TestCase { } }; - try { - cache.getUnchecked(new Object()); - fail(); - } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isEqualTo(cause); - } + UncheckedExecutionException expected = + assertThrows(UncheckedExecutionException.class, () -> cache.getUnchecked(new Object())); + assertThat(expected).hasCauseThat().isEqualTo(cause); Object newValue = new Object(); valueRef.set(newValue); diff --git a/guava-tests/test/com/google/common/cache/CacheBuilderSpecTest.java b/guava-tests/test/com/google/common/cache/CacheBuilderSpecTest.java index 09f2eb6c2..8a60cd30d 100644 --- a/guava-tests/test/com/google/common/cache/CacheBuilderSpecTest.java +++ b/guava-tests/test/com/google/common/cache/CacheBuilderSpecTest.java @@ -18,6 +18,7 @@ package com.google.common.cache; import static com.google.common.cache.CacheBuilderSpec.parse; import static com.google.common.cache.TestingWeighers.constantWeigher; +import static org.junit.Assert.assertThrows; import com.google.common.base.Suppliers; import com.google.common.cache.LocalCache.Strength; @@ -60,11 +61,8 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_initialCapacityRepeated() { - try { - parse("initialCapacity=10, initialCapacity=20"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, () -> parse("initialCapacity=10, initialCapacity=20")); } public void testParse_maximumSize() { @@ -81,11 +79,7 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_maximumSizeRepeated() { - try { - parse("maximumSize=10, maximumSize=20"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("maximumSize=10, maximumSize=20")); } public void testParse_maximumWeight() { @@ -102,19 +96,11 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_maximumWeightRepeated() { - try { - parse("maximumWeight=10, maximumWeight=20"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("maximumWeight=10, maximumWeight=20")); } public void testParse_maximumSizeAndMaximumWeight() { - try { - parse("maximumSize=10, maximumWeight=20"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("maximumSize=10, maximumWeight=20")); } public void testParse_concurrencyLevel() { @@ -132,11 +118,8 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_concurrencyLevelRepeated() { - try { - parse("concurrencyLevel=10, concurrencyLevel=20"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, () -> parse("concurrencyLevel=10, concurrencyLevel=20")); } public void testParse_weakKeys() { @@ -153,19 +136,11 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_weakKeysCannotHaveValue() { - try { - parse("weakKeys=true"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("weakKeys=true")); } public void testParse_repeatedKeyStrength() { - try { - parse("weakKeys, weakKeys"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("weakKeys, weakKeys")); } public void testParse_softValues() { @@ -182,11 +157,7 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_softValuesCannotHaveValue() { - try { - parse("softValues=true"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("softValues=true")); } public void testParse_weakValues() { @@ -203,37 +174,17 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_weakValuesCannotHaveValue() { - try { - parse("weakValues=true"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("weakValues=true")); } public void testParse_repeatedValueStrength() { - try { - parse("softValues, softValues"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } - - try { - parse("softValues, weakValues"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } - - try { - parse("weakValues, softValues"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } - - try { - parse("weakValues, weakValues"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("softValues, softValues")); + + assertThrows(IllegalArgumentException.class, () -> parse("softValues, weakValues")); + + assertThrows(IllegalArgumentException.class, () -> parse("weakValues, softValues")); + + assertThrows(IllegalArgumentException.class, () -> parse("weakValues, weakValues")); } public void testParse_writeExpirationDays() { @@ -276,11 +227,8 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_writeExpirationRepeated() { - try { - parse("expireAfterWrite=10s,expireAfterWrite=10m"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, () -> parse("expireAfterWrite=10s,expireAfterWrite=10m")); } public void testParse_accessExpirationDays() { @@ -325,11 +273,8 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_accessExpirationRepeated() { - try { - parse("expireAfterAccess=10s,expireAfterAccess=10m"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, () -> parse("expireAfterAccess=10s,expireAfterAccess=10m")); } public void testParse_recordStats() { @@ -339,19 +284,11 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_recordStatsValueSpecified() { - try { - parse("recordStats=True"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("recordStats=True")); } public void testParse_recordStatsRepeated() { - try { - parse("recordStats,recordStats"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("recordStats,recordStats")); } public void testParse_accessExpirationAndWriteExpiration() { @@ -419,31 +356,15 @@ public class CacheBuilderSpecTest extends TestCase { } public void testParse_unknownKey() { - try { - parse("foo=17"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("foo=17")); } public void testParse_extraCommaIsInvalid() { - try { - parse("weakKeys,"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("weakKeys,")); - try { - parse(",weakKeys"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse(",weakKeys")); - try { - parse("weakKeys,,softValues"); - fail("Expected exception"); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> parse("weakKeys,,softValues")); } public void testEqualsAndHashCode() { @@ -477,11 +398,9 @@ public class CacheBuilderSpecTest extends TestCase { @SuppressWarnings("ReturnValueIgnored") public void testMaximumWeight_withoutWeigher() { CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumWeight=9000")); - try { - builder.build(CacheLoader.from(Suppliers.ofInstance(null))); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows( + IllegalStateException.class, + () -> builder.build(CacheLoader.from(Suppliers.ofInstance(null)))); } @SuppressWarnings("ReturnValueIgnored") diff --git a/guava-tests/test/com/google/common/cache/CacheBuilderTest.java b/guava-tests/test/com/google/common/cache/CacheBuilderTest.java index dad5ff4fb..9e8a1a2db 100644 --- a/guava-tests/test/com/google/common/cache/CacheBuilderTest.java +++ b/guava-tests/test/com/google/common/cache/CacheBuilderTest.java @@ -26,6 +26,7 @@ import static com.google.common.truth.Truth.assertThat; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; @@ -164,11 +165,7 @@ public class CacheBuilderTest extends TestCase { @GwtIncompatible // maximumWeight public void testMaximumSize_andWeight() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().maximumSize(16); - try { - builder.maximumWeight(16); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> builder.maximumWeight(16)); } @GwtIncompatible // digs into internals of the non-GWT implementation @@ -182,98 +179,53 @@ public class CacheBuilderTest extends TestCase { @GwtIncompatible // maximumWeight public void testMaximumWeight_negative() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); - try { - builder.maximumWeight(-1); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> builder.maximumWeight(-1)); } @GwtIncompatible // maximumWeight public void testMaximumWeight_setTwice() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().maximumWeight(16); - try { - // even to the same value is not allowed - builder.maximumWeight(16); - fail(); - } catch (IllegalStateException expected) { - } - try { - builder.maximumSize(16); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> builder.maximumWeight(16)); + assertThrows(IllegalStateException.class, () -> builder.maximumSize(16)); } @GwtIncompatible // maximumWeight public void testMaximumWeight_withoutWeigher() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().maximumWeight(1); - try { - builder.build(identityLoader()); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> builder.build(identityLoader())); } @GwtIncompatible // weigher public void testWeigher_withoutMaximumWeight() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().weigher(constantWeigher(42)); - try { - builder.build(identityLoader()); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> builder.build(identityLoader())); } @GwtIncompatible // weigher public void testWeigher_withMaximumSize() { - try { - CacheBuilder.newBuilder().weigher(constantWeigher(42)).maximumSize(1); - fail(); - } catch (IllegalStateException expected) { - } - try { - CacheBuilder.newBuilder().maximumSize(1).weigher(constantWeigher(42)); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows( + IllegalStateException.class, + () -> CacheBuilder.newBuilder().weigher(constantWeigher(42)).maximumSize(1)); + assertThrows( + IllegalStateException.class, + () -> CacheBuilder.newBuilder().maximumSize(1).weigher(constantWeigher(42))); } @GwtIncompatible // weakKeys public void testKeyStrengthSetTwice() { CacheBuilder<Object, Object> builder1 = CacheBuilder.newBuilder().weakKeys(); - try { - builder1.weakKeys(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> builder1.weakKeys()); } @GwtIncompatible // weakValues public void testValueStrengthSetTwice() { CacheBuilder<Object, Object> builder1 = CacheBuilder.newBuilder().weakValues(); - try { - builder1.weakValues(); - fail(); - } catch (IllegalStateException expected) { - } - try { - builder1.softValues(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> builder1.weakValues()); + assertThrows(IllegalStateException.class, () -> builder1.softValues()); CacheBuilder<Object, Object> builder2 = CacheBuilder.newBuilder().softValues(); - try { - builder2.softValues(); - fail(); - } catch (IllegalStateException expected) { - } - try { - builder2.weakValues(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> builder2.softValues()); + assertThrows(IllegalStateException.class, () -> builder2.weakValues()); } @GwtIncompatible // java.time.Duration @@ -298,11 +250,9 @@ public class CacheBuilderTest extends TestCase { @GwtIncompatible // java.time.Duration public void testTimeToLive_negative_duration() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); - try { - builder.expireAfterWrite(java.time.Duration.ofSeconds(-1)); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> builder.expireAfterWrite(java.time.Duration.ofSeconds(-1))); } @SuppressWarnings("ReturnValueIgnored") @@ -326,12 +276,9 @@ public class CacheBuilderTest extends TestCase { public void testTimeToLive_setTwice_duration() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().expireAfterWrite(java.time.Duration.ofSeconds(3600)); - try { - // even to the same value is not allowed - builder.expireAfterWrite(java.time.Duration.ofSeconds(3600)); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows( + IllegalStateException.class, + () -> builder.expireAfterWrite(java.time.Duration.ofSeconds(3600))); } public void testTimeToIdle_negative() { @@ -346,11 +293,9 @@ public class CacheBuilderTest extends TestCase { @GwtIncompatible // java.time.Duration public void testTimeToIdle_negative_duration() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); - try { - builder.expireAfterAccess(java.time.Duration.ofSeconds(-1)); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, + () -> builder.expireAfterAccess(java.time.Duration.ofSeconds(-1))); } @SuppressWarnings("ReturnValueIgnored") @@ -374,12 +319,9 @@ public class CacheBuilderTest extends TestCase { public void testTimeToIdle_setTwice_duration() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().expireAfterAccess(java.time.Duration.ofSeconds(3600)); - try { - // even to the same value is not allowed - builder.expireAfterAccess(java.time.Duration.ofSeconds(3600)); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows( + IllegalStateException.class, + () -> builder.expireAfterAccess(java.time.Duration.ofSeconds(3600))); } @SuppressWarnings("ReturnValueIgnored") @@ -394,45 +336,30 @@ public class CacheBuilderTest extends TestCase { @GwtIncompatible // refreshAfterWrite public void testRefresh_zero() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); - try { - builder.refreshAfterWrite(0, SECONDS); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> builder.refreshAfterWrite(0, SECONDS)); } @GwtIncompatible // java.time.Duration public void testRefresh_zero_duration() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); - try { - builder.refreshAfterWrite(java.time.Duration.ZERO); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows( + IllegalArgumentException.class, () -> builder.refreshAfterWrite(java.time.Duration.ZERO)); } @GwtIncompatible // refreshAfterWrite public void testRefresh_setTwice() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().refreshAfterWrite(3600, SECONDS); - try { - // even to the same value is not allowed - builder.refreshAfterWrite(3600, SECONDS); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> builder.refreshAfterWrite(3600, SECONDS)); } @GwtIncompatible // java.time.Duration public void testRefresh_setTwice_duration() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().refreshAfterWrite(java.time.Duration.ofSeconds(3600)); - try { - // even to the same value is not allowed - builder.refreshAfterWrite(java.time.Duration.ofSeconds(3600)); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows( + IllegalStateException.class, + () -> builder.refreshAfterWrite(java.time.Duration.ofSeconds(3600))); } public void testTicker_setTwice() { diff --git a/guava-tests/test/com/google/common/cache/CacheLoadingTest.java b/guava-tests/test/com/google/common/cache/CacheLoadingTest.java index 9b193ed7f..df36a81c7 100644 --- a/guava-tests/test/com/google/common/cache/CacheLoadingTest.java +++ b/guava-tests/test/com/google/common/cache/CacheLoadingTest.java @@ -24,6 +24,7 @@ import static com.google.common.truth.Truth.assertThat; import static java.lang.Thread.currentThread; import static java.util.Arrays.asList; import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static org.junit.Assert.assertThrows; import com.google.common.cache.CacheLoader.InvalidCacheLoadException; import com.google.common.cache.TestingCacheLoaders.CountingLoader; @@ -521,11 +522,7 @@ public class CacheLoadingTest extends TestCase { assertSame(extraKey, cache.asMap().get(extraKey)); Object[] lookupKeys = new Object[] {new Object(), new Object(), new Object()}; - try { - cache.getAll(asList(lookupKeys)); - fail(); - } catch (InvalidCacheLoadException expected) { - } + assertThrows(InvalidCacheLoadException.class, () -> cache.getAll(asList(lookupKeys))); for (Object key : lookupKeys) { assertTrue(cache.asMap().containsKey(key)); @@ -561,11 +558,7 @@ public class CacheLoadingTest extends TestCase { assertSame(extraKey, cache.asMap().get(extraKey)); Object[] lookupKeys = new Object[] {new Object(), new Object(), new Object()}; - try { - cache.getAll(asList(lookupKeys)); - fail(); - } catch (InvalidCacheLoadException expected) { - } + assertThrows(InvalidCacheLoadException.class, () -> cache.getAll(asList(lookupKeys))); for (Object key : lookupKeys) { assertTrue(cache.asMap().containsKey(key)); @@ -595,11 +588,7 @@ public class CacheLoadingTest extends TestCase { LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().build(loader); Object[] lookupKeys = new Object[] {new Object(), new Object(), new Object()}; - try { - cache.getAll(asList(lookupKeys)); - fail(); - } catch (InvalidCacheLoadException expected) { - } + assertThrows(InvalidCacheLoadException.class, () -> cache.getAll(asList(lookupKeys))); assertSame(extraValue, cache.asMap().get(extraKey)); } @@ -612,22 +601,14 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.get(new Object()); - fail(); - } catch (InvalidCacheLoadException expected) { - } + assertThrows(InvalidCacheLoadException.class, () -> cache.get(new Object())); stats = cache.stats(); assertEquals(1, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); assertEquals(1, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getUnchecked(new Object()); - fail(); - } catch (InvalidCacheLoadException expected) { - } + assertThrows(InvalidCacheLoadException.class, () -> cache.getUnchecked(new Object())); stats = cache.stats(); assertEquals(2, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -642,22 +623,15 @@ public class CacheLoadingTest extends TestCase { assertEquals(3, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.get(new Object(), Callables.returning(null)); - fail(); - } catch (InvalidCacheLoadException expected) { - } + assertThrows( + InvalidCacheLoadException.class, () -> cache.get(new Object(), Callables.returning(null))); stats = cache.stats(); assertEquals(3, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); assertEquals(4, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (InvalidCacheLoadException expected) { - } + assertThrows(InvalidCacheLoadException.class, () -> cache.getAll(asList(new Object()))); stats = cache.stats(); assertEquals(4, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -828,11 +802,7 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (InvalidCacheLoadException expected) { - } + assertThrows(InvalidCacheLoadException.class, () -> cache.getAll(asList(new Object()))); stats = cache.stats(); assertEquals(1, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -863,11 +833,7 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (InvalidCacheLoadException expected) { - } + assertThrows(InvalidCacheLoadException.class, () -> cache.getAll(asList(new Object()))); stats = cache.stats(); assertEquals(1, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -885,24 +851,16 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.get(new Object()); - fail(); - } catch (ExecutionError expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + ExecutionError expected = assertThrows(ExecutionError.class, () -> cache.get(new Object())); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(1, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); assertEquals(1, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getUnchecked(new Object()); - fail(); - } catch (ExecutionError expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + expected = assertThrows(ExecutionError.class, () -> cache.getUnchecked(new Object())); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(2, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -918,31 +876,27 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.hitCount()); final Error callableError = new Error(); - try { - cache.get( - new Object(), - new Callable<Object>() { - @Override - public Object call() { - throw callableError; - } - }); - fail(); - } catch (ExecutionError expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(callableError); - } + expected = + assertThrows( + ExecutionError.class, + () -> + cache.get( + new Object(), + new Callable<Object>() { + @Override + public Object call() { + throw callableError; + } + })); + assertThat(expected).hasCauseThat().isSameInstanceAs(callableError); stats = cache.stats(); assertEquals(3, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); assertEquals(4, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (ExecutionError expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + expected = assertThrows(ExecutionError.class, () -> cache.getAll(asList(new Object()))); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(4, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -1118,12 +1072,9 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (ExecutionError expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + ExecutionError expected = + assertThrows(ExecutionError.class, () -> cache.getAll(asList(new Object()))); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(1, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -1141,24 +1092,17 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.get(new Object()); - fail(); - } catch (ExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + Exception expected = assertThrows(ExecutionException.class, () -> cache.get(new Object())); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(1, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); assertEquals(1, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getUnchecked(new Object()); - fail(); - } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + expected = + assertThrows(UncheckedExecutionException.class, () -> cache.getUnchecked(new Object())); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(2, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -1174,24 +1118,18 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.hitCount()); Exception callableException = new Exception(); - try { - cache.get(new Object(), throwing(callableException)); - fail(); - } catch (ExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(callableException); - } + expected = + assertThrows( + ExecutionException.class, () -> cache.get(new Object(), throwing(callableException))); + assertThat(expected).hasCauseThat().isSameInstanceAs(callableException); stats = cache.stats(); assertEquals(3, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); assertEquals(4, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (ExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + expected = assertThrows(ExecutionException.class, () -> cache.getAll(asList(new Object()))); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(4, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -1212,12 +1150,8 @@ public class CacheLoadingTest extends TestCase { // Sanity check: assertFalse(currentThread().interrupted()); - try { - cache.get(new Object()); - fail(); - } catch (ExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + Exception expected = assertThrows(ExecutionException.class, () -> cache.get(new Object())); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); assertTrue(currentThread().interrupted()); stats = cache.stats(); assertEquals(1, stats.missCount()); @@ -1225,12 +1159,9 @@ public class CacheLoadingTest extends TestCase { assertEquals(1, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getUnchecked(new Object()); - fail(); - } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + expected = + assertThrows(UncheckedExecutionException.class, () -> cache.getUnchecked(new Object())); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); assertTrue(currentThread().interrupted()); stats = cache.stats(); assertEquals(2, stats.missCount()); @@ -1248,12 +1179,10 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.hitCount()); Exception callableException = new InterruptedException(); - try { - cache.get(new Object(), throwing(callableException)); - fail(); - } catch (ExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(callableException); - } + expected = + assertThrows( + ExecutionException.class, () -> cache.get(new Object(), throwing(callableException))); + assertThat(expected).hasCauseThat().isSameInstanceAs(callableException); assertTrue(currentThread().interrupted()); stats = cache.stats(); assertEquals(3, stats.missCount()); @@ -1261,12 +1190,8 @@ public class CacheLoadingTest extends TestCase { assertEquals(4, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (ExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + expected = assertThrows(ExecutionException.class, () -> cache.getAll(asList(new Object()))); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); assertTrue(currentThread().interrupted()); stats = cache.stats(); assertEquals(4, stats.missCount()); @@ -1443,12 +1368,9 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (ExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + ExecutionException expected = + assertThrows(ExecutionException.class, () -> cache.getAll(asList(new Object()))); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(1, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -1467,12 +1389,9 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (ExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + ExecutionException expected = + assertThrows(ExecutionException.class, () -> cache.getAll(asList(new Object()))); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); assertTrue(currentThread().interrupted()); stats = cache.stats(); assertEquals(1, stats.missCount()); @@ -1491,24 +1410,18 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.get(new Object()); - fail(); - } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + UncheckedExecutionException expected = + assertThrows(UncheckedExecutionException.class, () -> cache.get(new Object())); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(1, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); assertEquals(1, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getUnchecked(new Object()); - fail(); - } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + expected = + assertThrows(UncheckedExecutionException.class, () -> cache.getUnchecked(new Object())); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(2, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -1524,24 +1437,20 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.hitCount()); Exception callableException = new RuntimeException(); - try { - cache.get(new Object(), throwing(callableException)); - fail(); - } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(callableException); - } + expected = + assertThrows( + UncheckedExecutionException.class, + () -> cache.get(new Object(), throwing(callableException))); + assertThat(expected).hasCauseThat().isSameInstanceAs(callableException); stats = cache.stats(); assertEquals(3, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); assertEquals(4, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + expected = + assertThrows(UncheckedExecutionException.class, () -> cache.getAll(asList(new Object()))); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(4, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -1717,12 +1626,9 @@ public class CacheLoadingTest extends TestCase { assertEquals(0, stats.loadExceptionCount()); assertEquals(0, stats.hitCount()); - try { - cache.getAll(asList(new Object())); - fail(); - } catch (UncheckedExecutionException expected) { - assertThat(expected).hasCauseThat().isSameInstanceAs(e); - } + UncheckedExecutionException expected = + assertThrows(UncheckedExecutionException.class, () -> cache.getAll(asList(new Object()))); + assertThat(expected).hasCauseThat().isSameInstanceAs(e); stats = cache.stats(); assertEquals(1, stats.missCount()); assertEquals(0, stats.loadSuccessCount()); @@ -1748,12 +1654,9 @@ public class CacheLoadingTest extends TestCase { LoadingCache<Integer, String> cache = CacheBuilder.newBuilder().removalListener(removalListener).build(failOnceFunction); - try { - cache.getUnchecked(1); - fail(); - } catch (UncheckedExecutionException ue) { - assertThat(ue).hasCauseThat().isSameInstanceAs(e); - } + UncheckedExecutionException ue = + assertThrows(UncheckedExecutionException.class, () -> cache.getUnchecked(1)); + assertThat(ue).hasCauseThat().isSameInstanceAs(e); assertEquals("1", cache.getUnchecked(1)); assertEquals(0, removalListener.getCount()); @@ -1871,12 +1774,10 @@ public class CacheLoadingTest extends TestCase { assertThat(caughtEe).hasCauseThat().isSameInstanceAs(uee); } - try { - cacheUnchecked.getUnchecked(new Object()); - fail(); - } catch (UncheckedExecutionException caughtUee) { - assertThat(caughtUee).hasCauseThat().isSameInstanceAs(uee); - } + UncheckedExecutionException caughtUee = + assertThrows( + UncheckedExecutionException.class, () -> cacheUnchecked.getUnchecked(new Object())); + assertThat(caughtUee).hasCauseThat().isSameInstanceAs(uee); cacheUnchecked.refresh(new Object()); checkLoggedCause(uee); @@ -1890,29 +1791,21 @@ public class CacheLoadingTest extends TestCase { assertThat(caughtEe).hasCauseThat().isSameInstanceAs(uee); } - try { - cacheChecked.get(new Object()); - fail(); - } catch (ExecutionException caughtEe) { - assertThat(caughtEe).hasCauseThat().isSameInstanceAs(ee); - } + ExecutionException caughtEe = + assertThrows(ExecutionException.class, () -> cacheChecked.get(new Object())); + assertThat(caughtEe).hasCauseThat().isSameInstanceAs(ee); - try { - cacheChecked.getUnchecked(new Object()); - fail(); - } catch (UncheckedExecutionException caughtUee) { - assertThat(caughtUee).hasCauseThat().isSameInstanceAs(ee); - } + caughtUee = + assertThrows( + UncheckedExecutionException.class, () -> cacheChecked.getUnchecked(new Object())); + assertThat(caughtUee).hasCauseThat().isSameInstanceAs(ee); cacheChecked.refresh(new Object()); checkLoggedCause(ee); - try { - cacheChecked.getAll(asList(new Object())); - fail(); - } catch (ExecutionException caughtEe) { - assertThat(caughtEe).hasCauseThat().isSameInstanceAs(ee); - } + caughtEe = + assertThrows(ExecutionException.class, () -> cacheChecked.getAll(asList(new Object()))); + assertThat(caughtEe).hasCauseThat().isSameInstanceAs(ee); } public void testBulkLoadingExceptionWithCause() { @@ -1934,12 +1827,9 @@ public class CacheLoadingTest extends TestCase { assertThat(caughtEe).hasCauseThat().isSameInstanceAs(uee); } - try { - cacheChecked.getAll(asList(new Object())); - fail(); - } catch (ExecutionException caughtEe) { - assertThat(caughtEe).hasCauseThat().isSameInstanceAs(ee); - } + ExecutionException caughtEe = + assertThrows(ExecutionException.class, () -> cacheChecked.getAll(asList(new Object()))); + assertThat(caughtEe).hasCauseThat().isSameInstanceAs(ee); } @AndroidIncompatible // Bug? expected:<1> but was:<2> diff --git a/guava-tests/test/com/google/common/cache/EmptyCachesTest.java b/guava-tests/test/com/google/common/cache/EmptyCachesTest.java index a5e3a59a0..ccdcb955d 100644 --- a/guava-tests/test/com/google/common/cache/EmptyCachesTest.java +++ b/guava-tests/test/com/google/common/cache/EmptyCachesTest.java @@ -19,6 +19,7 @@ import static com.google.common.cache.TestingCacheLoaders.identityLoader; import static java.util.Arrays.asList; import static java.util.concurrent.TimeUnit.DAYS; import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertThrows; import com.google.common.base.Function; import com.google.common.cache.CacheBuilderFactory.DurationSpec; @@ -89,22 +90,14 @@ public class EmptyCachesTest extends TestCase { public void testGet_null() throws ExecutionException { for (LoadingCache<Object, Object> cache : caches()) { - try { - cache.get(null); - fail("Expected NullPointerException"); - } catch (NullPointerException expected) { - } + assertThrows(NullPointerException.class, () -> cache.get(null)); checkEmpty(cache); } } public void testGetUnchecked_null() { for (LoadingCache<Object, Object> cache : caches()) { - try { - cache.getUnchecked(null); - fail("Expected NullPointerException"); - } catch (NullPointerException expected) { - } + assertThrows(NullPointerException.class, () -> cache.getUnchecked(null)); checkEmpty(cache); } } @@ -114,28 +107,17 @@ public class EmptyCachesTest extends TestCase { public void testKeySet_nullToArray() { for (LoadingCache<Object, Object> cache : caches()) { Set<Object> keys = cache.asMap().keySet(); - try { - keys.toArray((Object[]) null); - fail(); - } catch (NullPointerException expected) { - } + assertThrows(NullPointerException.class, () -> keys.toArray((Object[]) null)); checkEmpty(cache); } } public void testKeySet_addNotSupported() { for (LoadingCache<Object, Object> cache : caches()) { - try { - cache.asMap().keySet().add(1); - fail(); - } catch (UnsupportedOperationException expected) { - } - - try { - cache.asMap().keySet().addAll(asList(1, 2)); - fail(); - } catch (UnsupportedOperationException expected) { - } + assertThrows(UnsupportedOperationException.class, () -> cache.asMap().keySet().add(1)); + + assertThrows( + UnsupportedOperationException.class, () -> cache.asMap().keySet().addAll(asList(1, 2))); } } @@ -189,28 +171,17 @@ public class EmptyCachesTest extends TestCase { public void testValues_nullToArray() { for (LoadingCache<Object, Object> cache : caches()) { Collection<Object> values = cache.asMap().values(); - try { - values.toArray((Object[]) null); - fail(); - } catch (NullPointerException expected) { - } + assertThrows(NullPointerException.class, () -> values.toArray((Object[]) null)); checkEmpty(cache); } } public void testValues_addNotSupported() { for (LoadingCache<Object, Object> cache : caches()) { - try { - cache.asMap().values().add(1); - fail(); - } catch (UnsupportedOperationException expected) { - } - - try { - cache.asMap().values().addAll(asList(1, 2)); - fail(); - } catch (UnsupportedOperationException expected) { - } + assertThrows(UnsupportedOperationException.class, () -> cache.asMap().values().add(1)); + + assertThrows( + UnsupportedOperationException.class, () -> cache.asMap().values().addAll(asList(1, 2))); } } @@ -264,28 +235,20 @@ public class EmptyCachesTest extends TestCase { public void testEntrySet_nullToArray() { for (LoadingCache<Object, Object> cache : caches()) { Set<Entry<Object, Object>> entries = cache.asMap().entrySet(); - try { - entries.toArray((Entry<Object, Object>[]) null); - fail(); - } catch (NullPointerException expected) { - } + assertThrows( + NullPointerException.class, () -> entries.toArray((Entry<Object, Object>[]) null)); checkEmpty(cache); } } public void testEntrySet_addNotSupported() { for (LoadingCache<Object, Object> cache : caches()) { - try { - cache.asMap().entrySet().add(entryOf(1, 1)); - fail(); - } catch (UnsupportedOperationException expected) { - } - - try { - cache.asMap().values().addAll(asList(entryOf(1, 1), entryOf(2, 2))); - fail(); - } catch (UnsupportedOperationException expected) { - } + assertThrows( + UnsupportedOperationException.class, () -> cache.asMap().entrySet().add(entryOf(1, 1))); + + assertThrows( + UnsupportedOperationException.class, + () -> cache.asMap().values().addAll(asList(entryOf(1, 1), entryOf(2, 2)))); } } diff --git a/guava-tests/test/com/google/common/cache/LocalCacheMapComputeTest.java b/guava-tests/test/com/google/common/cache/LocalCacheMapComputeTest.java index 61f5fed2d..f418d105b 100644 --- a/guava-tests/test/com/google/common/cache/LocalCacheMapComputeTest.java +++ b/guava-tests/test/com/google/common/cache/LocalCacheMapComputeTest.java @@ -17,6 +17,7 @@ package com.google.common.cache; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import com.google.common.util.concurrent.UncheckedExecutionException; import java.util.ArrayList; @@ -180,20 +181,19 @@ public class LocalCacheMapComputeTest extends TestCase { } public void testComputeExceptionally() { - try { - doParallelCacheOp( - count, - n -> { - cache - .asMap() - .compute( - key, - (k, v) -> { - throw new RuntimeException(); - }); - }); - fail("Should not get here"); - } catch (RuntimeException ex) { - } + assertThrows( + RuntimeException.class, + () -> + doParallelCacheOp( + count, + n -> { + cache + .asMap() + .compute( + key, + (k, v) -> { + throw new RuntimeException(); + }); + })); } } diff --git a/guava-tests/test/com/google/common/cache/LocalCacheTest.java b/guava-tests/test/com/google/common/cache/LocalCacheTest.java index 7cc67e840..a16c0ab37 100644 --- a/guava-tests/test/com/google/common/cache/LocalCacheTest.java +++ b/guava-tests/test/com/google/common/cache/LocalCacheTest.java @@ -58,6 +58,7 @@ import com.google.common.testing.FakeTicker; import com.google.common.testing.NullPointerTester; import com.google.common.testing.SerializableTester; import com.google.common.testing.TestLogHandler; +import com.google.common.util.concurrent.UncheckedExecutionException; import java.io.Serializable; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; @@ -2688,8 +2689,86 @@ public class LocalCacheTest extends TestCase { assertEquals(localCacheTwo.ticker, localCacheThree.ticker); } + public void testLoadDifferentKeyInLoader() throws ExecutionException, InterruptedException { + LocalCache<String, String> cache = makeLocalCache(createCacheBuilder()); + String key1 = "key1"; + String key2 = "key2"; + + assertEquals( + key2, + cache.get( + key1, + new CacheLoader<String, String>() { + @Override + public String load(String key) throws Exception { + return cache.get(key2, identityLoader()); // loads a different key, should work + } + })); + } + + public void testRecursiveLoad() throws InterruptedException { + LocalCache<String, String> cache = makeLocalCache(createCacheBuilder()); + String key = "key"; + CacheLoader<String, String> loader = + new CacheLoader<String, String>() { + @Override + public String load(String key) throws Exception { + return cache.get(key, identityLoader()); // recursive load, this should fail + } + }; + testLoadThrows(key, cache, loader); + } + + public void testRecursiveLoadWithProxy() throws InterruptedException { + String key = "key"; + String otherKey = "otherKey"; + LocalCache<String, String> cache = makeLocalCache(createCacheBuilder()); + CacheLoader<String, String> loader = + new CacheLoader<String, String>() { + @Override + public String load(String key) throws Exception { + return cache.get( + key, + identityLoader()); // recursive load (same as the initial one), this should fail + } + }; + CacheLoader<String, String> proxyLoader = + new CacheLoader<String, String>() { + @Override + public String load(String key) throws Exception { + return cache.get(otherKey, loader); // loads another key, is ok + } + }; + testLoadThrows(key, cache, proxyLoader); + } + // utility methods + private void testLoadThrows( + String key, LocalCache<String, String> cache, CacheLoader<String, String> loader) + throws InterruptedException { + CountDownLatch doneSignal = new CountDownLatch(1); + Thread thread = + new Thread( + () -> { + try { + cache.get(key, loader); + } catch (UncheckedExecutionException | ExecutionException e) { + doneSignal.countDown(); + } + }); + thread.start(); + + boolean done = doneSignal.await(1, TimeUnit.SECONDS); + if (!done) { + StringBuilder builder = new StringBuilder(); + for (StackTraceElement trace : thread.getStackTrace()) { + builder.append("\tat ").append(trace).append('\n'); + } + fail(builder.toString()); + } + } + /** * Returns an iterable containing all combinations of maximumSize, expireAfterAccess/Write, * weakKeys and weak/softValues. diff --git a/guava-tests/test/com/google/common/cache/NullCacheTest.java b/guava-tests/test/com/google/common/cache/NullCacheTest.java index 89dc3fb14..6fa8f7b13 100644 --- a/guava-tests/test/com/google/common/cache/NullCacheTest.java +++ b/guava-tests/test/com/google/common/cache/NullCacheTest.java @@ -20,6 +20,7 @@ import static com.google.common.cache.TestingCacheLoaders.exceptionLoader; import static com.google.common.cache.TestingRemovalListeners.queuingRemovalListener; import static com.google.common.truth.Truth.assertThat; import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertThrows; import com.google.common.cache.CacheLoader.InvalidCacheLoadException; import com.google.common.cache.TestingRemovalListeners.QueuingRemovalListener; @@ -100,12 +101,7 @@ public class NullCacheTest extends TestCase { .removalListener(listener) .build(constantLoader(null)); - try { - cache.getUnchecked(new Object()); - fail(); - } catch (InvalidCacheLoadException e) { - /* expected */ - } + assertThrows(InvalidCacheLoadException.class, () -> cache.getUnchecked(new Object())); assertTrue(listener.isEmpty()); checkEmpty(cache); @@ -119,12 +115,9 @@ public class NullCacheTest extends TestCase { .removalListener(listener) .build(exceptionLoader(e)); - try { - map.getUnchecked(new Object()); - fail(); - } catch (UncheckedExecutionException uee) { - assertThat(uee).hasCauseThat().isSameInstanceAs(e); - } + UncheckedExecutionException uee = + assertThrows(UncheckedExecutionException.class, () -> map.getUnchecked(new Object())); + assertThat(uee).hasCauseThat().isSameInstanceAs(e); assertTrue(listener.isEmpty()); checkEmpty(map); } diff --git a/guava-tests/test/com/google/common/cache/PopulatedCachesTest.java b/guava-tests/test/com/google/common/cache/PopulatedCachesTest.java index b02b8ac61..5518190a5 100644 --- a/guava-tests/test/com/google/common/cache/PopulatedCachesTest.java +++ b/guava-tests/test/com/google/common/cache/PopulatedCachesTest.java @@ -20,6 +20,7 @@ import static com.google.common.cache.TestingCacheLoaders.identityLoader; import static com.google.common.truth.Truth.assertThat; import static java.util.concurrent.TimeUnit.DAYS; import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertThrows; import com.google.common.base.Function; import com.google.common.cache.CacheBuilderFactory.DurationSpec; @@ -279,11 +280,7 @@ public class PopulatedCachesTest extends TestCase { assertEquals(3, cache.getIfPresent(1)); checkValidState(cache); - try { - entry.setValue(null); - fail(); - } catch (NullPointerException expected) { - } + assertThrows(NullPointerException.class, () -> entry.setValue(null)); checkValidState(cache); } } |