Added files for BAEL-2494 (#6370)

This commit is contained in:
Urvy Agrawal 2019-02-18 23:00:58 +05:30 committed by maibin
parent 243c671edb
commit fe289003fa
5 changed files with 186 additions and 6 deletions

View File

@ -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;
}
}

View File

@ -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<String> phoneNumbers = new ArrayList<>();
@JacksonXmlElementWrapper(localName = "addresses")
private List<Address> address = new ArrayList<>();
public List<Address> getAddress() {
return address;
}
public void setAddress(List<Address> 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<String> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List<String> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}

View File

@ -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 {
@ -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<String> ph = new ArrayList<>();
ph.add("9911778981");
ph.add("9991111111");
person.setPhoneNumbers(ph);
List<Address> 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();

View File

@ -0,0 +1,18 @@
<person>
<firstName></firstName>
<lastName></lastName>
<phoneNumbers>9911778981</phoneNumbers>
<phoneNumbers>9991111111</phoneNumbers>
<addresses>
<address>
<street_number>1</street_number>
<street_name>streetname1</street_name>
<city>city1</city>
</address>
<address>
<street_number>2</street_number>
<street_name>streetname2</street_name>
<city>city2</city>
</address>
</addresses>
</person>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<person>
<firstName>Rohan</firstName>
<lastName>Daye</lastName>
<phoneNumbers>9911034731</phoneNumbers>
<phoneNumbers>9911033478</phoneNumbers>
<addresses>
<address>
<street_number>1</street_number>
<street_name>Name1</street_name>
<city>City1</city>
</address>
<address>
<street_number>2</street_number>
<street_name>Name2</street_name>
<city>City2</city>
</address>
</addresses>
</person>