Clean up XStream examples

This commit is contained in:
David Morley
2016-04-04 05:18:35 -05:00
parent dd61036a31
commit ed629e7a49
63 changed files with 1060 additions and 1072 deletions
@@ -0,0 +1,38 @@
package com.baeldung.pojo.test;
import com.baeldung.complex.pojo.Customer;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.thoughtworks.xstream.XStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.FileReader;
import java.io.IOException;
public class ComplexXmlToObjectAnnotationTest {
private XStream xstream = null;
@Before
public void dataSetup() {
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamInstance();
xstream.processAnnotations(Customer.class);
}
@Test
public void convertXmlToObjectFromFile() {
try {
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());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,42 @@
package com.baeldung.pojo.test;
import com.baeldung.complex.pojo.ContactDetails;
import com.baeldung.complex.pojo.Customer;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.thoughtworks.xstream.XStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.FileReader;
import java.io.IOException;
public class ComplexXmlToObjectAttributeCollectionTest {
private XStream xstream = null;
@Before
public void dataSetup() {
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamInstance();
xstream.processAnnotations(Customer.class);
}
@Test
public void convertXmlToObjectFromFile() {
try {
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());
for (ContactDetails contactDetails : customer.getContactDetailsList()) {
Assert.assertNotNull(contactDetails.getContactType());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,39 @@
package com.baeldung.pojo.test;
import com.baeldung.implicit.collection.pojo.Customer;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.thoughtworks.xstream.XStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.FileReader;
import java.io.IOException;
public class ComplexXmlToObjectCollectionTest {
private XStream xstream = null;
@Before
public void dataSetup() {
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamInstance();
xstream.processAnnotations(Customer.class);
}
@Test
public void convertXmlToObjectFromFile() {
try {
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());
//System.out.println(customer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,37 @@
package com.baeldung.pojo.test;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.baeldung.pojo.Customer;
import com.thoughtworks.xstream.XStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.FileReader;
import java.io.IOException;
public class XmlToObjectAliasTest {
private XStream xstream = null;
@Before
public void dataSetup() {
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamInstance();
xstream.alias("customer", Customer.class);
}
@Test
public void convertXmlToObjectFromFile() {
try {
ClassLoader classLoader = getClass().getClassLoader();
FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile());
Customer customer = (Customer) xstream.fromXML(reader);
Assert.assertNotNull(customer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,38 @@
package com.baeldung.pojo.test;
import com.baeldung.annotation.pojo.Customer;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.thoughtworks.xstream.XStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.FileReader;
import java.io.IOException;
public class XmlToObjectAnnotationTest {
private XStream xstream = null;
@Before
public void dataSetup() {
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamInstance();
xstream.processAnnotations(Customer.class);
}
@Test
public void convertXmlToObjectFromFile() {
try {
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());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,39 @@
package com.baeldung.pojo.test;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.baeldung.pojo.Customer;
import com.thoughtworks.xstream.XStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.FileReader;
import java.io.IOException;
public class XmlToObjectFieldAliasTest {
private XStream xstream = null;
@Before
public void dataSetup() {
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamInstance();
xstream.alias("customer", Customer.class);
xstream.aliasField("fn", Customer.class, "firstName");
}
@Test
public void convertXmlToObjectFromFile() {
try {
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());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,38 @@
package com.baeldung.pojo.test;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.baeldung.pojo.Customer;
import com.thoughtworks.xstream.XStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.FileReader;
import java.io.IOException;
public class XmlToObjectIgnoreFieldsTest {
private XStream xstream = null;
@Before
public void dataSetup() {
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamInstance();
xstream.alias("customer", Customer.class);
xstream.ignoreUnknownElements();
}
@Test
public void convertXmlToObjectFromFile() {
try {
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);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,46 @@
package com.baeldung.pojo.test;
import com.baeldung.initializer.SimpleXstreamInitializer;
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;
import java.io.FileReader;
import java.io.IOException;
public class XmlToObjectTest {
private XStream xstream = null;
@Before
public void dataSetup() {
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamInstance();
}
@Test
public void convertXmlToObjectFromFile() {
try {
ClassLoader classLoader = getClass().getClassLoader();
FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile());
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);
}
}
@@ -0,0 +1,57 @@
package com.baeldung.utility;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.baeldung.pojo.AddressDetails;
import com.baeldung.pojo.ContactDetails;
import com.baeldung.pojo.Customer;
import com.thoughtworks.xstream.XStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class XStreamSimpleXmlTest {
private Customer customer;
private String dataXml;
private XStream xstream;
@Before
public void dataSetup() {
customer = SimpleDataGeneration.generateData();
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
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);
}
@Test
public void testClassAliasedAnnotation() {
Assert.assertNotEquals(-1, dataXml.indexOf("<customer>"));
}
@Test
public void testFieldAliasedAnnotation() {
Assert.assertNotEquals(-1, dataXml.indexOf("<fn>"));
}
@Test
public void testImplicitCollection() {
Assert.assertEquals(-1, dataXml.indexOf("contactDetailsList"));
}
@Test
public void testDateFieldFormating() {
Assert.assertEquals("14-02-1986", dataXml.substring(dataXml.indexOf("<dob>") + 5, dataXml.indexOf("</dob>")));
}
@Test
public void testOmitField() {
Assert.assertEquals(-1, dataXml.indexOf("lastName"));
}
}