KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmlpull > v1 > tests > TestSetInput


1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
2 // for license see accompanying LICENSE_TESTS.txt file (available also at http://www.xmlpull.org)
3

4 package org.xmlpull.v1.tests;
5
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.Reader JavaDoc;
13 import java.io.StringReader JavaDoc;
14
15 import org.xmlpull.v1.XmlPullParser;
16 import org.xmlpull.v1.XmlPullParserFactory;
17 import org.xmlpull.v1.XmlPullParserException;
18
19 /**
20  * Tests to determine that setInput works as expected for different encodings.
21  *
22  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
23  */

24 public class TestSetInput extends UtilTestCase {
25     private XmlPullParserFactory factory;
26
27     public TestSetInput(String JavaDoc 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 JavaDoc {
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         // make input suspectible to read ...
53
ReaderWrapper reader = new ReaderWrapper(
54             new StringReader JavaDoc("<?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/*empty*/, 0);
64     }
65
66     public void testSetInput() throws Exception JavaDoc {
67         XmlPullParser xpp = factory.newPullParser();
68         assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
69
70         // another test
71

72         byte[] binput = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo/>").getBytes("UTF-8");
73         InputStreamWrapper isw = new InputStreamWrapper(
74             new ByteArrayInputStream JavaDoc( 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         //needs to resolve:
86
// java.lang.InternalError: Converter malfunction (UTF-16) -- please submit a bug report via http://java.sun.com/cgi-bin/bugreport.cgi
87

88 //
89
// // add BOM
90
// //byte[] binput1 = new byte[]{((byte)'\u00FE'), ((byte)'\u00FF')};
91
// //byte[] binput1 = new byte[]{((byte)'\u00FF'), ((byte)'\u00FE')};
92
// byte[] binput1 = new byte[0];
93
// byte[] binput2 =
94
// ("<?xml version=\"1.0\" encoding=\"UTF16\"?><foo/>").getBytes("UTF16");
95
// binput = new byte[ binput1.length + binput2.length ] ;
96
// System.arraycopy(binput1, 0, binput, 0, binput1.length);
97
// System.arraycopy(binput2, 0, binput, binput1.length, binput2.length);
98
// isw = new InputStreamWrapper(
99
// new ByteArrayInputStream( binput ));
100
// assertEquals("no read() called in just contructed reader", false, isw.calledRead());
101
//
102
// //xpp.setInput(isw, "UTF-16" ); //TODO why Xerces2 causes java Unicode decoder to fail ????
103
// xpp.setInput(isw, "UTF16" );
104
// //assertEquals("UTF-16", xpp.getInputEncoding());
105
// checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
106
// assertEquals("read() not called before next()", false, isw.calledRead());
107
//
108
// xpp.nextToken();
109
// assertEquals("read() must be called after next()", true, isw.calledRead());
110
//
111
// check input detecting -- for mutlibyte sequences ...
112
final String JavaDoc 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 JavaDoc( 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             //assertEquals("read() not called before next()", false, isw.calledRead());
127

128             xpp.nextToken();
129             assertEquals("read() must be called after next()", true, isw.calledRead());
130             assertEquals("UTF16", xpp.getInputEncoding());
131
132         }
133
134         // check input detecting -- default
135
binput =
136             ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><foo/>").getBytes("ISO-8859-1");
137         isw = new InputStreamWrapper(
138             new ByteArrayInputStream JavaDoc( 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         //assertEquals("read() not called before next()", false, isw.calledRead());
145

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/*empty*/, 0);
149
150         if(xpp.getFeature(FEATURE_DETECT_ENCODING)) {
151             assertEquals("ISO-8859-1", xpp.getInputEncoding());
152         }
153     }
154
155     // ------ allow to detect if input was read
156

157     private static class InputStreamWrapper extends InputStream JavaDoc {
158         InputStream JavaDoc is;
159         boolean calledRead;
160         public boolean calledRead() { return calledRead; }
161         public InputStreamWrapper(InputStream JavaDoc inputStream) {
162             is = inputStream;
163         }
164         public int read() throws IOException JavaDoc {
165             calledRead = true;
166             return is.read();
167         }
168     }
169
170     private static class ReaderWrapper extends Reader JavaDoc {
171         Reader JavaDoc r;
172         boolean calledRead;
173         public boolean calledRead() { return calledRead; }
174         public ReaderWrapper(Reader JavaDoc reader) {
175             r = reader;
176         }
177         public int read(char[]ch, int off, int len) throws IOException JavaDoc {
178             calledRead = true;
179             return r.read(ch, off, len);
180         }
181         public void close() throws IOException JavaDoc {
182             r.close();
183         }
184     }
185
186     public static void main (String JavaDoc[] args) {
187         junit.textui.TestRunner.run (new TestSuite(TestSetInput.class));
188     }
189
190 }
191
192
Popular Tags