Skip to content

Commit a29a9b7

Browse files
committed
Add some unit tests
1 parent 651b48b commit a29a9b7

File tree

2 files changed

+133
-16
lines changed

2 files changed

+133
-16
lines changed

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)