Debian Codegraph @

mod/rng-validate/src/main/com/thaiopensource/relaxng/util/TestDriver.java

raw
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.prop.rng.RngProperty;
9 import com.thaiopensource.xml.sax.ErrorHandlerImpl;
10 import com.thaiopensource.datatype.DatatypeLibraryLoader;
11 import org.xml.sax.SAXException;
12
13 import java.io.BufferedWriter;
14 import java.io.File;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.io.OutputStreamWriter;
18
19 class TestDriver {
20 static public void main(String[] args) throws IOException {
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[] args) throws IOException {
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 properties.put(RngProperty.DATATYPE_LIBRARY_FACTORY, 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(new OutputStreamWriter(new FileOutputStream(args[0]))));
56 properties.put(ValidateProperty.ERROR_HANDLER, 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(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 CORRECT_SCHEMA_NAME = "c.rng";
71 private static final String INCORRECT_SCHEMA_NAME = "i.rng";
72 private static final String VALID_INSTANCE_SUFFIX = ".v.xml";
73 private static final String INVALID_INSTANCE_SUFFIX = ".i.xml";
74
75 public int runTestSuite(File dir) throws IOException {
76 int result = 0;
77 String[] subdirs = dir.list();
78 for (int i = 0; i < subdirs.length; i++) {
79 File subdir = new File(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 dir) throws IOException {
90 File f = new File(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(dir, CORRECT_SCHEMA_NAME);
99 if (!f.exists())
100 return 0;
101 if (!loadSchema(f)) {
102 failed(f);
103 return 1;
104 }
105 String[] 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(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(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 f) {
127 System.err.println("Failed: " + f.toString());
128 }
129
130 private boolean loadSchema(File schema) throws IOException {
131 nTests++;
132 try {
133 if (driver.loadSchema(ValidationDriver.fileInputSource(schema)))
134 return true;
135 }
136 catch (SAXException e) {
137 eh.printException(e);
138 }
139 return false;
140 }
141
142 private boolean validateInstance(File instance) throws IOException {
143 nTests++;
144 try {
145 if (driver.validate(ValidationDriver.fileInputSource(instance)))
146 return true;
147 }
148 catch (SAXException e) {
149 eh.printException(e);
150 }
151 return false;
152 }
153 }