From ab3f68f1c29fed04560ba8a2eece66f403a44406 Mon Sep 17 00:00:00 2001 From: ankur-singhal Date: Fri, 8 Apr 2016 21:03:51 +0530 Subject: [PATCH 1/3] Test Cases - XStream Object to-from json Test Cases - XStream Object to-from json --- xstream/pom.xml | 6 +++ .../initializer/SimpleXstreamInitializer.java | 26 +++++----- .../baeldung/test/XStreamJettisonTest.java | 48 +++++++++++++++++++ .../test/XStreamJsonHierarchicalTest.java | 45 +++++++++++++++++ 4 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 xstream/src/test/java/com/baeldung/test/XStreamJettisonTest.java create mode 100644 xstream/src/test/java/com/baeldung/test/XStreamJsonHierarchicalTest.java diff --git a/xstream/pom.xml b/xstream/pom.xml index 8a5aec41e9..22c2ce2941 100644 --- a/xstream/pom.xml +++ b/xstream/pom.xml @@ -14,6 +14,12 @@ 1.4.5 + + org.codehaus.jettison + jettison + 1.3.7 + + junit junit diff --git a/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java index 5dec19d181..744b60f78e 100644 --- a/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -1,19 +1,23 @@ package com.baeldung.initializer; import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; +import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver; public class SimpleXstreamInitializer { - private XStream xtreamInstance; + public XStream getXstreamInstance() { + XStream xtreamInstance = new XStream(); + return xtreamInstance; + } - public XStream getXstreamInstance() { - if (xtreamInstance == null) { - synchronized (SimpleXstreamInitializer.class) { - if (xtreamInstance == null) { - xtreamInstance = new XStream(); - } - } - } - return xtreamInstance; - } + public XStream getXstreamJettisonMappedInstance() { + XStream xstreamInstance = new XStream(new JettisonMappedXmlDriver()); + return xstreamInstance; + } + + public XStream getXstreamJsonHierarchicalInstance() { + XStream xstreamInstance = new XStream(new JsonHierarchicalStreamDriver()); + return xstreamInstance; + } } \ No newline at end of file diff --git a/xstream/src/test/java/com/baeldung/test/XStreamJettisonTest.java b/xstream/src/test/java/com/baeldung/test/XStreamJettisonTest.java new file mode 100644 index 0000000000..6111dfbb24 --- /dev/null +++ b/xstream/src/test/java/com/baeldung/test/XStreamJettisonTest.java @@ -0,0 +1,48 @@ +package com.baeldung.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.ContactDetails; +import com.baeldung.pojo.Customer; +import com.baeldung.utility.SimpleDataGeneration; +import com.thoughtworks.xstream.XStream; + +public class XStreamJettisonTest { + + private Customer customer = null; + + private String dataJson = null; + + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamJettisonMappedInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertObjectToJson() { + customer = SimpleDataGeneration.generateData(); + xstream.alias("customer" , Customer.class); + xstream.alias("contactDetails" , ContactDetails.class); + xstream.aliasField("fn" , Customer.class , "firstName"); + dataJson = xstream.toXML(customer); + System.out.println(dataJson); + Assert.assertNotNull(dataJson); + } + + @Test + public void convertJsonToObject() { + customer = SimpleDataGeneration.generateData(); + dataJson = xstream.toXML(customer); + customer = (Customer) xstream.fromXML(dataJson); + System.out.println(customer); + Assert.assertNotNull(customer); + } + +} diff --git a/xstream/src/test/java/com/baeldung/test/XStreamJsonHierarchicalTest.java b/xstream/src/test/java/com/baeldung/test/XStreamJsonHierarchicalTest.java new file mode 100644 index 0000000000..f92faa7196 --- /dev/null +++ b/xstream/src/test/java/com/baeldung/test/XStreamJsonHierarchicalTest.java @@ -0,0 +1,45 @@ +package com.baeldung.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.initializer.SimpleXstreamInitializer; +import com.baeldung.pojo.ContactDetails; +import com.baeldung.pojo.Customer; +import com.baeldung.utility.SimpleDataGeneration; +import com.thoughtworks.xstream.XStream; + +public class XStreamJsonHierarchicalTest { + + private Customer customer = null; + private String dataJson = null; + private XStream xstream = null; + + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamJsonHierarchicalInstance(); + xstream.processAnnotations(Customer.class); + } + + @Test + public void convertObjectToJson() { + customer = SimpleDataGeneration.generateData(); + xstream.alias("customer", Customer.class); + xstream.alias("contactDetails", ContactDetails.class); + xstream.aliasField("fn", Customer.class, "firstName"); + dataJson = xstream.toXML(customer); + System.out.println(dataJson); + Assert.assertNotNull(dataJson); + } + + @Test + public void convertJsonToObject() { + customer = SimpleDataGeneration.generateData(); + dataJson = xstream.toXML(customer); + // customer = (Customer) xstream.fromXML(dataJson); + // Assert.assertNotNull(customer); + } + +} From 44c327d1a36f3a83c9bf20bf9c8e72363e005fb6 Mon Sep 17 00:00:00 2001 From: David Morley Date: Mon, 11 Apr 2016 05:26:22 -0500 Subject: [PATCH 2/3] Add expected exception handling to test --- .../test/XStreamJsonHierarchicalTest.java | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/xstream/src/test/java/com/baeldung/test/XStreamJsonHierarchicalTest.java b/xstream/src/test/java/com/baeldung/test/XStreamJsonHierarchicalTest.java index f92faa7196..1e71cb7066 100644 --- a/xstream/src/test/java/com/baeldung/test/XStreamJsonHierarchicalTest.java +++ b/xstream/src/test/java/com/baeldung/test/XStreamJsonHierarchicalTest.java @@ -1,45 +1,44 @@ package com.baeldung.test; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - import com.baeldung.initializer.SimpleXstreamInitializer; import com.baeldung.pojo.ContactDetails; 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; public class XStreamJsonHierarchicalTest { - private Customer customer = null; - private String dataJson = null; - private XStream xstream = null; + private Customer customer = null; + private String dataJson = null; + private XStream xstream = null; - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamJsonHierarchicalInstance(); - xstream.processAnnotations(Customer.class); - } + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamJsonHierarchicalInstance(); + xstream.processAnnotations(Customer.class); + } - @Test - public void convertObjectToJson() { - customer = SimpleDataGeneration.generateData(); - xstream.alias("customer", Customer.class); - xstream.alias("contactDetails", ContactDetails.class); - xstream.aliasField("fn", Customer.class, "firstName"); - dataJson = xstream.toXML(customer); - System.out.println(dataJson); - Assert.assertNotNull(dataJson); - } + @Test + public void convertObjectToJson() { + customer = SimpleDataGeneration.generateData(); + xstream.alias("customer", Customer.class); + xstream.alias("contactDetails", ContactDetails.class); + xstream.aliasField("fn", Customer.class, "firstName"); + dataJson = xstream.toXML(customer); + System.out.println(dataJson); + Assert.assertNotNull(dataJson); + } - @Test - public void convertJsonToObject() { - customer = SimpleDataGeneration.generateData(); - dataJson = xstream.toXML(customer); - // customer = (Customer) xstream.fromXML(dataJson); - // Assert.assertNotNull(customer); - } + @Test(expected = UnsupportedOperationException.class) + public void convertJsonToObject() { + customer = SimpleDataGeneration.generateData(); + dataJson = xstream.toXML(customer); + customer = (Customer) xstream.fromXML(dataJson); + Assert.assertNotNull(customer); + } } From ce2891ed68adad0c96665f43509183394eecbcba Mon Sep 17 00:00:00 2001 From: David Morley Date: Mon, 11 Apr 2016 05:26:50 -0500 Subject: [PATCH 3/3] Clean up formatting --- xstream/pom.xml | 88 +++++++++---------- .../initializer/SimpleXstreamInitializer.java | 24 ++--- xstream/src/main/resources/log4j.properties | 2 - .../baeldung/test/XStreamJettisonTest.java | 61 +++++++------ 4 files changed, 86 insertions(+), 89 deletions(-) diff --git a/xstream/pom.xml b/xstream/pom.xml index 22c2ce2941..f505019d71 100644 --- a/xstream/pom.xml +++ b/xstream/pom.xml @@ -1,50 +1,50 @@ - 4.0.0 - org.baeldung - xstream-introduction - 0.0.1-SNAPSHOT - xstream-introduction - An Introduction To XStream + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + org.baeldung + xstream-introduction + 0.0.1-SNAPSHOT + xstream-introduction + An Introduction To XStream - - - com.thoughtworks.xstream - xstream - 1.4.5 - + + + com.thoughtworks.xstream + xstream + 1.4.5 + - - org.codehaus.jettison - jettison - 1.3.7 - - - - junit - junit - 4.12 - - - - log4j - log4j - 1.2.17 - - + + org.codehaus.jettison + jettison + 1.3.7 + - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - 1.8 - 1.8 - - - - + + junit + junit + 4.12 + + + + log4j + log4j + 1.2.17 + + + + + + + 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/initializer/SimpleXstreamInitializer.java b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java index 744b60f78e..a15bea5481 100644 --- a/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java +++ b/xstream/src/main/java/com/baeldung/initializer/SimpleXstreamInitializer.java @@ -6,18 +6,18 @@ import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver; public class SimpleXstreamInitializer { - public XStream getXstreamInstance() { - XStream xtreamInstance = new XStream(); - return xtreamInstance; - } + public XStream getXstreamInstance() { + XStream xtreamInstance = new XStream(); + return xtreamInstance; + } - public XStream getXstreamJettisonMappedInstance() { - XStream xstreamInstance = new XStream(new JettisonMappedXmlDriver()); - return xstreamInstance; - } + public XStream getXstreamJettisonMappedInstance() { + XStream xstreamInstance = new XStream(new JettisonMappedXmlDriver()); + return xstreamInstance; + } - public XStream getXstreamJsonHierarchicalInstance() { - XStream xstreamInstance = new XStream(new JsonHierarchicalStreamDriver()); - return xstreamInstance; - } + public XStream getXstreamJsonHierarchicalInstance() { + XStream xstreamInstance = new XStream(new JsonHierarchicalStreamDriver()); + return xstreamInstance; + } } \ No newline at end of file diff --git a/xstream/src/main/resources/log4j.properties b/xstream/src/main/resources/log4j.properties index 9cdafc6bdb..03d8c51aa0 100644 --- a/xstream/src/main/resources/log4j.properties +++ b/xstream/src/main/resources/log4j.properties @@ -1,12 +1,10 @@ # 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 diff --git a/xstream/src/test/java/com/baeldung/test/XStreamJettisonTest.java b/xstream/src/test/java/com/baeldung/test/XStreamJettisonTest.java index 6111dfbb24..f37605cc98 100644 --- a/xstream/src/test/java/com/baeldung/test/XStreamJettisonTest.java +++ b/xstream/src/test/java/com/baeldung/test/XStreamJettisonTest.java @@ -1,48 +1,47 @@ package com.baeldung.test; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - import com.baeldung.initializer.SimpleXstreamInitializer; import com.baeldung.pojo.ContactDetails; 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; public class XStreamJettisonTest { - private Customer customer = null; + private Customer customer = null; - private String dataJson = null; + private String dataJson = null; - private XStream xstream = null; + private XStream xstream = null; - @Before - public void dataSetup() { - SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); - xstream = simpleXstreamInitializer.getXstreamJettisonMappedInstance(); - xstream.processAnnotations(Customer.class); - } + @Before + public void dataSetup() { + SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer(); + xstream = simpleXstreamInitializer.getXstreamJettisonMappedInstance(); + xstream.processAnnotations(Customer.class); + } - @Test - public void convertObjectToJson() { - customer = SimpleDataGeneration.generateData(); - xstream.alias("customer" , Customer.class); - xstream.alias("contactDetails" , ContactDetails.class); - xstream.aliasField("fn" , Customer.class , "firstName"); - dataJson = xstream.toXML(customer); - System.out.println(dataJson); - Assert.assertNotNull(dataJson); - } + @Test + public void convertObjectToJson() { + customer = SimpleDataGeneration.generateData(); + xstream.alias("customer", Customer.class); + xstream.alias("contactDetails", ContactDetails.class); + xstream.aliasField("fn", Customer.class, "firstName"); + dataJson = xstream.toXML(customer); + System.out.println(dataJson); + Assert.assertNotNull(dataJson); + } - @Test - public void convertJsonToObject() { - customer = SimpleDataGeneration.generateData(); - dataJson = xstream.toXML(customer); - customer = (Customer) xstream.fromXML(dataJson); - System.out.println(customer); - Assert.assertNotNull(customer); - } + @Test + public void convertJsonToObject() { + customer = SimpleDataGeneration.generateData(); + dataJson = xstream.toXML(customer); + customer = (Customer) xstream.fromXML(dataJson); + System.out.println(customer); + Assert.assertNotNull(customer); + } }