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/30 06:44:27 UTC

(causeway) branch master updated: CAUSEWAY-3651: Some test stability improvements

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 536317edbf CAUSEWAY-3651: Some test stability improvements
536317edbf is described below

commit 536317edbf1bd99119b68a294687aca53f9b765f
Author: andi-huber <ah...@apache.org>
AuthorDate: Thu Nov 30 07:44:21 2023 +0100

    CAUSEWAY-3651: Some test stability improvements
---
 .../bootstrap/BSMenuBars_roundtrip_Test.java       | 12 ++---
 .../applib/util/schema/CommonDtoUtils_Test.java    | 11 ++---
 .../commons/internal/codec/_DocumentFactories.java | 18 +++++++-
 .../commons/internal/testing/_DocumentTester.java  | 51 ++++++++++++++++++++++
 .../services/grid/GridXmlRoundtripTest.java        | 10 ++---
 .../applib/dom/pojo/PojoTester_broken_Test.java    | 14 +++---
 6 files changed, 85 insertions(+), 31 deletions(-)

diff --git a/api/applib/src/test/java/org/apache/causeway/applib/layout/menubars/bootstrap/BSMenuBars_roundtrip_Test.java b/api/applib/src/test/java/org/apache/causeway/applib/layout/menubars/bootstrap/BSMenuBars_roundtrip_Test.java
index cfbd916484..a8f71f4769 100644
--- a/api/applib/src/test/java/org/apache/causeway/applib/layout/menubars/bootstrap/BSMenuBars_roundtrip_Test.java
+++ b/api/applib/src/test/java/org/apache/causeway/applib/layout/menubars/bootstrap/BSMenuBars_roundtrip_Test.java
@@ -22,13 +22,11 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 import org.apache.causeway.applib.layout.component.ServiceActionLayoutData;
 import org.apache.causeway.applib.services.jaxb.JaxbService;
+import org.apache.causeway.commons.internal.testing._DocumentTester;
 
 public class BSMenuBars_roundtrip_Test {
 
@@ -69,17 +67,15 @@ public class BSMenuBars_roundtrip_Test {
         menuBars.getPrimary().getMenus().add(menu);
 
         // when
-        String xml = jaxbService.toXml(menuBars);
+        String xml1 = jaxbService.toXml(menuBars);
 
         // when
         BSMenuBars menuBars2 =
-                jaxbService.fromXml(BSMenuBars.class, xml);
+                jaxbService.fromXml(BSMenuBars.class, xml1);
 
         // then
         String xml2 = jaxbService.toXml(menuBars2);
-
-        assertThat(xml, is(equalTo(xml2)));
-
+        _DocumentTester.assertXmlEqualsIgnoreOrder(xml1, xml2);
     }
 
 }
diff --git a/api/applib/src/test/java/org/apache/causeway/applib/util/schema/CommonDtoUtils_Test.java b/api/applib/src/test/java/org/apache/causeway/applib/util/schema/CommonDtoUtils_Test.java
index fb2f7a30c9..72f8ad3374 100644
--- a/api/applib/src/test/java/org/apache/causeway/applib/util/schema/CommonDtoUtils_Test.java
+++ b/api/applib/src/test/java/org/apache/causeway/applib/util/schema/CommonDtoUtils_Test.java
@@ -24,8 +24,8 @@ import org.junit.jupiter.api.Test;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertEquals;
 
+import org.apache.causeway.commons.internal.testing._DocumentTester;
 import org.apache.causeway.schema.cmd.v2.MapDto;
 import org.apache.causeway.schema.common.v2.TypedTupleDto;
 import org.apache.causeway.schema.common.v2.ValueType;
@@ -88,19 +88,16 @@ class CommonDtoUtils_Test {
 
         val json = CommonDtoUtils.getCompositeValueAsJson(compositeDto);
 
-        //XXX there is no guarantee, that the ordering of keys is exactly as follows - should do for now
         val expectedJson = "{\"elements\":["
                 + "{\"long\":123456,\"type\":\"long\",\"name\":\"epochMillis\"},"
                 + "{\"string\":\"sample\",\"type\":\"string\",\"name\":\"calendarName\"}],"
                 + "\"type\":\"org.apache.causeway.applib.util.schema.CommonDtoUtils_Test$CompositeSample\","
                 + "\"cardinality\":2}";
-
-        assertEquals(expectedJson, json);
+        _DocumentTester.assertJsonEqualsIgnoreOrder(expectedJson, json);
 
         val compositeAfterRoundtrip = CommonDtoUtils.getCompositeValueFromJson(json);
-
-        assertEquals(expectedJson, CommonDtoUtils.getCompositeValueAsJson(compositeAfterRoundtrip));
-
+        val jsonAfterRoundtrip = CommonDtoUtils.getCompositeValueAsJson(compositeAfterRoundtrip);
+        _DocumentTester.assertJsonEqualsIgnoreOrder(expectedJson, jsonAfterRoundtrip);
     }
 
 }
\ No newline at end of file
diff --git a/commons/src/main/java/org/apache/causeway/commons/internal/codec/_DocumentFactories.java b/commons/src/main/java/org/apache/causeway/commons/internal/codec/_DocumentFactories.java
index fa503d7f1f..710ef32d88 100644
--- a/commons/src/main/java/org/apache/causeway/commons/internal/codec/_DocumentFactories.java
+++ b/commons/src/main/java/org/apache/causeway/commons/internal/codec/_DocumentFactories.java
@@ -18,6 +18,8 @@
  */
 package org.apache.causeway.commons.internal.codec;
 
+import java.io.ByteArrayInputStream;
+
 import javax.xml.XMLConstants;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -28,7 +30,11 @@ import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerFactory;
 
 import org.jdom2.input.SAXBuilder;
+import org.springframework.lang.Nullable;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
 
+import lombok.SneakyThrows;
 import lombok.val;
 import lombok.experimental.UtilityClass;
 
@@ -90,7 +96,15 @@ public class _DocumentFactories {
         return builder;
     }
 
-
-
+    @Nullable
+    @SneakyThrows
+    public Document parseDocument(final @Nullable String xml) {
+        if(xml==null) return null;
+        var dbf = _DocumentFactories.documentBuilderFactory();
+        dbf.setNamespaceAware(true);
+        var documentBuilder = dbf.newDocumentBuilder();
+        var doc = documentBuilder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
+        return doc;
+    }
 
 }
diff --git a/commons/src/main/java/org/apache/causeway/commons/internal/testing/_DocumentTester.java b/commons/src/main/java/org/apache/causeway/commons/internal/testing/_DocumentTester.java
new file mode 100644
index 0000000000..614a6cdf58
--- /dev/null
+++ b/commons/src/main/java/org/apache/causeway/commons/internal/testing/_DocumentTester.java
@@ -0,0 +1,51 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.causeway.commons.internal.testing;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.apache.causeway.commons.internal.assertions._Assert;
+import org.apache.causeway.commons.internal.codec._DocumentFactories;
+
+import lombok.NonNull;
+import lombok.SneakyThrows;
+import lombok.experimental.UtilityClass;
+
+@UtilityClass
+public class _DocumentTester {
+
+    @SneakyThrows
+    public void assertXmlEqualsIgnoreOrder(final @NonNull String xml1, final @NonNull String xml2) {
+        var doc1 = _DocumentFactories.parseDocument(xml1);
+        var doc2 = _DocumentFactories.parseDocument(xml2);
+        doc1.normalizeDocument();
+        doc2.normalizeDocument();
+        _Assert.assertTrue(doc1.isEqualNode(doc2));
+    }
+
+    /**
+     * @see "https://www.baeldung.com/jackson-compare-two-json-objects"
+     */
+    @SneakyThrows
+    public void assertJsonEqualsIgnoreOrder(final @NonNull String json1, final @NonNull String json2) {
+        var mapper = new ObjectMapper();
+        _Assert.assertEquals(mapper.readTree(json1), mapper.readTree(json2));
+    }
+
+}
diff --git a/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/services/grid/GridXmlRoundtripTest.java b/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/services/grid/GridXmlRoundtripTest.java
index c3fee01c53..143fcfa3e0 100644
--- a/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/services/grid/GridXmlRoundtripTest.java
+++ b/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/services/grid/GridXmlRoundtripTest.java
@@ -24,10 +24,6 @@ import javax.xml.bind.Marshaller;
 
 import org.junit.jupiter.api.Test;
 
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
 import org.apache.causeway.applib.layout.component.ActionLayoutData;
 import org.apache.causeway.applib.layout.component.CollectionLayoutData;
 import org.apache.causeway.applib.layout.component.DomainObjectLayoutData;
@@ -43,6 +39,7 @@ import org.apache.causeway.applib.services.jaxb.CausewaySchemas;
 import org.apache.causeway.applib.services.jaxb.JaxbService;
 import org.apache.causeway.commons.internal.collections._Lists;
 import org.apache.causeway.commons.internal.collections._Maps;
+import org.apache.causeway.commons.internal.testing._DocumentTester;
 import org.apache.causeway.core.metamodel.MetaModelTestAbstract;
 
 class GridXmlRoundtripTest
@@ -135,8 +132,7 @@ extends MetaModelTestAbstract {
         BSGrid bsPageroundtripped = jaxbService.fromXml(BSGrid.class, xml);
         String xmlRoundtripped = jaxbService.toXml(bsPageroundtripped,
                 _Maps.unmodifiable(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocations));
-        assertThat(xml, is(equalTo(xmlRoundtripped)));
-
+        _DocumentTester.assertXmlEqualsIgnoreOrder(xml, xmlRoundtripped);
 
         println("==========");
 
@@ -151,7 +147,7 @@ extends MetaModelTestAbstract {
         }
     }
 
-    private void println(String string) {
+    private void println(final String string) {
         //for test debugging only
     }
 
diff --git a/testing/unittestsupport/applib/src/test/java/org/apache/causeway/testing/unittestsupport/applib/dom/pojo/PojoTester_broken_Test.java b/testing/unittestsupport/applib/src/test/java/org/apache/causeway/testing/unittestsupport/applib/dom/pojo/PojoTester_broken_Test.java
index 3698e549dc..5dc356af47 100644
--- a/testing/unittestsupport/applib/src/test/java/org/apache/causeway/testing/unittestsupport/applib/dom/pojo/PojoTester_broken_Test.java
+++ b/testing/unittestsupport/applib/src/test/java/org/apache/causeway/testing/unittestsupport/applib/dom/pojo/PojoTester_broken_Test.java
@@ -29,25 +29,25 @@ public class PojoTester_broken_Test {
         public String getFirstName() {
             return firstName + lastName; // this is the deliberate error
         }
-        public void setFirstName(String firstName) {
+        public void setFirstName(final String firstName) {
             this.firstName = firstName;
         }
-        
+
         private String lastName = "";
         public String getLastName() {
             return lastName;
         }
-        public void setLastName(String lastName) {
+        public void setLastName(final String lastName) {
             this.lastName = lastName;
         }
     }
-    
+
     @Test
     public void strict_when_interference_between_properties() {
         Assertions.assertThatThrownBy(() -> {
             PojoTester.create().exercise(new CustomerWithInterferingProperties());
         }).isInstanceOf(AssertionFailedError.class)
-                .hasMessageContaining("firstName");
+                .hasMessageMatching(".*(firstName|lastName).*");
     }
 
     public static class BrokenCustomer {
@@ -55,12 +55,12 @@ public class PojoTester_broken_Test {
         public String getSomeString() {
             return someString;
         }
-        public void setSomeString(String someString) {
+        public void setSomeString(final String someString) {
             // no-op
         }
     }
 
-    @org.junit.jupiter.api.Test
+    @Test
     public void exercise_broken() {
 
         // given