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); + } + + +}