KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
7
8 import junit.framework.TestCase;
9
10 import org.xmlpull.v1.XmlPullParser;
11 import org.xmlpull.v1.XmlPullParserFactory;
12 import org.xmlpull.v1.XmlPullParserException;
13
14 /**
15  * Some common utilities to help with XMLPULL tests.
16  *
17  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
18  */

19 public class UtilTestCase extends TestCase {
20     protected static final String JavaDoc FEATURE_XML_ROUNDTRIP=
21         "http://xmlpull.org/v1/doc/features.html#xml-roundtrip";
22     protected final static String JavaDoc PROPERTY_XMLDECL_VERSION =
23         "http://xmlpull.org/v1/doc/properties.html#xmldecl-version";
24     protected final static String JavaDoc PROPERTY_XMLDECL_STANDALONE =
25         "http://xmlpull.org/v1/doc/properties.html#xmldecl-standalone";
26     protected final static String JavaDoc PROPERTY_XMLDECL_CONTENT =
27         "http://xmlpull.org/v1/doc/properties.html#xmldecl-content";
28
29
30     protected final static String JavaDoc TEST_XML =
31         "<root>\n"+
32         "<foo>bar</foo>\r\n"+
33         "<hugo xmlns=\"http://www.xmlpull.org/temp\"> \n\r \n"+
34         " <hugochild>This is in a <!-- comment -->new namespace</hugochild>"+
35         "</hugo>\t\n"+
36         "<bar testattr='123abc' />"+
37         "</root>\n"+
38         "\n"+
39         "<!-- an xml sample document without meaningful content -->\n";
40
41     //private static XmlPullParserFactory factory;
42
private static boolean printedFactoryName;
43
44     public UtilTestCase(String JavaDoc name) {
45         super(name);
46     }
47
48     public static XmlPullParserFactory factoryNewInstance() throws XmlPullParserException {
49         String JavaDoc property = System.getProperty(XmlPullParserFactory.PROPERTY_NAME);
50         //property = "org.xmlpull.mxp1.MXParserFactory";
51
//"org.xmlpull.v1.xni2xmlpull1.X2ParserFactory",
52
//property = "org.xmlpull.mxp1.MXParser,org.xmlpull.mxp1_serializer.MXSerializer";
53
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(
54             property,
55             null //Thread.currentThread().getContextClassLoader().getClass(), //NOT ON JDK 1.1
56
);
57         //System.out.println("factory="+factory+" property="+property);
58
if(PackageTests.runnigAllTests() == false && printedFactoryName == false) {
59             System.out.println("factory="+factory+" property="+property);
60             printedFactoryName = true;
61         }
62         return factory;
63     }
64
65     /**
66      *
67      * Mpve to next token and gather getText() for successive tokens of the same type.
68      * Parser will be positioned on next token of different type.
69      */

70     public String JavaDoc nextTokenGathered(XmlPullParser xpp, int type, boolean expectedWhitespaces)
71         throws XmlPullParserException, IOException JavaDoc
72     {
73
74         xpp.nextToken();
75         return gatherTokenText(xpp, type, expectedWhitespaces);
76
77     }
78
79     /**
80      * Gathers getText() for successive tokens of the same type.
81      * Parser will be positioned on next token of different type.
82      */

83     public String JavaDoc gatherTokenText(XmlPullParser xpp, int type, boolean expectedWhitespaces)
84         throws XmlPullParserException, IOException JavaDoc
85     {
86         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
87         assertEquals(xpp.TYPES[ type ], xpp.TYPES[ xpp.getEventType() ]);
88         do {
89             buf.append(xpp.getText());
90             if(expectedWhitespaces) {
91                 assertTrue(xpp.isWhitespace());
92             }
93         } while(xpp.nextToken() == type);
94         return buf.toString();
95
96     }
97
98     public void checkParserState(
99         XmlPullParser xpp,
100         int depth,
101         int type,
102         String JavaDoc name,
103         String JavaDoc text,
104         boolean isEmpty,
105         int attribCount
106     ) throws XmlPullParserException, IOException JavaDoc
107     {
108         assertTrue("line number must be -1 or >= 1 not "+xpp.getLineNumber(),
109                    xpp.getLineNumber() == -1 || xpp.getLineNumber() >= 1);
110         assertTrue("column number must be -1 or >= 0 not "+xpp.getColumnNumber(),
111                    xpp.getColumnNumber() == -1 || xpp.getColumnNumber() >= 0);
112
113         assertEquals("PROCESS_NAMESPACES", false, xpp.getFeature(xpp.FEATURE_PROCESS_NAMESPACES));
114         assertEquals("TYPES[getType()]", xpp.TYPES[type], xpp.TYPES[xpp.getEventType()]);
115         assertEquals("getType()", type, xpp.getEventType());
116         assertEquals("getDepth()", depth, xpp.getDepth());
117         assertEquals("getPrefix()", null, xpp.getPrefix());
118         assertEquals("getNamespacesCount(getDepth())", 0, xpp.getNamespaceCount(depth));
119         if(xpp.getEventType() == xpp.START_TAG || xpp.getEventType() == xpp.END_TAG) {
120             assertEquals("getNamespace()", "", xpp.getNamespace());
121         } else {
122             assertEquals("getNamespace()", null, xpp.getNamespace());
123         }
124         assertEquals("getName()", name, xpp.getName());
125
126         if(xpp.getEventType() != xpp.START_TAG && xpp.getEventType() != xpp.END_TAG) {
127             assertEquals("getText()", printable(text), printable(xpp.getText()));
128
129             int [] holderForStartAndLength = new int[2];
130             char[] buf = xpp.getTextCharacters(holderForStartAndLength);
131             if(buf != null) {
132                 String JavaDoc s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
133                 assertEquals("getText(holder)", printable(text), printable(s));
134             } else {
135                 assertEquals("getTextCharacters()", null, text);
136             }
137         }
138         if(type == xpp.START_TAG) {
139             assertEquals("isEmptyElementTag()", isEmpty, xpp.isEmptyElementTag());
140         } else {
141             try {
142                 xpp.isEmptyElementTag();
143                 fail("isEmptyElementTag() must throw exception if parser not on START_TAG");
144             } catch(XmlPullParserException ex) {
145             }
146         }
147         assertEquals("getAttributeCount()", attribCount, xpp.getAttributeCount());
148     }
149
150     public void checkParserStateNs(
151         XmlPullParser xpp,
152         int depth,
153         int type,
154         int nsCount,
155         String JavaDoc namespace,
156         String JavaDoc name,
157         boolean isEmpty,
158         int attribCount
159     ) throws XmlPullParserException, IOException JavaDoc
160     {
161         assertTrue("line number must be -1 or >= 1 not "+xpp.getLineNumber(),
162                    xpp.getLineNumber() == -1 || xpp.getLineNumber() >= 1);
163         assertTrue("column number must be -1 or >= 0 not "+xpp.getColumnNumber(),
164                    xpp.getColumnNumber() == -1 || xpp.getColumnNumber() >= 0);
165
166         // this methid can be used with enabled and not enabled namespaces
167
//assertEquals("PROCESS_NAMESPACES", true, xpp.getFeature(xpp.FEATURE_PROCESS_NAMESPACES));
168
assertEquals("TYPES[getEventType()]", xpp.TYPES[type], xpp.TYPES[xpp.getEventType()]);
169         assertEquals("getEventType()", type, xpp.getEventType());
170         assertEquals("getName()", name, xpp.getName());
171
172         assertEquals("getDepth()", depth, xpp.getDepth());
173         assertEquals("getNamespacesCount(getDepth())", nsCount, xpp.getNamespaceCount(depth));
174         assertEquals("getNamespace()", namespace, xpp.getNamespace());
175
176         if(type == xpp.START_TAG) {
177             assertEquals("isEmptyElementTag()", isEmpty, xpp.isEmptyElementTag());
178         } else {
179             try {
180                 xpp.isEmptyElementTag();
181                 fail("isEmptyElementTag() must throw exception if parser not on START_TAG");
182             } catch(XmlPullParserException ex) {
183             }
184         }
185         assertEquals("getAttributeCount()", attribCount, xpp.getAttributeCount());
186     }
187
188     public void checkParserStateNs(
189         XmlPullParser xpp,
190         int depth,
191         int type,
192         String JavaDoc prefix,
193         int nsCount,
194         String JavaDoc namespace,
195         String JavaDoc name,
196         boolean isEmpty,
197         int attribCount
198     ) throws XmlPullParserException, IOException JavaDoc
199     {
200         checkParserStateNs(xpp, depth, type, nsCount, namespace, name, isEmpty, attribCount);
201         assertEquals("getPrefix()", prefix, xpp.getPrefix());
202     }
203
204     public void checkParserStateNs(
205         XmlPullParser xpp,
206         int depth,
207         int type,
208         String JavaDoc prefix,
209         int nsCount,
210         String JavaDoc namespace,
211         String JavaDoc name,
212         String JavaDoc text,
213         boolean isEmpty,
214         int attribCount
215     ) throws XmlPullParserException, IOException JavaDoc
216     {
217         checkParserStateNs(xpp, depth, type, prefix, nsCount, namespace, name, isEmpty, attribCount);
218
219         if(xpp.getEventType() != xpp.START_TAG && xpp.getEventType() != xpp.END_TAG) {
220             assertEquals("getText()", printable(text), printable(xpp.getText()));
221
222             int [] holderForStartAndLength = new int[2];
223             char[] buf = xpp.getTextCharacters(holderForStartAndLength);
224             if(buf != null) {
225                 String JavaDoc s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
226                 // ENTITY_REF is a special case when getText != (getTextCharacters == getName)
227
if(xpp.getEventType() != xpp.ENTITY_REF) {
228                     assertEquals("getText(holder)", printable(text), printable(s));
229                 } else {
230                     assertEquals("getText(holder) ENTITY_REF", printable(name), printable(s));
231                 }
232             } else {
233                 assertEquals("getTextCharacters()", null, text);
234             }
235
236         }
237
238
239     }
240
241     public void checkAttrib(
242         XmlPullParser xpp,
243         int pos,
244         String JavaDoc name,
245         String JavaDoc value
246     ) throws XmlPullParserException, IOException JavaDoc
247     {
248         assertEquals("must be on START_TAG", xpp.START_TAG, xpp.getEventType());
249         assertEquals("getAttributePrefix()",null, xpp.getAttributePrefix(pos));
250         assertEquals("getAttributeNamespace()","", xpp.getAttributeNamespace(pos));
251         assertEquals("getAttributeName()",name, xpp.getAttributeName(pos));
252         assertEquals("getAttributeValue()",value, xpp.getAttributeValue(pos));
253         assertEquals("getAttributeValue(name)",value, xpp.getAttributeValue(null, name));
254         assertEquals("getAttributeType()","CDATA", xpp.getAttributeType(pos));
255         assertEquals("isAttributeDefault()",false, xpp.isAttributeDefault(pos));
256     }
257
258
259     public void checkAttribNs(
260         XmlPullParser xpp,
261         int pos,
262         String JavaDoc namespace,
263         String JavaDoc name,
264         String JavaDoc value
265     ) throws XmlPullParserException, IOException JavaDoc
266     {
267         assertEquals("must be on START_TAG", xpp.START_TAG, xpp.getEventType());
268         assertEquals("getAttributeNamespace()",namespace, xpp.getAttributeNamespace(pos));
269         assertEquals("getAttributeName()",name, xpp.getAttributeName(pos));
270         assertEquals("getAttributeValue()",printable(value), printable(xpp.getAttributeValue(pos)));
271         assertEquals("getAttributeValue(ns,name)",
272                      printable(value), printable(xpp.getAttributeValue(namespace, name)));
273         assertEquals("getAttributeType()","CDATA", xpp.getAttributeType(pos));
274         assertEquals("isAttributeDefault()",false, xpp.isAttributeDefault(pos));
275     }
276
277     public void checkAttribNs(
278         XmlPullParser xpp,
279         int pos,
280         String JavaDoc prefix,
281         String JavaDoc namespace,
282         String JavaDoc name,
283         String JavaDoc value
284     ) throws XmlPullParserException, IOException JavaDoc
285     {
286         checkAttribNs(xpp, pos, namespace, name, value);
287         assertEquals("getAttributePrefix()",prefix, xpp.getAttributePrefix(pos));
288     }
289
290     public void checkNamespace(
291         XmlPullParser xpp,
292         int pos,
293         String JavaDoc prefix,
294         String JavaDoc uri,
295         boolean checkMapping
296     ) throws XmlPullParserException, IOException JavaDoc
297     {
298         assertEquals("getNamespacePrefix(pos)",prefix, xpp.getNamespacePrefix(pos));
299         assertEquals("getNamespaceUri(pos)",uri, xpp.getNamespaceUri(pos));
300         if(checkMapping) {
301             assertEquals("getNamespace(prefix)", uri, xpp.getNamespace (prefix));
302         }
303     }
304
305     protected String JavaDoc printable(char ch) {
306         if(ch == '\n') {
307             return "\\n";
308         } else if(ch == '\r') {
309             return "\\r";
310         } else if(ch == '\t') {
311             return "\\t";
312         } if(ch > 127 || ch < 32) {
313             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("\\u");
314             String JavaDoc hex = Integer.toHexString((int)ch);
315             for (int i = 0; i < 4-hex.length(); i++)
316             {
317                 buf.append('0');
318             }
319             buf.append(hex);
320             return buf.toString();
321         }
322         return ""+ch;
323     }
324
325     protected String JavaDoc printable(String JavaDoc s) {
326         if(s == null) return null;
327         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
328         for(int i = 0; i < s.length(); ++i) {
329             buf.append(printable(s.charAt(i)));
330         }
331         s = buf.toString();
332         return s;
333     }
334
335 }
336
337
Popular Tags