2222import java .io .InputStream ;
2323import java .net .MalformedURLException ;
2424import 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;
3740 */
3841public 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 ("\t Expected:" );
538+ System .err .print ("\t V:" +awaitedResult .isValid ());
539+ System .err .print ("\t E:" +awaitedResult .getErrors ());
540+ System .err .println ("\t W:" +awaitedResult .getWarnings ());
541+ System .err .print ("\t Result:\t " );
542+ System .err .print ("\t V:" +result .isValid ());
543+ System .err .print ("\t E:" +result .getErrors ());
544+ System .err .println ("\t W:" +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}
0 commit comments