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 19, 2020
1 parent 57fd253 commit 4d858e3
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 37 deletions.
42 changes: 21 additions & 21 deletions xstream-distribution/src/content/download.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
<h1 id="stable">Stable Version: <span class="version">1.4.11.1</span></h1>

<ul>
<li><b><a href="https://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream-distribution/1.4.11.1/xstream-distribution-1.4.11.1-bin.zip">Binary distribution:</a></b>
<li><b><a href="http://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream-distribution/1.4.11.1/xstream-distribution-1.4.11.1-bin.zip">Binary distribution:</a></b>
Contains the XStream jar files, the Hibernate and Benchmark modules and all the dependencies.</li>
<li><b><a href="https://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream-distribution/1.4.11.1/xstream-distribution-1.4.11.1-src.zip">Source distribution:</a></b>
<li><b><a href="http://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream-distribution/1.4.11.1/xstream-distribution-1.4.11.1-src.zip">Source distribution:</a></b>
Contains the complete XStream project as if checked out from the Subversion version tag.</li>
<li><b><a href="https://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.jar">XStream Core only:</a>
<li><b><a href="http://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.jar">XStream Core only:</a>
The xstream.jar only as it is downloaded automatically when it is referenced as Maven dependency.</b></li>
<li><b><a href="https://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream-hibernate/1.4.11.1/xstream-hibernate-1.4.11.1.jar">XStream Hibernate module:</a></b>
<li><b><a href="http://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream-hibernate/1.4.11.1/xstream-hibernate-1.4.11.1.jar">XStream Hibernate module:</a></b>
The xstream-hibernate.jar as it is downloaded automatically when it is referenced as Maven dependency.</li>
<li><b><a href="https://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream-jmh/1.4.11.1/xstream-jmh-1.4.11.1-app.zip">XStream JMH module:</a></b>
<li><b><a href="http://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream-jmh/1.4.11.1/xstream-jmh-1.4.11.1-app.zip">XStream JMH module:</a></b>
The xstream-jmh-app.zip as standalone application with start scripts and all required libraries.</li>
<li><b><a href="https://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream/1.4.11-java7/xstream-1.4.11-java7.jar">XStream Core for Java 7 only:</a>
<li><b><a href="http://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream/1.4.11-java7/xstream-1.4.11-java7.jar">XStream Core for Java 7 only:</a>
The xstream.jar only <a href="faq.html#Compatibility_Android">without the Java 8 stuff</a> as it is downloaded automatically when it is referenced as Maven dependency.</b></li>
</ul>

Expand All @@ -40,8 +40,8 @@ <h1 id="previous-releases">Previous Releases</h1>
<p>Previous releases of XStream are also available. However, use of the latest stable version is recommended.</p>

<ul>
<li><a href="https://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream/">Previous releases (&gt;= 1.2)</a></li>
<li><a href="https://repo1.maven.org/maven2/xstream/xstream/">Previous releases (&lt;= 1.2)</a></li>
<li><a href="http://repo1.maven.org/maven2/com/thoughtworks/xstream/xstream/">Previous releases (&gt;= 1.2)</a></li>
<li><a href="http://repo1.maven.org/maven2/xstream/xstream/">Previous releases (&lt;= 1.2)</a></li>
</ul>

<h1 id="optional-deps">Optional Dependencies</h1>
Expand All @@ -55,24 +55,24 @@ <h1 id="optional-deps">Optional Dependencies</h1>
<ul>
<li>Supported XML parsers and packages:
<ul>
<li><a href="https://repo1.maven.org/maven2/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar">XmlPull</a>, the <a href="http://www.xmlpull.org/">XML pull parser API</a> and factory to detect available implementations.</li>
<li><a href="http://repo1.maven.org/maven2/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar">XmlPull</a>, the <a href="http://www.xmlpull.org/">XML pull parser API</a> and factory to detect available implementations.</li>
<li><a href="http://www.extreme.indiana.edu/dist/java-repository/xpp3/jars/xpp3_min-1.1.4c.jar">Xpp3</a>, an XML pull parser (recommended).</li>
<li><a href="http://downloads.sourceforge.net/kxml/kxml2-2.3.0.jar">kXML2</a> or <a href="http://downloads.sourceforge.net/kxml/kxml2-min-2.3.0.jar">kXML2-min</a>, an XML pull parser.</li>
<li><a href="https://github.com/dom4j/dom4j/releases/download/version-2.0.2/dom4j-2.0.2.jar">DOM4J</a>, easy XML representation and manipulation framework.</li>
<li><a href="http://www.jdom.org/dist/binary/archive/jdom-1.1.3.zip">JDOM</a>, easy XML representation and manipulation (superseded by JDOM2).</li>
<li><a href="http://www.jdom.org/dist/binary/jdom-2.0.6.zip">JDOM2</a>, easy XML representation and manipulation, successor of JDOM.</li>
<li>StaX, the <a href="https://repo1.maven.org/maven2/stax/stax/1.2.0/stax-1.2.0.jar">reference implementation</a> of the <a href="https://repo1.maven.org/maven2/stax/stax-api/1.0.1/stax-api-1.0.1.jar">Streaming API for XML</a>.</li>
<li><a href="https://repo1.maven.org/maven2/com/fasterxml/woodstox/woodstox-core/5.2.0/woodstox-core-5.2.0.jar">Woodstox</a>, an alternate open source StaX implementation.</li>
<li>StaX, the <a href="http://repo1.maven.org/maven2/stax/stax-1.2.0.jar">reference implementation</a> of the <a href="http://repo1.maven.org/maven2/stax/stax-api-1.0.1.jar">Streaming API for XML</a>.</li>
<li><a href="http://repo1.maven.org/maven2/com.fasterxml.woodstox/woodstox-core-5.2.0.jar">Woodstox</a>, an alternate open source StaX implementation.</li>
<li><a href="http://www.cafeconleche.org/XOM/xom-1.3.2.zip">XOM</a>, another alternative XML API.</li>
</ul>
</li>
<li>Other optional 3rd party dependencies:
<ul>
<li><a href="https://repo1.maven.org/maven2/javax/activation/jaxax.activation-api/1.2.0/jaxax.activation-api-1.2.0.jar">Java Activation module</a> for the ActivationDataFlavorConverter. The dependency is required for the Java 11 runtime.</li>
<li><a href="http://repo1.maven.org/maven2/javax/activation/jaxax.activation-api/1.2.0/jaxax.activation-api-1.2.0.jar">Java Activation module</a> for the ActivationDataFlavorConverter. The dependency is required for the Java 11 runtime.</li>
<li><a href="https://github.com/JodaOrg/joda-time/releases/download/v2.10.1/joda-time-2.10.1-dist.zip">Joda Time</a> for optional ISO8601 date/time converters in JDK 1.7 or below.</li>
<li><a href="http://downloads.sourceforge.net/cglib/cglib-nodep-2.2.jar">CGLIB</a> for optional support of some proxies generated with the CGLIB Enhancer.</li>
<li><a href="https://repo1.maven.org/maven2/org/codehaus/jettison/jettison/1.2/jettison-1.2.jar">Jettison</a> for serialization and deserialization support with JSON. Note, that newer versions 1.3.x are no longer compatible with XStream.</li>
<li><a href="https://repo1.maven.org/maven2/org/codehaus/jettison/jettison/1.0/jettison-1.0.jar">Jettison 1.0.1</a> for serialization and deserialization support with JSON in JDK 1.4. Note, that newer version 1.1 is not compatible with XStream.</li>
<li><a href="http://repo1.maven.org/maven2/org/codehaus/jettison/jettison/1.2/jettison-1.2.jar">Jettison</a> for serialization and deserialization support with JSON. Note, that newer versions 1.3.x are no longer compatible with XStream.</li>
<li><a href="http://repo1.maven.org/maven2/org/codehaus/jettison/jettison/1.0/jettison-1.0.jar">Jettison 1.0.1</a> for serialization and deserialization support with JSON in JDK 1.4. Note, that newer version 1.1 is not compatible with XStream.</li>
</ul>
</li>
</ul>
Expand All @@ -82,15 +82,15 @@ <h1 id="hibernate">Dependencies Hibernate Module</h1>
<ul>
<li>Supported Hibernate versions:
<ul>
<li><a href="https://repo1.maven.org/maven2/org/hibernate/hibernate-core/4.2.5.Final/hibernate-core-4.2.5.Final.jar">Hibernate Core 4.2.5</a>, for Java 6 or higher.</li>
<li><a href="https://repo1.maven.org/maven2/org/hibernate/hibernate-core/3.6.6.Final/hibernate-core-3.6.6.Final.jar">Hibernate Core 3.6.6</a>, for Java 5.</li>
<li><a href="https://repo1.maven.org/maven2/org/hibernate/hibernate-core/3.3.2.GA/hibernate-core-3.3.2.GA.jar">Hibernate Core 3.3.2</a>, for Java 1.4.</li>
<li><a href="http://repo1.maven.org/maven2/org/hibernate/hibernate-core/4.2.5.Final/hibernate-core-4.2.5.Final.jar">Hibernate Core 4.2.5</a>, for Java 6 or higher.</li>
<li><a href="http://repo1.maven.org/maven2/org/hibernate/hibernate-core/3.6.6.Final/hibernate-core-3.6.6.Final.jar">Hibernate Core 3.6.6</a>, for Java 5.</li>
<li><a href="http://repo1.maven.org/maven2/org/hibernate/hibernate-core/3.3.2.GA/hibernate-core-3.3.2.GA.jar">Hibernate Core 3.3.2</a>, for Java 1.4.</li>
</ul>
</li>
<li>Supported Hibernate Envers versions:
<ul>
<li><a href="https://repo1.maven.org/maven2/org/hibernate/hibernate-envers/4.2.5.Final/hibernate-envers-4.2.5.Final.jar">Hibernate Envers 4.2.5</a>, for Java 6 or higher.</li>
<li><a href="https://repo1.maven.org/maven2/org/hibernate/hibernate-envers/3.6.6.Final/hibernate-envers-3.6.6.Final.jar">Hibernate Envers 3.6.6</a>, for Java 5.</li>
<li><a href="http://repo1.maven.org/maven2/org/hibernate/hibernate-envers/4.2.5.Final/hibernate-envers-4.2.5.Final.jar">Hibernate Envers 4.2.5</a>, for Java 6 or higher.</li>
<li><a href="http://repo1.maven.org/maven2/org/hibernate/hibernate-envers/3.6.6.Final/hibernate-envers-3.6.6.Final.jar">Hibernate Envers 3.6.6</a>, for Java 5.</li>
</ul>
</li>
</ul>
Expand All @@ -100,8 +100,8 @@ <h1 id="jmh">Dependencies JMH Module</h1>
<ul>
<li>JMH dependencies:
<ul>
<li><a href="https://repo1.maven.org/maven2/org/openjdk/jmh/jmh-core/1.19/jmh-core-1.19.jar">JMH Core 1.19</a>, for Java 6 or higher.</li>
<li><a href="https://repo1.maven.org/maven2/org/openjdk/jmh/jmh-generator-annprocess/1.19/jmh-generator-annprocess-1.19.jar">JMH Generator Annotation Processor 1.19</a>, for Java 6 or higher.</li>
<li><a href="http://repo1.maven.org/maven2/org/openjdk/jmh-core/1.19/jmh-core-1.19.jar">JMH Core 1.19</a>, for Java 6 or higher.</li>
<li><a href="http://repo1.maven.org/maven2/org/openjdk/jmh-generator-annprocess/1.19/jmh-generator-annprocess-1.19.jar">JMH Generator Annotation Processor 1.19</a>, for Java 6 or higher.</li>
</ul>
</li>
</ul>
Expand Down
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 4d858e3

Please sign in to comment.