From eccd4e6e1b1da4b6ed0e9451e2ffff3f57a5be69 Mon Sep 17 00:00:00 2001 From: Urvy Agrawal Date: Thu, 21 Feb 2019 22:01:04 +0530 Subject: [PATCH] Bael2494 (#6387) * Added files for BAEL-2494 * Added review comments for BAEL 2494 * fixed build issue * formatting changes * Added annotation to fixed build issue --- .../com/baeldung/jackson/dtos/Address.java | 7 ---- .../com/baeldung/jackson/dtos/Person.java | 6 +-- .../xml/XMLSerializeDeserializeUnitTest.java | 38 +++++++++++-------- .../src/test/resources/PersonGenerated.xml | 18 --------- jackson/src/test/resources/person.xml | 19 ---------- 5 files changed, 23 insertions(+), 65 deletions(-) delete mode 100644 jackson/src/test/resources/PersonGenerated.xml delete mode 100644 jackson/src/test/resources/person.xml diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/Address.java b/jackson/src/test/java/com/baeldung/jackson/dtos/Address.java index 19e7d4c53c..985851f456 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/Address.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/Address.java @@ -1,16 +1,9 @@ package com.baeldung.jackson.dtos; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; - public class Address { - @JacksonXmlProperty(localName = "street_number") String streetNumber; - - @JacksonXmlProperty(localName = "street_name") String streetName; - - @JacksonXmlProperty(localName = "city") String city; public String getStreetNumber() { diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/Person.java b/jackson/src/test/java/com/baeldung/jackson/dtos/Person.java index 7891595cc6..13093cdcad 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/Person.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/Person.java @@ -3,16 +3,13 @@ package com.baeldung.jackson.dtos; import java.util.ArrayList; import java.util.List; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; @JacksonXmlRootElement(localName = "person") public final class Person { private String firstName; private String lastName; - @JacksonXmlElementWrapper(useWrapping = false) private List phoneNumbers = new ArrayList<>(); - @JacksonXmlElementWrapper(localName = "addresses") private List
address = new ArrayList<>(); public List
getAddress() { @@ -38,6 +35,7 @@ public final class Person { public void setLastName(String lastName) { this.lastName = lastName; } + public List getPhoneNumbers() { return phoneNumbers; } @@ -46,6 +44,4 @@ public final class Person { this.phoneNumbers = phoneNumbers; } - - } \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java index 039edd45bc..1d430e9758 100644 --- a/jackson/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java @@ -2,8 +2,10 @@ package com.baeldung.jackson.xml; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -56,17 +58,14 @@ public class XMLSerializeDeserializeUnitTest { @Test public void whenJavaGotFromXmlStrWithCapitalElem_thenCorrect() throws IOException { XmlMapper xmlMapper = new XmlMapper(); - SimpleBeanForCapitalizedFields value = xmlMapper. - readValue("12", - SimpleBeanForCapitalizedFields.class); + SimpleBeanForCapitalizedFields value = xmlMapper.readValue("12", SimpleBeanForCapitalizedFields.class); assertTrue(value.getX() == 1 && value.getY() == 2); } @Test public void whenJavaSerializedToXmlFileWithCapitalizedField_thenCorrect() throws IOException { XmlMapper xmlMapper = new XmlMapper(); - xmlMapper.writeValue(new File("target/simple_bean_capitalized.xml"), - new SimpleBeanForCapitalizedFields()); + xmlMapper.writeValue(new File("target/simple_bean_capitalized.xml"), new SimpleBeanForCapitalizedFields()); File file = new File("target/simple_bean_capitalized.xml"); assertNotNull(file); } @@ -74,7 +73,9 @@ public class XMLSerializeDeserializeUnitTest { @Test public void whenJavaDeserializedFromXmlFile_thenCorrect() throws IOException { XmlMapper xmlMapper = new XmlMapper(); - Person value = xmlMapper.readValue(new File("src/test/resources/person.xml"), Person.class); + + String xml = "RohanDaye99110347319911033478
1Name1City1
2Name2City2
"; + Person value = xmlMapper.readValue(xml, Person.class); assertTrue(value.getAddress() .get(0) @@ -90,33 +91,38 @@ public class XMLSerializeDeserializeUnitTest { public void whenJavaSerializedToXmlFile_thenSuccess() throws IOException { XmlMapper xmlMapper = new XmlMapper(); + String expectedXml = "RohanDaye99110347319911033478
1Name1City1
2Name2City2
"; + Person person = new Person(); person.setFirstName("Rohan"); person.setLastName("Daye"); List ph = new ArrayList<>(); - ph.add("9911778981"); - ph.add("9991111111"); + ph.add("9911034731"); + ph.add("9911033478"); person.setPhoneNumbers(ph); List
addresses = new ArrayList<>(); - + Address address1 = new Address(); address1.setStreetNumber("1"); - address1.setStreetName("streetname1"); - address1.setCity("city1"); - + address1.setStreetName("Name1"); + address1.setCity("City1"); + Address address2 = new Address(); address2.setStreetNumber("2"); - address2.setStreetName("streetname2"); - address2.setCity("city2"); - + address2.setStreetName("Name2"); + address2.setCity("City2"); + addresses.add(address1); addresses.add(address2); + person.setAddress(addresses); - xmlMapper.writeValue(new File("src/test/resources/PersonGenerated.xml"), person); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + xmlMapper.writeValue(byteArrayOutputStream, person); + assertEquals(expectedXml, byteArrayOutputStream.toString()); } private static String inputStreamToString(InputStream is) throws IOException { diff --git a/jackson/src/test/resources/PersonGenerated.xml b/jackson/src/test/resources/PersonGenerated.xml deleted file mode 100644 index 6ebadd971a..0000000000 --- a/jackson/src/test/resources/PersonGenerated.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - 9911778981 - 9991111111 - -
- 1 - streetname1 - city1 -
-
- 2 - streetname2 - city2 -
-
-
\ No newline at end of file diff --git a/jackson/src/test/resources/person.xml b/jackson/src/test/resources/person.xml deleted file mode 100644 index 39a6e859c4..0000000000 --- a/jackson/src/test/resources/person.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - Rohan - Daye - 9911034731 - 9911033478 - -
- 1 - Name1 - City1 -
-
- 2 - Name2 - City2 -
-
-
\ No newline at end of file