gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] branch master updated: Fix parser: detect 2+ children cases.


From: gnunet
Subject: [libeufin] branch master updated: Fix parser: detect 2+ children cases.
Date: Mon, 20 Jul 2020 15:48:44 +0200

This is an automated email from the git hooks/post-receive script.

ms pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new 48660ac  Fix parser: detect 2+ children cases.
48660ac is described below

commit 48660ac2b778976be71713799c408df465fa7999
Author: MS <ms@taler.net>
AuthorDate: Mon Jul 20 15:47:49 2020 +0200

    Fix parser: detect 2+ children cases.
---
 nexus/src/test/kotlin/Iso20022Test.kt  | 19 +++++++++++++++++++
 util/src/main/kotlin/XmlCombinators.kt | 10 +++++-----
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/nexus/src/test/kotlin/Iso20022Test.kt 
b/nexus/src/test/kotlin/Iso20022Test.kt
index 2e266fb..7447ed1 100644
--- a/nexus/src/test/kotlin/Iso20022Test.kt
+++ b/nexus/src/test/kotlin/Iso20022Test.kt
@@ -3,7 +3,9 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
 import org.junit.Test
 import org.w3c.dom.Document
 import tech.libeufin.nexus.iso20022.*
+import tech.libeufin.util.DestructionError
 import tech.libeufin.util.XMLUtil
+import tech.libeufin.util.destructXml
 import java.math.BigDecimal
 import kotlin.test.assertEquals
 import kotlin.test.assertNotNull
@@ -19,6 +21,23 @@ fun loadXmlResource(name: String): Document {
 }
 
 class Iso20022Test {
+    @Test(expected = DestructionError::class)
+    fun testUniqueChild() {
+        val xml = """
+            <a>
+              <b/>
+              <b/>
+            </a>
+        """.trimIndent()
+        // when XML is invalid, DestructionError is thrown.
+        val doc = XMLUtil.parseStringIntoDom(xml)
+        destructXml(doc) {
+            requireRootElement("a") {
+                requireOnlyChild {  }
+            }
+        }
+
+    }
     @Test
     fun testTransactionsImport() {
         val camt53 = 
loadXmlResource("iso20022-samples/camt.053/de.camt.053.001.02.xml")
diff --git a/util/src/main/kotlin/XmlCombinators.kt 
b/util/src/main/kotlin/XmlCombinators.kt
index fe58475..f777ec4 100644
--- a/util/src/main/kotlin/XmlCombinators.kt
+++ b/util/src/main/kotlin/XmlCombinators.kt
@@ -129,11 +129,11 @@ private fun Element.getChildElements(ns: String, tag: 
String): List<Element> {
 
 class XmlElementDestructor internal constructor(val d: Document, val e: 
Element) {
     fun <T> requireOnlyChild(f: XmlElementDestructor.(e: Element) -> T): T {
-        val child =
-            e.getChildElements("*", "*").elementAtOrNull(0)
-                ?: throw DestructionError("expected singleton child tag")
-        val destr = XmlElementDestructor(d, child)
-        return f(destr, child)
+        val children = e.getChildElements("*", "*")
+        if (children.size != 1) throw DestructionError("expected singleton 
child tag (2+ found)")
+        if (children[0] == null) throw DestructionError("expected singleton 
child tag (none found)")
+        val destr = XmlElementDestructor(d, children[0])
+        return f(destr, children[0])
     }
 
     fun <T> mapEachChildNamed(s: String, f: XmlElementDestructor.(e: Element) 
-> T): List<T> {

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]