Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 76 additions & 75 deletions src/test/java/org/zeplinko/commons/lang/ext/core/TryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ void test_givenSuccess_whenRecoverIsCalled_thenSameSuccessIsReturned() {
@Test
void test_givenNullMapper_whenPredicateRecoverIsCalled_thenExceptionIsThrown() {
Try<String> failure = Try.failure(new RuntimeException("error"));
@SuppressWarnings("DataFlowIssue")
NullPointerException nullPointerException = assertThrows(
NullPointerException.class,
() -> failure.recover((Predicate<? super Exception>) IllegalArgumentException.class::isInstance, null)
Expand All @@ -362,7 +361,6 @@ void test_givenNullMapper_whenPredicateRecoverIsCalled_thenExceptionIsThrown() {
@Test
void test_givenNullPredicate_whenPredicateRecoverIsCalled_thenExceptionIsThrown() {
Try<String> failure = Try.failure(new RuntimeException("error"));
@SuppressWarnings("DataFlowIssue")
NullPointerException nullPointerException = assertThrows(
NullPointerException.class,
() -> failure.recover((Predicate<? super Exception>) null, e -> Try.success("ignored"))
Expand Down Expand Up @@ -402,7 +400,6 @@ void test_givenFailureWhichSatisfiesThePredicate_whenPredicateRecoverIsCalled_th
@Test
void test_givenNullExceptionType_whenRecoverWithExceptionTypeIsCalled_thenExceptionIsThrown() {
Try<Integer> failure = Try.failure(new RuntimeException("error"));
@SuppressWarnings("DataFlowIssue")
NullPointerException nullPointerException = assertThrows(
NullPointerException.class,
() -> failure.recover((Class<Exception>) null, e -> Try.success(0))
Expand Down Expand Up @@ -612,7 +609,6 @@ void test_givenFailureWhichSatisfiesPredicate_whenPredicateOnFailureIsCalled_the
@Test
void test_givenNullExceptionType_whenOnFailureWithExceptionTypeIsCalled_thenExceptionIsThrown() {
Try<Integer> failure = Try.failure(new RuntimeException("error"));
@SuppressWarnings("DataFlowIssue")
NullPointerException nullPointerException = assertThrows(
NullPointerException.class,
() -> failure.onFailure((Class<Exception>) null, e -> {
Expand Down Expand Up @@ -704,7 +700,6 @@ void test_givenChainedOnFailureWithCatchAll_whenFailureOccurs_thenAllMatchingCon
@Test
void test_givenNullExceptionTypesArray_whenOnFailureWithVarargsIsCalled_thenExceptionIsThrown() {
Try<Integer> failure = Try.failure(new RuntimeException("error"));
@SuppressWarnings({ "DataFlowIssue", "unchecked" })
NullPointerException nullPointerException = assertThrows(
NullPointerException.class,
() -> failure.onFailure(e -> {
Expand Down Expand Up @@ -838,7 +833,6 @@ void test_givenFailureAndNullExceptionTypesInVarargs_whenOnFailureWithVarargsIsC
Exception error = new IllegalArgumentException("error");
Try<String> failure = Try.failure(error);
StringBuilder consumed = new StringBuilder();
@SuppressWarnings("unchecked")
Try<String> returnedTry = failure.onFailure(
e -> consumed.append(e.getMessage()),
null,
Expand All @@ -855,7 +849,6 @@ void test_givenFailureAndAllNullExceptionTypesInVarargs_whenOnFailureWithVarargs
Exception error = new IllegalArgumentException("error");
Try<String> failure = Try.failure(error);
StringBuilder consumed = new StringBuilder();
@SuppressWarnings("unchecked")
Try<String> returnedTry = failure.onFailure(
e -> consumed.append(e.getMessage()),
null,
Expand Down Expand Up @@ -947,7 +940,6 @@ void test_givenFailure_whenToResultIsCalled_thenResultIsReturned() {
@Test
void test_givenNullFailureMapper_whenRecoverWithVarargsIsCalled_thenExceptionIsThrown() {
Try<Integer> failure = Try.failure(new RuntimeException("error"));
@SuppressWarnings("DataFlowIssue")
NullPointerException nullPointerException = assertThrows(
NullPointerException.class,
() -> failure.recover(null, RuntimeException.class, IllegalArgumentException.class)
Expand All @@ -958,7 +950,6 @@ void test_givenNullFailureMapper_whenRecoverWithVarargsIsCalled_thenExceptionIsT
@Test
void test_givenNullExceptionTypesArray_whenRecoverWithVarargsIsCalled_thenExceptionIsThrown() {
Try<Integer> failure = Try.failure(new RuntimeException("error"));
@SuppressWarnings({ "DataFlowIssue", "unchecked" })
NullPointerException nullPointerException = assertThrows(
NullPointerException.class,
() -> failure.recover(e -> Try.success(0), (Class<? extends Exception>[]) null)
Expand Down Expand Up @@ -1098,7 +1089,6 @@ void test_givenFailureWithMatchingException_whenRecoverWithVarargsReturnsFailure
void test_givenFailureAndNullExceptionTypesInVarargs_whenRecoverWithVarargsIsCalled_thenNullsAreIgnored() {
Exception error = new IllegalArgumentException("error");
Try<String> failure = Try.failure(error);
@SuppressWarnings("unchecked")
Try<String> result = failure.recover(
e -> Try.success("recovered"),
null,
Expand All @@ -1114,7 +1104,6 @@ void test_givenFailureAndNullExceptionTypesInVarargs_whenRecoverWithVarargsIsCal
void test_givenFailureAndAllNullExceptionTypesInVarargs_whenRecoverWithVarargsIsCalled_thenSameFailureIsReturned() {
Exception error = new IllegalArgumentException("error");
Try<String> failure = Try.failure(error);
@SuppressWarnings("unchecked")
Try<String> result = failure.recover(
e -> Try.success("recovered"),
null,
Expand Down Expand Up @@ -1221,16 +1210,19 @@ void test_whenRunIsCalledWithRunnableThatThrowsException_thenFailureTryIsReturne

@Test
void test_whenRunIsCalledWithRunnableThatThrowsInterruptedException_thenFailureTryIsReturned() {
InterruptedException expectedError = new InterruptedException("interrupted");

Try<Empty> 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<Empty> result = Try.run(() -> {
throw expectedError;
});

assertTrue(result.isFailure());
assertSame(expectedError, result.getError());
assertTrue(Thread.currentThread().isInterrupted());
} finally {
Thread.interrupted();
}
Comment thread
ShivamNagpal marked this conversation as resolved.
}

@Test
Expand All @@ -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);
}
Expand Down Expand Up @@ -1298,29 +1290,32 @@ void test_whenWithResourcesActionThrowsException_thenFailureTryIsReturnedAndReso

@Test
void test_whenWithResourcesActionThrowsInterruptedException_thenFailureTryIsReturned() {
TestResource resource = new TestResource();
InterruptedException expectedError = new InterruptedException("interrupted");

Try<String> 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<String> result = Try.withResources(
() -> resource,
r -> {
throw expectedError;
}
);

assertTrue(result.isFailure());
assertSame(expectedError, result.getError());
assertTrue(resource.isClosed());
assertTrue(Thread.currentThread().isInterrupted());
} finally {
Thread.interrupted();
}
Comment thread
ShivamNagpal marked this conversation as resolved.
}

@Test
void test_whenWithResourcesTwoIsCalledWithNullFirstSupplier_thenExceptionIsThrown() {
@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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -1370,7 +1365,7 @@ void test_whenWithResourcesTwoFirstSupplierThrows_thenFailureTryIsReturned() {
() -> {
throw expectedError;
},
() -> new TestResource(),
TestResource::new,
(r1, r2) -> "success"
);

Expand Down Expand Up @@ -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<String> 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<String> 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();
}
Comment thread
ShivamNagpal marked this conversation as resolved.
}

@Test
Expand All @@ -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);
}
Expand Down Expand Up @@ -1510,21 +1508,24 @@ void test_whenConsumeResourceActionThrows_thenFailureTryIsReturnedAndResourceIsC

@Test
void test_whenConsumeResourceActionThrowsInterruptedException_thenFailureTryIsReturned() {
TestResource resource = new TestResource();
InterruptedException expectedError = new InterruptedException("interrupted");

Try<Empty> 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<Empty> result = Try.consumeResource(
() -> resource,
r -> {
throw expectedError;
}
);

assertTrue(result.isFailure());
assertSame(expectedError, result.getError());
assertTrue(resource.isClosed());
assertTrue(Thread.currentThread().isInterrupted());
} finally {
Thread.interrupted();
}
Comment thread
ShivamNagpal marked this conversation as resolved.
}

private static class TestResource implements AutoCloseable {
Expand Down
Loading