1 package com.thoughtworks.xstream.io.xml; 2 3 import com.megginson.sax.DataWriter; 4 import com.thoughtworks.acceptance.someobjects.X; 5 import com.thoughtworks.acceptance.someobjects.Y; 6 import com.thoughtworks.xstream.XStream; 7 import junit.framework.TestCase; 8 9 import javax.xml.transform.ErrorListener ; 10 import javax.xml.transform.Templates ; 11 import javax.xml.transform.Transformer ; 12 import javax.xml.transform.TransformerException ; 13 import javax.xml.transform.TransformerFactory ; 14 import javax.xml.transform.stream.StreamResult ; 15 import javax.xml.transform.stream.StreamSource ; 16 import java.io.StringReader ; 17 import java.io.StringWriter ; 18 import java.io.Writer ; 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.Collections ; 22 import java.util.List ; 23 24 27 public class SaxWriterTest extends TestCase { 28 29 private final static String IDENTITY_STYLESHEET = 30 "<xsl:stylesheet version=\"1.0\"" + 31 " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" + 32 "\n" + 33 " <xsl:output method=\"xml\"" + 34 " omit-xml-declaration=\"yes\" indent=\"no\"/>\n" + 35 "\n" + 36 " <xsl:template" + 37 " match=\"*|@*|comment()|processing-instruction()|text()\">\n" + 38 " <xsl:copy>\n" + 39 " <xsl:apply-templates" + 40 " select=\"*|@*|comment()|processing-instruction()|text()\"/>\n" + 41 " </xsl:copy>\n" + 42 " </xsl:template>\n" + 43 "</xsl:stylesheet>"; 44 45 private XStream xstream; 46 private X testInput; 47 private Templates identityStylesheet; 48 49 protected void setUp() throws Exception { 50 super.setUp(); 51 xstream = new XStream(); 52 xstream.alias("x", X.class); 53 xstream.alias("y", Y.class); 54 55 testInput = new X(); 56 testInput.anInt = 9; 57 testInput.aStr = "zzz"; 58 testInput.innerObj = new Y(); 59 testInput.innerObj.yField = "ooo"; 60 61 identityStylesheet = TransformerFactory.newInstance().newTemplates(new StreamSource (new StringReader (IDENTITY_STYLESHEET))); 62 } 63 64 public void testMarshalsObjectToSAX() { 65 String expected = 66 "<?xml version=\"1.0\" standalone=\"yes\"?>\n\n" + 67 "<x>\n" + 68 " <aStr>zzz</aStr>\n" + 69 " <anInt>9</anInt>\n" + 70 " <innerObj>\n" + 71 " <yField>ooo</yField>\n" + 72 " </innerObj>\n" + 73 "</x>\n\n"; 74 75 Writer buffer = new StringWriter (); 76 SaxWriter writer = new SaxWriter(); 77 DataWriter outputter = new DataWriter(writer, buffer); 78 outputter.setIndentStep(2); 79 80 writer.setContentHandler(outputter); 81 82 xstream.marshal(testInput, writer); 83 84 assertEquals(expected, buffer.toString()); 85 } 86 87 public void testAllowsStartAndEndDocCallbacksToBeSkipped() { 88 String expected = 89 "<int>1</int>\n" + 90 "<int>2</int>\n" + 91 "<int>3</int>\n"; 92 93 Writer buffer = new StringWriter (); 94 SaxWriter writer = new SaxWriter(false); 95 DataWriter outputter = new DataWriter(writer, buffer); 96 outputter.setIndentStep(2); 97 98 writer.setContentHandler(outputter); 99 100 xstream.marshal(new Integer (1), writer); 101 xstream.marshal(new Integer (2), writer); 102 xstream.marshal(new Integer (3), writer); 103 104 assertEquals(expected, buffer.toString()); 105 } 106 107 public void testMarshalsObjectToTrAX() throws Exception { 108 String expected = 109 "<x><aStr>zzz</aStr><anInt>9</anInt>" + 110 "<innerObj><yField>ooo</yField></innerObj>" + 111 "</x>" + 112 "<y><yField>ooo</yField></y>"; 113 114 TraxSource traxSource = new TraxSource(); 115 traxSource.setXStream(xstream); 116 traxSource.setSourceAsList(Arrays.asList(new Object []{testInput, testInput.innerObj})); 117 118 Writer buffer = new StringWriter (); 119 Transformer transformer = identityStylesheet.newTransformer(); 120 121 transformer.transform(traxSource, new StreamResult (buffer)); 122 123 assertEquals(expected, buffer.toString()); 124 } 125 126 public void testNullSourceObject() { 127 TraxSource traxSource = new TraxSource(); 128 129 try { 130 traxSource.setSource(null); 131 fail("Null source object not rejected"); 132 } catch (IllegalArgumentException e) { 133 } 134 } 135 136 public void testNullSourceList() { 137 TraxSource traxSource = new TraxSource(); 138 139 try { 140 traxSource.setSourceAsList(null); 141 fail("Null source list not rejected"); 142 } catch (IllegalArgumentException e) { 143 } 144 } 145 146 public void testEmptySourceList() { 147 TraxSource traxSource = new TraxSource(); 148 149 try { 150 traxSource.setSourceAsList(Collections.EMPTY_LIST); 151 fail("Empty source list not rejected"); 152 } catch (IllegalArgumentException e) { 153 } 154 } 155 156 173 public void testEmptySourceListAtParse() throws Exception { 174 TraxSource traxSource = new TraxSource(); 175 Writer buffer = new StringWriter (); 176 177 List list = new ArrayList (); 178 list.add(testInput); 179 180 traxSource.setSourceAsList(list); 181 list.clear(); 182 183 Transformer transformer = identityStylesheet.newTransformer(); 184 transformer.setErrorListener(new TrAXErrorListener()); 185 186 try { 187 transformer.transform(traxSource, new StreamResult (buffer)); 188 189 fail("Empty source list not rejected"); 190 } catch (Exception expectedException) { 191 if (expectedException.getMessage().endsWith("shall not be an empty list")) { 192 } else { 194 throw expectedException; 195 } 196 } 197 } 198 199 private static class TrAXErrorListener implements ErrorListener { 200 public TrAXErrorListener() { 201 super(); 202 } 203 204 public void warning(TransformerException e) 205 throws TransformerException { 206 207 } 208 209 public void error(TransformerException e) 210 throws TransformerException { 211 212 } 213 214 public void fatalError(TransformerException e) 215 throws TransformerException { 216 throw e; 217 } 218 } 219 } 220 221 | Popular Tags |