You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2021/06/09 13:37:59 UTC
[isis] 04/04: ISIS-2727: decouples common from ThrowingRunnable
This is an automated email from the ASF dual-hosted git repository.
danhaywood pushed a commit to branch ISIS-2727
in repository https://gitbox.apache.org/repos/asf/isis.git
commit 5315fb12cd5b4eca84023a43d87318c520137778
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Wed Jun 9 14:37:36 2021 +0100
ISIS-2727: decouples common from ThrowingRunnable
... preparing to move ThrowingRunnable into applib
... moves Result.ofVoid(ThrowingRunnable) to ThrowingRunnable.resultOf(ThrowingRunnable)
---
.../org/apache/isis/commons/functional/Result.java | 4 -
.../isis/commons/functional/ThrowingRunnable.java | 7 +-
.../apache/isis/commons/functions/ResultTest.java | 95 +++++++++++-----------
...xceptionTranslationTest_usingTransactional.java | 63 +++++++-------
...xceptionTranslationTest_usingTransactional.java | 63 +++++++-------
5 files changed, 115 insertions(+), 117 deletions(-)
diff --git a/commons/src/main/java/org/apache/isis/commons/functional/Result.java b/commons/src/main/java/org/apache/isis/commons/functional/Result.java
index 0197c68..5ad15d6 100644
--- a/commons/src/main/java/org/apache/isis/commons/functional/Result.java
+++ b/commons/src/main/java/org/apache/isis/commons/functional/Result.java
@@ -61,10 +61,6 @@ public final class Result<L> {
}
}
- public static Result<Void> ofVoid(final @NonNull ThrowingRunnable runnable) {
- return of(ThrowingRunnable.toCallable(runnable));
- }
-
public static <L> Result<L> success(final @Nullable L value) {
return of(value, null, true);
}
diff --git a/commons/src/main/java/org/apache/isis/commons/functional/ThrowingRunnable.java b/commons/src/main/java/org/apache/isis/commons/functional/ThrowingRunnable.java
index ffe8df7..988aa21 100644
--- a/commons/src/main/java/org/apache/isis/commons/functional/ThrowingRunnable.java
+++ b/commons/src/main/java/org/apache/isis/commons/functional/ThrowingRunnable.java
@@ -25,6 +25,7 @@ import lombok.NonNull;
@FunctionalInterface
public interface ThrowingRunnable {
+
// -- INTERFACE
void run() throws Exception;
@@ -32,11 +33,13 @@ public interface ThrowingRunnable {
// -- UTILITY
static Callable<Void> toCallable(final @NonNull ThrowingRunnable runnable) {
- final Callable<Void> callable = ()->{
+ return ()->{
runnable.run();
return null;
};
- return callable;
}
+ static Result<Void> resultOf(final @NonNull ThrowingRunnable runnable) {
+ return Result.of(toCallable(runnable));
+ }
}
diff --git a/commons/src/test/java/org/apache/isis/commons/functions/ResultTest.java b/commons/src/test/java/org/apache/isis/commons/functions/ResultTest.java
index 6821e06..94efa4c 100644
--- a/commons/src/test/java/org/apache/isis/commons/functions/ResultTest.java
+++ b/commons/src/test/java/org/apache/isis/commons/functions/ResultTest.java
@@ -30,85 +30,86 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.apache.isis.commons.functional.Result;
+import org.apache.isis.commons.functional.ThrowingRunnable;
import lombok.val;
class ResultTest {
// -- TEST DUMMIES
-
+
String hello_happy() {
return "hello";
}
-
+
String hello_nullable() {
return null;
}
-
+
String hello_throwing_uncatched() {
throw new RuntimeException("hello failed");
}
-
+
String hello_throwing_catched() throws Exception {
throw new Exception("hello failed");
}
void void_happy() {
-
+
}
-
+
void void_throwing_uncatched() {
throw new RuntimeException("void failed");
}
-
+
void void_throwing_catched() throws Exception {
throw new Exception("void failed");
}
-
-
+
+
// -- TESTS
-
+
@Test
void hello_happy_case() {
-
+
val result = Result.<String>of(this::hello_happy);
assertTrue(result.isSuccess());
assertFalse(result.isFailure());
assertEquals("hello", result.presentElse(""));
assertEquals("hello", result.getValue().orElse(null));
assertEquals("hello", result.presentElseFail());
-
+
// non-evaluated code-path
result.presentElseGet(()->fail("unexpected code reach"));
-
+
// default value is not allowed to be null
assertThrows(NullPointerException.class, ()->result.presentElse(null));
-
+
val mandatory = result.mapSuccessWithEmptyValueToNoSuchElement();
assertTrue(mandatory.isSuccess());
assertEquals("hello", mandatory.presentElse(""));
-
+
}
-
+
@Test
void hello_nullable_case() {
-
+
val result = Result.<String>of(this::hello_nullable);
assertTrue(result.isSuccess());
assertFalse(result.isFailure());
assertEquals("no value", result.getValue().orElse("no value"));
assertThrows(NoSuchElementException.class, ()->result.presentElseFail());
assertEquals(Optional.empty(), result.optionalElseFail());
-
+
val mandatory = result.mapSuccessWithEmptyValueToNoSuchElement();
assertTrue(mandatory.isFailure());
assertThrows(NoSuchElementException.class, ()->mandatory.optionalElseFail());
-
+
}
-
+
@Test
void hello_throwing_uncatched_case() {
-
+
val result = Result.<String>of(this::hello_throwing_uncatched);
assertFalse(result.isSuccess());
assertTrue(result.isFailure());
@@ -117,15 +118,15 @@ class ResultTest {
assertEquals("it failed", result.presentElseGet(()->"it failed"));
assertThrows(RuntimeException.class, ()->result.presentElseFail());
assertEquals(Optional.empty(), result.getValue());
-
+
val mandatory = result.mapSuccessWithEmptyValueToNoSuchElement();
assertTrue(mandatory.isFailure());
-
+
}
-
+
@Test
void hello_throwing_catched_case() {
-
+
val result = Result.<String>of(this::hello_throwing_catched);
assertFalse(result.isSuccess());
assertTrue(result.isFailure());
@@ -134,63 +135,63 @@ class ResultTest {
assertEquals("it failed", result.presentElseGet(()->"it failed"));
assertThrows(Exception.class, ()->result.presentElseFail());
assertEquals(Optional.empty(), result.getValue());
-
+
val mandatory = result.mapSuccessWithEmptyValueToNoSuchElement();
assertTrue(mandatory.isFailure());
-
+
}
-
+
@Test
void void_happy_case() {
-
- val result = Result.ofVoid(this::void_happy);
+
+ val result = ThrowingRunnable.resultOf(this::void_happy);
assertTrue(result.isSuccess());
assertFalse(result.isFailure());
assertEquals(null, result.getValue().orElse(null));
-
+
assertThrows(NoSuchElementException.class, ()->result.presentElseFail());
-
+
// default value is not allowed to be null
assertThrows(NullPointerException.class, ()->result.presentElse(null));
-
+
}
-
-
+
+
@Test
void void_throwing_uncatched_case() {
-
- val result = Result.ofVoid(this::void_throwing_uncatched);
+
+ val result = ThrowingRunnable.resultOf(this::void_throwing_uncatched);
assertFalse(result.isSuccess());
assertTrue(result.isFailure());
assertEquals("void failed", result.getFailure().get().getMessage());
-
+
// default value is not allowed to be null
assertThrows(NullPointerException.class, ()->result.presentElse(null));
assertThrows(NoSuchElementException.class, ()->result.presentElseGet(()->null));
assertThrows(Exception.class, ()->result.presentElseFail());
assertEquals(Optional.empty(), result.getValue());
-
+
val mandatory = result.mapSuccessWithEmptyValueToNoSuchElement();
assertTrue(mandatory.isFailure());
}
-
+
@Test
void void_throwing_catched_case() {
-
- val result = Result.ofVoid(this::void_throwing_catched);
+
+ val result = ThrowingRunnable.resultOf(this::void_throwing_catched);
assertFalse(result.isSuccess());
assertTrue(result.isFailure());
assertEquals("void failed", result.getFailure().get().getMessage());
-
+
// default value is not allowed to be null
assertThrows(NullPointerException.class, ()->result.presentElse(null));
assertThrows(NoSuchElementException.class, ()->result.presentElseGet(()->null));
assertThrows(Exception.class, ()->result.presentElseFail());
assertEquals(Optional.empty(), result.getValue());
-
+
val mandatory = result.mapSuccessWithEmptyValueToNoSuchElement();
assertTrue(mandatory.isFailure());
}
-
-
-}
\ No newline at end of file
+
+
+}
diff --git a/regressiontests/stable-persistence-jdo/src/test/java/org/apache/isis/testdomain/persistence/jdo/JdoExceptionTranslationTest_usingTransactional.java b/regressiontests/stable-persistence-jdo/src/test/java/org/apache/isis/testdomain/persistence/jdo/JdoExceptionTranslationTest_usingTransactional.java
index 927e3ec..d3a499e 100644
--- a/regressiontests/stable-persistence-jdo/src/test/java/org/apache/isis/testdomain/persistence/jdo/JdoExceptionTranslationTest_usingTransactional.java
+++ b/regressiontests/stable-persistence-jdo/src/test/java/org/apache/isis/testdomain/persistence/jdo/JdoExceptionTranslationTest_usingTransactional.java
@@ -42,7 +42,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.apache.isis.applib.services.repository.RepositoryService;
-import org.apache.isis.commons.functional.Result;
import org.apache.isis.commons.functional.ThrowingRunnable;
import org.apache.isis.core.config.presets.IsisPresets;
import org.apache.isis.core.interaction.session.InteractionFactory;
@@ -54,12 +53,12 @@ import org.apache.isis.testdomain.jdo.entities.JdoInventory;
import lombok.val;
@SpringBootTest(
- classes = {
+ classes = {
Configuration_usingJdo.class,
JdoInventoryDao.class,
})
@TestPropertySources({
- @TestPropertySource(IsisPresets.UseLog4j2Test)
+ @TestPropertySource(IsisPresets.UseLog4j2Test)
})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class JdoExceptionTranslationTest_usingTransactional
@@ -77,77 +76,77 @@ class JdoExceptionTranslationTest_usingTransactional
// launch H2Console for troubleshooting ...
// Util_H2Console.main(null);
}
-
- @Test @Order(1)
+
+ @Test @Order(1)
@Transactional @Rollback(false)
void booksUniqueByIsbn_setupPhase() {
interactionFactory.runAnonymous(()->{
-
+
_TestFixtures.setUp3Books(repositoryService);
-
+
});
}
- @Test @Order(2)
+ @Test @Order(2)
void booksUniqueByIsbn_whenViolated_shouldThrowTranslatedException() {
// when adding a book for which one with same ISBN already exists in the database,
// we expect to see a Spring recognized DataAccessException been thrown
-
- final ThrowingRunnable uniqueConstraintViolator =
+
+ final ThrowingRunnable uniqueConstraintViolator =
()->inventoryDao.get().addBook_havingIsbnA_usingRepositoryService();
-
+
assertThrows(DataIntegrityViolationException.class, ()->{
-
+
interactionFactory.runAnonymous(()->{
-
- Result.ofVoid(uniqueConstraintViolator)
+
+ ThrowingRunnable.resultOf(uniqueConstraintViolator)
.ifSuccess(__->fail("expected to fail, but did not"))
//.mapFailure(ex->_JdoExceptionTranslator.translate(ex, txManager))
.ifFailure(ex->assertTrue(ex instanceof DataIntegrityViolationException))
.optionalElseFail();
-
+
});
-
+
});
-
- }
-
- @Test @Order(3)
+
+ }
+
+ @Test @Order(3)
@Transactional @Rollback(false)
void booksUniqueByIsbn_verifyPhase() {
// expected post condition: ONE inventory with 3 books
-
+
interactionFactory.runAnonymous(()->{
-
+
val inventories = repositoryService.allInstances(JdoInventory.class);
assertEquals(1, inventories.size());
-
+
val inventory = inventories.get(0);
assertNotNull(inventory);
-
+
assertNotNull(inventory);
assertNotNull(inventory.getProducts());
assertEquals(3, inventory.getProducts().size());
_TestFixtures.assertInventoryHasBooks(inventory.getProducts(), 1, 2, 3);
-
+
});
-
+
}
-
- @Test @Order(4)
+
+ @Test @Order(4)
@Transactional @Rollback(false)
void booksUniqueByIsbn_cleanupPhase() {
interactionFactory.runAnonymous(()->{
_TestFixtures.cleanUp(repositoryService);
-
+
});
-
+
}
-
-
+
+
}
diff --git a/regressiontests/stable-persistence-jpa/src/test/java/org/apache/isis/testdomain/persistence/jpa/JpaExceptionTranslationTest_usingTransactional.java b/regressiontests/stable-persistence-jpa/src/test/java/org/apache/isis/testdomain/persistence/jpa/JpaExceptionTranslationTest_usingTransactional.java
index fef6619..ec4d9d0 100644
--- a/regressiontests/stable-persistence-jpa/src/test/java/org/apache/isis/testdomain/persistence/jpa/JpaExceptionTranslationTest_usingTransactional.java
+++ b/regressiontests/stable-persistence-jpa/src/test/java/org/apache/isis/testdomain/persistence/jpa/JpaExceptionTranslationTest_usingTransactional.java
@@ -43,7 +43,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.apache.isis.applib.services.repository.RepositoryService;
-import org.apache.isis.commons.functional.Result;
import org.apache.isis.commons.functional.ThrowingRunnable;
import org.apache.isis.core.config.presets.IsisPresets;
import org.apache.isis.core.interaction.session.InteractionFactory;
@@ -54,12 +53,12 @@ import org.apache.isis.testdomain.jpa.entities.JpaInventory;
import lombok.val;
@SpringBootTest(
- classes = {
+ classes = {
Configuration_usingJpa.class,
JpaInventoryDao.class
})
@TestPropertySources({
- @TestPropertySource(IsisPresets.UseLog4j2Test)
+ @TestPropertySource(IsisPresets.UseLog4j2Test)
})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class JpaExceptionTranslationTest_usingTransactional
@@ -77,66 +76,66 @@ class JpaExceptionTranslationTest_usingTransactional
// launch H2Console for troubleshooting ...
// Util_H2Console.main(null);
}
-
- @Test @Order(1)
+
+ @Test @Order(1)
@Transactional @Rollback(false)
void booksUniqueByIsbn_setupPhase() {
interactionFactory.runAnonymous(()->{
-
+
_TestFixtures.setUp3Books(repositoryService);
-
+
});
}
-
+
@Test @Order(2)
void booksUniqueByIsbn_whenViolated_shouldThrowTranslatedException() {
// when adding a book for which one with same ISBN already exists in the database,
- // we expect to see a Spring recognized DataAccessException been thrown
-
- final ThrowingRunnable uniqueConstraintViolator =
+ // we expect to see a Spring recognized DataAccessException been thrown
+
+ final ThrowingRunnable uniqueConstraintViolator =
()->inventoryDao.get().addBook_havingIsbnA_usingRepositoryService();
-
+
assertThrows(DataIntegrityViolationException.class, ()->{
-
+
interactionFactory.runAnonymous(()->{
-
- Result.ofVoid(uniqueConstraintViolator)
+
+ ThrowingRunnable.resultOf(uniqueConstraintViolator)
.ifSuccess(__->fail("expected to fail, but did not"))
- // .mapFailure(ex->_JpaExceptionTranslator.translate(ex, txManager))
+ // .mapFailure(ex->_JpaExceptionTranslator.translate(ex, txManager))
.ifFailure(ex->assertTrue(ex instanceof DataIntegrityViolationException))
.optionalElseFail();
-
+
});
-
+
});
-
- }
-
+
+ }
+
@Test @Order(3)
@Transactional @Rollback(false)
void booksUniqueByIsbn_verifyPhase() {
// expected post condition: ONE inventory with 3 books
-
+
interactionFactory.runAnonymous(()->{
-
+
val inventories = repositoryService.allInstances(JpaInventory.class);
assertEquals(1, inventories.size());
-
+
val inventory = inventories.get(0);
assertNotNull(inventory);
-
+
assertNotNull(inventory);
assertNotNull(inventory.getProducts());
assertEquals(3, inventory.getProducts().size());
_TestFixtures.assertInventoryHasBooks(inventory.getProducts(), 1, 2, 3);
-
+
});
-
+
}
-
+
@Test @Order(4)
@Transactional @Rollback(false)
void booksUniqueByIsbn_cleanupPhase() {
@@ -144,10 +143,10 @@ class JpaExceptionTranslationTest_usingTransactional
interactionFactory.runAnonymous(()->{
_TestFixtures.cleanUp(repositoryService);
-
+
});
-
+
}
-
-
+
+
}