KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > datatype > xsd > regex > test > TestDriver


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 JavaDoc;
10 import org.xml.sax.InputSource JavaDoc;
11 import org.xml.sax.Locator JavaDoc;
12 import org.xml.sax.SAXException JavaDoc;
13 import org.xml.sax.XMLReader JavaDoc;
14 import org.xml.sax.helpers.DefaultHandler JavaDoc;
15
16 import javax.xml.parsers.SAXParserFactory JavaDoc;
17 import javax.xml.parsers.ParserConfigurationException JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.util.Enumeration JavaDoc;
20
21 public class TestDriver extends DefaultHandler JavaDoc {
22   private final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
23   private Regex regex;
24   private int nFail = 0;
25   private int nTests = 0;
26   private Locator JavaDoc loc;
27   private final RegexEngine engine;
28
29   static public void main(String JavaDoc[] args) throws SAXException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc {
30     if (args.length != 2) {
31       System.err.println("usage: TestDriver class testfile");
32       System.exit(2);
33     }
34     SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
35     factory.setNamespaceAware(true);
36     factory.setValidating(false);
37     XMLReader JavaDoc xr = factory.newSAXParser().getXMLReader();
38
39     Enumeration JavaDoc 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 JavaDoc in = new InputSource JavaDoc(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 JavaDoc locator) {
65     this.loc = locator;
66   }
67
68   public void characters(char ch[], int start, int length)
69           throws SAXException JavaDoc {
70     buf.append(ch, start, length);
71   }
72
73   public void ignorableWhitespace(char ch[], int start, int length)
74           throws SAXException JavaDoc {
75     buf.append(ch, start, length);
76   }
77
78   public void startElement(String JavaDoc uri, String JavaDoc localName,
79                            String JavaDoc qName, Attributes JavaDoc attributes)
80           throws SAXException JavaDoc {
81     buf.setLength(0);
82   }
83
84   public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
85           throws SAXException JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc ERROR_MARKER = ">>>>";
144
145   static String JavaDoc display(String JavaDoc str, int pos) {
146     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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("&amp;");
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