From fe289003faf09e7468882a5a11aed33d694c5cac Mon Sep 17 00:00:00 2001 From: Urvy Agrawal Date: Mon, 18 Feb 2019 23:00:58 +0530 Subject: [PATCH] Added files for BAEL-2494 (#6370) --- .../com/baeldung/jackson/dtos/Address.java | 40 ++++++++++++ .../com/baeldung/jackson/dtos/Person.java | 51 +++++++++++++++ .../xml/XMLSerializeDeserializeUnitTest.java | 64 +++++++++++++++++-- .../src/test/resources/PersonGenerated.xml | 18 ++++++ jackson/src/test/resources/person.xml | 19 ++++++ 5 files changed, 186 insertions(+), 6 deletions(-) create mode 100644 jackson/src/test/java/com/baeldung/jackson/dtos/Address.java create mode 100644 jackson/src/test/java/com/baeldung/jackson/dtos/Person.java create mode 100644 jackson/src/test/resources/PersonGenerated.xml create 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 new file mode 100644 index 0000000000..19e7d4c53c --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/Address.java @@ -0,0 +1,40 @@ +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() { + return streetNumber; + } + + public void setStreetNumber(String streetNumber) { + this.streetNumber = streetNumber; + } + + public String getStreetName() { + return streetName; + } + + public void setStreetName(String streetName) { + this.streetName = streetName; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/Person.java b/jackson/src/test/java/com/baeldung/jackson/dtos/Person.java new file mode 100644 index 0000000000..7891595cc6 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/Person.java @@ -0,0 +1,51 @@ +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() { + return address; + } + + public void setAddress(List
address) { + this.address = address; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + public List getPhoneNumbers() { + return phoneNumbers; + } + + public void setPhoneNumbers(List phoneNumbers) { + 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 0e2a52e75c..039edd45bc 100644 --- a/jackson/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/xml/XMLSerializeDeserializeUnitTest.java @@ -9,12 +9,16 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; import org.junit.Test; +import com.baeldung.jackson.dtos.Address; +import com.baeldung.jackson.dtos.Person; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.dataformat.xml.XmlMapper; -import com.fasterxml.jackson.annotation.JsonProperty; public class XMLSerializeDeserializeUnitTest { @@ -49,7 +53,7 @@ public class XMLSerializeDeserializeUnitTest { assertTrue(value.getX() == 1 && value.getY() == 2); } - @Test + @Test public void whenJavaGotFromXmlStrWithCapitalElem_thenCorrect() throws IOException { XmlMapper xmlMapper = new XmlMapper(); SimpleBeanForCapitalizedFields value = xmlMapper. @@ -67,6 +71,54 @@ public class XMLSerializeDeserializeUnitTest { assertNotNull(file); } + @Test + public void whenJavaDeserializedFromXmlFile_thenCorrect() throws IOException { + XmlMapper xmlMapper = new XmlMapper(); + Person value = xmlMapper.readValue(new File("src/test/resources/person.xml"), Person.class); + + assertTrue(value.getAddress() + .get(0) + .getCity() + .equalsIgnoreCase("city1") + && value.getAddress() + .get(1) + .getCity() + .equalsIgnoreCase("city2")); + } + + @Test + public void whenJavaSerializedToXmlFile_thenSuccess() throws IOException { + XmlMapper xmlMapper = new XmlMapper(); + + Person person = new Person(); + + person.setFirstName("Rohan"); + person.setLastName("Daye"); + + List ph = new ArrayList<>(); + ph.add("9911778981"); + ph.add("9991111111"); + person.setPhoneNumbers(ph); + + List
addresses = new ArrayList<>(); + + Address address1 = new Address(); + address1.setStreetNumber("1"); + address1.setStreetName("streetname1"); + address1.setCity("city1"); + + Address address2 = new Address(); + address2.setStreetNumber("2"); + address2.setStreetName("streetname2"); + address2.setCity("city2"); + + addresses.add(address1); + addresses.add(address2); + person.setAddress(addresses); + + xmlMapper.writeValue(new File("src/test/resources/PersonGenerated.xml"), person); + } + private static String inputStreamToString(InputStream is) throws IOException { BufferedReader br; StringBuilder sb = new StringBuilder(); @@ -103,10 +155,10 @@ class SimpleBean { } -class SimpleBeanForCapitalizedFields { - @JsonProperty("X") - private int x = 1; - private int y = 2; +class SimpleBeanForCapitalizedFields { + @JsonProperty("X") + private int x = 1; + private int y = 2; public int getX() { return x; diff --git a/jackson/src/test/resources/PersonGenerated.xml b/jackson/src/test/resources/PersonGenerated.xml new file mode 100644 index 0000000000..6ebadd971a --- /dev/null +++ b/jackson/src/test/resources/PersonGenerated.xml @@ -0,0 +1,18 @@ + + + + 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 new file mode 100644 index 0000000000..39a6e859c4 --- /dev/null +++ b/jackson/src/test/resources/person.xml @@ -0,0 +1,19 @@ + + + Rohan + Daye + 9911034731 + 9911033478 + +
+ 1 + Name1 + City1 +
+
+ 2 + Name2 + City2 +
+
+
\ No newline at end of file