001: /*
002: * Copyright (C) 2006 Joe Walnes.
003: * Copyright (C) 2006, 2007, 2008 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 25. June 2006 by Guilherme Silveira
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.StandardObject;
015: import com.thoughtworks.xstream.XStream;
016: import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
017: import com.thoughtworks.xstream.io.xml.Dom4JDriver;
018: import com.thoughtworks.xstream.io.xml.DomDriver;
019: import com.thoughtworks.xstream.io.xml.JDomDriver;
020: import com.thoughtworks.xstream.io.xml.StaxDriver;
021: import com.thoughtworks.xstream.io.xml.XomDriver;
022: import com.thoughtworks.xstream.io.xml.XppDomDriver;
023: import com.thoughtworks.xstream.io.xml.XppDriver;
024:
025: import junit.framework.Assert;
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import java.io.ByteArrayInputStream;
031: import java.io.ByteArrayOutputStream;
032: import java.io.IOException;
033: import java.io.OutputStreamWriter;
034:
035: /**
036: * @author Sanjiv Jivan
037: * @author Guilherme Silveira
038: * @author Jörg Schaible
039: */
040: public class EncodingTestSuite extends TestSuite {
041:
042: public static class TestObject extends StandardObject {
043: private String data;
044: }
045:
046: public EncodingTestSuite() {
047: super (EncodingTestSuite.class.getName());
048: addDriverTest(new Dom4JDriver());
049: addDriverTest(new DomDriver());
050: addDriverTest(new JDomDriver());
051: addDriverTest(new StaxDriver());
052: addDriverTest(new XppDomDriver());
053: addDriverTest(new XppDriver());
054: addDriverTest(new XomDriver());
055: }
056:
057: private void test(HierarchicalStreamDriver driver, String encoding)
058: throws IOException {
059: String headerLine = encoding != null ? ("<?xml version=\"1.0\" encoding=\""
060: + encoding + "\"?>\n")
061: : "";
062: String xmlData = headerLine // force code format
063: + "<test>\n" + " <data>J\u00f6rg</data>\n" + "</test>";
064:
065: XStream xstream = new XStream(driver);
066: xstream.alias("test", TestObject.class);
067: TestObject obj = new TestObject();
068: obj.data = "J\u00f6rg";
069:
070: ByteArrayOutputStream bos = new ByteArrayOutputStream();
071: OutputStreamWriter writer = encoding != null ? new OutputStreamWriter(
072: bos, encoding)
073: : new OutputStreamWriter(bos);
074: xstream.toXML(obj, writer);
075: writer.close();
076:
077: String generated = encoding != null ? bos.toString(encoding)
078: : bos.toString();
079: Assert.assertTrue("'" + obj.data + "' was not found", generated
080: .indexOf(obj.data) > 0);
081:
082: Object restored = xstream.fromXML(new ByteArrayInputStream(
083: encoding != null ? xmlData.getBytes(encoding) : xmlData
084: .getBytes()));
085: Assert.assertEquals(obj, restored);
086: }
087:
088: private void addDriverTest(final HierarchicalStreamDriver driver) {
089: String testName = getShortName(driver);
090: addTest(new TestCase(testName + "Native") {
091: protected void runTest() throws Throwable {
092: test(driver, null);
093: }
094: });
095: addTest(new TestCase(testName + "WithUTF_8Encoding") {
096: protected void runTest() throws Throwable {
097: test(driver, "UTF-8");
098: }
099: });
100: addTest(new TestCase(testName + "WithIS0_8859_1Encoding") {
101: protected void runTest() throws Throwable {
102: test(driver, "ISO-8859-1");
103: }
104: });
105: }
106:
107: private String getShortName(HierarchicalStreamDriver driver) {
108: String result = driver.getClass().getName();
109: result = result.substring(result.lastIndexOf('.') + 1);
110: return result;
111: }
112:
113: public static Test suite() {
114: return new EncodingTestSuite();
115: }
116:
117: }
|