1 2 4 package org.xmlpull.v1.tests; 5 6 import junit.framework.TestCase; 7 import junit.framework.TestSuite; 8 9 import java.io.ByteArrayOutputStream ; 10 import java.io.StringWriter ; 11 12 import java.io.ByteArrayInputStream ; 13 import java.io.StringReader ; 14 15 import org.xmlpull.v1.XmlPullParser; 16 import org.xmlpull.v1.XmlPullParserFactory; 17 import org.xmlpull.v1.XmlPullParserException; 18 import org.xmlpull.v1.XmlSerializer; 19 20 25 public class TestSerialize extends UtilTestCase { 26 private XmlPullParserFactory factory; 27 private XmlPullParser xpp; 28 29 30 public static void main (String [] args) { 31 junit.textui.TestRunner.run (new TestSuite(TestSerialize.class)); 32 } 33 34 public TestSerialize(String name) { 35 super(name); 36 } 37 38 protected void setUp() throws XmlPullParserException { 39 factory = factoryNewInstance(); 40 xpp = factory.newPullParser(); 41 assertEquals(false, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 42 } 43 44 protected void tearDown() { 45 } 46 47 public void testSimpleWriter() throws Exception { 48 XmlSerializer ser = factory.newSerializer(); 49 PackageTests.addNote("* default serializer "+ser.getClass()+"\n"); 50 51 53 try { 55 ser.startTag(null, "foo"); 56 fail("exception was expected of serializer if no input was set on parser"); 57 } catch(Exception ex) {} 58 59 ser.setOutput(null); 60 61 try { 63 ser.startTag(null, "foo"); 64 fail("exception was expected of serializer if no input was set on parser"); 65 } catch(Exception ex) {} 66 67 StringWriter sw = new StringWriter (); 68 69 ser.setOutput(sw); 70 71 try { 72 ser.setOutput(null, null); 73 fail("exception was expected of setOutput() if output stream is null"); 74 } catch(IllegalArgumentException ex) {} 75 76 78 ser.setOutput(sw); 79 80 82 ser.startDocument("ISO-8859-1", Boolean.TRUE); 83 ser.startTag(null, "foo"); 84 85 ser.endTag(null, "foo"); 86 ser.endDocument(); 87 88 90 String serialized = sw.toString(); 92 xpp.setInput(new StringReader (serialized)); 93 94 assertEquals(null, xpp.getInputEncoding()); 95 checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1); 96 xpp.next(); 97 checkParserState(xpp, 1, xpp.START_TAG, "foo", null, xpp.isEmptyElementTag() , 0); 98 xpp.next(); 99 checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1); 100 xpp.next(); 101 checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1); 102 } 103 104 public void testSimpleStream() throws Exception { 105 XmlSerializer ser = factory.newSerializer(); 106 107 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 108 ser.setOutput(baos, "UTF-8"); 109 ser.startDocument("UTF-8", null); 110 ser.startTag("", "foo"); 111 ser.text("test"); 112 ser.endTag("", "foo"); 113 ser.endDocument(); 114 115 118 byte[] binput = baos.toByteArray(); 119 120 xpp.setInput(new ByteArrayInputStream ( binput ), "UTF-8" ); 121 assertEquals("UTF-8", xpp.getInputEncoding()); 122 123 checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1); 125 xpp.next(); 126 checkParserState(xpp, 1, xpp.START_TAG, "foo", null, false , 0); 127 xpp.next(); 128 checkParserState(xpp, 1, xpp.TEXT, null, "test", false, -1); 129 assertEquals(false, xpp.isWhitespace()); 130 xpp.next(); 131 checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1); 132 xpp.next(); 133 checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1); 134 } 135 136 public void testSimpleSerWithAttribute() throws Exception { 137 XmlSerializer ser = factory.newSerializer(); 138 140 StringWriter sw = new StringWriter (); 141 142 assertEquals(0, ser.getDepth()); 143 ser.setOutput(sw); 144 assertEquals(0, ser.getDepth()); 145 146 ser.startDocument(null, null); 147 assertEquals(0, ser.getDepth()); 148 assertEquals(null, ser.getNamespace()); 149 assertEquals(null, ser.getName()); 150 151 ser.startTag(null, "foo"); 152 assertEquals(1, ser.getDepth()); 153 assertEquals(null, ser.getNamespace()); 154 assertEquals("foo", ser.getName()); 155 156 ser.attribute(null, "attrName", "attrVal"); 157 assertEquals(1, ser.getDepth()); 158 assertEquals(null, ser.getNamespace()); 159 assertEquals("foo", ser.getName()); 160 161 ser.text("bar"); 162 assertEquals(1, ser.getDepth()); 163 assertEquals(null, ser.getNamespace()); 164 assertEquals("foo", ser.getName()); 165 166 ser.startTag("", "p:t"); 167 assertEquals(2, ser.getDepth()); 168 assertEquals("", ser.getNamespace()); 169 assertEquals("p:t", ser.getName()); 170 171 ser.text("\n\t "); 172 173 ser.endTag("", "p:t"); 174 assertEquals(1, ser.getDepth()); 175 assertEquals(null, ser.getNamespace()); 176 assertEquals("foo", ser.getName()); 177 178 ser.endTag(null, "foo"); 179 assertEquals(0, ser.getDepth()); 180 assertEquals(null, ser.getNamespace()); 181 assertEquals(null, ser.getName()); 182 183 ser.endDocument(); 184 assertEquals(0, ser.getDepth()); 185 assertEquals(null, ser.getNamespace()); 186 assertEquals(null, ser.getName()); 187 188 String serialized = sw.toString(); 190 xpp.setInput(new StringReader (serialized)); 191 checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1); 192 xpp.next(); 193 checkParserState(xpp, 1, xpp.START_TAG, "foo", null, false, 1); 194 checkAttrib(xpp, 0, "attrName", "attrVal"); 195 xpp.next(); 196 checkParserState(xpp, 1, xpp.TEXT, null, "bar", false, -1); 197 assertEquals(false, xpp.isWhitespace()); 198 xpp.next(); 199 checkParserState(xpp, 2, xpp.START_TAG, "p:t", null, false, 0); 200 xpp.next(); 201 checkParserState(xpp, 2, xpp.TEXT, null, "\n\t ", false, -1); 202 assertTrue(xpp.isWhitespace()); 203 xpp.next(); 204 checkParserState(xpp, 2, xpp.END_TAG, "p:t", null, false, -1); 205 xpp.next(); 206 checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1); 207 xpp.next(); 208 checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1); 209 210 211 } 212 213 } 214 215 | Popular Tags |