1 2 4 package org.xmlpull.v1.tests; 5 6 import junit.framework.TestCase; 7 import junit.framework.TestSuite; 8 9 import java.io.ByteArrayInputStream ; 10 import java.io.InputStream ; 11 import java.io.IOException ; 12 import java.io.Reader ; 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 19 24 public class TestSetInput extends UtilTestCase { 25 private XmlPullParserFactory factory; 26 27 public TestSetInput(String name) { 28 super(name); 29 } 30 31 protected void setUp() throws XmlPullParserException { 32 factory = factoryNewInstance(); 33 factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 34 assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 35 } 36 37 protected void tearDown() { 38 } 39 40 public void testSetReader() throws Exception { 41 XmlPullParser xpp = factory.newPullParser(); 42 assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 43 44 xpp.setInput(null); 45 assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType()); 46 try { 47 xpp.next(); 48 fail("exception was expected of next() if no input was set on parser"); 49 } catch(XmlPullParserException ex) {} 50 51 52 ReaderWrapper reader = new ReaderWrapper( 54 new StringReader ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><foo/>")); 55 assertEquals("no read() called in just contructed reader", false, reader.calledRead()); 56 57 xpp.setInput(reader); 58 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 59 assertEquals("read() not called before next()", false, reader.calledRead()); 60 61 xpp.next(); 62 assertEquals("read() must be called after next()", true, reader.calledRead()); 63 checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, true, 0); 64 } 65 66 public void testSetInput() throws Exception { 67 XmlPullParser xpp = factory.newPullParser(); 68 assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); 69 70 72 byte[] binput = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo/>").getBytes("UTF-8"); 73 InputStreamWrapper isw = new InputStreamWrapper( 74 new ByteArrayInputStream ( binput )); 75 assertEquals("no read() called in just contructed reader", false, isw.calledRead()); 76 77 xpp.setInput(isw, "UTF-8"); 78 assertEquals("UTF-8", xpp.getInputEncoding()); 79 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 80 assertEquals("read() not called before next()", false, isw.calledRead()); 81 82 xpp.nextToken(); 83 assertEquals("read() must be called after next()", true, isw.calledRead()); 84 85 88 final String FEATURE_DETECT_ENCODING = 113 "http://xmlpull.org/v1/doc/features.html#detect-encoding"; 114 if(xpp.getFeature(FEATURE_DETECT_ENCODING)) { 115 116 PackageTests.addNote("* feature "+FEATURE_DETECT_ENCODING+" is supported\n"); 117 118 isw = new InputStreamWrapper( 119 new ByteArrayInputStream ( binput )); 120 assertEquals("no read() called in just contructed reader", false, isw.calledRead()); 121 122 xpp.setInput(isw, null ); 123 assertEquals(null, xpp.getInputEncoding()); 124 125 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 126 128 xpp.nextToken(); 129 assertEquals("read() must be called after next()", true, isw.calledRead()); 130 assertEquals("UTF16", xpp.getInputEncoding()); 131 132 } 133 134 binput = 136 ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><foo/>").getBytes("ISO-8859-1"); 137 isw = new InputStreamWrapper( 138 new ByteArrayInputStream ( binput )); 139 assertEquals("no read() called in just contructed reader", false, isw.calledRead()); 140 141 xpp.setInput(isw, null ); 142 assertEquals(null, xpp.getInputEncoding()); 143 checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1); 144 146 xpp.next(); 147 assertEquals("read() must be called after next()", true, isw.calledRead()); 148 checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, true, 0); 149 150 if(xpp.getFeature(FEATURE_DETECT_ENCODING)) { 151 assertEquals("ISO-8859-1", xpp.getInputEncoding()); 152 } 153 } 154 155 157 private static class InputStreamWrapper extends InputStream { 158 InputStream is; 159 boolean calledRead; 160 public boolean calledRead() { return calledRead; } 161 public InputStreamWrapper(InputStream inputStream) { 162 is = inputStream; 163 } 164 public int read() throws IOException { 165 calledRead = true; 166 return is.read(); 167 } 168 } 169 170 private static class ReaderWrapper extends Reader { 171 Reader r; 172 boolean calledRead; 173 public boolean calledRead() { return calledRead; } 174 public ReaderWrapper(Reader reader) { 175 r = reader; 176 } 177 public int read(char[]ch, int off, int len) throws IOException { 178 calledRead = true; 179 return r.read(ch, off, len); 180 } 181 public void close() throws IOException { 182 r.close(); 183 } 184 } 185 186 public static void main (String [] args) { 187 junit.textui.TestRunner.run (new TestSuite(TestSetInput.class)); 188 } 189 190 } 191 192 | Popular Tags |