1 package com.thaiopensource.datatype.xsd.regex.test; 2 3 import com.thaiopensource.datatype.xsd.regex.Regex; 4 import com.thaiopensource.datatype.xsd.regex.RegexEngine; 5 import com.thaiopensource.datatype.xsd.regex.RegexSyntaxException; 6 import com.thaiopensource.util.Service; 7 import com.thaiopensource.util.UriOrFile; 8 import com.thaiopensource.util.Utf16; 9 import org.xml.sax.Attributes ; 10 import org.xml.sax.InputSource ; 11 import org.xml.sax.Locator ; 12 import org.xml.sax.SAXException ; 13 import org.xml.sax.XMLReader ; 14 import org.xml.sax.helpers.DefaultHandler ; 15 16 import javax.xml.parsers.SAXParserFactory ; 17 import javax.xml.parsers.ParserConfigurationException ; 18 import java.io.IOException ; 19 import java.util.Enumeration ; 20 21 public class TestDriver extends DefaultHandler { 22 private final StringBuffer buf = new StringBuffer (); 23 private Regex regex; 24 private int nFail = 0; 25 private int nTests = 0; 26 private Locator loc; 27 private final RegexEngine engine; 28 29 static public void main(String [] args) throws SAXException , IOException , ParserConfigurationException { 30 if (args.length != 2) { 31 System.err.println("usage: TestDriver class testfile"); 32 System.exit(2); 33 } 34 SAXParserFactory factory = SAXParserFactory.newInstance(); 35 factory.setNamespaceAware(true); 36 factory.setValidating(false); 37 XMLReader xr = factory.newSAXParser().getXMLReader(); 38 39 Enumeration e = new Service(RegexEngine.class).getProviders(); 40 RegexEngine engine; 41 for (;;) { 42 if (!e.hasMoreElements()) { 43 System.err.println("couldn't find regex engine"); 44 System.exit(2); 45 } 46 engine = (RegexEngine)e.nextElement(); 47 if (engine.getClass().getName().equals(args[0])) 48 break; 49 } 50 TestDriver tester = new TestDriver(engine); 51 xr.setContentHandler(tester); 52 InputSource in = new InputSource (UriOrFile.fileToUri(args[1])); 53 xr.parse(in); 54 System.err.println(tester.nTests + " tests performed"); 55 System.err.println(tester.nFail + " failures"); 56 if (tester.nFail > 0) 57 System.exit(1); 58 } 59 60 public TestDriver(RegexEngine engine) { 61 this.engine = engine; 62 } 63 64 public void setDocumentLocator(Locator locator) { 65 this.loc = locator; 66 } 67 68 public void characters(char ch[], int start, int length) 69 throws SAXException { 70 buf.append(ch, start, length); 71 } 72 73 public void ignorableWhitespace(char ch[], int start, int length) 74 throws SAXException { 75 buf.append(ch, start, length); 76 } 77 78 public void startElement(String uri, String localName, 79 String qName, Attributes attributes) 80 throws SAXException { 81 buf.setLength(0); 82 } 83 84 public void endElement(String uri, String localName, String qName) 85 throws SAXException { 86 if (localName.equals("valid")) 87 valid(buf.toString()); 88 else if (localName.equals("invalid")) 89 invalid(buf.toString()); 90 else if (localName.equals("correct")) 91 correct(buf.toString()); 92 else if (localName.equals("incorrect")) 93 incorrect(buf.toString()); 94 } 95 96 private void correct(String str) { 97 nTests++; 98 regex = null; 99 try { 100 regex = engine.compile(str); 101 } 102 catch (RegexSyntaxException e) { 103 error("unexpected error: " + e.getMessage() + ": " + display(str, e.getPosition())); 104 } 105 } 106 107 private void incorrect(String str) { 108 nTests++; 109 regex = null; 110 try { 111 engine.compile(str); 112 error("failed to detect error in regex: " + display(str, -1)); 113 } 114 catch (RegexSyntaxException e) { } 115 } 116 117 private void valid(String str) { 118 if (regex == null) 119 return; 120 nTests++; 121 if (!regex.matches(str)) 122 error("match failed for string: " + display(str, -1)); 123 } 124 125 private void invalid(String str) { 126 if (regex == null) 127 return; 128 nTests++; 129 if (regex.matches(str)) 130 error("match incorrectly succeeded for string: " + display(str, -1)); 131 } 132 133 private void error(String str) { 134 int line = -1; 135 if (loc != null) 136 line = loc.getLineNumber(); 137 if (line >= 0) 138 System.err.print("Line " + line + ": "); 139 System.err.println(str); 140 nFail++; 141 } 142 143 static final private String ERROR_MARKER = ">>>>"; 144 145 static String display(String str, int pos) { 146 StringBuffer buf = new StringBuffer (); 147 for (int i = 0, len = str.length(); i < len; i++) { 148 if (i == pos) 149 buf.append(ERROR_MARKER); 150 char c = str.charAt(i); 151 if (Utf16.isSurrogate1(c)) 152 buf.append("&#x" + Integer.toHexString(Utf16.scalarValue(c, str.charAt(++i))) + ";"); 153 else if (c < ' ' || c >= 0x7F) 154 buf.append("&#x" + Integer.toHexString(c) + ";"); 155 else if (c == '&') 156 buf.append("&"); 157 else 158 buf.append(c); 159 } 160 if (str.length() == pos) 161 buf.append(ERROR_MARKER); 162 return buf.toString(); 163 } 164 165 } 166 | Popular Tags |