Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2013-2015 Mozilla Foundation
   *
   * Permission is hereby granted, free of charge, to any person obtaining a
   * copy of this software and associated documentation files (the "Software"),
   * to deal in the Software without restriction, including without limitation
   * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   * and/or sell copies of the Software, and to permit persons to whom the
   * Software is furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  */
 
 package nu.validator.client;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
 
 
 
 
 
 public class TestRunner implements ErrorHandler {
 
     private boolean inError = false;
 
     private boolean emitMessages = false;
 
     private boolean exceptionIsWarning = false;
 
     private boolean expectingError = false;
 
     private Exception exception = null;
 
 
     private PrintWriter err;
 
     private PrintWriter out;
 
     private String schema = "http://s.validator.nu/html5-all.rnc";
 
     private boolean failed = false;
 
     private static File messagesFile;
 
     private static String[] ignoreList = null;
 
     private static boolean writeMessages;
 
     private static boolean verbose;
 
     private String baseDir = null;
 
     private Map<StringStringexpectedMessages;
 
     private Map<StringStringreportedMessages;
 
     public TestRunner() throws IOException {
          = new LinkedHashMap<StringString>();
          = new SimpleDocumentValidator();
         try {
             this. = new PrintWriter(new OutputStreamWriter(.,
                     "UTF-8"));
             this. = new PrintWriter(new OutputStreamWriter(.,
                     "UTF-8"));
         } catch (Exception e) {
             // If this happens, the JDK is too broken anyway
            throw new RuntimeException(e);
        }
    }
    private void checkHtmlFile(File filethrows IOExceptionSAXException {
        if (!file.exists()) {
            if () {
                .println(String.format("\"%s\": warning: File not found.",
                        file.toURI().toURL().toString()));
                .flush();
            }
            return;
        }
        if () {
            .println(file);
            .flush();
        }
        if (isHtml(file)) {
            .checkHtmlFile(filetrue);
        } else if (isXhtml(file)) {
            .checkXmlFile(file);
        } else {
            if () {
                .println(String.format(
                        "\"%s\": warning: File was not checked."
                                + " Files must have a .html, .xhtml, .htm,"
                                + " or .xht extension.",
                        file.toURI().toURL().toString()));
                .flush();
            }
        }
    }
    private boolean isXhtml(File file) {
        String name = file.getName();
        return name.endsWith(".xhtml") || name.endsWith(".xht");
    }
    private boolean isHtml(File file) {
        String name = file.getName();
        return name.endsWith(".html") || name.endsWith(".htm");
    }
    private boolean isCheckableFile(File file) {
        return file.isFile() && (isHtml(file) || isXhtml(file));
    }
    private void recurseDirectory(File directorythrows SAXException,
            IOException {
        File[] files = directory.listFiles();
        for (int i = 0; i < files.lengthi++) {
            File file = files[i];
            if (file.isDirectory()) {
                recurseDirectory(file);
            } else {
                checkHtmlFile(file);
            }
        }
    }
    private boolean isIgnorable(File filethrows IOException {
        String testPathname = file.getAbsolutePath().substring(
                .length() + 1);
        if ( != null) {
            for (String substring : ) {
                if (testPathname.contains(substring)) {
                    if () {
                        .println(String.format(
                                "\"%s\": warning: File ignored.",
                                file.toURI().toURL().toString()));
                        .flush();
                    }
                    return true;
                }
            }
        }
        return false;
    }
    private void checkFiles(List<Filefilesthrows IOException {
        for (File file : files) {
            if (isIgnorable(file)) {
                continue;
            }
            reset();
             = true;
            try {
                if (file.isDirectory()) {
                    recurseDirectory(file);
                } else {
                    checkHtmlFile(file);
                }
            } catch (IOException e) {
            } catch (SAXException e) {
            }
            if () {
                 = true;
            }
        }
    }
    private boolean messageMatches(String testFilename) {
        // p{C} = Other = Control+Format+Private_Use+Surrogate+Unassigned
        // http://www.regular-expressions.info/unicode.html#category
        // http://www.unicode.org/reports/tr18/#General_Category_Property
        String messageReported = .getMessage().replaceAll("\\p{C}",
                "?");
        String messageExpected = .get(testFilename).replaceAll(
                "\\p{C}""?");
        // FIXME: The string replacements below are a hack to "normalize"
        // error messages reported for bad values of the ins/del datetime
        // attribute, to work around the fact that in Java 8, parts of
        // those error messages don't always get emitted in the same order
        // that they do in Java 7 and earlier.
        Pattern p;
        p = Pattern.compile("(Bad datetime with timezone: .+) (Bad date: .+)");
        messageExpected = p.matcher(messageExpected).replaceAll("$2 $1");
        messageReported = p.matcher(messageReported).replaceAll("$2 $1");
        return messageReported.equals(messageExpected);
    }
    private void checkInvalidFiles(List<Filefilesthrows IOException {
        String testFilename;
         = true;
        for (File file : files) {
            if (isIgnorable(file)) {
                continue;
            }
            reset();
            try {
                if (file.isDirectory()) {
                    recurseDirectory(file);
                } else {
                    checkHtmlFile(file);
                }
            } catch (IOException e) {
            } catch (SAXException e) {
            }
            if ( != null) {
                testFilename = file.getAbsolutePath().substring(
                        .length() + 1);
                if () {
                    .put(testFilename.getMessage());
                } else if ( != null
                        && .get(testFilename) == null) {
                    try {
                        .println(String.format(
                                "\"%s\": warning: No expected message in"
                                        + " messages file.",
                                file.toURI().toURL().toString()));
                        .flush();
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    }
                } else if ( != null
                        && !messageMatches(testFilename)) {
                     = true;
                    try {
                        .println(String.format(
                                "\"%s\": error: Expected \"%s\""
                                        + " but instead encountered \"%s\".",
                                file.toURI().toURL().toString(),
                                .get(testFilename),
                                .getMessage()));
                        .flush();
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            if (!) {
                 = true;
                try {
                    .println(String.format(
                            "\"%s\": error: Expected an error but did not"
                                    + " encounter any.",
                            file.toURI().toURL().toString()));
                    .flush();
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    private void checkHasWarningFiles(List<Filefilesthrows IOException {
        String testFilename;
         = false;
        for (File file : files) {
            if (isIgnorable(file)) {
                continue;
            }
            reset();
            try {
                if (file.isDirectory()) {
                    recurseDirectory(file);
                } else {
                    checkHtmlFile(file);
                }
            } catch (IOException e) {
            } catch (SAXException e) {
            }
            if ( != null) {
                testFilename = file.getAbsolutePath().substring(
                        .length() + 1);
                if () {
                    .put(testFilename.getMessage());
                } else if ( != null
                        && .get(testFilename) == null) {
                    try {
                        .println(String.format(
                                "\"%s\": warning: No expected message in"
                                        + " messages file.",
                                file.toURI().toURL().toString()));
                        .flush();
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    }
                } else if ( != null
                        && !messageMatches(testFilename)) {
                    try {
                        .println(String.format(
                                "\"%s\": error: Expected \"%s\""
                                        + " but instead encountered \"%s\".",
                                file.toURI().toURL().toString(),
                                .get(testFilename),
                                .getMessage()));
                        .flush();
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            if () {
                 = true;
                try {
                    .println(String.format(
                            "\"%s\": error: Expected a warning but encountered"
                                    + " an error first.",
                            file.toURI().toURL().toString()));
                    .flush();
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                }
            } else if (!) {
                try {
                    .println(String.format(
                            "\"%s\": error: Expected a warning but did not"
                                    + " encounter any.",
                            file.toURI().toURL().toString()));
                    .flush();
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                }
            }
            if () {
                 = true;
                try {
                    .println(String.format(
                            "\"%s\": error: Expected a warning only but"
                                    + " encountered at least one error.",
                            file.toURI().toURL().toString()));
                    .flush();
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    private enum State {
        EXPECTING_INVALID_FILES, EXPECTING_VALID_FILES, EXPECTING_ANYTHING
    }
    private void checkTestDirectoryAgainstSchema(File directory,
            String schemaUrlthrows SAXExceptionException {
        .setUpMainSchema(schemaUrlthis);
        checkTestFiles(directory.);
    }
    private void checkTestFiles(File directoryState state)
            throws SAXExceptionIOException {
        File[] files = directory.listFiles();
        List<FilevalidFiles = new ArrayList<File>();
        List<FileinvalidFiles = new ArrayList<File>();
        List<FilehasWarningFiles = new ArrayList<File>();
        if (files == null) {
            if () {
                try {
                    .println(String.format(
                            "\"%s\": warning: No files found in directory.",
                            directory.toURI().toURL().toString()));
                    .flush();
                } catch (MalformedURLException mue) {
                    throw new RuntimeException(mue);
                }
            }
            return;
        }
        for (int i = 0; i < files.lengthi++) {
            File file = files[i];
            if (file.isDirectory()) {
                if (state != .) {
                    checkTestFiles(filestate);
                } else if ("invalid".equals(file.getName())) {
                    checkTestFiles(file.);
                } else if ("valid".equals(file.getName())) {
                    checkTestFiles(file.);
                } else {
                    checkTestFiles(file.);
                }
            } else if (isCheckableFile(file)) {
                if (state == .) {
                    invalidFiles.add(file);
                } else if (state == .) {
                    validFiles.add(file);
                } else if (file.getPath().indexOf("novalid") > 0) {
                    invalidFiles.add(file);
                } else if (file.getPath().indexOf("haswarn") > 0) {
                    hasWarningFiles.add(file);
                } else {
                    validFiles.add(file);
                }
            }
        }
        if (validFiles.size() > 0) {
            .setUpValidatorAndParsers(thisfalsefalse);
            checkFiles(validFiles);
        }
        if (invalidFiles.size() > 0) {
            .setUpValidatorAndParsers(thisfalsefalse);
            checkInvalidFiles(invalidFiles);
        }
        if (hasWarningFiles.size() > 0) {
            .setUpValidatorAndParsers(thisfalsefalse);
            checkHasWarningFiles(hasWarningFiles);
        }
        if () {
            OutputStreamWriter out = new OutputStreamWriter(
                    new FileOutputStream(), "utf-8");
            BufferedWriter bw = new BufferedWriter(out);
            bw.write(JSON.toString());
            bw.close();
        }
    }
    public boolean runTestSuite() throws SAXExceptionException {
        if ( != null) {
             = .getAbsoluteFile().getParent();
            FileInputStream fis = new FileInputStream();
            InputStreamReader reader = new InputStreamReader(fis"UTF-8");
             = (HashMap<StringString>) JSON.parse(reader);
        } else {
             = System.getProperty("user.dir");
        }
        for (File directory : new File().listFiles()) {
            if (directory.isDirectory()) {
                if (directory.getName().contains("rdfalite")) {
                    checkTestDirectoryAgainstSchema(directory,
                            "http://s.validator.nu/html5-rdfalite.rnc");
                } else if (directory.getName().contains("xhtml")) {
                    checkTestDirectoryAgainstSchema(directory,
                            "http://s.validator.nu/xhtml5-all.rnc");
                } else {
                    checkTestDirectoryAgainstSchema(directory);
                }
            }
        }
        if () {
            if () {
                .println("Failure!");
                .flush();
            } else {
                .println("Success!");
                .flush();
            }
        }
        return !;
    }
    private void emitMessage(SAXParseException eString messageType) {
        String systemId = e.getSystemId();
        .write((systemId == null) ? "" : '\"' + systemId + '\"');
        .write(":");
        .write(Integer.toString(e.getLineNumber()));
        .write(":");
        .write(Integer.toString(e.getColumnNumber()));
        .write(": ");
        .write(messageType);
        .write(": ");
        .write(e.getMessage());
        .write("\n");
        .flush();
    }
    public void warning(SAXParseException ethrows SAXException {
        if () {
            emitMessage(e"warning");
        } else if ( == null && !) {
             = e;
             = true;
        }
    }
    public void error(SAXParseException ethrows SAXException {
        if () {
            emitMessage(e"error");
        } else if ( == null) {
             = e;
            if (e instanceof BadAttributeValueException) {
                BadAttributeValueException ex = (BadAttributeValueExceptione;
                Map<StringDatatypeExceptiondatatypeErrors = ex.getExceptions();
                for (Map.Entry<StringDatatypeExceptionentry : datatypeErrors.entrySet()) {
                    DatatypeException dex = entry.getValue();
                    if (dex instanceof Html5DatatypeException) {
                        Html5DatatypeException ex5 = (Html5DatatypeExceptiondex;
                        if (ex5.isWarning()) {
                             = true;
                            return;
                        }
                    }
                }
            }
        }
         = true;
    }
    public void fatalError(SAXParseException ethrows SAXException {
         = true;
        if () {
            emitMessage(e"fatal error");
            return;
        } else if ( == null) {
             = e;
        }
    }
    public void reset() {
         = null;
         = false;
         = false;
         = false;
    }
    public static void main(String[] argsthrows SAXExceptionException {
        if (args.length < 1) {
            usage();
            System.exit(0);
        }
         = false;
        String messagesFilename = null;
        System.setProperty("nu.validator.datatype.warn""true");
        for (int i = 0; i < args.lengthi++) {
            if ("--verbose".equals(args[i])) {
                 = true;
            } else if ("--errors-only".equals(args[i])) {
                System.setProperty("nu.validator.datatype.warn""false");
            } else if ("--write-messages".equals(args[i])) {
                 = true;
            } else if (args[i].startsWith("--ignore=")) {
                 = args[i].substring(9, args[i].length()).split(",");
            } else if (args[i].startsWith("--")) {
                ..println(String.format(
                        "\nError: There is no option \"%s\"."args[i]));
                usage();
                System.exit(1);
            } else {
                if (args[i].endsWith(".json")) {
                    messagesFilename = args[i];
                } else {
                    ..println("\nError: Expected the name of a messages"
                            + " file with a .json extension.");
                    usage();
                    System.exit(1);
                }
            }
        }
        if (messagesFilename != null) {
             = new File(messagesFilename);
            if (!.exists()) {
                ..println("\nError: \"" + messagesFilename
                        + "\" file not found.");
                System.exit(1);
            } else if (!.isFile()) {
                ..println("\nError: \"" + messagesFilename
                        + "\" is not a file.");
                System.exit(1);
            }
        } else if () {
            ..println("\nError: Expected the name of a messages"
                    + " file with a .json extension.");
            usage();
            System.exit(1);
        }
        TestRunner tr = new TestRunner();
        if (tr.runTestSuite()) {
            System.exit(0);
        } else {
            System.exit(1);
        }
    }
    private static void usage() {
        ..println("\nUsage:");
        ..println("\n    java nu.validator.client.TestRunner [--errors-only] [--write-messages]");
        ..println("          [--verbose] [MESSAGES.json]");
        ..println("\n...where the MESSAGES.json file contains name/value pairs in which the name is");
        ..println("a pathname of a document to check and the value is the first error message or");
        ..println("warning message the validator is expected to report when checking that document.");
        ..println("Use the --write-messages option to create the file.");
    }
New to GrepCode? Check out our FAQ X