KickJava   Java API By Example, From Geeks To Geeks.

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


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.Test;
7 import junit.framework.TestResult;
8 import junit.framework.TestSuite;
9 import junit.textui.TestRunner;
10 import org.xmlpull.v1.XmlPullParser;
11 import org.xmlpull.v1.XmlPullParserFactory;
12 import org.xmlpull.v1.XmlSerializer;
13
14 /**
15  * TODO: add tests for
16  * <pre>
17  * test mixed next() with nextToken()
18  * </pre>
19  *
20  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
21  */

22 public class PackageTests extends TestRunner
23 {
24     private static boolean runAll;
25
26     public static boolean runnigAllTests() { return runAll; }
27
28     public PackageTests()
29     {
30         super();
31     }
32
33     public static Test suite()
34     {
35         TestSuite suite = new TestSuite("XmlPull V1 API TESTS");
36
37         suite.addTestSuite(TestFactory.class);
38         suite.addTestSuite(TestSimple.class);
39         suite.addTestSuite(TestSimpleWithNs.class);
40         suite.addTestSuite(TestSerialize.class);
41         suite.addTestSuite(TestSerializeWithNs.class);
42         suite.addTestSuite(TestSimpleToken.class);
43         suite.addTestSuite(TestAttributes.class);
44         suite.addTestSuite(TestEolNormalization.class);
45         suite.addTestSuite(TestEntityReplacement.class);
46         suite.addTestSuite(TestCdsect.class);
47         suite.addTestSuite(TestEvent.class);
48         suite.addTestSuite(TestToken.class);
49         suite.addTestSuite(TestMisc.class);
50         suite.addTestSuite(TestSetInput.class);
51         suite.addTestSuite(TestSimpleProcessDocdecl.class);
52         suite.addTestSuite(TestSimpleValidation.class);
53         suite.addTestSuite(TestProcessDocdecl.class);
54
55         // finally run tests based on XML input files
56
suite.addTestSuite(TestBootstrapXmlTests.class);
57         suite.addTestSuite(TestXmlSimple.class);
58         suite.addTestSuite(TestXmlTypical.class);
59         suite.addTestSuite(TestXmlCdsect.class);
60
61
62         return suite;
63     }
64
65     public synchronized void startTest(Test test) {
66         writer().print(".");
67         //if (fColumn++ >= 40) {
68
// writer().println();
69
// fColumn= 0;
70
//}
71
}
72
73     private static StringBuffer JavaDoc notes = new StringBuffer JavaDoc();
74     public static void addNote(String JavaDoc note) {
75         notes.append(note);
76     }
77
78
79     public void runPackageTests(String JavaDoc testFactoryName) {
80         writer().println("Executing XMLPULL tests"
81                              +(testFactoryName != null ? " for '"+testFactoryName+"'" : ""));
82         notes.setLength(0);
83         XmlPullParserFactory f = null;
84         try {
85             f = UtilTestCase.factoryNewInstance();
86             addNote("* factory "+f.getClass()+"\n");//+" created from property "
87
//+System.getProperty(XmlPullParserFactory.PROPERTY_NAME)+"\n");
88
} catch (Exception JavaDoc ex) {
89             System.err.println(
90                 "ERROR: tests aborted - could not create instance of XmlPullParserFactory:");
91             ex.printStackTrace();
92             System.exit(1);
93         }
94         XmlPullParser parser = null;
95         try {
96             parser=f.newPullParser();
97         } catch (Exception JavaDoc ex) {
98             System.err.println(
99                 "ERROR: tests aborted - could not create instance of XmlPullParser from factory "
100                     +f.getClass());
101             ex.printStackTrace();
102             System.exit(2);
103         }
104
105         XmlSerializer serializer = null;
106         try {
107             serializer=f.newSerializer();
108         } catch (Exception JavaDoc ex) {
109             System.err.println(
110                 "ERROR: tests aborted - could not create instance of XmlSerializer from factory "
111                     +f.getClass());
112             ex.printStackTrace();
113             System.exit(3);
114         }
115         String JavaDoc name = getClass().getName();
116         name = name.substring(name.lastIndexOf('.')+1);
117         System.out.println(name+" uses factory="+f.getClass().getName()
118                                +" parser="+parser.getClass().getName()
119                                +" serializer="+serializer.getClass().getName()
120                           );
121
122         // now run all tests ...
123
//junit.textui.TestRunner.run(suite());
124
TestRunner aTestRunner= new TestRunner();
125         try {
126             TestResult r = doRun(suite(), false);
127             if (!r.wasSuccessful())
128                 System.exit(-1);
129             //System.exit(0);
130
} catch(Exception JavaDoc e) {
131             System.err.println(e.getMessage());
132             System.exit(-2);
133         }
134
135         if(notes.length() > 0) {
136             writer().println("Test results "
137                                  +(testFactoryName != null ? "for '"+testFactoryName+"'" : "")
138                                  +"\n"+notes+"\n");
139         }
140     }
141
142     public void printFinalReport() {
143         writer().println("\nAll tests were passed.");
144     }
145
146     public static void main (String JavaDoc[] args) {
147         final PackageTests driver = new PackageTests();
148         final String JavaDoc listOfTests = System.getProperty("org.xmlpull.v1.tests");
149         final String JavaDoc name = XmlPullParserFactory.PROPERTY_NAME;
150         final String JavaDoc oldValue = System.getProperty(name);
151         runAll = true;
152         if(listOfTests != null) {
153             int pos = 0;
154             while (pos < listOfTests.length()) {
155                 int cut = listOfTests.indexOf(':', pos);
156                 if (cut == -1) cut = listOfTests.length();
157                 String JavaDoc testFactoryName = listOfTests.substring(pos, cut);
158                 if("DEFAULT".equals(testFactoryName)) {
159                     if(oldValue != null) {
160                         System.setProperty(name, oldValue);
161                     } else {
162                         // overcoming limitation of System.setProperty not allowing
163
// null or empty values (who knows how to unset property ?!)
164
System.setProperty(name, "DEFAULT");
165                     }
166                 } else {
167                     System.setProperty(name, testFactoryName);
168                 }
169                 driver.runPackageTests(testFactoryName);
170                 pos = cut + 1;
171             }
172             driver.printFinalReport();
173
174         } else {
175             driver.runPackageTests(null);
176         }
177         System.exit(0);
178     }
179
180 }
181
182
Popular Tags