From 42936ee82f4ec38c5b9cca7786966ad68b5f73a3 Mon Sep 17 00:00:00 2001 From: Bharath G R Date: Tue, 10 Oct 2023 20:34:15 +0530 Subject: [PATCH 1/2] added a fix for bug which trims whitespace from xml elements when used with print or karate.log --- .../main/java/com/intuit/karate/XmlUtils.java | 7 ++++-- .../java/com/intuit/karate/XmlUtilsTest.java | 17 +++++++++++++ .../com/intuit/karate/core/xml/xml.feature | 24 ++++++++++++++++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/karate-core/src/main/java/com/intuit/karate/XmlUtils.java b/karate-core/src/main/java/com/intuit/karate/XmlUtils.java index 1a1da60c1..f8db74975 100755 --- a/karate-core/src/main/java/com/intuit/karate/XmlUtils.java +++ b/karate-core/src/main/java/com/intuit/karate/XmlUtils.java @@ -72,10 +72,13 @@ public static String toString(Node node) { } public static String toString(Node node, boolean pretty) { + Node nodeToSerialize = node; + // In case of pretty string, we clone the node so that we don't modify the original node while trimming whitespaces if (pretty) { - trimWhiteSpace(node); + nodeToSerialize = node.cloneNode(true); + trimWhiteSpace(nodeToSerialize); } - DOMSource domSource = new DOMSource(node); + DOMSource domSource = new DOMSource(nodeToSerialize); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); diff --git a/karate-core/src/test/java/com/intuit/karate/XmlUtilsTest.java b/karate-core/src/test/java/com/intuit/karate/XmlUtilsTest.java index ae6b4cc6d..0717b83d0 100755 --- a/karate-core/src/test/java/com/intuit/karate/XmlUtilsTest.java +++ b/karate-core/src/test/java/com/intuit/karate/XmlUtilsTest.java @@ -226,6 +226,23 @@ void testPrettyPrint() { assertEquals(temp, expected); } + @Test + void testPrettyPrintWithWhiteSpace() { + String xml = "bazmoo "; + Document doc = XmlUtils.toXmlDoc(xml); + String temp = XmlUtils.toString(doc, true); + String expected + = "\n" + + " baz\n" + + " \n" + + " moo\n" + + " \n" + + "\n"; + + expected = expected.replace("\n", System.lineSeparator()); + assertEquals(temp, expected); + } + @Test void testCreatingNewDocumentFromSomeChildNode() { String xml = "baz"; diff --git a/karate-core/src/test/java/com/intuit/karate/core/xml/xml.feature b/karate-core/src/test/java/com/intuit/karate/core/xml/xml.feature index 7e7d85f5c..10e98858d 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/xml/xml.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/xml/xml.feature @@ -504,4 +504,26 @@ Scenario: xml matching involving karate-schema substitutions x """ -* match test2 == schema \ No newline at end of file +* match test2 == schema + +Scenario: Xml whitespace trim after print bug fix +* def setForecastRequestXml = + """ + + T + + """ +* match setForecastRequestXml //myelement == 'T ' +* print setForecastRequestXml +* match setForecastRequestXml //myelement == 'T ' + +Scenario: Xml whitespace trim after karate.log bug fix +* def setForecastRequestXml = + """ + + T + + """ +* match setForecastRequestXml //myelement == ' T ' +* karate.log(setForecastRequestXml) +* match setForecastRequestXml //myelement == ' T ' \ No newline at end of file From 5f4993ff5465b6890a4965a00b3f578ae025bd88 Mon Sep 17 00:00:00 2001 From: Bharath G R Date: Tue, 10 Oct 2023 20:48:17 +0530 Subject: [PATCH 2/2] added one more check to see if the whitespace trim had not happened --- .../src/test/java/com/intuit/karate/core/xml/xml.feature | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/karate-core/src/test/java/com/intuit/karate/core/xml/xml.feature b/karate-core/src/test/java/com/intuit/karate/core/xml/xml.feature index 10e98858d..64ea3cd81 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/xml/xml.feature +++ b/karate-core/src/test/java/com/intuit/karate/core/xml/xml.feature @@ -516,6 +516,7 @@ Scenario: Xml whitespace trim after print bug fix * match setForecastRequestXml //myelement == 'T ' * print setForecastRequestXml * match setForecastRequestXml //myelement == 'T ' +* match setForecastRequestXml //myelement != 'T' Scenario: Xml whitespace trim after karate.log bug fix * def setForecastRequestXml = @@ -526,4 +527,5 @@ Scenario: Xml whitespace trim after karate.log bug fix """ * match setForecastRequestXml //myelement == ' T ' * karate.log(setForecastRequestXml) -* match setForecastRequestXml //myelement == ' T ' \ No newline at end of file +* match setForecastRequestXml //myelement == ' T ' +* match setForecastRequestXml //myelement != 'T' \ No newline at end of file