KickJava   Java API By Example, From Geeks To Geeks.

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


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
5 /*
6  SYNTAX: (// marks unimplemented)
7  <tests xmlns="http://xmlpull.org/v1/tests/2002-08.xsd">
8  <test-parser name="descriptive name of test">
9  <input-inline>inlined xml</input-inline>?
10 // <input-file>name of file</input-file>?
11  <set-feature>name of feature</set-feature>*
12  <expect
13  type="START_DOCUMENT|END_DOCUMENT|START_TAG|END_TAG|TEXT|COMMENT|..."?
14  name="getName()"? namespace="getNamespace()"?
15 // prefix="getPrefix()"? depth="getDepth()"? empty="isEmptyElementTag"?
16 // namespaceCount="getNamespacesCount(getDepth())"? attributeCount="getAttributeCount()"?
17 // text="getText()" isWhitespace="isWhitespace():
18  />*
19 // <check-attribute pos="position" prefix="getAttributePrefix(pos)"? namespace=...? name=...?
20 // value="..." is-default="0|1"?/>
21 // <check-namespace pos="position" prefix="getNamespacePrefix(pos)"? namespace=...?/>
22  <next>*
23 // <nextToken>*
24 // <nextTag>*
25 // <nextText>*
26 // <!-- what about require(), getTextCharacters(), getLineNumber/getColumnNumber(), defineEntityReplacementText()?/>
27  </test-parser>*
28 // <test-serializer>
29 // <start-tag name="..." namespace="..."/>
30 // </test-serializer>
31  </tests>
32  */

33
34 package org.xmlpull.v1.tests;
35
36 import java.io.FileInputStream JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.StringReader JavaDoc;
40 import org.xmlpull.v1.XmlPullParser;
41 import org.xmlpull.v1.XmlPullParserException;
42 import org.xmlpull.v1.XmlPullParserFactory;
43 //import org.xmlpull.v1.util.XmlPullWrapper;
44

45 //import org.xmlpull.v1.tests.UtilTestCase;
46

47 public class XmlTestCase extends UtilTestCase {
48     private static final String JavaDoc NS_URI = "http://xmlpull.org/v1/tests/2002-08.xsd";
49     //private static boolean standalone = true;
50
private static final boolean verbose = false;
51
52     //public static void main (String[] args) {
53
// junit.textui.TestRunner.run (new TestSuite(XmlTestCase.class));
54
//}
55

56     public XmlTestCase(String JavaDoc name) {
57         super(name);
58         //standalone = false;
59
}
60
61     protected void verbose(String JavaDoc msg)
62     {
63         if(verbose) {
64             if(PackageTests.runnigAllTests()) {
65                 //PackageTests.addNote(msg+"\n");
66
System.out.println(msg);
67             } else {
68                 System.out.println(msg);
69             }
70         }
71     }
72
73     protected void info(String JavaDoc msg)
74     {
75         if(PackageTests.runnigAllTests()) {
76             PackageTests.addNote(msg+"\n");
77         } else {
78             System.out.println(msg);
79         }
80     }
81
82     private final static String JavaDoc RESOURCE_PREFIX = "/org/xmlpull/v1/tests/xml/";
83     private InputStream JavaDoc openStream(String JavaDoc name) throws IOException JavaDoc {
84         //String fullName = "src/xml/tests/"+testName;
85
InputStream JavaDoc is = null;
86         if(is == null) {
87             try {
88                 is = new FileInputStream JavaDoc(name);
89             } catch(Exception JavaDoc ex) {
90             }
91         }
92         if(is == null) {
93             try {
94                 is = getClass().getResourceAsStream (RESOURCE_PREFIX+name);
95             } catch(Exception JavaDoc ex) {
96             }
97         }
98         if(is == null) {
99             try {
100                 is = getClass().getResourceAsStream ("/"+name);
101             } catch(Exception JavaDoc ex) {
102             }
103         }
104         /*if(is == null) { // this seems to causes loading of simple.xml in the kxml samples dir
105             try {
106                 is = getClass().getResourceAsStream (name);
107             } catch(Exception ex) {
108             }
109          }*/

110
111
112         if (is == null) {
113             throw new IOException JavaDoc("could not open XML test for '"+name+"'");
114         }
115         return is;
116     }
117     /**
118      *
119      */

120     public void testXml(String JavaDoc testName)
121         throws IOException JavaDoc, XmlPullParserException
122     {
123
124         XmlPullParserFactory factory = factoryNewInstance();
125         factory.setNamespaceAware(true);
126         //XmlPullWrapper wrapper;
127

128
129         XmlPullParser pp = factory.newPullParser();
130         //wrapper = new XmlPullWrapper(pp);
131

132
133         InputStream JavaDoc is = openStream(testName);
134         verbose("> LOADING TESTS '"+testName+"'");
135         pp.setInput(is, null);
136         executeTests(pp);
137         is.close();
138         verbose("< FINISHED TESTS '"+testName+"'");
139     }
140
141
142     protected void executeTests(XmlPullParser pp) throws IOException JavaDoc, XmlPullParserException
143     {
144
145         //wrapper.nextStartTag(NS_URI, "tests");
146
pp.nextTag();
147         pp.require(pp.START_TAG, NS_URI, "tests");
148         while(pp.nextTag() == pp.START_TAG) {
149             executeTestParser(pp);
150         }
151         pp.require(pp.END_TAG, NS_URI, "tests");
152     }
153
154     protected void executeTestParser(XmlPullParser pp) throws IOException JavaDoc, XmlPullParserException
155     {
156         pp.require(pp.START_TAG, NS_URI, "test-parser");
157         String JavaDoc testName = pp.getAttributeValue("", "name");
158         verbose(">> START TEST '"+testName+"'");
159         //testFactory = XmlPullParserFactory.newInstance();
160
XmlPullParserFactory testFactory = factoryNewInstance();
161         verbose(">> using testFactory='"+testFactory.getClass()+"'");
162         XmlPullParser testParser = null;
163         boolean testInputReady = false;
164         while(pp.nextTag() == pp.START_TAG) {
165             String JavaDoc action = pp.getName();
166             if("create-parser".equals(action)) {
167                 testParser = testFactory.newPullParser();
168                 verbose(">> using testParser='"+testParser.getClass()+"'");
169                 pp.nextText();
170             } else if("input-inline".equals(action)) {
171                 if(testInputReady) {
172                     throw new RuntimeException JavaDoc("input already set"+pp.getPositionDescription());
173                 }
174                 testInputReady = true;
175                 String JavaDoc input = pp.nextText();
176                 verbose(">>> "+action+" to '"+printable(input)+"'");
177                 verbose(">>> "+action+" to '"+input+"'");
178                 testParser.setInput(new StringReader JavaDoc( input ));
179             } else if("expect".equals(action)) {
180                 String JavaDoc type = pp.getAttributeValue("", "type");
181                 String JavaDoc namespace = pp.getAttributeValue("", "namespace");
182                 String JavaDoc name = pp.getAttributeValue("", "name");
183                 String JavaDoc empty = pp.getAttributeValue("", "empty");
184                 verbose(">>> "+action
185                             +" type="+type
186                             +(namespace != null ? " namespace="+namespace : "")
187                             +(name != null ? " name="+name : "")
188                             +(empty != null ? " empty="+empty : ""));
189                 if(type != null) {
190                     int event = convertToEventType(pp,type);
191                     // comapring string give better error messages
192
assertEquals(pp.TYPES[ event ], pp.TYPES[ testParser.getEventType() ]);
193                     // now compare actual int values
194
assertEquals(event, testParser.getEventType());
195                 }
196                 if(namespace != null) assertEquals(namespace, testParser.getNamespace());
197                 if(name != null) assertEquals(name, testParser.getName());
198                 if(empty != null) {
199                     boolean isEmpty = Boolean.valueOf( empty ).booleanValue();
200                     assertEquals(isEmpty, testParser.isEmptyElementTag());
201                 }
202                 pp.nextText();
203             } else if("next".equals(action)) {
204                 verbose(">>> "+action);
205                 testParser.next();
206                 pp.nextText();
207             } else if("next-tag".equals(action)) {
208                 verbose(">>> "+action);
209                 testParser.nextTag();
210                 pp.nextText();
211             } else if("next-text".equals(action)) {
212                 verbose(">>> "+action); //+" on "+testParser.getPositionDescription()+"");
213
String JavaDoc expected = pp.getAttributeValue("", "text");
214                 String JavaDoc testText = testParser.nextText();
215                 if(expected != null) {
216                     verbose(">>> "+action+" testParser="+testParser.getPositionDescription()
217                            +" excpectecd='"+printable(expected)+"' test='"+printable(testText)+"'");
218                     assertEquals(testParser.getPositionDescription(), printable(expected), printable(testText));
219                     assertEquals(expected, testText);
220                 }
221                 pp.nextText();
222             } else if("set-feature".equals(action)) {
223                 String JavaDoc feature = pp.nextText();
224                 verbose(">>> "+action+" "+feature);
225                 testParser.setFeature(feature, true);
226             } else {
227                 verbose(">>> UNKNONW ACTION '"+action+"' ("+pp.getPositionDescription()+")");
228                 String JavaDoc content = pp.nextText();
229                 if(content.length() > 0) {
230                     if(verbose) System.err.println(">>> CONTENT='"+printable(content)+"'");
231                 }
232
233             }
234         }
235
236         pp.require(pp.END_TAG, NS_URI, "test-parser");
237         verbose(">> END TEST '"+testName+"'");
238     }
239
240
241     private int convertToEventType(XmlPullParser pp, String JavaDoc eventName) {
242         for (int i = 0; i < pp.TYPES.length; i++)
243         {
244             if( eventName.equals(pp.TYPES[ i ]) ) {
245                 return i;
246             }
247         }
248         throw new RuntimeException JavaDoc("unknown event type "+eventName+pp.getPositionDescription());
249     }
250
251 }
252
253
Popular Tags