From 452070aa354a24f82583260048fa3a7935f6ce3a Mon Sep 17 00:00:00 2001 From: ankur-singhal Date: Sun, 20 Mar 2016 20:46:58 +0530 Subject: [PATCH] XStream- Object to Xml Closing Review comments, test cases added. --- xstream-introduction/.classpath | 15 ----- .../initializer/SimpleXstreamInitializer.java | 2 +- .../baeldung/pojo/AddressDetails.java | 2 +- .../baeldung/pojo/ContactDetails.java | 2 +- .../{org => com}/baeldung/pojo/Customer.java | 4 +- .../baeldung/pojo/CustomerAddressDetails.java | 2 +- .../baeldung/pojo/CustomerPortfolio.java | 2 +- .../baeldung/utility/MyDateConverter.java | 2 +- .../utility/MySingleValueConverter.java | 5 +- .../utility/SimpleDataGeneration.java | 6 +- .../utility/XStreamSimpleXmlTest.java | 62 +++++++++++++++++++ .../utility/XStreamSimpleXmlTest.java | 46 -------------- 12 files changed, 75 insertions(+), 75 deletions(-) delete mode 100644 xstream-introduction/.classpath rename xstream-introduction/src/main/java/{org => com}/baeldung/initializer/SimpleXstreamInitializer.java (91%) rename xstream-introduction/src/main/java/{org => com}/baeldung/pojo/AddressDetails.java (96%) rename xstream-introduction/src/main/java/{org => com}/baeldung/pojo/ContactDetails.java (94%) rename xstream-introduction/src/main/java/{org => com}/baeldung/pojo/Customer.java (95%) rename xstream-introduction/src/main/java/{org => com}/baeldung/pojo/CustomerAddressDetails.java (96%) rename xstream-introduction/src/main/java/{org => com}/baeldung/pojo/CustomerPortfolio.java (94%) rename xstream-introduction/src/main/java/{org => com}/baeldung/utility/MyDateConverter.java (97%) rename xstream-introduction/src/main/java/{org => com}/baeldung/utility/MySingleValueConverter.java (90%) rename xstream-introduction/src/main/java/{org => com}/baeldung/utility/SimpleDataGeneration.java (89%) create mode 100644 xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java delete mode 100644 xstream-introduction/src/test/java/org/baeldung/utility/XStreamSimpleXmlTest.java diff --git a/xstream-introduction/.classpath b/xstream-introduction/.classpath deleted file mode 100644 index 138df490af..0000000000 --- a/xstream-introduction/.classpath +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/xstream-introduction/src/main/java/org/baeldung/initializer/SimpleXstreamInitializer.java b/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java similarity index 91% rename from xstream-introduction/src/main/java/org/baeldung/initializer/SimpleXstreamInitializer.java rename to xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java index 59912539ca..618df877b9 100644 --- a/xstream-introduction/src/main/java/org/baeldung/initializer/SimpleXstreamInitializer.java +++ b/xstream-introduction/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -1,4 +1,4 @@ -package org.baeldung.initializer; +package com.baeldung.initializer; import com.thoughtworks.xstream.XStream; diff --git a/xstream-introduction/src/main/java/org/baeldung/pojo/AddressDetails.java b/xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java similarity index 96% rename from xstream-introduction/src/main/java/org/baeldung/pojo/AddressDetails.java rename to xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java index 16930cb8ab..e9e30bf5fc 100644 --- a/xstream-introduction/src/main/java/org/baeldung/pojo/AddressDetails.java +++ b/xstream-introduction/src/main/java/com/baeldung/pojo/AddressDetails.java @@ -1,4 +1,4 @@ -package org.baeldung.pojo; +package com.baeldung.pojo; import java.util.List; diff --git a/xstream-introduction/src/main/java/org/baeldung/pojo/ContactDetails.java b/xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java similarity index 94% rename from xstream-introduction/src/main/java/org/baeldung/pojo/ContactDetails.java rename to xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java index 1cb353414b..66475b9d8e 100644 --- a/xstream-introduction/src/main/java/org/baeldung/pojo/ContactDetails.java +++ b/xstream-introduction/src/main/java/com/baeldung/pojo/ContactDetails.java @@ -1,4 +1,4 @@ -package org.baeldung.pojo; +package com.baeldung.pojo; import com.thoughtworks.xstream.annotations.XStreamAlias; diff --git a/xstream-introduction/src/main/java/org/baeldung/pojo/Customer.java b/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java similarity index 95% rename from xstream-introduction/src/main/java/org/baeldung/pojo/Customer.java rename to xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java index aeb6e0aaf3..2ed11dcdab 100644 --- a/xstream-introduction/src/main/java/org/baeldung/pojo/Customer.java +++ b/xstream-introduction/src/main/java/com/baeldung/pojo/Customer.java @@ -1,4 +1,4 @@ -package org.baeldung.pojo; +package com.baeldung.pojo; import java.util.Date; import java.util.List; @@ -10,7 +10,7 @@ import com.thoughtworks.xstream.annotations.XStreamOmitField; @XStreamAlias("customer") public class Customer { - @XStreamOmitField + //@XStreamOmitField private String firstName; private String lastName; diff --git a/xstream-introduction/src/main/java/org/baeldung/pojo/CustomerAddressDetails.java b/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java similarity index 96% rename from xstream-introduction/src/main/java/org/baeldung/pojo/CustomerAddressDetails.java rename to xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java index e4ce5ae5cd..30fda1b92c 100644 --- a/xstream-introduction/src/main/java/org/baeldung/pojo/CustomerAddressDetails.java +++ b/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerAddressDetails.java @@ -1,4 +1,4 @@ -package org.baeldung.pojo; +package com.baeldung.pojo; import java.util.List; diff --git a/xstream-introduction/src/main/java/org/baeldung/pojo/CustomerPortfolio.java b/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java similarity index 94% rename from xstream-introduction/src/main/java/org/baeldung/pojo/CustomerPortfolio.java rename to xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java index cc65128650..6f1ce4b651 100644 --- a/xstream-introduction/src/main/java/org/baeldung/pojo/CustomerPortfolio.java +++ b/xstream-introduction/src/main/java/com/baeldung/pojo/CustomerPortfolio.java @@ -1,4 +1,4 @@ -package org.baeldung.pojo; +package com.baeldung.pojo; import java.util.List; diff --git a/xstream-introduction/src/main/java/org/baeldung/utility/MyDateConverter.java b/xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java similarity index 97% rename from xstream-introduction/src/main/java/org/baeldung/utility/MyDateConverter.java rename to xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java index a11b58bd12..564a28d1c5 100644 --- a/xstream-introduction/src/main/java/org/baeldung/utility/MyDateConverter.java +++ b/xstream-introduction/src/main/java/com/baeldung/utility/MyDateConverter.java @@ -1,4 +1,4 @@ -package org.baeldung.utility; +package com.baeldung.utility; import java.text.ParseException; import java.text.SimpleDateFormat; diff --git a/xstream-introduction/src/main/java/org/baeldung/utility/MySingleValueConverter.java b/xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java similarity index 90% rename from xstream-introduction/src/main/java/org/baeldung/utility/MySingleValueConverter.java rename to xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java index 0cabc4fe03..358d647835 100644 --- a/xstream-introduction/src/main/java/org/baeldung/utility/MySingleValueConverter.java +++ b/xstream-introduction/src/main/java/com/baeldung/utility/MySingleValueConverter.java @@ -1,10 +1,9 @@ -package org.baeldung.utility; +package com.baeldung.utility; import java.text.SimpleDateFormat; import java.util.Date; -import org.baeldung.pojo.Customer; - +import com.baeldung.pojo.Customer; import com.thoughtworks.xstream.converters.SingleValueConverter; public class MySingleValueConverter implements SingleValueConverter { diff --git a/xstream-introduction/src/main/java/org/baeldung/utility/SimpleDataGeneration.java b/xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java similarity index 89% rename from xstream-introduction/src/main/java/org/baeldung/utility/SimpleDataGeneration.java rename to xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java index 2b2a7a8bb8..22d0f0ce77 100644 --- a/xstream-introduction/src/main/java/org/baeldung/utility/SimpleDataGeneration.java +++ b/xstream-introduction/src/main/java/com/baeldung/utility/SimpleDataGeneration.java @@ -1,11 +1,11 @@ -package org.baeldung.utility; +package com.baeldung.utility; import java.util.ArrayList; import java.util.Calendar; import java.util.List; -import org.baeldung.pojo.ContactDetails; -import org.baeldung.pojo.Customer; +import com.baeldung.pojo.ContactDetails; +import com.baeldung.pojo.Customer; public class SimpleDataGeneration { diff --git a/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java b/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java new file mode 100644 index 0000000000..50d02528bd --- /dev/null +++ b/xstream-introduction/src/test/java/com/baeldung/utility/XStreamSimpleXmlTest.java @@ -0,0 +1,62 @@ +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(); + 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); + System.out.println(dataXml); + } + + @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/java/org/baeldung/utility/XStreamSimpleXmlTest.java b/xstream-introduction/src/test/java/org/baeldung/utility/XStreamSimpleXmlTest.java deleted file mode 100644 index 57d0bd2b55..0000000000 --- a/xstream-introduction/src/test/java/org/baeldung/utility/XStreamSimpleXmlTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.baeldung.utility; - -import org.baeldung.initializer.SimpleXstreamInitializer; -import org.baeldung.pojo.AddressDetails; -import org.baeldung.pojo.ContactDetails; -import org.baeldung.pojo.Customer; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -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(); - xstream = SimpleXstreamInitializer.getXstreamInstance(); - xstream.processAnnotations(Customer.class); - xstream.processAnnotations(AddressDetails.class); - xstream.processAnnotations(ContactDetails.class); - xstream.omitField(Customer.class , "firstName"); - xstream.registerConverter(new MyDateConverter()); - //xtream.registerConverter(new MySingleValueConverter()); - xstream.aliasField("fn", Customer.class, "firstName"); - - dataXml = xstream.toXML(customer); - System.out.println(dataXml); - } - - @Test - public void convertDataToXml() { - Assert.assertNotNull(dataXml); - } - - @Test - public void convertXmlToObject() { - customer = (Customer) xstream.fromXML(dataXml); - Assert.assertNotNull(customer); - } - -}