Test Cases - XStream Object to-from json
Test Cases - XStream Object to-from json
This commit is contained in:
parent
63f1bd7665
commit
ab3f68f1c2
|
@ -14,6 +14,12 @@
|
|||
<version>1.4.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jettison</groupId>
|
||||
<artifactId>jettison</artifactId>
|
||||
<version>1.3.7</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue