Skip to content

Commit 74ef256

Browse files
committed
allow running autotest towards jar instead of web service
Permit to build autotest without running the tests. This add a 2nd argument to the AutoTest.main which is the engine to use for tests. It can be either 'servlet' (URL to css-validator servlet at jigsaw.w3.org) or 'jar' (the presently built css-validator.jar) or 'cli' (a command line wrapper script called css-validator'
1 parent 3e0649b commit 74ef256

File tree

4 files changed

+179
-24
lines changed

4 files changed

+179
-24
lines changed

autotest/AutoTest.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@
3232
public class AutoTest {
3333

3434
XMLReader saxReader;
35+
AutoTestContentHandler autoTestContentHandler;
3536

3637
/**
3738
* Constructor.
3839
*
3940
* @throws SAXException
4041
*/
41-
public AutoTest() throws SAXException {
42+
public AutoTest(String testInstance) throws SAXException {
4243
saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
43-
saxReader.setContentHandler(new AutoTestContentHandler());
44+
autoTestContentHandler = new AutoTestContentHandler(testInstance);
45+
saxReader.setContentHandler(autoTestContentHandler);
4446
}
4547

4648
/**
@@ -62,18 +64,48 @@ public void parse(String uri) throws IOException, SAXException {
6264
* list of arguments of the program: uri [options]
6365
*/
6466
public static void main(String[] args) {
65-
if (args.length != 1) {
66-
System.out.println("Usage : AutoTest uri");
67+
if (args.length == 0) {
68+
System.out.println("Usage : AutoTest uri [instance]");
6769
System.exit(1);
6870
}
6971

70-
String uri = args[0];
72+
String uri = null;
73+
String instance = null;
7174

75+
if (args.length >= 1) {
76+
uri = args[0];
77+
}
78+
79+
if (args.length >= 2) {
80+
instance = args[1];
81+
}
82+
83+
System.out.println("Processing: " + uri);
7284
try {
73-
AutoTest parser = new AutoTest();
85+
AutoTest parser = new AutoTest(instance);
7486
parser.parse(uri);
87+
if (parser.autoTestContentHandler.hasErrors()) {
88+
System.err.println("\tTest outcome:" +
89+
" " +
90+
parser.autoTestContentHandler.getTestSuccessCount() +
91+
" success(es)." +
92+
" " +
93+
parser.autoTestContentHandler.getTestFailCount() +
94+
" failure(s)."
95+
);
96+
System.exit(1);
97+
} else {
98+
System.out.println("\tTest outcome:" +
99+
" " +
100+
parser.autoTestContentHandler.getTestSuccessCount() +
101+
" success(es)." +
102+
" " +
103+
parser.autoTestContentHandler.getTestFailCount() +
104+
" failure(s)."
105+
);
106+
}
75107
} catch (Throwable t) {
76108
t.printStackTrace();
77109
}
78110
}
79-
}
111+
}

autotest/AutoTestContentHandler.java

Lines changed: 123 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.io.InputStream;
2323
import java.net.MalformedURLException;
2424
import java.net.URL;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.Map;
2528

2629

2730
//import org.xml.sax.helpers.LocatorImpl;
@@ -37,6 +40,7 @@
3740
*/
3841
public class AutoTestContentHandler implements ContentHandler {
3942

43+
public static final String CLI_PARAMS = "--output=soap12";
4044
public static final String VALIDATOR = "http://jigsaw.w3.org/css-validator/validator?";
4145
public static final String PARAMS = "&output=soap12";
4246
public static final int TESTSUITE = "testsuite".hashCode();
@@ -63,6 +67,8 @@ public class AutoTestContentHandler implements ContentHandler {
6367
boolean inDesc = false;
6468
boolean inErrors = false;
6569
boolean inWarnings = false;
70+
int testFailCount = 0;
71+
int testSuccessCount = 0;
6672
String urlString = "";
6773
String file = "";
6874
String desc = "";
@@ -71,14 +77,18 @@ public class AutoTestContentHandler implements ContentHandler {
7177
String profile;
7278
String warning;
7379
String medium;
80+
String testInstance = "servlet";
7481

7582
/**
7683
* Default Constructor.
7784
*/
78-
public AutoTestContentHandler() {
85+
public AutoTestContentHandler(String testInstance) {
7986
super();
8087
// On definit le locator par defaut.
8188
// locator = new LocatorImpl();
89+
if (testInstance != null) {
90+
this.testInstance = testInstance;
91+
}
8292
}
8393

8494
/**
@@ -259,9 +269,10 @@ public void endElement(String nameSpaceURI, String localName, String rawName)
259269
System.err.println(e.getMessage());
260270
}
261271
} else if (element == TEST) {
262-
String val;
263-
System.err.println(urlString);
272+
System.out.print(urlString + "... ");
264273
String validURL = createValidURL(urlString);
274+
String val;
275+
List<String> command = new ArrayList<>();
265276
if (isFile) {
266277
InputStream content;
267278
String text = "";
@@ -283,32 +294,63 @@ public void endElement(String nameSpaceURI, String localName, String rawName)
283294

284295
if (warning != null) {
285296
val += "&warning=" + warning;
297+
command.add("--warning=" + warning);
286298
}
287299
if (profile != null) {
288300
val += "&profile=" + profile;
301+
command.add("--profile=" + profile);
289302
}
290303
if (medium != null) {
291304
val += "&medium=" + medium;
305+
command.add("--medium=" + medium);
292306
}
293307
val += PARAMS;
308+
command.add(CLI_PARAMS);
309+
310+
if (isFile) {
311+
command.add("file:" + urlString);
312+
} else {
313+
command.add(urlString);
314+
}
294315

295316
try {
296-
HttpManager manager = HttpManager.getManager();
297-
Request request = manager.createRequest();
298-
request.setMethod(HTTP.GET);
299-
System.err.println(val);
300-
request.setURL(new URL(val));
301-
Reply reply = manager.runRequest(request);
302-
// Get the reply input stream that contains the actual data:
303-
InputStream res = reply.getInputStream();
317+
InputStream res = null;
318+
Reply reply = null;
319+
if (testInstance.equals("servlet")) {
320+
HttpManager manager = HttpManager.getManager();
321+
Request request = manager.createRequest();
322+
request.setMethod(HTTP.GET);
323+
// System.out.println(val);
324+
request.setURL(new URL(val));
325+
reply = manager.runRequest(request);
326+
res = reply.getInputStream();
327+
} else if (testInstance.equals("jar")) {
328+
Runtime r = Runtime.getRuntime();
329+
command.add(0, "java");
330+
command.add(1, "org.w3c.css.css.CssValidator");
331+
ProcessBuilder pb = new ProcessBuilder(command);
332+
Map<String, String> env = pb.environment();
333+
env.put("CLASSPATH", env.get("CLASSPATH") + ":css-validator.jar");
334+
Process p = pb.start();
335+
res = p.getInputStream();
336+
} else if (testInstance.equals("cli")) {
337+
Runtime r = Runtime.getRuntime();
338+
command.add(0, "css-validator");
339+
ProcessBuilder pb = new ProcessBuilder(command);
340+
Process p = pb.start();
341+
res = p.getInputStream();
342+
} else {
343+
System.err.println("Unsupported operation. Invalid instance or instance not set: " + testInstance);
344+
System.exit(2);
345+
}
304346

305347
int currentChar;
306348
StringBuffer buf = new StringBuffer();
307349
while ((currentChar = res.read()) != -1) {
308350
buf.append((char) currentChar);
309351
}
310352

311-
if (reply.getStatus() == 500) { // Internal Server Error
353+
if (testInstance.equals("servlet") && reply.getStatus() == 500) { // Internal Server Error
312354
if (buf.indexOf("env:Sender") != -1) {
313355
printError(val, "Reply status code: 500<br/>"
314356
+ "Invalid URL: Sender error");
@@ -342,14 +384,18 @@ public void endElement(String nameSpaceURI, String localName, String rawName)
342384
result.setWarnings(Integer.parseInt(warn));
343385
}
344386
printResult(val.substring(0, val.length() - 14));
387+
printResultToConsole(urlString);
345388
}
346389

347390
} catch (MalformedURLException e) {
348391
printError(val, e.getMessage());
392+
printResultToConsole(urlString);
349393
} catch (IOException e) {
350394
printError(val, e.getMessage());
395+
printResultToConsole(urlString);
351396
} catch (HttpException e) {
352397
printError(val, e.getMessage());
398+
printResultToConsole(urlString);
353399
}
354400

355401
isFile = false;
@@ -449,6 +495,56 @@ private void printResult(String validatorPage) {
449495
print(" </div>");
450496
}
451497

498+
/**
499+
* Return whether "Valid" status is equal
500+
*
501+
*/
502+
private boolean isValidEqual() {
503+
return (awaitedResult.isValid() == result.isValid());
504+
}
505+
506+
/**
507+
* Return whether "Warnings" status is equal
508+
*
509+
*/
510+
private boolean isWarningsEqual() {
511+
return (awaitedResult.getWarnings() == result.getWarnings());
512+
}
513+
514+
/**
515+
* Return whether "Errors" status is equal
516+
*
517+
*/
518+
private boolean isErrorsEqual() {
519+
return (awaitedResult.getErrors() == result.getErrors());
520+
}
521+
522+
/**
523+
* Prints an HTML result of a validation to StdOut
524+
*
525+
* @param validatorPage
526+
* the validator page result
527+
*/
528+
private void printResultToConsole(String urlString) {
529+
530+
if (isValidEqual() && isWarningsEqual() && isErrorsEqual()) {
531+
testSuccessCount++;
532+
System.out.println(" Success");
533+
} else {
534+
testFailCount++;
535+
System.out.println(" \u001B[31mFailure\u001B[0m");
536+
System.err.println("\t" + urlString);
537+
System.err.print("\tExpected:");
538+
System.err.print("\tV:"+awaitedResult.isValid());
539+
System.err.print("\tE:"+awaitedResult.getErrors());
540+
System.err.println("\tW:"+awaitedResult.getWarnings());
541+
System.err.print("\tResult:\t");
542+
System.err.print("\tV:"+result.isValid());
543+
System.err.print("\tE:"+result.getErrors());
544+
System.err.println("\tW:"+result.getWarnings());
545+
}
546+
}
547+
452548
/**
453549
* Used when an error occurs
454550
*
@@ -514,4 +610,19 @@ public String createValidURL(String url) {
514610
return res;
515611
}
516612

613+
public boolean hasErrors() {
614+
if (testFailCount > 0) {
615+
return true;
616+
}
617+
return false;
618+
}
619+
620+
public int getTestFailCount() {
621+
return testFailCount;
622+
}
623+
624+
public int getTestSuccessCount() {
625+
return testSuccessCount;
626+
}
627+
517628
}

autotest/autotest.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ javac -classpath css-validator.jar \
1717
echo "Usage: autotest.sh ./testsuite/xml/FILENAME.xml" && \
1818
exit 1
1919

20-
java -cp css-validator.jar autotest.AutoTest "$1"
20+
java -cp css-validator.jar autotest.AutoTest "$1" "${ENGINE:-servlet}"

build.xml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,15 @@
159159
</war>
160160
</target>
161161

162-
<target name="autotest" depends="jar">
162+
<target name="autotest.build" depends="prepare">
163+
<javac srcdir="autotest"
164+
classpathref="build.class.path"
165+
classpath="."
166+
debug="yes"
167+
includeantruntime="false"/>
168+
</target>
169+
170+
<target name="autotest" depends="autotest.build,jar">
163171
<fail message="&#xa0;${line.separator}${line.separator}
164172
Usage: ant autotest -Dtestfile=testsuite/xml/FILENAME.xml">
165173
<condition>
@@ -168,10 +176,10 @@
168176
</not>
169177
</condition>
170178
</fail>
171-
<javac srcdir="autotest" classpath="css-validator.jar"
172-
includeantruntime="false"/>
173-
<java classname="autotest.AutoTest" classpath="css-validator.jar">
179+
<property name="autotest.engine" value="jar"/>
180+
<java classname="autotest.AutoTest" classpath="css-validator.jar:.">
174181
<arg value="${testfile}"/>
182+
<arg value="${autotest.engine}"/>
175183
</java>
176184
</target>
177185

@@ -199,6 +207,10 @@
199207
<delete dir="./build"/>
200208
<delete dir="./javadoc"/>
201209
<delete dir="./tmp"/>
210+
<delete file="autotest/results/results.html"/>
211+
<delete>
212+
<fileset dir="autotest" includes="*.class"/>
213+
</delete>
202214
</target>
203215

204216
</project>

0 commit comments

Comments
 (0)