Skip to content

Commit 4d858e3

Browse files
committed
Add some unit tests
1 parent 57fd253 commit 4d858e3

File tree

3 files changed

+154
-37
lines changed

3 files changed

+154
-37
lines changed

xstream-distribution/src/content/download.html

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@
2121
<h1 id="stable">Stable Version: <span class="version">1.4.11.1</span></h1>
2222

2323
<ul>
24-
<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>
24+
<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>
2525
Contains the XStream jar files, the Hibernate and Benchmark modules and all the dependencies.</li>
26-
<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>
26+
<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>
2727
Contains the complete XStream project as if checked out from the Subversion version tag.</li>
28-
<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>
28+
<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>
2929
The xstream.jar only as it is downloaded automatically when it is referenced as Maven dependency.</b></li>
30-
<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>
30+
<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>
3131
The xstream-hibernate.jar as it is downloaded automatically when it is referenced as Maven dependency.</li>
32-
<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>
32+
<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>
3333
The xstream-jmh-app.zip as standalone application with start scripts and all required libraries.</li>
34-
<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>
34+
<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>
3535
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>
3636
</ul>
3737

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

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

4747
<h1 id="optional-deps">Optional Dependencies</h1>
@@ -55,24 +55,24 @@ <h1 id="optional-deps">Optional Dependencies</h1>
5555
<ul>
5656
<li>Supported XML parsers and packages:
5757
<ul>
58-
<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>
58+
<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>
5959
<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>
6060
<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>
6161
<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>
6262
<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>
6363
<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>
64-
<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>
65-
<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>
64+
<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>
65+
<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>
6666
<li><a href="http://www.cafeconleche.org/XOM/xom-1.3.2.zip">XOM</a>, another alternative XML API.</li>
6767
</ul>
6868
</li>
6969
<li>Other optional 3rd party dependencies:
7070
<ul>
71-
<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>
71+
<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>
7272
<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>
7373
<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>
74-
<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>
75-
<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>
74+
<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>
75+
<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>
7676
</ul>
7777
</li>
7878
</ul>
@@ -82,15 +82,15 @@ <h1 id="hibernate">Dependencies Hibernate Module</h1>
8282
<ul>
8383
<li>Supported Hibernate versions:
8484
<ul>
85-
<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>
86-
<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>
87-
<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>
85+
<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>
86+
<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>
87+
<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>
8888
</ul>
8989
</li>
9090
<li>Supported Hibernate Envers versions:
9191
<ul>
92-
<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>
93-
<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>
92+
<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>
93+
<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>
9494
</ul>
9595
</li>
9696
</ul>
@@ -100,8 +100,8 @@ <h1 id="jmh">Dependencies JMH Module</h1>
100100
<ul>
101101
<li>JMH dependencies:
102102
<ul>
103-
<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>
104-
<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>
103+
<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>
104+
<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>
105105
</ul>
106106
</li>
107107
</ul>

xstream/src/test/com/thoughtworks/acceptance/XStreamerTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.thoughtworks.xstream.XStream;
2727
import com.thoughtworks.xstream.XStreamer;
2828
import com.thoughtworks.xstream.converters.ConversionException;
29+
import com.thoughtworks.xstream.io.xml.StaxDriver;
2930
import com.thoughtworks.xstream.security.TypePermission;
3031

3132

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

92+
public void testCanSerializeSelfContainedAndUsePermissions() throws ClassNotFoundException, ObjectStreamException {
93+
final OpenSourceSoftware oos = new OpenSourceSoftware("Walnes", "XStream", "BSD");
94+
xstream.alias("software", OpenSourceSoftware.class);
95+
final String xml = new XStreamer().toXML(xstream, oos);
96+
assertEquals(oos, new XStreamer().fromXML(xml, XStreamer.getDefaultPermissions()));
97+
}
98+
99+
public void testCanSerializeSelfContainedAndUseNewDriver() throws ClassNotFoundException, ObjectStreamException {
100+
final OpenSourceSoftware oos = new OpenSourceSoftware("Walnes", "XStream", "BSD");
101+
xstream.alias("software", OpenSourceSoftware.class);
102+
final String xml = new XStreamer().toXML(xstream, oos);
103+
assertEquals(oos, new XStreamer().fromXML(new StaxDriver(), xml));
104+
}
105+
106+
public void testCanSerializeSelfContainedUsePermissionAndNewDriver() throws ClassNotFoundException, ObjectStreamException {
107+
final OpenSourceSoftware oos = new OpenSourceSoftware("Walnes", "XStream", "BSD");
108+
xstream.alias("software", OpenSourceSoftware.class);
109+
final String xml = new XStreamer().toXML(xstream, oos);
110+
assertEquals(oos, new XStreamer().fromXML(new StaxDriver(), xml, XStreamer.getDefaultPermissions()));
111+
}
112+
91113
private String normalizedXStreamXML(final String xml) throws TransformerException {
92114
final StringWriter writer = new StringWriter();
93115
transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(writer));

xstream/src/test/com/thoughtworks/xstream/io/DriverEndToEndTestSuite.java

Lines changed: 111 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
import java.io.ByteArrayInputStream;
1515
import java.io.ByteArrayOutputStream;
16+
import java.io.File;
17+
import java.io.FileOutputStream;
18+
import java.net.URL;
1619
import java.util.ArrayList;
1720

1821
import com.thoughtworks.acceptance.AbstractAcceptanceTest;
@@ -22,22 +25,7 @@
2225
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
2326
import com.thoughtworks.xstream.io.binary.BinaryStreamDriver;
2427
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
25-
import com.thoughtworks.xstream.io.xml.BEAStaxDriver;
26-
import com.thoughtworks.xstream.io.xml.Dom4JDriver;
27-
import com.thoughtworks.xstream.io.xml.DomDriver;
28-
import com.thoughtworks.xstream.io.xml.JDom2Driver;
29-
import com.thoughtworks.xstream.io.xml.JDomDriver;
30-
import com.thoughtworks.xstream.io.xml.KXml2DomDriver;
31-
import com.thoughtworks.xstream.io.xml.KXml2Driver;
32-
import com.thoughtworks.xstream.io.xml.SimpleStaxDriver;
33-
import com.thoughtworks.xstream.io.xml.StandardStaxDriver;
34-
import com.thoughtworks.xstream.io.xml.StaxDriver;
35-
import com.thoughtworks.xstream.io.xml.WstxDriver;
36-
import com.thoughtworks.xstream.io.xml.XomDriver;
37-
import com.thoughtworks.xstream.io.xml.Xpp3DomDriver;
38-
import com.thoughtworks.xstream.io.xml.Xpp3Driver;
39-
import com.thoughtworks.xstream.io.xml.XppDomDriver;
40-
import com.thoughtworks.xstream.io.xml.XppDriver;
28+
import com.thoughtworks.xstream.io.xml.*;
4129

4230
import junit.framework.Assert;
4331
import junit.framework.Test;
@@ -140,6 +128,44 @@ private void testStream(final HierarchicalStreamDriver driver) {
140128
reader.close();
141129
}
142130

131+
static class Phone {
132+
String name;
133+
int number;
134+
}
135+
136+
private void testDriverFromFile(final HierarchicalStreamDriver driver, final File file) throws Exception {
137+
final XStream xStream = new XStream(driver);
138+
xStream.alias("phone", Phone.class);
139+
xStream.allowTypesByWildcard(this.getClass().getName() + "$*");
140+
141+
final Phone phone = xStream.fromXML(file);
142+
Assert.assertEquals("apple", phone.name);
143+
Assert.assertEquals(20200317, phone.number);
144+
}
145+
146+
private void testDriverFromURL(final HierarchicalStreamDriver driver, final URL url, final String expect) {
147+
final XStream xStream = new XStream(driver);
148+
xStream.allowTypesByWildcard(this.getClass().getName() + "$*");
149+
xStream.allowTypesByWildcard(AbstractAcceptanceTest.class.getPackage().getName() + ".*Object.**");
150+
xStream.alias("url", URL.class);
151+
String result = xStream.toXML(url);
152+
Assert.assertEquals(expect, result);
153+
154+
final URL resultURL= xStream.fromXML(result);
155+
Assert.assertEquals(url, resultURL);
156+
}
157+
158+
private void testBinaryStreamDriverFromURL(final HierarchicalStreamDriver driver, final URL url) {
159+
final XStream xStream = new XStream(driver);
160+
xStream.allowTypesByWildcard(this.getClass().getName() + "$*");
161+
xStream.allowTypesByWildcard(AbstractAcceptanceTest.class.getPackage().getName() + ".*Object.**");
162+
ByteArrayOutputStream buff = new ByteArrayOutputStream();
163+
xStream.toXML(url, buff);
164+
165+
final URL resultURL= xStream.fromXML(new ByteArrayInputStream(buff.toByteArray()));
166+
Assert.assertEquals(url, resultURL);
167+
}
168+
143169
private void addDriverTest(final HierarchicalStreamDriver driver) {
144170
final String testName = getShortName(driver);
145171
addTest(new TestCase(testName + "_Object") {
@@ -154,6 +180,47 @@ protected void runTest() throws Throwable {
154180
testStream(driver);
155181
}
156182
});
183+
addTest(new TestCase(testName + "_File") {
184+
@Override
185+
protected void runTest() throws Throwable {
186+
if(driver instanceof BEAStaxDriver || driver instanceof BinaryStreamDriver) {
187+
//
188+
} else if(driver instanceof JettisonMappedXmlDriver) {
189+
testDriverFromFile(driver, createTestJsonFile());
190+
} else {
191+
testDriverFromFile(driver, createTestFile());
192+
}
193+
}
194+
});
195+
196+
addTest(new TestCase(testName + "_URL") {
197+
@Override
198+
protected void runTest() throws Throwable {
199+
runDriverFromURLTest(driver, new URL("http://x-stream.github.io"), "<url>http://x-stream.github.io</url>");
200+
runDriverFromURLTest(driver, new URL("file:/c:/winnt/blah.txt"), "<url>file:/c:/winnt/blah.txt</url>");
201+
}
202+
});
203+
}
204+
205+
private void runDriverFromURLTest(final HierarchicalStreamDriver driver, final URL url, final String expect) {
206+
if (driver instanceof BinaryStreamDriver) {
207+
testBinaryStreamDriverFromURL(driver, url);
208+
} else if (driver instanceof BEAStaxDriver) {
209+
testDriverFromURL(driver, url, "<?xml version='1.0' encoding='utf-8'?>" + expect);
210+
} else if (driver instanceof StandardStaxDriver) {
211+
testDriverFromURL(driver, url, "<?xml version=\"1.0\" ?>" + expect);
212+
} else if (driver instanceof WstxDriver || driver instanceof StaxDriver) {
213+
testDriverFromURL(driver, url, "<?xml version='1.0' encoding='UTF-8'?>" + expect);
214+
} else if (driver instanceof Dom4JDriver) {
215+
testDriverFromURL(driver, url, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" + expect);
216+
} else if (driver instanceof JettisonMappedXmlDriver) {
217+
final String expectJson = "<url>http://x-stream.github.io</url>".equals(expect)
218+
? "{\"url\":\"http:\\/\\/x-stream.github.io\"}"
219+
: "{\"url\":\"file:\\/c:\\/winnt\\/blah.txt\"}";
220+
testDriverFromURL(driver, url, expectJson);
221+
} else {
222+
testDriverFromURL(driver, url, expect);
223+
}
157224
}
158225

159226
private String getShortName(final HierarchicalStreamDriver driver) {
@@ -162,4 +229,32 @@ private String getShortName(final HierarchicalStreamDriver driver) {
162229
return result;
163230
}
164231

232+
private File createTestFile() throws Exception {
233+
final String xml = "" //
234+
+ "<phone>\n"
235+
+ " <name>apple</name>\n"
236+
+ " <number>20200317</number>\n"
237+
+ "</phone>";
238+
239+
final File dir = new File("target/test-data");
240+
dir.mkdirs();
241+
final File file = new File(dir, "test.xml");
242+
final FileOutputStream fos = new FileOutputStream(file);
243+
fos.write(xml.getBytes("UTF-8"));
244+
fos.close();
245+
return file;
246+
}
247+
248+
private File createTestJsonFile() throws Exception {
249+
final String json = "{'phone':{'name':'apple','number':20200317}}".replace('\'','"');
250+
251+
final File dir = new File("target/test-data");
252+
dir.mkdirs();
253+
final File file = new File(dir, "test.json");
254+
final FileOutputStream fos = new FileOutputStream(file);
255+
fos.write(json.getBytes("UTF-8"));
256+
fos.close();
257+
return file;
258+
}
259+
165260
}

0 commit comments

Comments
 (0)