Add overloaded doOperationSafely(:ExceptionThrowingOperation<?>, :Supplier<?>) method to the ObjectUtils class.

Edit Javadoc.
This commit is contained in:
John Blum
2021-01-13 21:13:06 -08:00
parent 7191e658ab
commit f997c3e590
2 changed files with 93 additions and 36 deletions

View File

@@ -25,6 +25,7 @@ import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newR
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.function.Supplier;
import org.junit.Test;
@@ -162,8 +163,17 @@ public class ObjectUtilsUnitTests {
"default value")).isEqualTo("default value");
}
@Test
public void doOperationSafelyReturnsSuppliedValue() {
Supplier<String> suppliedValue = () -> "supplied value";
assertThat(ObjectUtils.<String>doOperationSafely(() -> { throw newRuntimeException("test"); }, suppliedValue))
.isEqualTo("supplied value");
}
@Test(expected = IllegalStateException.class)
public void doOperationSafelyThrowsIllegalStateException() {
public void doOperationSafelyWithNullDefaultValueThrowsIllegalStateException() {
try {
ObjectUtils.doOperationSafely(() -> { throw newRuntimeException("test"); }, (Object) null);
@@ -179,6 +189,25 @@ public class ObjectUtilsUnitTests {
}
}
@Test(expected = IllegalStateException.class)
public void doOperationSafelyWithNullSuppliedValueThrowsIllegalStateException() {
Supplier<String> suppliedValue = () -> null;
try {
ObjectUtils.<String>doOperationSafely(() -> { throw newRuntimeException("test"); }, suppliedValue);
}
catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Failed to execute operation");
assertThat(expected).hasCauseInstanceOf(RuntimeException.class);
assertThat(expected.getCause()).hasMessage("test");
assertThat(expected.getCause()).hasNoCause();
throw expected;
}
}
@Test
public void findMethodUsingExactArguments() {