Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zeshuai007 committed Mar 18, 2020
1 parent 651b48b commit a29a9b7
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 16 deletions.
22 changes: 22 additions & 0 deletions xstream/src/test/com/thoughtworks/acceptance/XStreamerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.XStreamer;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.thoughtworks.xstream.security.TypePermission;


Expand Down Expand Up @@ -88,6 +89,27 @@ public void testCanSerializeSelfContained() throws ClassNotFoundException, Objec
assertEquals(oos, new XStreamer().fromXML(xml));
}

public void testCanSerializeSelfContainedAndUsePermissions() throws ClassNotFoundException, ObjectStreamException {
final OpenSourceSoftware oos = new OpenSourceSoftware("Walnes", "XStream", "BSD");
xstream.alias("software", OpenSourceSoftware.class);
final String xml = new XStreamer().toXML(xstream, oos);
assertEquals(oos, new XStreamer().fromXML(xml, XStreamer.getDefaultPermissions()));
}

public void testCanSerializeSelfContainedAndUseNewDriver() throws ClassNotFoundException, ObjectStreamException {
final OpenSourceSoftware oos = new OpenSourceSoftware("Walnes", "XStream", "BSD");
xstream.alias("software", OpenSourceSoftware.class);
final String xml = new XStreamer().toXML(xstream, oos);
assertEquals(oos, new XStreamer().fromXML(new StaxDriver(), xml));
}

public void testCanSerializeSelfContainedUsePermissionAndNewDriver() throws ClassNotFoundException, ObjectStreamException {
final OpenSourceSoftware oos = new OpenSourceSoftware("Walnes", "XStream", "BSD");
xstream.alias("software", OpenSourceSoftware.class);
final String xml = new XStreamer().toXML(xstream, oos);
assertEquals(oos, new XStreamer().fromXML(new StaxDriver(), xml, XStreamer.getDefaultPermissions()));
}

private String normalizedXStreamXML(final String xml) throws TransformerException {
final StringWriter writer = new StringWriter();
transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(writer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.ArrayList;

import com.thoughtworks.acceptance.AbstractAcceptanceTest;
Expand All @@ -22,22 +25,7 @@
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
import com.thoughtworks.xstream.io.binary.BinaryStreamDriver;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.xml.BEAStaxDriver;
import com.thoughtworks.xstream.io.xml.Dom4JDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.JDom2Driver;
import com.thoughtworks.xstream.io.xml.JDomDriver;
import com.thoughtworks.xstream.io.xml.KXml2DomDriver;
import com.thoughtworks.xstream.io.xml.KXml2Driver;
import com.thoughtworks.xstream.io.xml.SimpleStaxDriver;
import com.thoughtworks.xstream.io.xml.StandardStaxDriver;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.thoughtworks.xstream.io.xml.WstxDriver;
import com.thoughtworks.xstream.io.xml.XomDriver;
import com.thoughtworks.xstream.io.xml.Xpp3DomDriver;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;
import com.thoughtworks.xstream.io.xml.XppDomDriver;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.thoughtworks.xstream.io.xml.*;

import junit.framework.Assert;
import junit.framework.Test;
Expand Down Expand Up @@ -140,6 +128,44 @@ private void testStream(final HierarchicalStreamDriver driver) {
reader.close();
}

static class Phone {
String name;
int number;
}

private void testDriverFromFile(final HierarchicalStreamDriver driver, final File file) throws Exception {
final XStream xStream = new XStream(driver);
xStream.alias("phone", Phone.class);
xStream.allowTypesByWildcard(this.getClass().getName() + "$*");

final Phone phone = xStream.fromXML(file);
Assert.assertEquals("apple", phone.name);
Assert.assertEquals(20200317, phone.number);
}

private void testDriverFromURL(final HierarchicalStreamDriver driver, final URL url, final String expect) {
final XStream xStream = new XStream(driver);
xStream.allowTypesByWildcard(this.getClass().getName() + "$*");
xStream.allowTypesByWildcard(AbstractAcceptanceTest.class.getPackage().getName() + ".*Object.**");
xStream.alias("url", URL.class);
String result = xStream.toXML(url);
Assert.assertEquals(expect, result);

final URL resultURL= xStream.fromXML(result);
Assert.assertEquals(url, resultURL);
}

private void testBinaryStreamDriverFromURL(final HierarchicalStreamDriver driver, final URL url) {
final XStream xStream = new XStream(driver);
xStream.allowTypesByWildcard(this.getClass().getName() + "$*");
xStream.allowTypesByWildcard(AbstractAcceptanceTest.class.getPackage().getName() + ".*Object.**");
ByteArrayOutputStream buff = new ByteArrayOutputStream();
xStream.toXML(url, buff);

final URL resultURL= xStream.fromXML(new ByteArrayInputStream(buff.toByteArray()));
Assert.assertEquals(url, resultURL);
}

private void addDriverTest(final HierarchicalStreamDriver driver) {
final String testName = getShortName(driver);
addTest(new TestCase(testName + "_Object") {
Expand All @@ -154,6 +180,47 @@ protected void runTest() throws Throwable {
testStream(driver);
}
});
addTest(new TestCase(testName + "_File") {
@Override
protected void runTest() throws Throwable {
if(driver instanceof BEAStaxDriver || driver instanceof BinaryStreamDriver) {
//
} else if(driver instanceof JettisonMappedXmlDriver) {
testDriverFromFile(driver, createTestJsonFile());
} else {
testDriverFromFile(driver, createTestFile());
}
}
});

addTest(new TestCase(testName + "_URL") {
@Override
protected void runTest() throws Throwable {
runDriverFromURLTest(driver, new URL("http://x-stream.github.io"), "<url>http://x-stream.github.io</url>");
runDriverFromURLTest(driver, new URL("file:/c:/winnt/blah.txt"), "<url>file:/c:/winnt/blah.txt</url>");
}
});
}

private void runDriverFromURLTest(final HierarchicalStreamDriver driver, final URL url, final String expect) {
if (driver instanceof BinaryStreamDriver) {
testBinaryStreamDriverFromURL(driver, url);
} else if (driver instanceof BEAStaxDriver) {
testDriverFromURL(driver, url, "<?xml version='1.0' encoding='utf-8'?>" + expect);
} else if (driver instanceof StandardStaxDriver) {
testDriverFromURL(driver, url, "<?xml version=\"1.0\" ?>" + expect);
} else if (driver instanceof WstxDriver || driver instanceof StaxDriver) {
testDriverFromURL(driver, url, "<?xml version='1.0' encoding='UTF-8'?>" + expect);
} else if (driver instanceof Dom4JDriver) {
testDriverFromURL(driver, url, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" + expect);
} else if (driver instanceof JettisonMappedXmlDriver) {
final String expectJson = "<url>http://x-stream.github.io</url>".equals(expect)
? "{\"url\":\"http:\\/\\/x-stream.github.io\"}"
: "{\"url\":\"file:\\/c:\\/winnt\\/blah.txt\"}";
testDriverFromURL(driver, url, expectJson);
} else {
testDriverFromURL(driver, url, expect);
}
}

private String getShortName(final HierarchicalStreamDriver driver) {
Expand All @@ -162,4 +229,32 @@ private String getShortName(final HierarchicalStreamDriver driver) {
return result;
}

private File createTestFile() throws Exception {
final String xml = "" //
+ "<phone>\n"
+ " <name>apple</name>\n"
+ " <number>20200317</number>\n"
+ "</phone>";

final File dir = new File("target/test-data");
dir.mkdirs();
final File file = new File(dir, "test.xml");
final FileOutputStream fos = new FileOutputStream(file);
fos.write(xml.getBytes("UTF-8"));
fos.close();
return file;
}

private File createTestJsonFile() throws Exception {
final String json = "{'phone':{'name':'apple','number':20200317}}".replace('\'','"');

final File dir = new File("target/test-data");
dir.mkdirs();
final File file = new File(dir, "test.json");
final FileOutputStream fos = new FileOutputStream(file);
fos.write(json.getBytes("UTF-8"));
fos.close();
return file;
}

}

0 comments on commit a29a9b7

Please sign in to comment.