Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some unit tests #190

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion xstream/src/test/com/thoughtworks/acceptance/XStreamerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2006 Joe Walnes.
* Copyright (C) 2006, 2007, 2014, 2018 XStream Committers.
* Copyright (C) 2006, 2007, 2014, 2018, 2020 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand All @@ -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
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2005 Joe Walnes.
* Copyright (C) 2006, 2007, 2011, 2013, 2014, 2016, 2018, 2019 XStream Committers.
* Copyright (C) 2006, 2007, 2011, 2013, 2014, 2016, 2018, 2019, 2020 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand All @@ -13,8 +13,12 @@

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.bea.xml.stream.MXParserFactory;
import com.thoughtworks.acceptance.AbstractAcceptanceTest;
import com.thoughtworks.acceptance.objects.SampleLists;
import com.thoughtworks.xstream.XStream;
Expand Down Expand Up @@ -140,6 +144,45 @@ 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);
// Coding questions not in the scope of this use case test, igone for now
Assert.assertEquals(replaceEncodeAndEscape(expect), replaceEncodeAndEscape(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 +197,52 @@ 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
|| (driver instanceof StaxDriver && ((StaxDriver)driver).getInputFactory() instanceof MXParserFactory)) {
// igone for now
} 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 replaceEncodeAndEscape(String str){
return str.replace("utf-8","UTF-8").replace("\\","");
}

private String getShortName(final HierarchicalStreamDriver driver) {
Expand All @@ -162,4 +251,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;
}

}