You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2022/12/22 10:56:24 UTC

[isis] branch master updated: ISIS-3316: JaxbUtils: adds JUnit test to verify type-safe xml unmarshalling

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/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new a494a9cc35 ISIS-3316: JaxbUtils: adds JUnit test to verify type-safe xml unmarshalling
a494a9cc35 is described below

commit a494a9cc35b2f2abeb16f55458ec30cb0ef953fb
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Dec 22 11:56:19 2022 +0100

    ISIS-3316: JaxbUtils: adds JUnit test to verify type-safe xml
    unmarshalling
---
 .../causeway/applib/services/jaxb/JaxbService.java |  2 +-
 commons/src/main/java/module-info.java             |  1 +
 .../apache/causeway/commons/io/JaxbUtilsTest.java  | 81 ++++++++++++++++++++++
 3 files changed, 83 insertions(+), 1 deletion(-)

diff --git a/api/applib/src/main/java/org/apache/causeway/applib/services/jaxb/JaxbService.java b/api/applib/src/main/java/org/apache/causeway/applib/services/jaxb/JaxbService.java
index d3b9996caf..8cb8fcb94d 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/services/jaxb/JaxbService.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/services/jaxb/JaxbService.java
@@ -101,7 +101,7 @@ public interface JaxbService {
             CausewaySchemas causewaySchemas);
 
 
-    // no injection point resolving
+    /** 'Simple' because no injection point resolving or advanced {@link DomainObjectList} handling. */
     class Simple implements JaxbService {
 
         @Override
diff --git a/commons/src/main/java/module-info.java b/commons/src/main/java/module-info.java
index c14b7bd28e..64d6499f39 100644
--- a/commons/src/main/java/module-info.java
+++ b/commons/src/main/java/module-info.java
@@ -78,5 +78,6 @@ module org.apache.causeway.commons {
 
     // JAXB JUnit test
     opens org.apache.causeway.commons.internal.resources to java.xml.bind;
+    opens org.apache.causeway.commons.io to java.xml.bind;
 
 }
\ No newline at end of file
diff --git a/commons/src/test/java/org/apache/causeway/commons/io/JaxbUtilsTest.java b/commons/src/test/java/org/apache/causeway/commons/io/JaxbUtilsTest.java
new file mode 100644
index 0000000000..6cdb3f6361
--- /dev/null
+++ b/commons/src/test/java/org/apache/causeway/commons/io/JaxbUtilsTest.java
@@ -0,0 +1,81 @@
+/*
+ *  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.io;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.val;
+
+class JaxbUtilsTest {
+
+    @XmlRootElement(name = "type-a")
+    @XmlType
+    @XmlAccessorType(XmlAccessType.FIELD)
+    @EqualsAndHashCode
+    public static class A {
+
+        @XmlElement(required = false)
+        @Getter @Setter private B nested;
+    }
+
+    @XmlRootElement(name = "type-b")
+    @XmlType
+    @XmlAccessorType(XmlAccessType.FIELD)
+    @EqualsAndHashCode
+    public static class B {
+
+        @XmlElement(required = false)
+        @Getter @Setter private String string;
+    }
+
+    /** Works for arbitrary {@link XmlRootElement#name()} combinations,
+     * except you cannot use the same {@code name="root"} say on both {@link A} and {@link B}. */
+    @Test
+    void typesafeUnmarshallingFromAmbiguousContext() {
+
+        // given
+        val b = new B();
+        b.setString("b-string");
+        val a = new A();
+        a.setNested(b);
+
+        // when
+        val aXml = JaxbUtils.toStringUtf8(a);
+        val bXml = JaxbUtils.toStringUtf8(b);
+
+        val aRecovered = JaxbUtils.tryRead(A.class, aXml).ifFailureFail().ifAbsentFail().getValue().get();
+        val bRecovered = JaxbUtils.tryRead(B.class, bXml).ifFailureFail().ifAbsentFail().getValue().get();
+
+        assertEquals(a, aRecovered);
+        assertEquals(b, bRecovered);
+
+    }
+
+}