From b15d8786fbea10bb374a03222cf5d6f09b9cafe2 Mon Sep 17 00:00:00 2001 From: Shivam Nagpal Date: Sat, 28 Mar 2026 22:27:13 +0530 Subject: [PATCH] fix: #85 | Resolve the warnings in the TryTest class --- .../commons/lang/ext/core/TryTest.java | 151 +++++++++--------- 1 file changed, 76 insertions(+), 75 deletions(-) diff --git a/src/test/java/org/zeplinko/commons/lang/ext/core/TryTest.java b/src/test/java/org/zeplinko/commons/lang/ext/core/TryTest.java index db291e6..da03d2d 100644 --- a/src/test/java/org/zeplinko/commons/lang/ext/core/TryTest.java +++ b/src/test/java/org/zeplinko/commons/lang/ext/core/TryTest.java @@ -351,7 +351,6 @@ void test_givenSuccess_whenRecoverIsCalled_thenSameSuccessIsReturned() { @Test void test_givenNullMapper_whenPredicateRecoverIsCalled_thenExceptionIsThrown() { Try failure = Try.failure(new RuntimeException("error")); - @SuppressWarnings("DataFlowIssue") NullPointerException nullPointerException = assertThrows( NullPointerException.class, () -> failure.recover((Predicate) IllegalArgumentException.class::isInstance, null) @@ -362,7 +361,6 @@ void test_givenNullMapper_whenPredicateRecoverIsCalled_thenExceptionIsThrown() { @Test void test_givenNullPredicate_whenPredicateRecoverIsCalled_thenExceptionIsThrown() { Try failure = Try.failure(new RuntimeException("error")); - @SuppressWarnings("DataFlowIssue") NullPointerException nullPointerException = assertThrows( NullPointerException.class, () -> failure.recover((Predicate) null, e -> Try.success("ignored")) @@ -402,7 +400,6 @@ void test_givenFailureWhichSatisfiesThePredicate_whenPredicateRecoverIsCalled_th @Test void test_givenNullExceptionType_whenRecoverWithExceptionTypeIsCalled_thenExceptionIsThrown() { Try failure = Try.failure(new RuntimeException("error")); - @SuppressWarnings("DataFlowIssue") NullPointerException nullPointerException = assertThrows( NullPointerException.class, () -> failure.recover((Class) null, e -> Try.success(0)) @@ -612,7 +609,6 @@ void test_givenFailureWhichSatisfiesPredicate_whenPredicateOnFailureIsCalled_the @Test void test_givenNullExceptionType_whenOnFailureWithExceptionTypeIsCalled_thenExceptionIsThrown() { Try failure = Try.failure(new RuntimeException("error")); - @SuppressWarnings("DataFlowIssue") NullPointerException nullPointerException = assertThrows( NullPointerException.class, () -> failure.onFailure((Class) null, e -> { @@ -704,7 +700,6 @@ void test_givenChainedOnFailureWithCatchAll_whenFailureOccurs_thenAllMatchingCon @Test void test_givenNullExceptionTypesArray_whenOnFailureWithVarargsIsCalled_thenExceptionIsThrown() { Try failure = Try.failure(new RuntimeException("error")); - @SuppressWarnings({ "DataFlowIssue", "unchecked" }) NullPointerException nullPointerException = assertThrows( NullPointerException.class, () -> failure.onFailure(e -> { @@ -838,7 +833,6 @@ void test_givenFailureAndNullExceptionTypesInVarargs_whenOnFailureWithVarargsIsC Exception error = new IllegalArgumentException("error"); Try failure = Try.failure(error); StringBuilder consumed = new StringBuilder(); - @SuppressWarnings("unchecked") Try returnedTry = failure.onFailure( e -> consumed.append(e.getMessage()), null, @@ -855,7 +849,6 @@ void test_givenFailureAndAllNullExceptionTypesInVarargs_whenOnFailureWithVarargs Exception error = new IllegalArgumentException("error"); Try failure = Try.failure(error); StringBuilder consumed = new StringBuilder(); - @SuppressWarnings("unchecked") Try returnedTry = failure.onFailure( e -> consumed.append(e.getMessage()), null, @@ -947,7 +940,6 @@ void test_givenFailure_whenToResultIsCalled_thenResultIsReturned() { @Test void test_givenNullFailureMapper_whenRecoverWithVarargsIsCalled_thenExceptionIsThrown() { Try failure = Try.failure(new RuntimeException("error")); - @SuppressWarnings("DataFlowIssue") NullPointerException nullPointerException = assertThrows( NullPointerException.class, () -> failure.recover(null, RuntimeException.class, IllegalArgumentException.class) @@ -958,7 +950,6 @@ void test_givenNullFailureMapper_whenRecoverWithVarargsIsCalled_thenExceptionIsT @Test void test_givenNullExceptionTypesArray_whenRecoverWithVarargsIsCalled_thenExceptionIsThrown() { Try failure = Try.failure(new RuntimeException("error")); - @SuppressWarnings({ "DataFlowIssue", "unchecked" }) NullPointerException nullPointerException = assertThrows( NullPointerException.class, () -> failure.recover(e -> Try.success(0), (Class[]) null) @@ -1098,7 +1089,6 @@ void test_givenFailureWithMatchingException_whenRecoverWithVarargsReturnsFailure void test_givenFailureAndNullExceptionTypesInVarargs_whenRecoverWithVarargsIsCalled_thenNullsAreIgnored() { Exception error = new IllegalArgumentException("error"); Try failure = Try.failure(error); - @SuppressWarnings("unchecked") Try result = failure.recover( e -> Try.success("recovered"), null, @@ -1114,7 +1104,6 @@ void test_givenFailureAndNullExceptionTypesInVarargs_whenRecoverWithVarargsIsCal void test_givenFailureAndAllNullExceptionTypesInVarargs_whenRecoverWithVarargsIsCalled_thenSameFailureIsReturned() { Exception error = new IllegalArgumentException("error"); Try failure = Try.failure(error); - @SuppressWarnings("unchecked") Try result = failure.recover( e -> Try.success("recovered"), null, @@ -1221,16 +1210,19 @@ void test_whenRunIsCalledWithRunnableThatThrowsException_thenFailureTryIsReturne @Test void test_whenRunIsCalledWithRunnableThatThrowsInterruptedException_thenFailureTryIsReturned() { - InterruptedException expectedError = new InterruptedException("interrupted"); - - Try result = Try.run(() -> { - throw expectedError; - }); - - assertTrue(result.isFailure()); - assertSame(expectedError, result.getError()); - assertTrue(Thread.currentThread().isInterrupted()); - Thread.interrupted(); + try { + InterruptedException expectedError = new InterruptedException("interrupted"); + + Try result = Try.run(() -> { + throw expectedError; + }); + + assertTrue(result.isFailure()); + assertSame(expectedError, result.getError()); + assertTrue(Thread.currentThread().isInterrupted()); + } finally { + Thread.interrupted(); + } } @Test @@ -1248,7 +1240,7 @@ void test_whenWithResourcesIsCalledWithNullAction_thenExceptionIsThrown() { @SuppressWarnings("DataFlowIssue") NullPointerException exception = assertThrows( NullPointerException.class, - () -> Try.withResources(() -> new TestResource(), null) + () -> Try.withResources(TestResource::new, null) ); assertNotNull(exception); } @@ -1298,21 +1290,24 @@ void test_whenWithResourcesActionThrowsException_thenFailureTryIsReturnedAndReso @Test void test_whenWithResourcesActionThrowsInterruptedException_thenFailureTryIsReturned() { - TestResource resource = new TestResource(); - InterruptedException expectedError = new InterruptedException("interrupted"); - - Try result = Try.withResources( - () -> resource, - r -> { - throw expectedError; - } - ); - - assertTrue(result.isFailure()); - assertSame(expectedError, result.getError()); - assertTrue(resource.isClosed()); - assertTrue(Thread.currentThread().isInterrupted()); - Thread.interrupted(); + try { + TestResource resource = new TestResource(); + InterruptedException expectedError = new InterruptedException("interrupted"); + + Try result = Try.withResources( + () -> resource, + r -> { + throw expectedError; + } + ); + + assertTrue(result.isFailure()); + assertSame(expectedError, result.getError()); + assertTrue(resource.isClosed()); + assertTrue(Thread.currentThread().isInterrupted()); + } finally { + Thread.interrupted(); + } } @Test @@ -1320,7 +1315,7 @@ void test_whenWithResourcesTwoIsCalledWithNullFirstSupplier_thenExceptionIsThrow @SuppressWarnings("DataFlowIssue") NullPointerException exception = assertThrows( NullPointerException.class, - () -> Try.withResources(null, () -> new TestResource(), (r1, r2) -> "result") + () -> Try.withResources(null, TestResource::new, (r1, r2) -> "result") ); assertNotNull(exception); } @@ -1330,7 +1325,7 @@ void test_whenWithResourcesTwoIsCalledWithNullSecondSupplier_thenExceptionIsThro @SuppressWarnings("DataFlowIssue") NullPointerException exception = assertThrows( NullPointerException.class, - () -> Try.withResources(() -> new TestResource(), null, (r1, r2) -> "result") + () -> Try.withResources(TestResource::new, null, (r1, r2) -> "result") ); assertNotNull(exception); } @@ -1340,7 +1335,7 @@ void test_whenWithResourcesTwoIsCalledWithNullAction_thenExceptionIsThrown() { @SuppressWarnings("DataFlowIssue") NullPointerException exception = assertThrows( NullPointerException.class, - () -> Try.withResources(() -> new TestResource(), () -> new TestResource(), null) + () -> Try.withResources(TestResource::new, TestResource::new, null) ); assertNotNull(exception); } @@ -1370,7 +1365,7 @@ void test_whenWithResourcesTwoFirstSupplierThrows_thenFailureTryIsReturned() { () -> { throw expectedError; }, - () -> new TestResource(), + TestResource::new, (r1, r2) -> "success" ); @@ -1418,24 +1413,27 @@ void test_whenWithResourcesTwoActionThrows_thenFailureTryIsReturnedAndBothResour @Test void test_whenWithResourcesTwoActionThrowsInterruptedException_thenFailureTryIsReturned() { - TestResource resource1 = new TestResource(); - TestResource resource2 = new TestResource(); - InterruptedException expectedError = new InterruptedException("interrupted"); - - Try result = Try.withResources( - () -> resource1, - () -> resource2, - (r1, r2) -> { - throw expectedError; - } - ); - - assertTrue(result.isFailure()); - assertSame(expectedError, result.getError()); - assertTrue(resource1.isClosed()); - assertTrue(resource2.isClosed()); - assertTrue(Thread.currentThread().isInterrupted()); - Thread.interrupted(); + try { + TestResource resource1 = new TestResource(); + TestResource resource2 = new TestResource(); + InterruptedException expectedError = new InterruptedException("interrupted"); + + Try result = Try.withResources( + () -> resource1, + () -> resource2, + (r1, r2) -> { + throw expectedError; + } + ); + + assertTrue(result.isFailure()); + assertSame(expectedError, result.getError()); + assertTrue(resource1.isClosed()); + assertTrue(resource2.isClosed()); + assertTrue(Thread.currentThread().isInterrupted()); + } finally { + Thread.interrupted(); + } } @Test @@ -1454,7 +1452,7 @@ void test_whenConsumeResourceIsCalledWithNullAction_thenExceptionIsThrown() { @SuppressWarnings("DataFlowIssue") NullPointerException exception = assertThrows( NullPointerException.class, - () -> Try.consumeResource(() -> new TestResource(), null) + () -> Try.consumeResource(TestResource::new, null) ); assertNotNull(exception); } @@ -1510,21 +1508,24 @@ void test_whenConsumeResourceActionThrows_thenFailureTryIsReturnedAndResourceIsC @Test void test_whenConsumeResourceActionThrowsInterruptedException_thenFailureTryIsReturned() { - TestResource resource = new TestResource(); - InterruptedException expectedError = new InterruptedException("interrupted"); - - Try result = Try.consumeResource( - () -> resource, - r -> { - throw expectedError; - } - ); - - assertTrue(result.isFailure()); - assertSame(expectedError, result.getError()); - assertTrue(resource.isClosed()); - assertTrue(Thread.currentThread().isInterrupted()); - Thread.interrupted(); + try { + TestResource resource = new TestResource(); + InterruptedException expectedError = new InterruptedException("interrupted"); + + Try result = Try.consumeResource( + () -> resource, + r -> { + throw expectedError; + } + ); + + assertTrue(result.isFailure()); + assertSame(expectedError, result.getError()); + assertTrue(resource.isClosed()); + assertTrue(Thread.currentThread().isInterrupted()); + } finally { + Thread.interrupted(); + } } private static class TestResource implements AutoCloseable {