XStream- Object to Xml

Closing Review comments, test cases added.
This commit is contained in:
ankur-singhal 2016-03-20 20:46:58 +05:30
parent bd2afce4d6
commit 452070aa35
12 changed files with 75 additions and 75 deletions

View File

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**/*.java" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry including="**/*.java" kind="src" path="src/main/java"/>
<classpathentry excluding="**/*.java" kind="src" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="M2_REPO/com/thoughtworks/xstream/xstream/1.4.5/xstream-1.4.5.jar" sourcepath="/M2_REPO/com/thoughtworks/xstream/xstream/1.4.5/xstream-1.4.5-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar"/>
<classpathentry kind="var" path="M2_REPO/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar" sourcepath="M2_REPO/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/4.12/junit-4.12.jar"/>
<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar"/>
<classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.17/log4j-1.2.17.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,4 +1,4 @@
package org.baeldung.initializer;
package com.baeldung.initializer;
import com.thoughtworks.xstream.XStream;

View File

@ -1,4 +1,4 @@
package org.baeldung.pojo;
package com.baeldung.pojo;
import java.util.List;

View File

@ -1,4 +1,4 @@
package org.baeldung.pojo;
package com.baeldung.pojo;
import com.thoughtworks.xstream.annotations.XStreamAlias;

View File

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

View File

@ -1,4 +1,4 @@
package org.baeldung.pojo;
package com.baeldung.pojo;
import java.util.List;

View File

@ -1,4 +1,4 @@
package org.baeldung.utility;
package com.baeldung.utility;
import java.text.ParseException;
import java.text.SimpleDateFormat;

View File

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

View File

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

View File

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

View File

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