diff --git a/xstream/src/test/com/thoughtworks/acceptance/XStreamerTest.java b/xstream/src/test/com/thoughtworks/acceptance/XStreamerTest.java
index 06e212b9c..e3a88df6b 100644
--- a/xstream/src/test/com/thoughtworks/acceptance/XStreamerTest.java
+++ b/xstream/src/test/com/thoughtworks/acceptance/XStreamerTest.java
@@ -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
@@ -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;
@@ -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));
diff --git a/xstream/src/test/com/thoughtworks/xstream/io/DriverEndToEndTestSuite.java b/xstream/src/test/com/thoughtworks/xstream/io/DriverEndToEndTestSuite.java
index 848ff150a..1400ad7a9 100644
--- a/xstream/src/test/com/thoughtworks/xstream/io/DriverEndToEndTestSuite.java
+++ b/xstream/src/test/com/thoughtworks/xstream/io/DriverEndToEndTestSuite.java
@@ -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
@@ -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;
@@ -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") {
@@ -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"), "http://x-stream.github.io");
+ runDriverFromURLTest(driver, new URL("file:/c:/winnt/blah.txt"), "file:/c:/winnt/blah.txt");
+ }
+ });
+ }
+
+ 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, "" + expect);
+ } else if (driver instanceof StandardStaxDriver) {
+ testDriverFromURL(driver, url, "" + expect);
+ } else if (driver instanceof WstxDriver || driver instanceof StaxDriver) {
+ testDriverFromURL(driver, url, "" + expect);
+ } else if (driver instanceof Dom4JDriver) {
+ testDriverFromURL(driver, url, "\n\n" + expect);
+ } else if (driver instanceof JettisonMappedXmlDriver) {
+ final String expectJson = "http://x-stream.github.io".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) {
@@ -162,4 +251,32 @@ private String getShortName(final HierarchicalStreamDriver driver) {
return result;
}
+ private File createTestFile() throws Exception {
+ final String xml = "" //
+ + "\n"
+ + " apple\n"
+ + " 20200317\n"
+ + "";
+
+ 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;
+ }
+
}