Xstream - XML to Object

xml moved to resources folder, test cases fixed
This commit is contained in:
ankur-singhal 2016-04-01 17:43:37 +05:30
parent f7594ee89c
commit f214e0a285
16 changed files with 97 additions and 49 deletions

View File

@ -1,23 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>xstream-xmlToObject</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
<name>xstream-xmlToObject</name>
<comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -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) {

View File

@ -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());

View File

@ -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());

View File

@ -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());

View File

@ -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);

View File

@ -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());

View File

@ -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());

View File

@ -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);

View File

@ -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);

View File

@ -0,0 +1,15 @@
<customer>
<firstName>XStream</firstName>
<lastName>Java</lastName>
<dob>1986-02-14 04:14:05.874 UTC</dob>
<contactDetailsList>
<ContactDetails contactType="Home">
<mobile>6673543265</mobile>
<landline>0124-2460311</landline>
</ContactDetails>
<ContactDetails contactType="Office">
<mobile>4676543565</mobile>
<landline>0120-223312</landline>
</ContactDetails>
</contactDetailsList>
</customer>

View File

@ -0,0 +1,5 @@
<customer>
<fn>XStream</fn>
<lastName>Java</lastName>
<dob>1986-02-14 03:46:16.381 UTC</dob>
</customer>

View File

@ -0,0 +1,13 @@
<customer>
<firstName>XStream</firstName>
<lastName>Java</lastName>
<dob>1986-02-14 04:14:20.541 UTC</dob>
<ContactDetails contactType="Home">
<mobile>6673543265</mobile>
<landline>0124-2460311</landline>
</ContactDetails>
<ContactDetails contactType="Office">
<mobile>4676543565</mobile>
<landline>0120-223312</landline>
</ContactDetails>
</customer>

View File

@ -0,0 +1,5 @@
<customer>
<firstName>XStream</firstName>
<lastName>Java</lastName>
<dob>1986-02-14 03:46:16.381 UTC</dob>
</customer>

View File

@ -0,0 +1,6 @@
<customer>
<firstName>XStream</firstName>
<lastName>Java</lastName>
<dob>1986-02-14 04:14:20.541 UTC</dob>
<fullName>XStream Java</fullName>
</customer>

View File

@ -0,0 +1,5 @@
<com.baeldung.pojo.Customer>
<firstName>XStream</firstName>
<lastName>Java</lastName>
<dob>1986-02-14 03:46:16.381 UTC</dob>
</com.baeldung.pojo.Customer>