You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/11/19 16:51:53 UTC

(causeway) branch master updated: CAUSEWAY-3647: refactor prev. fix commit: make the fix more explicit

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/causeway.git


The following commit(s) were added to refs/heads/master by this push:
     new 618182b613 CAUSEWAY-3647: refactor prev. fix commit: make the fix more explicit
618182b613 is described below

commit 618182b613411b59a609782987a0c04f4aeea59e
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sun Nov 19 17:51:48 2023 +0100

    CAUSEWAY-3647: refactor prev. fix commit: make the fix more explicit
    
    - remove the non obvious side-effect yielding
    MetaModelContext.setOrReplace(mmc)
---
 .../applib/CausewayInteractionHandler.java         | 30 ++++++++++++++++++++++
 .../testing/integtestsupport/applib/_Helper.java   | 11 +-------
 2 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/testing/integtestsupport/applib/src/main/java/org/apache/causeway/testing/integtestsupport/applib/CausewayInteractionHandler.java b/testing/integtestsupport/applib/src/main/java/org/apache/causeway/testing/integtestsupport/applib/CausewayInteractionHandler.java
index 39181fe736..1113ab430a 100644
--- a/testing/integtestsupport/applib/src/main/java/org/apache/causeway/testing/integtestsupport/applib/CausewayInteractionHandler.java
+++ b/testing/integtestsupport/applib/src/main/java/org/apache/causeway/testing/integtestsupport/applib/CausewayInteractionHandler.java
@@ -18,11 +18,16 @@
  */
 package org.apache.causeway.testing.integtestsupport.applib;
 
+import java.util.Optional;
+
 import org.junit.jupiter.api.extension.AfterEachCallback;
 import org.junit.jupiter.api.extension.BeforeEachCallback;
 import org.junit.jupiter.api.extension.ExtensionContext;
+import org.springframework.context.ApplicationContext;
 
 import org.apache.causeway.applib.services.iactnlayer.InteractionService;
+import org.apache.causeway.commons.internal.assertions._Assert;
+import org.apache.causeway.core.metamodel.context.MetaModelContext;
 
 /**
  * @since 2.0 {@index}
@@ -31,6 +36,17 @@ public class CausewayInteractionHandler implements BeforeEachCallback, AfterEach
 
     @Override
     public void beforeEach(final ExtensionContext extensionContext) throws Exception {
+
+        //[CAUSEWAY-3647] set MMC singleton reference explicitly on each test run
+        _Helper
+            .getSpringContext(extensionContext)
+            .ifPresent(springContext->{
+                var mmc = springContext.getBean(MetaModelContext.class);
+                _Assert.assertNotNull(mmc, ()->
+                        "MetaModelContext not found on Spring's test context.");
+                MetaModelContext.setOrReplace(mmc);
+            });
+
         _Helper
             .getInteractionFactory(extensionContext)
             .ifPresent(interactionService->
@@ -49,4 +65,18 @@ public class CausewayInteractionHandler implements BeforeEachCallback, AfterEach
     }
 
 
+    static Optional<ApplicationContext> getSpringContext(final ExtensionContext extensionContext) {
+        return extensionContext.getTestInstance()
+            .filter(CausewayIntegrationTestAbstract.class::isInstance)
+            .map(CausewayIntegrationTestAbstract.class::cast)
+            .map(causewayIntegrationTestAbstract -> causewayIntegrationTestAbstract.springContext)
+            .map(springContext->{
+                var mmc = springContext.getBean(MetaModelContext.class);
+                _Assert.assertNotNull(mmc, ()->
+                        "MetaModelContext not found on Spring's test context.");
+                MetaModelContext.setOrReplace(mmc);
+                return springContext;
+            });
+    }
+
 }
diff --git a/testing/integtestsupport/applib/src/main/java/org/apache/causeway/testing/integtestsupport/applib/_Helper.java b/testing/integtestsupport/applib/src/main/java/org/apache/causeway/testing/integtestsupport/applib/_Helper.java
index 3e94180365..8a4f36feea 100644
--- a/testing/integtestsupport/applib/src/main/java/org/apache/causeway/testing/integtestsupport/applib/_Helper.java
+++ b/testing/integtestsupport/applib/src/main/java/org/apache/causeway/testing/integtestsupport/applib/_Helper.java
@@ -26,9 +26,7 @@ import org.springframework.context.ApplicationContext;
 import org.apache.causeway.applib.services.exceprecog.ExceptionRecognizerService;
 import org.apache.causeway.applib.services.iactnlayer.InteractionContext;
 import org.apache.causeway.applib.services.iactnlayer.InteractionService;
-import org.apache.causeway.commons.internal.assertions._Assert;
 import org.apache.causeway.commons.internal.reflection._Annotations;
-import org.apache.causeway.core.metamodel.context.MetaModelContext;
 import org.apache.causeway.testing.integtestsupport.applib.annotation.InteractAs;
 import org.apache.causeway.testing.integtestsupport.applib.annotation.InteractAsUtils;
 
@@ -38,14 +36,7 @@ class _Helper {
         return extensionContext.getTestInstance()
             .filter(CausewayIntegrationTestAbstract.class::isInstance)
             .map(CausewayIntegrationTestAbstract.class::cast)
-            .map(causewayIntegrationTestAbstract -> causewayIntegrationTestAbstract.springContext)
-            .map(springContext->{
-                var mmc = springContext.getBean(MetaModelContext.class);
-                _Assert.assertNotNull(mmc, ()->
-                        "MetaModelContext not found on Spring's test context.");
-                MetaModelContext.setOrReplace(mmc);
-                return springContext;
-            });
+            .map(causewayIntegrationTestAbstract -> causewayIntegrationTestAbstract.springContext);
     }
 
     static <T> Optional<T> lookup(final Class<T> requiredType, final ExtensionContext extensionContext) {