| 1 |
package com.thaiopensource.relaxng.translate.test; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.edit.SchemaCollection; |
| 4 |
import com.thaiopensource.relaxng.input.InputFailedException; |
| 5 |
import com.thaiopensource.relaxng.input.InputFormat; |
| 6 |
import com.thaiopensource.relaxng.input.parse.compact.CompactParseInputFormat; |
| 7 |
import com.thaiopensource.relaxng.output.LocalOutputDirectory; |
| 8 |
import com.thaiopensource.relaxng.output.OutputDirectory; |
| 9 |
import com.thaiopensource.relaxng.output.OutputFailedException; |
| 10 |
import com.thaiopensource.relaxng.output.OutputFormat; |
| 11 |
import com.thaiopensource.relaxng.output.rnc.RncOutputFormat; |
| 12 |
import com.thaiopensource.relaxng.output.rng.RngOutputFormat; |
| 13 |
import com.thaiopensource.relaxng.output.xsd.XsdOutputFormat; |
| 14 |
import com.thaiopensource.relaxng.translate.util.InvalidParamsException; |
| 15 |
import com.thaiopensource.resolver.xml.sax.SAXResolver; |
| 16 |
import com.thaiopensource.util.UriOrFile; |
| 17 |
import com.thaiopensource.xml.sax.ErrorHandlerImpl; |
| 18 |
import org.xml.sax.ErrorHandler; |
| 19 |
import org.xml.sax.SAXException; |
| 20 |
|
| 21 |
import java.io.BufferedWriter; |
| 22 |
import java.io.File; |
| 23 |
import java.io.FileOutputStream; |
| 24 |
import java.io.IOException; |
| 25 |
import java.io.OutputStreamWriter; |
| 26 |
|
| 27 |
public class CompactTestDriver { |
| 28 |
|
| 29 |
private final SAXResolver saxResolver = new SAXResolver(); |
| 30 |
private ErrorHandler eh; |
| 31 |
private final InputFormat inputFormat = new CompactParseInputFormat(); |
| 32 |
private OutputFormat outputFormat; |
| 33 |
private OutputFormat compactOutputFormat; |
| 34 |
private String toDir; |
| 35 |
private String toExt; |
| 36 |
|
| 37 |
private CompactTestDriver() { |
| 38 |
} |
| 39 |
|
| 40 |
static public void main(String[] args) throws IOException { |
| 41 |
System.exit(new CompactTestDriver().doMain(args)); |
| 42 |
} |
| 43 |
|
| 44 |
private int doMain(String[] args) throws IOException { |
| 45 |
eh = new ErrorHandlerImpl(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[0])))); |
| 46 |
if (args[2].equals("xsd")) { |
| 47 |
outputFormat = new XsdOutputFormat(); |
| 48 |
toExt = XSD_EXTENSION; |
| 49 |
toDir = XSD_DIR; |
| 50 |
} |
| 51 |
else { |
| 52 |
outputFormat = new RngOutputFormat(); |
| 53 |
compactOutputFormat = new RncOutputFormat(); |
| 54 |
toExt = XML_EXTENSION; |
| 55 |
toDir = XML_DIR; |
| 56 |
} |
| 57 |
return runTestSuite(new File(args[1])) ? 0 : 1; |
| 58 |
} |
| 59 |
|
| 60 |
private boolean runTestSuite(File dir) throws IOException { |
| 61 |
boolean passed = true; |
| 62 |
String[] subdirs = dir.list(); |
| 63 |
for (int i = 0; i < subdirs.length; i++) { |
| 64 |
File subdir = new File(dir, subdirs[i]); |
| 65 |
if (subdir.isDirectory()) { |
| 66 |
if (!runTestCase(subdir)) |
| 67 |
passed = false; |
| 68 |
} |
| 69 |
} |
| 70 |
return passed; |
| 71 |
} |
| 72 |
|
| 73 |
static private final String XML_DIR = "xml"; |
| 74 |
static private final String XSD_DIR = "xsd"; |
| 75 |
static private final String COMPACT_DIR = "compact"; |
| 76 |
static private final String OUT_DIR = "out"; |
| 77 |
static private final String CORRECT_SCHEMA_NAME = "c"; |
| 78 |
static private final String INCORRECT_SCHEMA_NAME = "i"; |
| 79 |
static private final String COMPACT_EXTENSION = ".rnc"; |
| 80 |
static private final String XML_EXTENSION = ".rng"; |
| 81 |
static private final String XSD_EXTENSION = ".xsd"; |
| 82 |
static private final String OUTPUT_ENCODING = "UTF-8"; |
| 83 |
static private final int LINE_LENGTH = 72; |
| 84 |
static private final int INDENT = 2; |
| 85 |
|
| 86 |
private boolean runTestCase(File dir) throws IOException { |
| 87 |
File xmlDir = new File(dir, toDir); |
| 88 |
File compactDir = new File(dir, COMPACT_DIR); |
| 89 |
File outputDir = new File(dir, OUT_DIR); |
| 90 |
File correct = new File(compactDir, CORRECT_SCHEMA_NAME + COMPACT_EXTENSION); |
| 91 |
File incorrect = new File(compactDir, INCORRECT_SCHEMA_NAME + COMPACT_EXTENSION); |
| 92 |
boolean passed = true; |
| 93 |
if (correct.exists()) { |
| 94 |
File output = new File(outputDir, CORRECT_SCHEMA_NAME + toExt); |
| 95 |
if (!run(correct, output, outputFormat, toExt) |
| 96 |
|| !compareDir(xmlDir, outputDir)) { |
| 97 |
passed = false; |
| 98 |
failed(correct); |
| 99 |
} |
| 100 |
else if (toExt.equals(XML_EXTENSION)) { |
| 101 |
cleanDir(outputDir); |
| 102 |
File output2 = new File(outputDir, CORRECT_SCHEMA_NAME + COMPACT_EXTENSION); |
| 103 |
if (!run(correct, output2, compactOutputFormat, COMPACT_EXTENSION) |
| 104 |
|| !run(output2, output, outputFormat, toExt) |
| 105 |
|| !compareDir(xmlDir, outputDir)) { |
| 106 |
passed = false; |
| 107 |
failed(correct); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
if (incorrect.exists()) { |
| 112 |
File output = new File(outputDir, INCORRECT_SCHEMA_NAME + toExt); |
| 113 |
if (run(incorrect, output, outputFormat, toExt)) { |
| 114 |
passed = false; |
| 115 |
failed(incorrect); |
| 116 |
} |
| 117 |
} |
| 118 |
return passed; |
| 119 |
} |
| 120 |
|
| 121 |
private boolean compareDir(File goodDir, File testDir) { |
| 122 |
try { |
| 123 |
String[] files = goodDir.list(); |
| 124 |
for (int i = 0; i < files.length; i++) { |
| 125 |
File file = new File(goodDir, files[i]); |
| 126 |
if (file.isDirectory()) { |
| 127 |
if (!compareDir(file, new File(testDir, files[i]))) |
| 128 |
return false; |
| 129 |
} |
| 130 |
else if (!Compare.compare(file, new File(testDir, files[i]), saxResolver)) |
| 131 |
return false; |
| 132 |
} |
| 133 |
return true; |
| 134 |
} |
| 135 |
catch (SAXException e) { |
| 136 |
} |
| 137 |
catch (IOException e) { |
| 138 |
} |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
private void cleanDir(File dir) { |
| 143 |
String[] files = dir.list(); |
| 144 |
for (int i = 0; i < files.length; i++) { |
| 145 |
File file = new File(dir, files[i]); |
| 146 |
if (file.isDirectory()) |
| 147 |
cleanDir(file); |
| 148 |
file.delete(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
private static void failed(File f) { |
| 153 |
System.err.println(f.toString() + " failed"); |
| 154 |
} |
| 155 |
|
| 156 |
private boolean run(File in, File out, OutputFormat of, String outExt) throws IOException { |
| 157 |
try { |
| 158 |
SchemaCollection sc = inputFormat.load(UriOrFile.fileToUri(in), new String[0], null, eh, saxResolver.getResolver()); |
| 159 |
OutputDirectory od = new LocalOutputDirectory(sc.getMainUri(), out, outExt, null, LINE_LENGTH, INDENT); |
| 160 |
od.setEncoding(OUTPUT_ENCODING); |
| 161 |
of.output(sc, od, new String[0], null, eh); |
| 162 |
return true; |
| 163 |
} |
| 164 |
catch (SAXException e) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
catch (InvalidParamsException e) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
catch (InputFailedException e) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
catch (OutputFailedException e) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
} |