KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > relaxng > util > TestDriver


1 package com.thaiopensource.relaxng.util;
2
3 import com.thaiopensource.util.OptionParser;
4 import com.thaiopensource.util.PropertyMapBuilder;
5 import com.thaiopensource.util.Localizer;
6 import com.thaiopensource.validate.ValidateProperty;
7 import com.thaiopensource.validate.ValidationDriver;
8 import com.thaiopensource.validate.rng.RngProperty;
9 import com.thaiopensource.xml.sax.ErrorHandlerImpl;
10 import org.xml.sax.SAXException JavaDoc;
11 import org.relaxng.datatype.helpers.DatatypeLibraryLoader;
12
13 import java.io.BufferedWriter JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.OutputStreamWriter JavaDoc;
18
19 class TestDriver {
20   static public void main(String JavaDoc[] args) throws IOException JavaDoc {
21     System.exit(new TestDriver().doMain(args));
22   }
23
24   private ValidationDriver driver;
25   private ErrorHandlerImpl eh;
26   private Localizer localizer = new Localizer(TestDriver.class);
27   private int nTests = 0;
28
29   public int doMain(String JavaDoc[] args) throws IOException JavaDoc {
30     long startTime = System.currentTimeMillis();
31     eh = new ErrorHandlerImpl(System.out);
32     OptionParser op = new OptionParser("i", args);
33     PropertyMapBuilder properties = new PropertyMapBuilder();
34     // This is an optimization. It ensures that all SchemaReaders share a
35
// single DatatypeLibraryLoader.
36
RngProperty.DATATYPE_LIBRARY_FACTORY.put(properties, new DatatypeLibraryLoader());
37     try {
38       while (op.moveToNextOption()) {
39         switch (op.getOptionChar()) {
40         case 'i':
41           RngProperty.CHECK_ID_IDREF.add(properties);
42           break;
43         }
44       }
45     }
46     catch (OptionParser.InvalidOptionException e) {
47       eh.print(localizer.message("invalid_option", op.getOptionCharString()));
48       return 2;
49     }
50     catch (OptionParser.MissingArgumentException e) {
51       eh.print(localizer.message("option_missing_argument", op.getOptionCharString()));
52       return 2;
53     }
54     args = op.getRemainingArgs();
55     eh = new ErrorHandlerImpl(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(args[0]))));
56     ValidateProperty.ERROR_HANDLER.put(properties, eh);
57     driver = new ValidationDriver(properties.toPropertyMap());
58     int result = 0;
59     for (int i = 1; i < args.length; i++) {
60       int n = runTestSuite(new File JavaDoc(args[i]));
61       if (n > result)
62     result = n;
63     }
64     System.err.println("Number of tests: " + nTests);
65     System.err.println("Elapsed time: " + (System.currentTimeMillis() - startTime));
66     eh.close();
67     return result;
68   }
69
70   private static final String JavaDoc CORRECT_SCHEMA_NAME = "c.rng";
71   private static final String JavaDoc INCORRECT_SCHEMA_NAME = "i.rng";
72   private static final String JavaDoc VALID_INSTANCE_SUFFIX = ".v.xml";
73   private static final String JavaDoc INVALID_INSTANCE_SUFFIX = ".i.xml";
74   
75   public int runTestSuite(File JavaDoc dir) throws IOException JavaDoc {
76     int result = 0;
77     String JavaDoc[] subdirs = dir.list();
78     for (int i = 0; i < subdirs.length; i++) {
79       File JavaDoc subdir = new File JavaDoc(dir, subdirs[i]);
80       if (subdir.isDirectory()) {
81     int n = runTestCase(subdir);
82     if (n > result)
83       result = n;
84       }
85     }
86     return result;
87   }
88
89   private int runTestCase(File JavaDoc dir) throws IOException JavaDoc {
90     File JavaDoc f = new File JavaDoc(dir, INCORRECT_SCHEMA_NAME);
91     if (f.exists()) {
92       if (loadSchema(f)) {
93     failed(f);
94     return 1;
95       }
96       return 0;
97     }
98     f = new File JavaDoc(dir, CORRECT_SCHEMA_NAME);
99     if (!f.exists())
100       return 0;
101     if (!loadSchema(f)) {
102       failed(f);
103       return 1;
104     }
105     String JavaDoc[] files = dir.list();
106     int result = 0;
107     for (int i = 0; i < files.length; i++) {
108       if (files[i].endsWith(VALID_INSTANCE_SUFFIX)) {
109     f = new File JavaDoc(dir, files[i]);
110     if (!validateInstance(f)) {
111       failed(f);
112       result = 1;
113     }
114       }
115       else if (files[i].endsWith(INVALID_INSTANCE_SUFFIX)) {
116     f = new File JavaDoc(dir, files[i]);
117     if (validateInstance(f)) {
118       failed(f);
119       result = 1;
120     }
121       }
122     }
123     return result;
124   }
125
126   private static void failed(File JavaDoc f) {
127     System.err.println("Failed: " + f.toString());
128   }
129
130   private boolean loadSchema(File JavaDoc schema) throws IOException JavaDoc {
131     nTests++;
132     try {
133       if (driver.loadSchema(ValidationDriver.fileInputSource(schema)))
134     return true;
135     }
136     catch (SAXException JavaDoc e) {
137       eh.printException(e);
138     }
139     return false;
140   }
141
142   private boolean validateInstance(File JavaDoc instance) throws IOException JavaDoc {
143     nTests++;
144     try {
145       if (driver.validate(ValidationDriver.fileInputSource(instance)))
146     return true;
147     }
148     catch (SAXException JavaDoc e) {
149       eh.printException(e);
150     }
151     return false;
152   }
153 }
154
Popular Tags