From bad790a040be022e79b451748136531745531e97 Mon Sep 17 00:00:00 2001 From: "Cook, Jeremy" Date: Wed, 21 Feb 2024 16:13:05 -0500 Subject: [PATCH 1/2] Added tests to show discrepancies when handling XML output by Jackson. --- approvaltests-tests/pom.xml | 5 + .../writers/ApprovalXmlWriterTest.java | 105 ++++++++++++------ 2 files changed, 79 insertions(+), 31 deletions(-) diff --git a/approvaltests-tests/pom.xml b/approvaltests-tests/pom.xml index ce8ef62af..05aae119a 100644 --- a/approvaltests-tests/pom.xml +++ b/approvaltests-tests/pom.xml @@ -104,6 +104,11 @@ jackson-databind 2.16.1 + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + 2.16.1 + org.apache.camel camel-xstream diff --git a/approvaltests-tests/src/test/java/org/approvaltests/writers/ApprovalXmlWriterTest.java b/approvaltests-tests/src/test/java/org/approvaltests/writers/ApprovalXmlWriterTest.java index 16265f26e..2937b8606 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/writers/ApprovalXmlWriterTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/writers/ApprovalXmlWriterTest.java @@ -1,39 +1,82 @@ package org.approvaltests.writers; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; import org.approvaltests.Approvals; import org.approvaltests.core.Options; import org.approvaltests.scrubbers.RegExScrubber; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class ApprovalXmlWriterTest -{ - @Test - public void plainXml() - { - Approvals.verifyXml("hi"); - } - @Test - public void xmlWithAttributes() - { - Approvals.verifyXml("hi"); - } - @Test - public void xmlWithDeepAttributes() - { - Approvals - .verifyXml("hi"); - } - @Test - public void xmlWithDeepAttributesWithScrubber() - { - Approvals.verifyXml( - "hi", - new Options(new RegExScrubber("hi", "hello"))); - } - @Test - public void invalidXml() - { - Approvals.verifyXml("hi"); - System.err.println("Note: The previous xml error () is expected "); - } +import static com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.Feature.WRITE_XML_DECLARATION; + +public class ApprovalXmlWriterTest { + @Test + public void plainXml() { + Approvals.verifyXml("hi"); + } + + @Test + public void xmlWithAttributes() { + Approvals.verifyXml("hi"); + } + + @Test + public void xmlWithDeepAttributes() { + Approvals + .verifyXml("hi"); + } + + @Test + public void xmlWithDeepAttributesWithScrubber() { + Approvals.verifyXml( + "hi", + new Options(new RegExScrubber("hi", "hello"))); + } + + @Test + public void invalidXml() { + Approvals.verifyXml("hi"); + System.err.println("Note: The previous xml error () is expected "); + } + + @Test + void xmlOutputByJacksonIsHandledCorrectly() throws JsonProcessingException { + ObjectWriter objectWriter = new XmlMapper() + .configure(WRITE_XML_DECLARATION, true) + .writer(); + + String jacksonXml = objectWriter.writeValueAsString(new JacksonTestPOJO()); + String approvalsWriterXml = ApprovalXmlWriter.prettyPrint(jacksonXml, 2); + + Assertions.assertEquals(jacksonXml, approvalsWriterXml); + } + + @Test + void xmlOutputByJacksonIsHandledCorrectlyWhenPrettyPrintingIsEnabled() throws JsonProcessingException { + ObjectWriter objectWriter = new XmlMapper() + .configure(WRITE_XML_DECLARATION, true) + .writerWithDefaultPrettyPrinter(); + + String jacksonXml = objectWriter.writeValueAsString(new JacksonTestPOJO()); + String approvalsWriterXml = ApprovalXmlWriter.prettyPrint(jacksonXml, 2); + + Assertions.assertEquals(jacksonXml, approvalsWriterXml); + } + + private static class JacksonTestPOJO { + + public String getSomeText() { + return "Some text"; + } + + public String getSomeTextWithAmpersand() { + return "Some more text & some more"; + } + + public String getEmoji() { + return "😸"; + } + } } From 88c52ff56597bc2ecaae3e8a46cf5a68751e480e Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 21 Feb 2024 21:19:10 +0000 Subject: [PATCH 2/2] a reformat code --- .../org/approvaltests/Parse1InputTest.java | 3 +- .../org/approvaltests/Parse2InputTest.java | 10 +- .../writers/ApprovalXmlWriterTest.java | 125 +++++++++--------- 3 files changed, 67 insertions(+), 71 deletions(-) diff --git a/approvaltests-tests/src/test/java/org/approvaltests/Parse1InputTest.java b/approvaltests-tests/src/test/java/org/approvaltests/Parse1InputTest.java index 27c167f3e..4fcac5f49 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/Parse1InputTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/Parse1InputTest.java @@ -22,6 +22,7 @@ void testWithTypesTransformersAndBoth() """; ParseInput.from(expected).withTypes(Integer.class).verifyAll(Integer::toBinaryString); ParseInput.from(expected).transformTo(Integer::parseInt).verifyAll(Integer::toBinaryString); - ParseInput.from(expected).withTypes(String.class).transformTo(Integer::parseInt).verifyAll(Integer::toBinaryString); + ParseInput.from(expected).withTypes(String.class).transformTo(Integer::parseInt) + .verifyAll(Integer::toBinaryString); } } diff --git a/approvaltests-tests/src/test/java/org/approvaltests/Parse2InputTest.java b/approvaltests-tests/src/test/java/org/approvaltests/Parse2InputTest.java index cd8a92926..a586e10e3 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/Parse2InputTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/Parse2InputTest.java @@ -8,12 +8,12 @@ public class Parse2InputTest void testWithTypesTransformersAndBoth() { var expected = """ - 1,2.2 -> 2.2 - 1,3.3 -> 3.3 - """; + 1,2.2 -> 2.2 + 1,3.3 -> 3.3 + """; ParseInput.from(expected).withTypes(Integer.class, Double.class).verifyAll(t -> t.getFirst() * t.getSecond()); // TODO: continue here -// ParseInput.from(expected).transformTo(Integer::parseInt).verifyAll(Integer::toBinaryString); -// ParseInput.from(expected).withTypes(String.class).transformTo(Integer::parseInt).verifyAll(Integer::toBinaryString); + // ParseInput.from(expected).transformTo(Integer::parseInt).verifyAll(Integer::toBinaryString); + // ParseInput.from(expected).withTypes(String.class).transformTo(Integer::parseInt).verifyAll(Integer::toBinaryString); } } diff --git a/approvaltests-tests/src/test/java/org/approvaltests/writers/ApprovalXmlWriterTest.java b/approvaltests-tests/src/test/java/org/approvaltests/writers/ApprovalXmlWriterTest.java index 2937b8606..1d9044bd2 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/writers/ApprovalXmlWriterTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/writers/ApprovalXmlWriterTest.java @@ -11,72 +11,67 @@ import static com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.Feature.WRITE_XML_DECLARATION; -public class ApprovalXmlWriterTest { - @Test - public void plainXml() { - Approvals.verifyXml("hi"); +public class ApprovalXmlWriterTest +{ + @Test + public void plainXml() + { + Approvals.verifyXml("hi"); + } + @Test + public void xmlWithAttributes() + { + Approvals.verifyXml("hi"); + } + @Test + public void xmlWithDeepAttributes() + { + Approvals + .verifyXml("hi"); + } + @Test + public void xmlWithDeepAttributesWithScrubber() + { + Approvals.verifyXml( + "hi", + new Options(new RegExScrubber("hi", "hello"))); + } + @Test + public void invalidXml() + { + Approvals.verifyXml("hi"); + System.err.println("Note: The previous xml error () is expected "); + } + @Test + void xmlOutputByJacksonIsHandledCorrectly() throws JsonProcessingException + { + ObjectWriter objectWriter = new XmlMapper().configure(WRITE_XML_DECLARATION, true).writer(); + String jacksonXml = objectWriter.writeValueAsString(new JacksonTestPOJO()); + String approvalsWriterXml = ApprovalXmlWriter.prettyPrint(jacksonXml, 2); + Assertions.assertEquals(jacksonXml, approvalsWriterXml); + } + @Test + void xmlOutputByJacksonIsHandledCorrectlyWhenPrettyPrintingIsEnabled() throws JsonProcessingException + { + ObjectWriter objectWriter = new XmlMapper().configure(WRITE_XML_DECLARATION, true) + .writerWithDefaultPrettyPrinter(); + String jacksonXml = objectWriter.writeValueAsString(new JacksonTestPOJO()); + String approvalsWriterXml = ApprovalXmlWriter.prettyPrint(jacksonXml, 2); + Assertions.assertEquals(jacksonXml, approvalsWriterXml); + } + private static class JacksonTestPOJO + { + public String getSomeText() + { + return "Some text"; } - - @Test - public void xmlWithAttributes() { - Approvals.verifyXml("hi"); - } - - @Test - public void xmlWithDeepAttributes() { - Approvals - .verifyXml("hi"); - } - - @Test - public void xmlWithDeepAttributesWithScrubber() { - Approvals.verifyXml( - "hi", - new Options(new RegExScrubber("hi", "hello"))); + public String getSomeTextWithAmpersand() + { + return "Some more text & some more"; } - - @Test - public void invalidXml() { - Approvals.verifyXml("hi"); - System.err.println("Note: The previous xml error () is expected "); - } - - @Test - void xmlOutputByJacksonIsHandledCorrectly() throws JsonProcessingException { - ObjectWriter objectWriter = new XmlMapper() - .configure(WRITE_XML_DECLARATION, true) - .writer(); - - String jacksonXml = objectWriter.writeValueAsString(new JacksonTestPOJO()); - String approvalsWriterXml = ApprovalXmlWriter.prettyPrint(jacksonXml, 2); - - Assertions.assertEquals(jacksonXml, approvalsWriterXml); - } - - @Test - void xmlOutputByJacksonIsHandledCorrectlyWhenPrettyPrintingIsEnabled() throws JsonProcessingException { - ObjectWriter objectWriter = new XmlMapper() - .configure(WRITE_XML_DECLARATION, true) - .writerWithDefaultPrettyPrinter(); - - String jacksonXml = objectWriter.writeValueAsString(new JacksonTestPOJO()); - String approvalsWriterXml = ApprovalXmlWriter.prettyPrint(jacksonXml, 2); - - Assertions.assertEquals(jacksonXml, approvalsWriterXml); - } - - private static class JacksonTestPOJO { - - public String getSomeText() { - return "Some text"; - } - - public String getSomeTextWithAmpersand() { - return "Some more text & some more"; - } - - public String getEmoji() { - return "😸"; - } + public String getEmoji() + { + return "😸"; } + } }