From 8d0823488f4bc7b46eca9ea50cd80110c21a879e Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Fri, 23 Jun 2023 09:22:48 +0200 Subject: [PATCH] Adds prefab values for various Exceptions --- CHANGELOG.md | 4 ++- .../prefabvalues/JavaApiPrefabValues.java | 20 +++++++++++ .../extended_contract/JavaApiClassesTest.java | 33 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a59e21f4..9f9ae96d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Adds prefab values for `Throwable`, `Exception` and `RuntimeException`. ([Issue 831](https://github.com/jqno/equalsverifier/issues/831)) + ## [3.14.2] - 2023-05-31 -- Adds prefab values for `DoubleAdder`, `DoubleAccumulator`, `LongAdder` and `LongAccumulator`. +- Adds prefab values for `DoubleAdder`, `DoubleAccumulator`, `LongAdder` and `LongAccumulator`. ([Issue 817](https://github.com/jqno/equalsverifier/issues/817)) ## [3.14.1] - 2023-03-16 diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/prefabvalues/JavaApiPrefabValues.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/prefabvalues/JavaApiPrefabValues.java index 64db89b28..ecab7beea 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/prefabvalues/JavaApiPrefabValues.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/prefabvalues/JavaApiPrefabValues.java @@ -108,6 +108,7 @@ private void addJavaClasses() { addSets(); addQueues(); addNioBuffers(); + addExceptions(); addReflectionClasses(); addAtomicClasses(); addAncientJavaApiClasses(); @@ -513,6 +514,25 @@ private void addNioBuffers() { ); } + private void addExceptions() { + Throwable redThrowable = new Throwable(); + Throwable blueThrowable = new Throwable(); + addValues(Throwable.class, redThrowable, blueThrowable, redThrowable); + + Exception redException = new Exception(); + Exception blueException = new Exception(); + addValues(Exception.class, redException, blueException, redException); + + RuntimeException redRuntimeException = new RuntimeException(); + RuntimeException blueRuntimeException = new RuntimeException(); + addValues( + RuntimeException.class, + redRuntimeException, + blueRuntimeException, + redRuntimeException + ); + } + @SuppressWarnings("unused") private static class JavaApiReflectionClassesContainer { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/JavaApiClassesTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/JavaApiClassesTest.java index da892419a..faa586d34 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/JavaApiClassesTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/JavaApiClassesTest.java @@ -157,6 +157,11 @@ public void succeed_whenClassContainsACommonJavaApiClass() { EqualsVerifier.forClass(CommonClassesContainer.class).verify(); } + @Test + public void succeed_whenClassContainsExceptions() { + EqualsVerifier.forClass(ExceptionsContainer.class).verify(); + } + @Test public void succeed_whenClassContainsReflectionApiClass() { EqualsVerifier @@ -703,6 +708,34 @@ public int hashCode() { } } + @SuppressWarnings("unused") // because of the use of defaultEquals and defaultHashCode + static final class ExceptionsContainer { + + private final Throwable throwable; + private final Exception exception; + private final RuntimeException runtimeException; + + public ExceptionsContainer( + Throwable throwable, + Exception exception, + RuntimeException runtimeException + ) { + this.throwable = throwable; + this.exception = exception; + this.runtimeException = runtimeException; + } + + @Override + public boolean equals(Object obj) { + return defaultEquals(this, obj); + } + + @Override + public int hashCode() { + return defaultHashCode(this); + } + } + @SuppressWarnings("unused") // because of the use of defaultEquals and defaultHashCode static final class ReflectionClassesContainer {