From 0ef0f31618d20ebeded74a3cd7161c4bf3070238 Mon Sep 17 00:00:00 2001 From: Ben Manes Date: Fri, 7 Apr 2023 09:05:43 -0700 Subject: [PATCH] refine assertion for broken equality log message (#900) --- .../caffeine/cache/BoundedLocalCacheTest.java | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/BoundedLocalCacheTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/BoundedLocalCacheTest.java index 0982f95df2..9328ac697d 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/BoundedLocalCacheTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/BoundedLocalCacheTest.java @@ -2359,10 +2359,8 @@ public void brokenEquality_eviction(BoundedLocalCache cache, assertThat(cache.estimatedSize()).isEqualTo(1); var event = Iterables.getOnlyElement(TestLoggerFactory.getLoggingEvents()); - assertThat(event.getFormattedMessage()).containsMatch( - "An invalid state was detected.*\\(key: \\d+, key type: MutableInt.*\\)"); - assertThat(event.getThrowable().orElseThrow()) - .isInstanceOf(IllegalStateException.class); + assertThat(event.getThrowable().orElseThrow()).isInstanceOf(IllegalStateException.class); + checkBrokenEqualityMessage(cache, key, event.getFormattedMessage()); assertThat(event.getLevel()).isEqualTo(ERROR); cache.data.clear(); @@ -2390,10 +2388,8 @@ public void brokenEquality_expiration( assertThat(cache.estimatedSize()).isEqualTo(1); var event = Iterables.getOnlyElement(TestLoggerFactory.getLoggingEvents()); - assertThat(event.getFormattedMessage()).containsMatch( - "An invalid state was detected.*\\(key: \\d+, key type: MutableInt.*\\)"); - assertThat(event.getThrowable().orElseThrow()) - .isInstanceOf(IllegalStateException.class); + assertThat(event.getThrowable().orElseThrow()).isInstanceOf(IllegalStateException.class); + checkBrokenEqualityMessage(cache, key, event.getFormattedMessage()); assertThat(event.getLevel()).isEqualTo(ERROR); cache.data.clear(); @@ -2414,10 +2410,8 @@ public void brokenEquality_clear(BoundedLocalCache cache, CacheCont assertThat(cache.estimatedSize()).isEqualTo(1); var event = Iterables.getOnlyElement(TestLoggerFactory.getLoggingEvents()); - assertThat(event.getFormattedMessage()).containsMatch( - "An invalid state was detected.*\\(key: \\d+, key type: MutableInt.*\\)"); - assertThat(event.getThrowable().orElseThrow()) - .isInstanceOf(IllegalStateException.class); + assertThat(event.getThrowable().orElseThrow()).isInstanceOf(IllegalStateException.class); + checkBrokenEqualityMessage(cache, key, event.getFormattedMessage()); assertThat(event.getLevel()).isEqualTo(ERROR); cache.data.clear(); @@ -2499,12 +2493,20 @@ private static void testForBrokenEquality(BoundedLocalCache cac key.decrement(); var e = assertThrows(IllegalStateException.class, () -> task.accept(key)); - assertThat(e).hasMessageThat().containsMatch( - "An invalid state was detected.*\\(key: \\d+, key type: MutableInt.*\\)"); + checkBrokenEqualityMessage(cache, key, e.getMessage()); cache.data.clear(); } + private static void checkBrokenEqualityMessage( + BoundedLocalCache cache, Object key, String msg) { + assertThat(msg).contains("An invalid state was detected"); + assertThat(msg).contains("cache type: " + cache.getClass().getSimpleName()); + assertThat(msg).contains("node type: " + cache.nodeFactory.getClass().getSimpleName()); + assertThat(msg).contains("key type: " + key.getClass().getSimpleName()); + assertThat(msg).contains("key: " + key); + } + /* --------------- Miscellaneous --------------- */ @Test