From f7594ee89cd7f919ab96bac7456238e94cc40c2f Mon Sep 17 00:00:00 2001 From: ankur-singhal Date: Thu, 31 Mar 2016 17:50:47 +0530 Subject: [PATCH 1/5] XStream-XML to Object XStream-XML to Object --- xstream-xmlToObject/.project | 23 ++++++++ .../data-file-alias-field-complex.xml | 15 +++++ xstream-xmlToObject/data-file-alias-field.xml | 5 ++ .../data-file-alias-implicit-collection.xml | 13 ++++ xstream-xmlToObject/data-file-alias.xml | 5 ++ .../data-file-ignore-field.xml | 6 ++ xstream-xmlToObject/data-file.xml | 5 ++ xstream-xmlToObject/pom.xml | 36 +++++++++++ .../baeldung/annotation/pojo/Customer.java | 46 +++++++++++++++ .../annotation/pojo/CustomerOmitField.java | 50 ++++++++++++++++ .../baeldung/complex/pojo/ContactDetails.java | 46 +++++++++++++++ .../com/baeldung/complex/pojo/Customer.java | 57 ++++++++++++++++++ .../collection/pojo/ContactDetails.java | 46 +++++++++++++++ .../implicit/collection/pojo/Customer.java | 59 +++++++++++++++++++ .../initializer/SimpleXstreamInitializer.java | 19 ++++++ .../main/java/com/baeldung/pojo/Customer.java | 42 +++++++++++++ .../utility/SimpleDataGeneration.java | 19 ++++++ .../src/main/resources/log4j.properties | 16 +++++ .../ComplexXmlToObjectAnnotationTest.java | 38 ++++++++++++ ...lexXmlToObjectAttributeCollectionTest.java | 42 +++++++++++++ .../ComplexXmlToObjectCollectionTest.java | 39 ++++++++++++ .../pojo/test/XmlToObjectAliasTest.java | 37 ++++++++++++ .../pojo/test/XmlToObjectAnnotationTest.java | 38 ++++++++++++ .../pojo/test/XmlToObjectFieldAliasTest.java | 39 ++++++++++++ .../test/XmlToObjectIgnoreFieldsTest.java | 38 ++++++++++++ .../baeldung/pojo/test/XmlToObjectTest.java | 46 +++++++++++++++ 26 files changed, 825 insertions(+) create mode 100644 xstream-xmlToObject/.project create mode 100644 xstream-xmlToObject/data-file-alias-field-complex.xml create mode 100644 xstream-xmlToObject/data-file-alias-field.xml create mode 100644 xstream-xmlToObject/data-file-alias-implicit-collection.xml create mode 100644 xstream-xmlToObject/data-file-alias.xml create mode 100644 xstream-xmlToObject/data-file-ignore-field.xml create mode 100644 xstream-xmlToObject/data-file.xml create mode 100644 xstream-xmlToObject/pom.xml create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java create mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java create mode 100644 xstream-xmlToObject/src/main/resources/log4j.properties create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java create mode 100644 xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java diff --git a/xstream-xmlToObject/.project b/xstream-xmlToObject/.project new file mode 100644 index 0000000000..133b92d690 --- /dev/null +++ b/xstream-xmlToObject/.project @@ -0,0 +1,23 @@ + + + xstream-xmlToObject + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/xstream-xmlToObject/data-file-alias-field-complex.xml b/xstream-xmlToObject/data-file-alias-field-complex.xml new file mode 100644 index 0000000000..2235aa7044 --- /dev/null +++ b/xstream-xmlToObject/data-file-alias-field-complex.xml @@ -0,0 +1,15 @@ + + XStream + Java + 1986-02-14 04:14:05.874 UTC + + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + + \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias-field.xml b/xstream-xmlToObject/data-file-alias-field.xml new file mode 100644 index 0000000000..9bc1d0990a --- /dev/null +++ b/xstream-xmlToObject/data-file-alias-field.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias-implicit-collection.xml b/xstream-xmlToObject/data-file-alias-implicit-collection.xml new file mode 100644 index 0000000000..d8731900b9 --- /dev/null +++ b/xstream-xmlToObject/data-file-alias-implicit-collection.xml @@ -0,0 +1,13 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias.xml b/xstream-xmlToObject/data-file-alias.xml new file mode 100644 index 0000000000..964157f1dd --- /dev/null +++ b/xstream-xmlToObject/data-file-alias.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-ignore-field.xml b/xstream-xmlToObject/data-file-ignore-field.xml new file mode 100644 index 0000000000..865e93d4b4 --- /dev/null +++ b/xstream-xmlToObject/data-file-ignore-field.xml @@ -0,0 +1,6 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + XStream Java + \ No newline at end of file diff --git a/xstream-xmlToObject/data-file.xml b/xstream-xmlToObject/data-file.xml new file mode 100644 index 0000000000..59f2ea1cca --- /dev/null +++ b/xstream-xmlToObject/data-file.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream-xmlToObject/pom.xml b/xstream-xmlToObject/pom.xml new file mode 100644 index 0000000000..4828ccb569 --- /dev/null +++ b/xstream-xmlToObject/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + + com.baeldung + xstream-xmlToObject + 0.0.1-SNAPSHOT + jar + + xstream-xmlToObject + http://maven.apache.org + + + UTF-8 + + + + + com.thoughtworks.xstream + xstream + 1.4.5 + + + + junit + junit + 4.12 + + + + log4j + log4j + 1.2.17 + + + diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java b/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java new file mode 100644 index 0000000000..42d1d039b0 --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java @@ -0,0 +1,46 @@ +package com.baeldung.annotation.pojo; + +import java.util.Date; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +@XStreamAlias("customer") +public class Customer { + + @XStreamAlias("fn") + private String firstName; + + private String lastName; + + private Date dob; + + 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 Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + "]"; + } +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java b/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java new file mode 100644 index 0000000000..881d7a5fc7 --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java @@ -0,0 +1,50 @@ +package com.baeldung.annotation.pojo; + +import java.util.Date; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamOmitField; + + +@XStreamAlias("customer") +public class CustomerOmitField { + + @XStreamOmitField + private String firstName; + + private String lastName; + + private Date dob; + + 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 Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + @Override + public String toString() { + return "CustomerOmitAnnotation [firstName=" + firstName + ", lastName=" + + lastName + ", dob=" + dob + "]"; + } + + +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java b/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java new file mode 100644 index 0000000000..88e721a84b --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java @@ -0,0 +1,46 @@ +package com.baeldung.complex.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; + +@XStreamAlias("ContactDetails") +public class ContactDetails { + + private String mobile; + + private String landline; + + @XStreamAsAttribute + private String contactType; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + + public String getContactType() { + return contactType; + } + + public void setContactType(String contactType) { + this.contactType = contactType; + } + + @Override + public String toString() { + return "ContactDetails [mobile=" + mobile + ", landline=" + landline + + ", contactType=" + contactType + "]"; + } + +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java b/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java new file mode 100644 index 0000000000..0cc3160c03 --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java @@ -0,0 +1,57 @@ +package com.baeldung.complex.pojo; + +import java.util.Date; +import java.util.List; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +@XStreamAlias("customer") +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + private List contactDetailsList; + + 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 Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + public List getContactDetailsList() { + return contactDetailsList; + } + + public void setContactDetailsList(List contactDetailsList) { + this.contactDetailsList = contactDetailsList; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList + + "]"; + } +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java b/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java new file mode 100644 index 0000000000..6d6a63bd7a --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java @@ -0,0 +1,46 @@ +package com.baeldung.implicit.collection.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; + +@XStreamAlias("ContactDetails") +public class ContactDetails { + + private String mobile; + + private String landline; + + @XStreamAsAttribute + private String contactType; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + + public String getContactType() { + return contactType; + } + + public void setContactType(String contactType) { + this.contactType = contactType; + } + + @Override + public String toString() { + return "ContactDetails [mobile=" + mobile + ", landline=" + landline + + ", contactType=" + contactType + "]"; + } + +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java b/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java new file mode 100644 index 0000000000..acbeff165e --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java @@ -0,0 +1,59 @@ +package com.baeldung.implicit.collection.pojo; + +import java.util.Date; +import java.util.List; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamImplicit; + +@XStreamAlias("customer") +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + @XStreamImplicit + private List contactDetailsList; + + 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 Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + public List getContactDetailsList() { + return contactDetailsList; + } + + public void setContactDetailsList(List contactDetailsList) { + this.contactDetailsList = contactDetailsList; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList + + "]"; + } +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java new file mode 100644 index 0000000000..8281678bab --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -0,0 +1,19 @@ +package com.baeldung.initializer; + +import com.thoughtworks.xstream.XStream; + +public class SimpleXstreamInitializer { + + private static XStream xtreamInstance; + + public static XStream getXstreamInstance() { + if (xtreamInstance == null) { + synchronized (SimpleXstreamInitializer.class) { + if (xtreamInstance == null) { + xtreamInstance = new XStream(); + } + } + } + return xtreamInstance; + } +} \ No newline at end of file diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java b/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java new file mode 100644 index 0000000000..affed65fff --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java @@ -0,0 +1,42 @@ +package com.baeldung.pojo; + +import java.util.Date; + +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + 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 Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + "]"; + } +} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java b/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java new file mode 100644 index 0000000000..14ca25952f --- /dev/null +++ b/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java @@ -0,0 +1,19 @@ +package com.baeldung.utility; + +import java.util.Calendar; + +import com.baeldung.pojo.Customer; + +public class SimpleDataGeneration { + + public static Customer generateData() { + Customer customer = new Customer(); + Calendar cal = Calendar.getInstance(); + cal.set(1986 , 01 , 14); + customer.setDob(cal.getTime()); + customer.setFirstName("Xstream"); + customer.setLastName("Java"); + + return customer; + } +} diff --git a/xstream-xmlToObject/src/main/resources/log4j.properties b/xstream-xmlToObject/src/main/resources/log4j.properties new file mode 100644 index 0000000000..9cdafc6bdb --- /dev/null +++ b/xstream-xmlToObject/src/main/resources/log4j.properties @@ -0,0 +1,16 @@ +# Root logger option +log4j.rootLogger=DEBUG, file + +# Redirect log messages to console +# log4j.appender.stdout=org.apache.log4j.ConsoleAppender +# log4j.appender.stdout.Target=System.out +# log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n + +# Redirect log messages to a log file, support file rolling. +log4j.appender.file=org.apache.log4j.RollingFileAppender +log4j.appender.file.File=D:\\Test\\xstream-application.log +log4j.appender.file.MaxFileSize=5MB +log4j.appender.file.MaxBackupIndex=10 +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java new file mode 100644 index 0000000000..dff671b6a6 --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.complex.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; + +public class ComplexXmlToObjectAnnotationTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias-field-complex.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java new file mode 100644 index 0000000000..4494cc833d --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java @@ -0,0 +1,42 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.implicit.collection.pojo.ContactDetails; +import com.baeldung.implicit.collection.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; + +public class ComplexXmlToObjectAttributeCollectionTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + for(ContactDetails contactDetails : customer.getContactDetailsList()){ + Assert.assertNotNull(contactDetails.getContactType()); + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java new file mode 100644 index 0000000000..b72ba44eb5 --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java @@ -0,0 +1,39 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.implicit.collection.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; + +public class ComplexXmlToObjectCollectionTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + System.out.println(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java new file mode 100644 index 0000000000..884071542e --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java @@ -0,0 +1,37 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; + +public class XmlToObjectAliasTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer" , Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java new file mode 100644 index 0000000000..09bed7d24f --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.annotation.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; + +public class XmlToObjectAnnotationTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias-field.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getFirstName()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java new file mode 100644 index 0000000000..c613a6517c --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java @@ -0,0 +1,39 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; + +public class XmlToObjectFieldAliasTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer" , Customer.class); + xstream.aliasField("fn", Customer.class, "firstName"); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-alias-field.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getFirstName()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java new file mode 100644 index 0000000000..aa88918402 --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; + +public class XmlToObjectIgnoreFieldsTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer" , Customer.class); + xstream.ignoreUnknownElements(); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file-ignore-field.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + System.out.println(customer); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java new file mode 100644 index 0000000000..cb7fc49b33 --- /dev/null +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java @@ -0,0 +1,46 @@ +package com.baeldung.pojo.test; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.baeldung.utility.SimpleDataGeneration; +import com.thoughtworks.xstream.XStream; + +public class XmlToObjectTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + xstream = SimpleXstreamInitializer.getXstreamInstance(); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + FileReader reader = new FileReader(new File("data-file.xml")); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void convertXmlToObjectFromString() { + Customer customer = SimpleDataGeneration.generateData(); + String dataXml = xstream.toXML(customer); + Customer convertedCustomer = (Customer) xstream.fromXML(dataXml); + Assert.assertNotNull(convertedCustomer); + } + + +} From f214e0a285ee153f04ba84867550a43b0afea3d4 Mon Sep 17 00:00:00 2001 From: ankur-singhal Date: Fri, 1 Apr 2016 17:43:37 +0530 Subject: [PATCH 2/5] Xstream - XML to Object xml moved to resources folder, test cases fixed --- xstream-xmlToObject/.project | 33 +++++++------------ .../initializer/SimpleXstreamInitializer.java | 4 +-- .../ComplexXmlToObjectAnnotationTest.java | 7 ++-- ...lexXmlToObjectAttributeCollectionTest.java | 11 ++++--- .../ComplexXmlToObjectCollectionTest.java | 7 ++-- .../pojo/test/XmlToObjectAliasTest.java | 7 ++-- .../pojo/test/XmlToObjectAnnotationTest.java | 7 ++-- .../pojo/test/XmlToObjectFieldAliasTest.java | 7 ++-- .../test/XmlToObjectIgnoreFieldsTest.java | 7 ++-- .../baeldung/pojo/test/XmlToObjectTest.java | 7 ++-- .../data-file-alias-field-complex.xml | 15 +++++++++ .../test/resources/data-file-alias-field.xml | 5 +++ .../data-file-alias-implicit-collection.xml | 13 ++++++++ .../src/test/resources/data-file-alias.xml | 5 +++ .../test/resources/data-file-ignore-field.xml | 6 ++++ .../src/test/resources/data-file.xml | 5 +++ 16 files changed, 97 insertions(+), 49 deletions(-) create mode 100644 xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml create mode 100644 xstream-xmlToObject/src/test/resources/data-file-alias-field.xml create mode 100644 xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml create mode 100644 xstream-xmlToObject/src/test/resources/data-file-alias.xml create mode 100644 xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml create mode 100644 xstream-xmlToObject/src/test/resources/data-file.xml diff --git a/xstream-xmlToObject/.project b/xstream-xmlToObject/.project index 133b92d690..4e1a3f6e92 100644 --- a/xstream-xmlToObject/.project +++ b/xstream-xmlToObject/.project @@ -1,23 +1,14 @@ - xstream-xmlToObject - - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - - org.eclipse.jdt.core.javanature - org.eclipse.m2e.core.maven2Nature - - + xstream-xmlToObject + NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. + + + + org.eclipse.jdt.core.javabuilder + + + + org.eclipse.jdt.core.javanature + + \ No newline at end of file diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java index 8281678bab..d7dc3f7a81 100644 --- a/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ b/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -4,9 +4,9 @@ import com.thoughtworks.xstream.XStream; public class SimpleXstreamInitializer { - private static XStream xtreamInstance; + private XStream xtreamInstance; - public static XStream getXstreamInstance() { + public XStream getXstreamInstance() { if (xtreamInstance == null) { synchronized (SimpleXstreamInitializer.class) { if (xtreamInstance == null) { diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java index dff671b6a6..10f59e843b 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,14 +17,16 @@ public class ComplexXmlToObjectAnnotationTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.processAnnotations(Customer.class); } @Test public void convertXmlToObjectFromFile() { try { - FileReader reader = new FileReader(new File("data-file-alias-field-complex.xml")); + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getContactDetailsList()); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java index 4494cc833d..876a407f76 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -8,8 +7,8 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import com.baeldung.implicit.collection.pojo.ContactDetails; -import com.baeldung.implicit.collection.pojo.Customer; +import com.baeldung.complex.pojo.ContactDetails; +import com.baeldung.complex.pojo.Customer; import com.baeldung.initializer.SimpleXstreamInitializer; import com.thoughtworks.xstream.XStream; @@ -19,14 +18,16 @@ public class ComplexXmlToObjectAttributeCollectionTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.processAnnotations(Customer.class); } @Test public void convertXmlToObjectFromFile() { try { - FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml")); + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getContactDetailsList()); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java index b72ba44eb5..1656d2f230 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,14 +17,16 @@ public class ComplexXmlToObjectCollectionTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.processAnnotations(Customer.class); } @Test public void convertXmlToObjectFromFile() { try { - FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml")); + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-implicit-collection.xml").getFile()); Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getContactDetailsList()); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java index 884071542e..cc6d945254 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,14 +17,16 @@ public class XmlToObjectAliasTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.alias("customer" , Customer.class); } @Test public void convertXmlToObjectFromFile() { try { - FileReader reader = new FileReader(new File("data-file-alias.xml")); + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile()); Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java index 09bed7d24f..f2012e2b12 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,14 +17,16 @@ public class XmlToObjectAnnotationTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.processAnnotations(Customer.class); } @Test public void convertXmlToObjectFromFile() { try { - FileReader reader = new FileReader(new File("data-file-alias-field.xml")); + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getFirstName()); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java index c613a6517c..c4bba86265 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,7 +17,8 @@ public class XmlToObjectFieldAliasTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.alias("customer" , Customer.class); xstream.aliasField("fn", Customer.class, "firstName"); } @@ -26,7 +26,8 @@ public class XmlToObjectFieldAliasTest { @Test public void convertXmlToObjectFromFile() { try { - FileReader reader = new FileReader(new File("data-file-alias-field.xml")); + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getFirstName()); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java index aa88918402..ca31a22b94 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -18,7 +17,8 @@ public class XmlToObjectIgnoreFieldsTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.alias("customer" , Customer.class); xstream.ignoreUnknownElements(); } @@ -26,7 +26,8 @@ public class XmlToObjectIgnoreFieldsTest { @Test public void convertXmlToObjectFromFile() { try { - FileReader reader = new FileReader(new File("data-file-ignore-field.xml")); + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile()); Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); System.out.println(customer); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java index cb7fc49b33..e24e54aea4 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java +++ b/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java @@ -1,6 +1,5 @@ package com.baeldung.pojo.test; -import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -19,13 +18,15 @@ public class XmlToObjectTest { @Before public void dataSetup() { - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); } @Test public void convertXmlToObjectFromFile() { try { - FileReader reader = new FileReader(new File("data-file.xml")); + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile()); Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml b/xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml new file mode 100644 index 0000000000..2235aa7044 --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml @@ -0,0 +1,15 @@ + + XStream + Java + 1986-02-14 04:14:05.874 UTC + + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + + \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-field.xml b/xstream-xmlToObject/src/test/resources/data-file-alias-field.xml new file mode 100644 index 0000000000..9bc1d0990a --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file-alias-field.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml b/xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml new file mode 100644 index 0000000000..d8731900b9 --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml @@ -0,0 +1,13 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias.xml b/xstream-xmlToObject/src/test/resources/data-file-alias.xml new file mode 100644 index 0000000000..964157f1dd --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file-alias.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml b/xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml new file mode 100644 index 0000000000..865e93d4b4 --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml @@ -0,0 +1,6 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + XStream Java + \ No newline at end of file diff --git a/xstream-xmlToObject/src/test/resources/data-file.xml b/xstream-xmlToObject/src/test/resources/data-file.xml new file mode 100644 index 0000000000..59f2ea1cca --- /dev/null +++ b/xstream-xmlToObject/src/test/resources/data-file.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file From cbc3d119a5882d9aa5dd4b09faa214078869858b Mon Sep 17 00:00:00 2001 From: ankur-singhal Date: Fri, 1 Apr 2016 17:56:04 +0530 Subject: [PATCH 3/5] Xstream - xml to object removing input xml from root folders --- .../data-file-alias-field-complex.xml | 15 --------------- xstream-xmlToObject/data-file-alias-field.xml | 5 ----- .../data-file-alias-implicit-collection.xml | 13 ------------- xstream-xmlToObject/data-file-alias.xml | 5 ----- xstream-xmlToObject/data-file-ignore-field.xml | 6 ------ xstream-xmlToObject/data-file.xml | 5 ----- 6 files changed, 49 deletions(-) delete mode 100644 xstream-xmlToObject/data-file-alias-field-complex.xml delete mode 100644 xstream-xmlToObject/data-file-alias-field.xml delete mode 100644 xstream-xmlToObject/data-file-alias-implicit-collection.xml delete mode 100644 xstream-xmlToObject/data-file-alias.xml delete mode 100644 xstream-xmlToObject/data-file-ignore-field.xml delete mode 100644 xstream-xmlToObject/data-file.xml diff --git a/xstream-xmlToObject/data-file-alias-field-complex.xml b/xstream-xmlToObject/data-file-alias-field-complex.xml deleted file mode 100644 index 2235aa7044..0000000000 --- a/xstream-xmlToObject/data-file-alias-field-complex.xml +++ /dev/null @@ -1,15 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:05.874 UTC - - - 6673543265 - 0124-2460311 - - - 4676543565 - 0120-223312 - - - \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias-field.xml b/xstream-xmlToObject/data-file-alias-field.xml deleted file mode 100644 index 9bc1d0990a..0000000000 --- a/xstream-xmlToObject/data-file-alias-field.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias-implicit-collection.xml b/xstream-xmlToObject/data-file-alias-implicit-collection.xml deleted file mode 100644 index d8731900b9..0000000000 --- a/xstream-xmlToObject/data-file-alias-implicit-collection.xml +++ /dev/null @@ -1,13 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:20.541 UTC - - 6673543265 - 0124-2460311 - - - 4676543565 - 0120-223312 - - \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-alias.xml b/xstream-xmlToObject/data-file-alias.xml deleted file mode 100644 index 964157f1dd..0000000000 --- a/xstream-xmlToObject/data-file-alias.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file diff --git a/xstream-xmlToObject/data-file-ignore-field.xml b/xstream-xmlToObject/data-file-ignore-field.xml deleted file mode 100644 index 865e93d4b4..0000000000 --- a/xstream-xmlToObject/data-file-ignore-field.xml +++ /dev/null @@ -1,6 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:20.541 UTC - XStream Java - \ No newline at end of file diff --git a/xstream-xmlToObject/data-file.xml b/xstream-xmlToObject/data-file.xml deleted file mode 100644 index 59f2ea1cca..0000000000 --- a/xstream-xmlToObject/data-file.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file From dd61036a31a8ebac3087fe94fd58c0bd2bef5df4 Mon Sep 17 00:00:00 2001 From: ankur-singhal Date: Mon, 4 Apr 2016 11:27:07 +0530 Subject: [PATCH 4/5] XStream-Introduction Merging changes for Article XML - to Object --- .../baeldung/annotation/pojo/Customer.java | 0 .../annotation/pojo/CustomerOmitField.java | 0 .../baeldung/complex/pojo/ContactDetails.java | 0 .../com/baeldung/complex/pojo/Customer.java | 0 .../collection/pojo/ContactDetails.java | 0 .../implicit/collection/pojo/Customer.java | 0 .../initializer/SimpleXstreamInitializer.java | 12 +++--- .../main/java/com/baeldung/pojo/Customer.java | 6 ++- .../ComplexXmlToObjectAnnotationTest.java | 0 ...lexXmlToObjectAttributeCollectionTest.java | 0 .../ComplexXmlToObjectCollectionTest.java | 2 +- .../pojo/test/XmlToObjectAliasTest.java | 0 .../pojo/test/XmlToObjectAnnotationTest.java | 0 .../pojo/test/XmlToObjectFieldAliasTest.java | 0 .../test/XmlToObjectIgnoreFieldsTest.java | 2 +- .../baeldung/pojo/test/XmlToObjectTest.java | 0 .../utility/XStreamSimpleXmlTest.java | 5 +-- .../data-file-alias-field-complex.xml | 0 .../test/resources/data-file-alias-field.xml | 0 .../data-file-alias-implicit-collection.xml | 0 .../src/test/resources/data-file-alias.xml | 0 .../test/resources/data-file-ignore-field.xml | 0 .../src/test/resources/data-file.xml | 0 xstream-xmlToObject/.project | 14 ------- xstream-xmlToObject/pom.xml | 36 ---------------- .../initializer/SimpleXstreamInitializer.java | 19 --------- .../main/java/com/baeldung/pojo/Customer.java | 42 ------------------- .../utility/SimpleDataGeneration.java | 19 --------- .../src/main/resources/log4j.properties | 16 ------- 29 files changed, 14 insertions(+), 159 deletions(-) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/annotation/pojo/Customer.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/complex/pojo/ContactDetails.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/complex/pojo/Customer.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java (96%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java (96%) rename {xstream-xmlToObject => xstream-introduction}/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file-alias-field-complex.xml (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file-alias-field.xml (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file-alias-implicit-collection.xml (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file-alias.xml (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file-ignore-field.xml (100%) rename {xstream-xmlToObject => xstream-introduction}/src/test/resources/data-file.xml (100%) delete mode 100644 xstream-xmlToObject/.project delete mode 100644 xstream-xmlToObject/pom.xml delete mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java delete mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java delete mode 100644 xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java delete mode 100644 xstream-xmlToObject/src/main/resources/log4j.properties diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/Customer.java rename to xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java b/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java rename to xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/ContactDetails.java rename to xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/complex/pojo/Customer.java rename to xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java rename to xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java similarity index 100% rename from xstream-xmlToObject/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java rename to xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java diff --git a/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java index 618df877b9..d7dc3f7a81 100644 --- a/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ b/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -4,16 +4,16 @@ import com.thoughtworks.xstream.XStream; public class SimpleXstreamInitializer { - private static XStream xstreamInstance; + private XStream xtreamInstance; - public static XStream getXstreamInstance() { - if (xstreamInstance == null) { + public XStream getXstreamInstance() { + if (xtreamInstance == null) { synchronized (SimpleXstreamInitializer.class) { - if (xstreamInstance == null) { - xstreamInstance = new XStream(); + if (xtreamInstance == null) { + xtreamInstance = new XStream(); } } } - return xstreamInstance; + return xtreamInstance; } } \ No newline at end of file diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java index 2ed11dcdab..9bed3394ef 100644 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java +++ b/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java @@ -5,12 +5,10 @@ import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamImplicit; -import com.thoughtworks.xstream.annotations.XStreamOmitField; @XStreamAlias("customer") public class Customer { - //@XStreamOmitField private String firstName; private String lastName; @@ -52,4 +50,8 @@ public class Customer { this.contactDetailsList = contactDetailsList; } + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + "]"; + } } diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java similarity index 96% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java index 1656d2f230..d701fc153b 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java +++ b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java @@ -30,7 +30,7 @@ public class ComplexXmlToObjectCollectionTest { Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); Assert.assertNotNull(customer.getContactDetailsList()); - System.out.println(customer); + //System.out.println(customer); } catch (IOException e) { e.printStackTrace(); diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java similarity index 96% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java index ca31a22b94..cb13bb9570 100644 --- a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java +++ b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java @@ -30,7 +30,7 @@ public class XmlToObjectIgnoreFieldsTest { FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile()); Customer customer = (Customer) xstream.fromXML(reader); Assert.assertNotNull(customer); - System.out.println(customer); + //System.out.println(customer); } catch (IOException e) { e.printStackTrace(); } diff --git a/xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java similarity index 100% rename from xstream-xmlToObject/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java rename to xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java diff --git a/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java b/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java index 50d02528bd..ea60e68743 100644 --- a/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java +++ b/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java @@ -22,7 +22,8 @@ public class XStreamSimpleXmlTest { @Before public void dataSetup() { customer = SimpleDataGeneration.generateData(); - xstream = SimpleXstreamInitializer.getXstreamInstance(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); xstream.processAnnotations(Customer.class); xstream.processAnnotations(AddressDetails.class); xstream.processAnnotations(ContactDetails.class); @@ -30,9 +31,7 @@ public class XStreamSimpleXmlTest { xstream.registerConverter(new MyDateConverter()); // xstream.registerConverter(new MySingleValueConverter()); xstream.aliasField("fn" , Customer.class , "firstName"); - dataXml = xstream.toXML(customer); - System.out.println(dataXml); } @Test diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml b/xstream-introduction/src/test/resources/data-file-alias-field-complex.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file-alias-field-complex.xml rename to xstream-introduction/src/test/resources/data-file-alias-field-complex.xml diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-field.xml b/xstream-introduction/src/test/resources/data-file-alias-field.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file-alias-field.xml rename to xstream-introduction/src/test/resources/data-file-alias-field.xml diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml b/xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file-alias-implicit-collection.xml rename to xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml diff --git a/xstream-xmlToObject/src/test/resources/data-file-alias.xml b/xstream-introduction/src/test/resources/data-file-alias.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file-alias.xml rename to xstream-introduction/src/test/resources/data-file-alias.xml diff --git a/xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml b/xstream-introduction/src/test/resources/data-file-ignore-field.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file-ignore-field.xml rename to xstream-introduction/src/test/resources/data-file-ignore-field.xml diff --git a/xstream-xmlToObject/src/test/resources/data-file.xml b/xstream-introduction/src/test/resources/data-file.xml similarity index 100% rename from xstream-xmlToObject/src/test/resources/data-file.xml rename to xstream-introduction/src/test/resources/data-file.xml diff --git a/xstream-xmlToObject/.project b/xstream-xmlToObject/.project deleted file mode 100644 index 4e1a3f6e92..0000000000 --- a/xstream-xmlToObject/.project +++ /dev/null @@ -1,14 +0,0 @@ - - - xstream-xmlToObject - NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. - - - - org.eclipse.jdt.core.javabuilder - - - - org.eclipse.jdt.core.javanature - - \ No newline at end of file diff --git a/xstream-xmlToObject/pom.xml b/xstream-xmlToObject/pom.xml deleted file mode 100644 index 4828ccb569..0000000000 --- a/xstream-xmlToObject/pom.xml +++ /dev/null @@ -1,36 +0,0 @@ - - 4.0.0 - - com.baeldung - xstream-xmlToObject - 0.0.1-SNAPSHOT - jar - - xstream-xmlToObject - http://maven.apache.org - - - UTF-8 - - - - - com.thoughtworks.xstream - xstream - 1.4.5 - - - - junit - junit - 4.12 - - - - log4j - log4j - 1.2.17 - - - diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java deleted file mode 100644 index d7dc3f7a81..0000000000 --- a/xstream-xmlToObject/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.initializer; - -import com.thoughtworks.xstream.XStream; - -public class SimpleXstreamInitializer { - - private XStream xtreamInstance; - - public XStream getXstreamInstance() { - if (xtreamInstance == null) { - synchronized (SimpleXstreamInitializer.class) { - if (xtreamInstance == null) { - xtreamInstance = new XStream(); - } - } - } - return xtreamInstance; - } -} \ No newline at end of file diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java b/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java deleted file mode 100644 index affed65fff..0000000000 --- a/xstream-xmlToObject/src/main/java/com/baeldung/pojo/Customer.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.pojo; - -import java.util.Date; - -public class Customer { - - private String firstName; - - private String lastName; - - private Date dob; - - 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 Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - @Override - public String toString() { - return "Customer [firstName=" + firstName + ", lastName=" + lastName - + ", dob=" + dob + "]"; - } -} diff --git a/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java b/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java deleted file mode 100644 index 14ca25952f..0000000000 --- a/xstream-xmlToObject/src/main/java/com/baeldung/utility/SimpleDataGeneration.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.utility; - -import java.util.Calendar; - -import com.baeldung.pojo.Customer; - -public class SimpleDataGeneration { - - public static Customer generateData() { - Customer customer = new Customer(); - Calendar cal = Calendar.getInstance(); - cal.set(1986 , 01 , 14); - customer.setDob(cal.getTime()); - customer.setFirstName("Xstream"); - customer.setLastName("Java"); - - return customer; - } -} diff --git a/xstream-xmlToObject/src/main/resources/log4j.properties b/xstream-xmlToObject/src/main/resources/log4j.properties deleted file mode 100644 index 9cdafc6bdb..0000000000 --- a/xstream-xmlToObject/src/main/resources/log4j.properties +++ /dev/null @@ -1,16 +0,0 @@ -# Root logger option -log4j.rootLogger=DEBUG, file - -# Redirect log messages to console -# log4j.appender.stdout=org.apache.log4j.ConsoleAppender -# log4j.appender.stdout.Target=System.out -# log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n - -# Redirect log messages to a log file, support file rolling. -log4j.appender.file=org.apache.log4j.RollingFileAppender -log4j.appender.file.File=D:\\Test\\xstream-application.log -log4j.appender.file.MaxFileSize=5MB -log4j.appender.file.MaxBackupIndex=10 -log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file From ed629e7a49f36ad0ec7dd40b22396f59e06efb39 Mon Sep 17 00:00:00 2001 From: David Morley Date: Mon, 4 Apr 2016 05:18:35 -0500 Subject: [PATCH 5/5] Clean up XStream examples --- xstream-introduction/.project | 14 ----- .../baeldung/annotation/pojo/Customer.java | 46 -------------- .../annotation/pojo/CustomerOmitField.java | 50 --------------- .../baeldung/complex/pojo/ContactDetails.java | 46 -------------- .../com/baeldung/complex/pojo/Customer.java | 57 ----------------- .../collection/pojo/ContactDetails.java | 46 -------------- .../implicit/collection/pojo/Customer.java | 59 ------------------ .../initializer/SimpleXstreamInitializer.java | 19 ------ .../com/baeldung/pojo/AddressDetails.java | 40 ------------ .../com/baeldung/pojo/ContactDetails.java | 28 --------- .../main/java/com/baeldung/pojo/Customer.java | 57 ----------------- .../baeldung/pojo/CustomerAddressDetails.java | 50 --------------- .../com/baeldung/pojo/CustomerPortfolio.java | 20 ------ .../com/baeldung/utility/MyDateConverter.java | 40 ------------ .../utility/MySingleValueConverter.java | 28 --------- .../utility/SimpleDataGeneration.java | 37 ----------- .../ComplexXmlToObjectAnnotationTest.java | 39 ------------ ...lexXmlToObjectAttributeCollectionTest.java | 43 ------------- .../ComplexXmlToObjectCollectionTest.java | 40 ------------ .../pojo/test/XmlToObjectAliasTest.java | 38 ------------ .../pojo/test/XmlToObjectAnnotationTest.java | 39 ------------ .../pojo/test/XmlToObjectFieldAliasTest.java | 40 ------------ .../test/XmlToObjectIgnoreFieldsTest.java | 39 ------------ .../baeldung/pojo/test/XmlToObjectTest.java | 47 -------------- .../utility/XStreamSimpleXmlTest.java | 61 ------------------- .../data-file-alias-field-complex.xml | 15 ----- .../test/resources/data-file-alias-field.xml | 5 -- .../data-file-alias-implicit-collection.xml | 13 ---- .../src/test/resources/data-file-alias.xml | 5 -- .../test/resources/data-file-ignore-field.xml | 6 -- .../src/test/resources/data-file.xml | 5 -- {xstream-introduction => xstream}/pom.xml | 14 +++++ .../baeldung/annotation/pojo/Customer.java | 46 ++++++++++++++ .../annotation/pojo/CustomerOmitField.java | 50 +++++++++++++++ .../baeldung/complex/pojo/ContactDetails.java | 46 ++++++++++++++ .../com/baeldung/complex/pojo/Customer.java | 57 +++++++++++++++++ .../collection/pojo/ContactDetails.java | 46 ++++++++++++++ .../implicit/collection/pojo/Customer.java | 59 ++++++++++++++++++ .../initializer/SimpleXstreamInitializer.java | 19 ++++++ .../com/baeldung/pojo/AddressDetails.java | 40 ++++++++++++ .../com/baeldung/pojo/ContactDetails.java | 28 +++++++++ .../main/java/com/baeldung/pojo/Customer.java | 57 +++++++++++++++++ .../baeldung/pojo/CustomerAddressDetails.java | 50 +++++++++++++++ .../com/baeldung/pojo/CustomerPortfolio.java | 20 ++++++ .../com/baeldung/utility/MyDateConverter.java | 40 ++++++++++++ .../utility/MySingleValueConverter.java | 28 +++++++++ .../utility/SimpleDataGeneration.java | 37 +++++++++++ .../src/main/resources/log4j.properties | 0 .../ComplexXmlToObjectAnnotationTest.java | 38 ++++++++++++ ...lexXmlToObjectAttributeCollectionTest.java | 42 +++++++++++++ .../ComplexXmlToObjectCollectionTest.java | 39 ++++++++++++ .../pojo/test/XmlToObjectAliasTest.java | 37 +++++++++++ .../pojo/test/XmlToObjectAnnotationTest.java | 38 ++++++++++++ .../pojo/test/XmlToObjectFieldAliasTest.java | 39 ++++++++++++ .../test/XmlToObjectIgnoreFieldsTest.java | 38 ++++++++++++ .../baeldung/pojo/test/XmlToObjectTest.java | 46 ++++++++++++++ .../utility/XStreamSimpleXmlTest.java | 57 +++++++++++++++++ .../data-file-alias-field-complex.xml | 15 +++++ .../test/resources/data-file-alias-field.xml | 5 ++ .../data-file-alias-implicit-collection.xml | 13 ++++ .../src/test/resources/data-file-alias.xml | 5 ++ .../test/resources/data-file-ignore-field.xml | 6 ++ xstream/src/test/resources/data-file.xml | 5 ++ 63 files changed, 1060 insertions(+), 1072 deletions(-) delete mode 100644 xstream-introduction/.project delete mode 100644 xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java delete mode 100644 xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java delete mode 100644 xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java delete mode 100644 xstream-introduction/src/test/resources/data-file-alias-field-complex.xml delete mode 100644 xstream-introduction/src/test/resources/data-file-alias-field.xml delete mode 100644 xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml delete mode 100644 xstream-introduction/src/test/resources/data-file-alias.xml delete mode 100644 xstream-introduction/src/test/resources/data-file-ignore-field.xml delete mode 100644 xstream-introduction/src/test/resources/data-file.xml rename {xstream-introduction => xstream}/pom.xml (74%) create mode 100644 xstream/src/main/java/com/baeldung/annotation/pojo/Customer.java create mode 100644 xstream/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java create mode 100644 xstream/src/main/java/com/baeldung/complex/pojo/ContactDetails.java create mode 100644 xstream/src/main/java/com/baeldung/complex/pojo/Customer.java create mode 100644 xstream/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java create mode 100644 xstream/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java create mode 100644 xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java create mode 100644 xstream/src/main/java/com/baeldung/pojo/AddressDetails.java create mode 100644 xstream/src/main/java/com/baeldung/pojo/ContactDetails.java create mode 100644 xstream/src/main/java/com/baeldung/pojo/Customer.java create mode 100644 xstream/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java create mode 100644 xstream/src/main/java/com/baeldung/pojo/CustomerPortfolio.java create mode 100644 xstream/src/main/java/com/baeldung/utility/MyDateConverter.java create mode 100644 xstream/src/main/java/com/baeldung/utility/MySingleValueConverter.java create mode 100644 xstream/src/main/java/com/baeldung/utility/SimpleDataGeneration.java rename {xstream-introduction => xstream}/src/main/resources/log4j.properties (100%) create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java create mode 100644 xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java create mode 100644 xstream/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java create mode 100644 xstream/src/test/resources/data-file-alias-field-complex.xml create mode 100644 xstream/src/test/resources/data-file-alias-field.xml create mode 100644 xstream/src/test/resources/data-file-alias-implicit-collection.xml create mode 100644 xstream/src/test/resources/data-file-alias.xml create mode 100644 xstream/src/test/resources/data-file-ignore-field.xml create mode 100644 xstream/src/test/resources/data-file.xml diff --git a/xstream-introduction/.project b/xstream-introduction/.project deleted file mode 100644 index cebf3c9cfa..0000000000 --- a/xstream-introduction/.project +++ /dev/null @@ -1,14 +0,0 @@ - - - xstream-introduction - An Introduction To XStream. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. - - - - org.eclipse.jdt.core.javabuilder - - - - org.eclipse.jdt.core.javanature - - \ No newline at end of file diff --git a/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java deleted file mode 100644 index 42d1d039b0..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/Customer.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.annotation.pojo; - -import java.util.Date; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("customer") -public class Customer { - - @XStreamAlias("fn") - private String firstName; - - private String lastName; - - private Date dob; - - 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 Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - @Override - public String toString() { - return "Customer [firstName=" + firstName + ", lastName=" + lastName - + ", dob=" + dob + "]"; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java b/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java deleted file mode 100644 index 881d7a5fc7..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.annotation.pojo; - -import java.util.Date; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamOmitField; - - -@XStreamAlias("customer") -public class CustomerOmitField { - - @XStreamOmitField - private String firstName; - - private String lastName; - - private Date dob; - - 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 Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - @Override - public String toString() { - return "CustomerOmitAnnotation [firstName=" + firstName + ", lastName=" - + lastName + ", dob=" + dob + "]"; - } - - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java deleted file mode 100644 index 88e721a84b..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/complex/pojo/ContactDetails.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.complex.pojo; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamAsAttribute; - -@XStreamAlias("ContactDetails") -public class ContactDetails { - - private String mobile; - - private String landline; - - @XStreamAsAttribute - private String contactType; - - public String getMobile() { - return mobile; - } - - public void setMobile(String mobile) { - this.mobile = mobile; - } - - public String getLandline() { - return landline; - } - - public void setLandline(String landline) { - this.landline = landline; - } - - public String getContactType() { - return contactType; - } - - public void setContactType(String contactType) { - this.contactType = contactType; - } - - @Override - public String toString() { - return "ContactDetails [mobile=" + mobile + ", landline=" + landline - + ", contactType=" + contactType + "]"; - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java deleted file mode 100644 index 0cc3160c03..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/complex/pojo/Customer.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.baeldung.complex.pojo; - -import java.util.Date; -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("customer") -public class Customer { - - private String firstName; - - private String lastName; - - private Date dob; - - private List contactDetailsList; - - 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 Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - public List getContactDetailsList() { - return contactDetailsList; - } - - public void setContactDetailsList(List contactDetailsList) { - this.contactDetailsList = contactDetailsList; - } - - @Override - public String toString() { - return "Customer [firstName=" + firstName + ", lastName=" + lastName - + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList - + "]"; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java deleted file mode 100644 index 6d6a63bd7a..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.baeldung.implicit.collection.pojo; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamAsAttribute; - -@XStreamAlias("ContactDetails") -public class ContactDetails { - - private String mobile; - - private String landline; - - @XStreamAsAttribute - private String contactType; - - public String getMobile() { - return mobile; - } - - public void setMobile(String mobile) { - this.mobile = mobile; - } - - public String getLandline() { - return landline; - } - - public void setLandline(String landline) { - this.landline = landline; - } - - public String getContactType() { - return contactType; - } - - public void setContactType(String contactType) { - this.contactType = contactType; - } - - @Override - public String toString() { - return "ContactDetails [mobile=" + mobile + ", landline=" + landline - + ", contactType=" + contactType + "]"; - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java deleted file mode 100644 index acbeff165e..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.baeldung.implicit.collection.pojo; - -import java.util.Date; -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamImplicit; - -@XStreamAlias("customer") -public class Customer { - - private String firstName; - - private String lastName; - - private Date dob; - - @XStreamImplicit - private List contactDetailsList; - - 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 Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - public List getContactDetailsList() { - return contactDetailsList; - } - - public void setContactDetailsList(List contactDetailsList) { - this.contactDetailsList = contactDetailsList; - } - - @Override - public String toString() { - return "Customer [firstName=" + firstName + ", lastName=" + lastName - + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList - + "]"; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java deleted file mode 100644 index d7dc3f7a81..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.initializer; - -import com.thoughtworks.xstream.XStream; - -public class SimpleXstreamInitializer { - - private XStream xtreamInstance; - - public XStream getXstreamInstance() { - if (xtreamInstance == null) { - synchronized (SimpleXstreamInitializer.class) { - if (xtreamInstance == null) { - xtreamInstance = new XStream(); - } - } - } - return xtreamInstance; - } -} \ No newline at end of file diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java b/xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java deleted file mode 100644 index e9e30bf5fc..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.pojo; - -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("AddressDetails") -public class AddressDetails { - - private String address; - - private String zipcode; - - private List contactDetails; - - public String getZipcode() { - return zipcode; - } - - public void setZipcode(String zipcode) { - this.zipcode = zipcode; - } - - public List getContactDetails() { - return contactDetails; - } - - public void setContactDetails(List contactDetails) { - this.contactDetails = contactDetails; - } - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java deleted file mode 100644 index 66475b9d8e..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.pojo; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("ContactDetails") -public class ContactDetails { - - private String mobile; - - private String landline; - - public String getMobile() { - return mobile; - } - - public void setMobile(String mobile) { - this.mobile = mobile; - } - - public String getLandline() { - return landline; - } - - public void setLandline(String landline) { - this.landline = landline; - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java deleted file mode 100644 index 9bed3394ef..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.baeldung.pojo; - -import java.util.Date; -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamImplicit; - -@XStreamAlias("customer") -public class Customer { - - private String firstName; - - private String lastName; - - private Date dob; - - @XStreamImplicit - private List contactDetailsList; - - 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 Date getDob() { - return dob; - } - - public void setDob(Date dob) { - this.dob = dob; - } - - public List getContactDetailsList() { - return contactDetailsList; - } - - public void setContactDetailsList(List contactDetailsList) { - this.contactDetailsList = contactDetailsList; - } - - @Override - public String toString() { - return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + "]"; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java b/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java deleted file mode 100644 index 30fda1b92c..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.pojo; - -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("CustomerAddressDetails") -public class CustomerAddressDetails { - - private List addressDetails; - - private String firstName; - - private String lastName; - - private int age; - - 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 int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - - public List getAddressDetails() { - return addressDetails; - } - - public void setAddressDetails(List addressDetails) { - this.addressDetails = addressDetails; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java b/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java deleted file mode 100644 index 6f1ce4b651..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.pojo; - -import java.util.List; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("CustomerPortfolio") -public class CustomerPortfolio { - - private List customerAddressDetailsList; - - public List getCustomerAddressDetailsList() { - return customerAddressDetailsList; - } - - public void setCustomerAddressDetailsList(List customerAddressDetailsList) { - this.customerAddressDetailsList = customerAddressDetailsList; - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java b/xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java deleted file mode 100644 index 564a28d1c5..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.utility; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.GregorianCalendar; - -import com.thoughtworks.xstream.converters.ConversionException; -import com.thoughtworks.xstream.converters.Converter; -import com.thoughtworks.xstream.converters.MarshallingContext; -import com.thoughtworks.xstream.converters.UnmarshallingContext; -import com.thoughtworks.xstream.io.HierarchicalStreamReader; -import com.thoughtworks.xstream.io.HierarchicalStreamWriter; - -public class MyDateConverter implements Converter { - - private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); - - @Override - public boolean canConvert(Class clazz) { - return Date.class.isAssignableFrom(clazz); - } - - @Override - public void marshal(Object value , HierarchicalStreamWriter writer , MarshallingContext arg2) { - Date date = (Date) value; - writer.setValue(formatter.format(date)); - } - - @Override - public Object unmarshal(HierarchicalStreamReader reader , UnmarshallingContext arg1) { - GregorianCalendar calendar = new GregorianCalendar(); - try { - calendar.setTime(formatter.parse(reader.getValue())); - } catch (ParseException e) { - throw new ConversionException(e.getMessage() , e); - } - return calendar; - } -} diff --git a/xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java b/xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java deleted file mode 100644 index 358d647835..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.utility; - -import java.text.SimpleDateFormat; -import java.util.Date; - -import com.baeldung.pojo.Customer; -import com.thoughtworks.xstream.converters.SingleValueConverter; - -public class MySingleValueConverter implements SingleValueConverter { - - @Override - public boolean canConvert(Class clazz) { - return Customer.class.isAssignableFrom(clazz); - } - - @Override - public Object fromString(String arg0) { - return null; - } - - @Override - public String toString(Object obj) { - SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); - Date date = ((Customer) obj).getDob(); - return ((Customer) obj).getFirstName() + "," + ((Customer) obj).getLastName() + "," + formatter.format(date); - } - -} diff --git a/xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java b/xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java deleted file mode 100644 index 22d0f0ce77..0000000000 --- a/xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.utility; - -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; - -import com.baeldung.pojo.ContactDetails; -import com.baeldung.pojo.Customer; - -public class SimpleDataGeneration { - - public static Customer generateData() { - Customer customer = new Customer(); - Calendar cal = Calendar.getInstance(); - cal.set(1986 , 01 , 14); - customer.setDob(cal.getTime()); - customer.setFirstName("XStream"); - customer.setLastName("Java"); - - List contactDetailsList = new ArrayList(); - - ContactDetails contactDetails1 = new ContactDetails(); - contactDetails1.setLandline("0124-2460311"); - contactDetails1.setMobile("6673543265"); - - ContactDetails contactDetails2 = new ContactDetails(); - contactDetails2.setLandline("0120-223312"); - contactDetails2.setMobile("4676543565"); - - contactDetailsList.add(contactDetails1); - contactDetailsList.add(contactDetails2); - - customer.setContactDetailsList(contactDetailsList); - return customer; - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java deleted file mode 100644 index 10f59e843b..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.complex.pojo.Customer; -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.thoughtworks.xstream.XStream; - -public class ComplexXmlToObjectAnnotationTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - Assert.assertNotNull(customer.getContactDetailsList()); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java deleted file mode 100644 index 876a407f76..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.complex.pojo.ContactDetails; -import com.baeldung.complex.pojo.Customer; -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.thoughtworks.xstream.XStream; - -public class ComplexXmlToObjectAttributeCollectionTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - Assert.assertNotNull(customer.getContactDetailsList()); - for(ContactDetails contactDetails : customer.getContactDetailsList()){ - Assert.assertNotNull(contactDetails.getContactType()); - } - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java deleted file mode 100644 index d701fc153b..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.implicit.collection.pojo.Customer; -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.thoughtworks.xstream.XStream; - -public class ComplexXmlToObjectCollectionTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias-implicit-collection.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - Assert.assertNotNull(customer.getContactDetailsList()); - //System.out.println(customer); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java deleted file mode 100644 index cc6d945254..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.baeldung.pojo.Customer; -import com.thoughtworks.xstream.XStream; - -public class XmlToObjectAliasTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.alias("customer" , Customer.class); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java deleted file mode 100644 index f2012e2b12..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.annotation.pojo.Customer; -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.thoughtworks.xstream.XStream; - -public class XmlToObjectAnnotationTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - Assert.assertNotNull(customer.getFirstName()); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java deleted file mode 100644 index c4bba86265..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.baeldung.pojo.Customer; -import com.thoughtworks.xstream.XStream; - -public class XmlToObjectFieldAliasTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.alias("customer" , Customer.class); - xstream.aliasField("fn", Customer.class, "firstName"); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - Assert.assertNotNull(customer.getFirstName()); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java deleted file mode 100644 index cb13bb9570..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.baeldung.pojo.Customer; -import com.thoughtworks.xstream.XStream; - -public class XmlToObjectIgnoreFieldsTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.alias("customer" , Customer.class); - xstream.ignoreUnknownElements(); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - //System.out.println(customer); - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java b/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java deleted file mode 100644 index e24e54aea4..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.pojo.test; - -import java.io.FileReader; -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.baeldung.pojo.Customer; -import com.baeldung.utility.SimpleDataGeneration; -import com.thoughtworks.xstream.XStream; - -public class XmlToObjectTest { - - private XStream xstream = null; - - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - } - - @Test - public void convertXmlToObjectFromFile() { - try { - ClassLoader classLoader = getClass().getClassLoader(); - FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile()); - Customer customer = (Customer) xstream.fromXML(reader); - Assert.assertNotNull(customer); - - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Test - public void convertXmlToObjectFromString() { - Customer customer = SimpleDataGeneration.generateData(); - String dataXml = xstream.toXML(customer); - Customer convertedCustomer = (Customer) xstream.fromXML(dataXml); - Assert.assertNotNull(convertedCustomer); - } - - -} diff --git a/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java b/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java deleted file mode 100644 index ea60e68743..0000000000 --- a/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.baeldung.utility; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.initializer.SimpleXstreamInitializer; -import com.baeldung.pojo.AddressDetails; -import com.baeldung.pojo.ContactDetails; -import com.baeldung.pojo.Customer; -import com.baeldung.utility.SimpleDataGeneration; -import com.thoughtworks.xstream.XStream; - -public class XStreamSimpleXmlTest { - - private Customer customer = null; - - private String dataXml = null; - - private XStream xstream = null; - - @Before - public void dataSetup() { - customer = SimpleDataGeneration.generateData(); - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - xstream.processAnnotations(AddressDetails.class); - xstream.processAnnotations(ContactDetails.class); - xstream.omitField(Customer.class , "lastName"); - xstream.registerConverter(new MyDateConverter()); - // xstream.registerConverter(new MySingleValueConverter()); - xstream.aliasField("fn" , Customer.class , "firstName"); - dataXml = xstream.toXML(customer); - } - - @Test - public void testClassAliasedAnnotation() { - Assert.assertNotEquals(-1 , dataXml.indexOf("")); - } - - @Test - public void testFieldAliasedAnnotation() { - Assert.assertNotEquals(-1 , dataXml.indexOf("")); - } - - @Test - public void testImplicitCollection() { - Assert.assertEquals(-1 , dataXml.indexOf("contactDetailsList")); - } - - @Test - public void testDateFieldFormating() { - Assert.assertEquals("14-02-1986" , dataXml.substring(dataXml.indexOf("") + 5 , dataXml.indexOf(""))); - } - - @Test - public void testOmitField() { - Assert.assertEquals(-1 , dataXml.indexOf("lastName")); - } -} diff --git a/xstream-introduction/src/test/resources/data-file-alias-field-complex.xml b/xstream-introduction/src/test/resources/data-file-alias-field-complex.xml deleted file mode 100644 index 2235aa7044..0000000000 --- a/xstream-introduction/src/test/resources/data-file-alias-field-complex.xml +++ /dev/null @@ -1,15 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:05.874 UTC - - - 6673543265 - 0124-2460311 - - - 4676543565 - 0120-223312 - - - \ No newline at end of file diff --git a/xstream-introduction/src/test/resources/data-file-alias-field.xml b/xstream-introduction/src/test/resources/data-file-alias-field.xml deleted file mode 100644 index 9bc1d0990a..0000000000 --- a/xstream-introduction/src/test/resources/data-file-alias-field.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file diff --git a/xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml b/xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml deleted file mode 100644 index d8731900b9..0000000000 --- a/xstream-introduction/src/test/resources/data-file-alias-implicit-collection.xml +++ /dev/null @@ -1,13 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:20.541 UTC - - 6673543265 - 0124-2460311 - - - 4676543565 - 0120-223312 - - \ No newline at end of file diff --git a/xstream-introduction/src/test/resources/data-file-alias.xml b/xstream-introduction/src/test/resources/data-file-alias.xml deleted file mode 100644 index 964157f1dd..0000000000 --- a/xstream-introduction/src/test/resources/data-file-alias.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file diff --git a/xstream-introduction/src/test/resources/data-file-ignore-field.xml b/xstream-introduction/src/test/resources/data-file-ignore-field.xml deleted file mode 100644 index 865e93d4b4..0000000000 --- a/xstream-introduction/src/test/resources/data-file-ignore-field.xml +++ /dev/null @@ -1,6 +0,0 @@ - - XStream - Java - 1986-02-14 04:14:20.541 UTC - XStream Java - \ No newline at end of file diff --git a/xstream-introduction/src/test/resources/data-file.xml b/xstream-introduction/src/test/resources/data-file.xml deleted file mode 100644 index 59f2ea1cca..0000000000 --- a/xstream-introduction/src/test/resources/data-file.xml +++ /dev/null @@ -1,5 +0,0 @@ - - XStream - Java - 1986-02-14 03:46:16.381 UTC - \ No newline at end of file diff --git a/xstream-introduction/pom.xml b/xstream/pom.xml similarity index 74% rename from xstream-introduction/pom.xml rename to xstream/pom.xml index 2f0f26d6f2..8a5aec41e9 100644 --- a/xstream-introduction/pom.xml +++ b/xstream/pom.xml @@ -27,4 +27,18 @@ + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/xstream/src/main/java/com/baeldung/annotation/pojo/Customer.java b/xstream/src/main/java/com/baeldung/annotation/pojo/Customer.java new file mode 100644 index 0000000000..2cdb0f56c9 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/annotation/pojo/Customer.java @@ -0,0 +1,46 @@ +package com.baeldung.annotation.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +import java.util.Date; + +@XStreamAlias("customer") +public class Customer { + + @XStreamAlias("fn") + private String firstName; + + private String lastName; + + private Date dob; + + 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 Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + "]"; + } +} diff --git a/xstream/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java b/xstream/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java new file mode 100644 index 0000000000..f5b98c9c1b --- /dev/null +++ b/xstream/src/main/java/com/baeldung/annotation/pojo/CustomerOmitField.java @@ -0,0 +1,50 @@ +package com.baeldung.annotation.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamOmitField; + +import java.util.Date; + + +@XStreamAlias("customer") +public class CustomerOmitField { + + @XStreamOmitField + private String firstName; + + private String lastName; + + private Date dob; + + 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 Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + @Override + public String toString() { + return "CustomerOmitAnnotation [firstName=" + firstName + ", lastName=" + + lastName + ", dob=" + dob + "]"; + } + + +} diff --git a/xstream/src/main/java/com/baeldung/complex/pojo/ContactDetails.java b/xstream/src/main/java/com/baeldung/complex/pojo/ContactDetails.java new file mode 100644 index 0000000000..e091492a1a --- /dev/null +++ b/xstream/src/main/java/com/baeldung/complex/pojo/ContactDetails.java @@ -0,0 +1,46 @@ +package com.baeldung.complex.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; + +@XStreamAlias("ContactDetails") +public class ContactDetails { + + private String mobile; + + private String landline; + + @XStreamAsAttribute + private String contactType; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + + public String getContactType() { + return contactType; + } + + public void setContactType(String contactType) { + this.contactType = contactType; + } + + @Override + public String toString() { + return "ContactDetails [mobile=" + mobile + ", landline=" + landline + + ", contactType=" + contactType + "]"; + } + +} diff --git a/xstream/src/main/java/com/baeldung/complex/pojo/Customer.java b/xstream/src/main/java/com/baeldung/complex/pojo/Customer.java new file mode 100644 index 0000000000..c6f98982f0 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/complex/pojo/Customer.java @@ -0,0 +1,57 @@ +package com.baeldung.complex.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +import java.util.Date; +import java.util.List; + +@XStreamAlias("customer") +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + private List contactDetailsList; + + 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 Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + public List getContactDetailsList() { + return contactDetailsList; + } + + public void setContactDetailsList(List contactDetailsList) { + this.contactDetailsList = contactDetailsList; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList + + "]"; + } +} diff --git a/xstream/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java b/xstream/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java new file mode 100644 index 0000000000..38ec7ff077 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/implicit/collection/pojo/ContactDetails.java @@ -0,0 +1,46 @@ +package com.baeldung.implicit.collection.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; + +@XStreamAlias("ContactDetails") +public class ContactDetails { + + private String mobile; + + private String landline; + + @XStreamAsAttribute + private String contactType; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + + public String getContactType() { + return contactType; + } + + public void setContactType(String contactType) { + this.contactType = contactType; + } + + @Override + public String toString() { + return "ContactDetails [mobile=" + mobile + ", landline=" + landline + + ", contactType=" + contactType + "]"; + } + +} diff --git a/xstream/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java b/xstream/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java new file mode 100644 index 0000000000..a50ac850dd --- /dev/null +++ b/xstream/src/main/java/com/baeldung/implicit/collection/pojo/Customer.java @@ -0,0 +1,59 @@ +package com.baeldung.implicit.collection.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamImplicit; + +import java.util.Date; +import java.util.List; + +@XStreamAlias("customer") +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + @XStreamImplicit + private List contactDetailsList; + + 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 Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + public List getContactDetailsList() { + return contactDetailsList; + } + + public void setContactDetailsList(List contactDetailsList) { + this.contactDetailsList = contactDetailsList; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + + ", dob=" + dob + ", contactDetailsList=" + contactDetailsList + + "]"; + } +} diff --git a/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java new file mode 100644 index 0000000000..5dec19d181 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -0,0 +1,19 @@ +package com.baeldung.initializer; + +import com.thoughtworks.xstream.XStream; + +public class SimpleXstreamInitializer { + + private XStream xtreamInstance; + + public XStream getXstreamInstance() { + if (xtreamInstance == null) { + synchronized (SimpleXstreamInitializer.class) { + if (xtreamInstance == null) { + xtreamInstance = new XStream(); + } + } + } + return xtreamInstance; + } +} \ No newline at end of file diff --git a/xstream/src/main/java/com/baeldung/pojo/AddressDetails.java b/xstream/src/main/java/com/baeldung/pojo/AddressDetails.java new file mode 100644 index 0000000000..53ba7e9a85 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/pojo/AddressDetails.java @@ -0,0 +1,40 @@ +package com.baeldung.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +import java.util.List; + +@XStreamAlias("AddressDetails") +public class AddressDetails { + + private String address; + + private String zipcode; + + private List contactDetails; + + public String getZipcode() { + return zipcode; + } + + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } + + public List getContactDetails() { + return contactDetails; + } + + public void setContactDetails(List contactDetails) { + this.contactDetails = contactDetails; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + +} diff --git a/xstream/src/main/java/com/baeldung/pojo/ContactDetails.java b/xstream/src/main/java/com/baeldung/pojo/ContactDetails.java new file mode 100644 index 0000000000..75408bdba8 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/pojo/ContactDetails.java @@ -0,0 +1,28 @@ +package com.baeldung.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +@XStreamAlias("ContactDetails") +public class ContactDetails { + + private String mobile; + + private String landline; + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getLandline() { + return landline; + } + + public void setLandline(String landline) { + this.landline = landline; + } + +} diff --git a/xstream/src/main/java/com/baeldung/pojo/Customer.java b/xstream/src/main/java/com/baeldung/pojo/Customer.java new file mode 100644 index 0000000000..728939c356 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/pojo/Customer.java @@ -0,0 +1,57 @@ +package com.baeldung.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamImplicit; + +import java.util.Date; +import java.util.List; + +@XStreamAlias("customer") +public class Customer { + + private String firstName; + + private String lastName; + + private Date dob; + + @XStreamImplicit + private List contactDetailsList; + + 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 Date getDob() { + return dob; + } + + public void setDob(Date dob) { + this.dob = dob; + } + + public List getContactDetailsList() { + return contactDetailsList; + } + + public void setContactDetailsList(List contactDetailsList) { + this.contactDetailsList = contactDetailsList; + } + + @Override + public String toString() { + return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + "]"; + } +} diff --git a/xstream/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java b/xstream/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java new file mode 100644 index 0000000000..f203c9cce9 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java @@ -0,0 +1,50 @@ +package com.baeldung.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +import java.util.List; + +@XStreamAlias("CustomerAddressDetails") +public class CustomerAddressDetails { + + private List addressDetails; + + private String firstName; + + private String lastName; + + private int age; + + 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 int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + + public List getAddressDetails() { + return addressDetails; + } + + public void setAddressDetails(List addressDetails) { + this.addressDetails = addressDetails; + } +} diff --git a/xstream/src/main/java/com/baeldung/pojo/CustomerPortfolio.java b/xstream/src/main/java/com/baeldung/pojo/CustomerPortfolio.java new file mode 100644 index 0000000000..90722feb71 --- /dev/null +++ b/xstream/src/main/java/com/baeldung/pojo/CustomerPortfolio.java @@ -0,0 +1,20 @@ +package com.baeldung.pojo; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +import java.util.List; + +@XStreamAlias("CustomerPortfolio") +public class CustomerPortfolio { + + private List customerAddressDetailsList; + + public List getCustomerAddressDetailsList() { + return customerAddressDetailsList; + } + + public void setCustomerAddressDetailsList(List customerAddressDetailsList) { + this.customerAddressDetailsList = customerAddressDetailsList; + } + +} diff --git a/xstream/src/main/java/com/baeldung/utility/MyDateConverter.java b/xstream/src/main/java/com/baeldung/utility/MyDateConverter.java new file mode 100644 index 0000000000..af7ca19aac --- /dev/null +++ b/xstream/src/main/java/com/baeldung/utility/MyDateConverter.java @@ -0,0 +1,40 @@ +package com.baeldung.utility; + +import com.thoughtworks.xstream.converters.ConversionException; +import com.thoughtworks.xstream.converters.Converter; +import com.thoughtworks.xstream.converters.MarshallingContext; +import com.thoughtworks.xstream.converters.UnmarshallingContext; +import com.thoughtworks.xstream.io.HierarchicalStreamReader; +import com.thoughtworks.xstream.io.HierarchicalStreamWriter; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.GregorianCalendar; + +public class MyDateConverter implements Converter { + + private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); + + @Override + public boolean canConvert(Class clazz) { + return Date.class.isAssignableFrom(clazz); + } + + @Override + public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext arg2) { + Date date = (Date) value; + writer.setValue(formatter.format(date)); + } + + @Override + public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext arg1) { + GregorianCalendar calendar = new GregorianCalendar(); + try { + calendar.setTime(formatter.parse(reader.getValue())); + } catch (ParseException e) { + throw new ConversionException(e.getMessage(), e); + } + return calendar; + } +} diff --git a/xstream/src/main/java/com/baeldung/utility/MySingleValueConverter.java b/xstream/src/main/java/com/baeldung/utility/MySingleValueConverter.java new file mode 100644 index 0000000000..9b242f1c7c --- /dev/null +++ b/xstream/src/main/java/com/baeldung/utility/MySingleValueConverter.java @@ -0,0 +1,28 @@ +package com.baeldung.utility; + +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.converters.SingleValueConverter; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class MySingleValueConverter implements SingleValueConverter { + + @Override + public boolean canConvert(Class clazz) { + return Customer.class.isAssignableFrom(clazz); + } + + @Override + public Object fromString(String arg0) { + return null; + } + + @Override + public String toString(Object obj) { + SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); + Date date = ((Customer) obj).getDob(); + return ((Customer) obj).getFirstName() + "," + ((Customer) obj).getLastName() + "," + formatter.format(date); + } + +} diff --git a/xstream/src/main/java/com/baeldung/utility/SimpleDataGeneration.java b/xstream/src/main/java/com/baeldung/utility/SimpleDataGeneration.java new file mode 100644 index 0000000000..cf038bfd1b --- /dev/null +++ b/xstream/src/main/java/com/baeldung/utility/SimpleDataGeneration.java @@ -0,0 +1,37 @@ +package com.baeldung.utility; + +import com.baeldung.pojo.ContactDetails; +import com.baeldung.pojo.Customer; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +public class SimpleDataGeneration { + + public static Customer generateData() { + Customer customer = new Customer(); + Calendar cal = Calendar.getInstance(); + cal.set(1986, 01, 14); + customer.setDob(cal.getTime()); + customer.setFirstName("XStream"); + customer.setLastName("Java"); + + List contactDetailsList = new ArrayList(); + + ContactDetails contactDetails1 = new ContactDetails(); + contactDetails1.setLandline("0124-2460311"); + contactDetails1.setMobile("6673543265"); + + ContactDetails contactDetails2 = new ContactDetails(); + contactDetails2.setLandline("0120-223312"); + contactDetails2.setMobile("4676543565"); + + contactDetailsList.add(contactDetails1); + contactDetailsList.add(contactDetails2); + + customer.setContactDetailsList(contactDetailsList); + return customer; + } + +} diff --git a/xstream-introduction/src/main/resources/log4j.properties b/xstream/src/main/resources/log4j.properties similarity index 100% rename from xstream-introduction/src/main/resources/log4j.properties rename to xstream/src/main/resources/log4j.properties diff --git a/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java new file mode 100644 index 0000000000..479500c4a0 --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAnnotationTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import com.baeldung.complex.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class ComplexXmlToObjectAnnotationTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java new file mode 100644 index 0000000000..8c569aa11e --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectAttributeCollectionTest.java @@ -0,0 +1,42 @@ +package com.baeldung.pojo.test; + +import com.baeldung.complex.pojo.ContactDetails; +import com.baeldung.complex.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class ComplexXmlToObjectAttributeCollectionTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + for (ContactDetails contactDetails : customer.getContactDetailsList()) { + Assert.assertNotNull(contactDetails.getContactType()); + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java new file mode 100644 index 0000000000..29ef7a5d64 --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/ComplexXmlToObjectCollectionTest.java @@ -0,0 +1,39 @@ +package com.baeldung.pojo.test; + +import com.baeldung.implicit.collection.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class ComplexXmlToObjectCollectionTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-implicit-collection.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getContactDetailsList()); + //System.out.println(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java new file mode 100644 index 0000000000..8a4de3b70a --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAliasTest.java @@ -0,0 +1,37 @@ +package com.baeldung.pojo.test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class XmlToObjectAliasTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer", Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java new file mode 100644 index 0000000000..4a7ff2f74a --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectAnnotationTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import com.baeldung.annotation.pojo.Customer; +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class XmlToObjectAnnotationTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getFirstName()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java new file mode 100644 index 0000000000..3b1b8326ab --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectFieldAliasTest.java @@ -0,0 +1,39 @@ +package com.baeldung.pojo.test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class XmlToObjectFieldAliasTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer", Customer.class); + xstream.aliasField("fn", Customer.class, "firstName"); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + Assert.assertNotNull(customer.getFirstName()); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java new file mode 100644 index 0000000000..95a034b3e7 --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectIgnoreFieldsTest.java @@ -0,0 +1,38 @@ +package com.baeldung.pojo.test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class XmlToObjectIgnoreFieldsTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.alias("customer", Customer.class); + xstream.ignoreUnknownElements(); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + //System.out.println(customer); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java new file mode 100644 index 0000000000..b6b64ce8da --- /dev/null +++ b/xstream/src/test/java/com/baeldung/pojo/test/XmlToObjectTest.java @@ -0,0 +1,46 @@ +package com.baeldung.pojo.test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.Customer; +import com.baeldung.utility.SimpleDataGeneration; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; + +public class XmlToObjectTest { + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + } + + @Test + public void convertXmlToObjectFromFile() { + try { + ClassLoader classLoader = getClass().getClassLoader(); + FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile()); + Customer customer = (Customer) xstream.fromXML(reader); + Assert.assertNotNull(customer); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void convertXmlToObjectFromString() { + Customer customer = SimpleDataGeneration.generateData(); + String dataXml = xstream.toXML(customer); + Customer convertedCustomer = (Customer) xstream.fromXML(dataXml); + Assert.assertNotNull(convertedCustomer); + } + + +} diff --git a/xstream/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java b/xstream/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java new file mode 100644 index 0000000000..83a965ce1b --- /dev/null +++ b/xstream/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java @@ -0,0 +1,57 @@ +package com.baeldung.utility; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.AddressDetails; +import com.baeldung.pojo.ContactDetails; +import com.baeldung.pojo.Customer; +import com.thoughtworks.xstream.XStream; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class XStreamSimpleXmlTest { + + private Customer customer; + private String dataXml; + private XStream xstream; + + @Before + public void dataSetup() { + customer = SimpleDataGeneration.generateData(); + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamInstance(); + xstream.processAnnotations(Customer.class); + xstream.processAnnotations(AddressDetails.class); + xstream.processAnnotations(ContactDetails.class); + xstream.omitField(Customer.class, "lastName"); + xstream.registerConverter(new MyDateConverter()); + // xstream.registerConverter(new MySingleValueConverter()); + xstream.aliasField("fn", Customer.class, "firstName"); + dataXml = xstream.toXML(customer); + } + + @Test + public void testClassAliasedAnnotation() { + Assert.assertNotEquals(-1, dataXml.indexOf("")); + } + + @Test + public void testFieldAliasedAnnotation() { + Assert.assertNotEquals(-1, dataXml.indexOf("")); + } + + @Test + public void testImplicitCollection() { + Assert.assertEquals(-1, dataXml.indexOf("contactDetailsList")); + } + + @Test + public void testDateFieldFormating() { + Assert.assertEquals("14-02-1986", dataXml.substring(dataXml.indexOf("") + 5, dataXml.indexOf(""))); + } + + @Test + public void testOmitField() { + Assert.assertEquals(-1, dataXml.indexOf("lastName")); + } +} diff --git a/xstream/src/test/resources/data-file-alias-field-complex.xml b/xstream/src/test/resources/data-file-alias-field-complex.xml new file mode 100644 index 0000000000..06050cd1ed --- /dev/null +++ b/xstream/src/test/resources/data-file-alias-field-complex.xml @@ -0,0 +1,15 @@ + + XStream + Java + 1986-02-14 04:14:05.874 UTC + + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + + \ No newline at end of file diff --git a/xstream/src/test/resources/data-file-alias-field.xml b/xstream/src/test/resources/data-file-alias-field.xml new file mode 100644 index 0000000000..7e71d721ca --- /dev/null +++ b/xstream/src/test/resources/data-file-alias-field.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream/src/test/resources/data-file-alias-implicit-collection.xml b/xstream/src/test/resources/data-file-alias-implicit-collection.xml new file mode 100644 index 0000000000..0cb852fc04 --- /dev/null +++ b/xstream/src/test/resources/data-file-alias-implicit-collection.xml @@ -0,0 +1,13 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + + 6673543265 + 0124-2460311 + + + 4676543565 + 0120-223312 + + \ No newline at end of file diff --git a/xstream/src/test/resources/data-file-alias.xml b/xstream/src/test/resources/data-file-alias.xml new file mode 100644 index 0000000000..61ee9f1ac3 --- /dev/null +++ b/xstream/src/test/resources/data-file-alias.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file diff --git a/xstream/src/test/resources/data-file-ignore-field.xml b/xstream/src/test/resources/data-file-ignore-field.xml new file mode 100644 index 0000000000..7dc8023b96 --- /dev/null +++ b/xstream/src/test/resources/data-file-ignore-field.xml @@ -0,0 +1,6 @@ + + XStream + Java + 1986-02-14 04:14:20.541 UTC + XStream Java + \ No newline at end of file diff --git a/xstream/src/test/resources/data-file.xml b/xstream/src/test/resources/data-file.xml new file mode 100644 index 0000000000..b8dbce32c0 --- /dev/null +++ b/xstream/src/test/resources/data-file.xml @@ -0,0 +1,5 @@ + + XStream + Java + 1986-02-14 03:46:16.381 UTC + \ No newline at end of file