📄 jsdriver.java
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is the Java port of jsDriver.pl. * * The Initial Developer of the Original Code is * David P. Caldwell. * Portions created by David P. Caldwell are Copyright (C) * 2007 David P. Caldwell. All Rights Reserved. * * * Contributor(s): * David P. Caldwell <inonit@inonit.com> * Norris Boyd <norrisboyd@gmail.com> * * Alternatively, the contents of this file may be used under the terms of * the GNU General Public License Version 2 or later (the "GPL"), in which * case the provisions of the GPL are applicable instead of those above. If * you wish to allow use of your version of this file only under the terms of * the GPL and not to allow others to use your version of this file under the * MPL, indicate your decision by deleting the provisions above and replacing * them with the notice and other provisions required by the GPL. If you do * not delete the provisions above, a recipient may use your version of this * file under either the MPL or the GPL. * * ***** END LICENSE BLOCK ***** */package org.mozilla.javascript.drivers;import java.io.*;import java.util.*;import org.w3c.dom.*;import org.mozilla.javascript.tools.shell.*;/** * @version $Id: JsDriver.java,v 1.5 2007/10/11 19:44:10 szegedia%freemail.hu Exp $ */public class JsDriver { private JsDriver() { } private static String join(String[] list) { String rv = ""; for (int i=0; i<list.length; i++) { rv += list[i]; if (i+1 != list.length) { rv += ","; } } return rv; } private static class Tests { private File testDirectory; private String[] list; private String[] skip; Tests(File testDirectory, String[] list, String[] skip) { this.testDirectory = testDirectory; this.list = getTestList(list); this.skip = getTestList(skip); } private String[] getTestList(String[] tests) { ArrayList list = new ArrayList(); for (int i=0; i < tests.length; i++) { if (tests[i].startsWith("@")) addTestsFromFile(tests[i].substring(1), list); else list.add(tests[i]); } return (String[])list.toArray(new String[0]); } private void addTestsFromFile(String filename, ArrayList list) { try { Properties props = new Properties(); props.load(new FileInputStream(new File(filename))); list.addAll(props.keySet()); } catch (IOException e) { throw new RuntimeException("Could not read file '" + filename + "'", e); } } private boolean matches(String[] patterns, String path) { for (int i=0; i<patterns.length; i++) { if (path.startsWith(patterns[i])) { return true; } } return false; } private boolean matches(String path) { if (list.length == 0) return true; return matches(list, path); } private boolean excluded(String path) { if (skip.length == 0) return false; return matches(skip, path); } private void addFiles(List rv, String prefix, File directory) { File[] files = directory.listFiles(); if (files == null) throw new RuntimeException("files null for " + directory); for (int i=0; i<files.length; i++) { String path = prefix + files[i].getName(); if (ShellTest.DIRECTORY_FILTER.accept(files[i])) { addFiles(rv, path + "/", files[i]); } else { boolean isTopLevel = prefix.length() == 0; if (ShellTest.TEST_FILTER.accept(files[i]) && matches(path) && !excluded(path) && !isTopLevel) { rv.add(new Script(path, files[i])); } } } } static class Script { private String path; private File file; Script(String path, File file) { this.path = path; this.file = file; } String getPath() { return path; } File getFile() { return file; } } Script[] getFiles() { ArrayList rv = new ArrayList(); addFiles(rv, "", testDirectory); return (Script[])rv.toArray(new Script[0]); } } private static class ConsoleStatus extends ShellTest.Status { private File jsFile; private Arguments.Console console; private boolean trace; private boolean failed; ConsoleStatus(Arguments.Console console, boolean trace) { this.console = console; this.trace = trace; } void running(File jsFile) { try { console.println("Running: " + jsFile.getCanonicalPath()); this.jsFile = jsFile; } catch (IOException e) { throw new RuntimeException(e); } } void failed(String s) { console.println("Failed: " + jsFile + ": " + s); failed = true; } void threw(Throwable t) { console.println("Failed: " + jsFile + " with exception."); console.println(ShellTest.getStackTrace(t)); failed = true; } void timedOut() { console.println("Failed: " + jsFile + ": timed out."); failed = true; } void exitCodesWere(int expected, int actual) { if (expected != actual) { console.println("Failed: " + jsFile + " expected " + expected + " actual " + actual); failed = true; } } void outputWas(String s) { if (!failed) { console.println("Passed: " + jsFile); if (trace) { console.println(s); } } } } // returns true if node was found, false otherwise private static boolean setContent(Element node, String id, String content) { if (node.getAttribute("id").equals(id)) { node.setTextContent(node.getTextContent() + "\n" + content); return true; } else { NodeList children = node.getChildNodes(); for (int i=0; i<children.getLength(); i++) { if (children.item(i) instanceof Element) { Element e = (Element)children.item(i); boolean rv = setContent( e, id, content ); if (rv) { return true; } } } } return false; } private static Element getElementById(Element node, String id) { if (node.getAttribute("id").equals(id)) { return node; } else { NodeList children = node.getChildNodes(); for (int i=0; i<children.getLength(); i++) { if (children.item(i) instanceof Element) { Element rv = getElementById( (Element)children.item(i), id ); if (rv != null) { return rv; } } } } return null; } private static String newlineLineEndings(String s) { StringBuffer rv = new StringBuffer(); for (int i=0; i<s.length(); i++) { if (s.charAt(i) == '\r') { if (i+1<s.length() && s.charAt(i+1) == '\n') { // just skip \r } else { // Macintosh, substitute \n rv.append('\n'); } } else { rv.append(s.charAt(i)); } } return rv.toString(); } private static class HtmlStatus extends ShellTest.Status { private String testPath; private String bugUrl; private String lxrUrl; private Document html; private Element failureHtml; private boolean failed; private String output; HtmlStatus(String lxrUrl, String bugUrl, String testPath, Document html, Element failureHtml) { this.testPath = testPath; this.bugUrl = bugUrl; this.lxrUrl = lxrUrl; this.html = html; this.failureHtml = failureHtml; } void running(File file) { } void failed(String s) { failed = true; setContent(failureHtml, "failureDetails.reason", "Failure reason: \n" + s); } void exitCodesWere(int expected, int actual) { if (expected != actual) { failed = true; setContent(failureHtml, "failureDetails.reason", "expected exit code " + expected + " but got " + actual); } } void threw(Throwable e) { failed = true; setContent(failureHtml, "failureDetails.reason", "Threw Java exception:\n" + newlineLineEndings(ShellTest.getStackTrace(e))); } void timedOut() { failed = true; setContent(failureHtml, "failureDetails.reason", "Timed out."); } void outputWas(String s) { this.output = s; } private String getLinesStartingWith(String prefix) { BufferedReader r = new BufferedReader(new StringReader(output)); String line = null; String rv = ""; try { while( (line = r.readLine()) != null ) { if (line.startsWith(prefix)) { if (rv.length() > 0) { rv += "\n"; } rv += line; } } return rv; } catch (IOException e) { throw new RuntimeException("Can't happen."); } } boolean failed() { return failed; } void finish() { if (failed) { getElementById(failureHtml, "failureDetails.status").setTextContent(getLinesStartingWith("STATUS:")); String bn = getLinesStartingWith("BUGNUMBER:"); Element bnlink = getElementById(failureHtml, "failureDetails.bug.href"); if (bn.length() > 0) { String number = bn.substring("BUGNUMBER: ".length()); if (!number.equals("none")) { bnlink.setAttribute("href", bugUrl + number); getElementById(bnlink, "failureDetails.bug.number").setTextContent(number); } else { bnlink.getParentNode().removeChild(bnlink); } } else { bnlink.getParentNode().removeChild(bnlink); } getElementById(failureHtml, "failureDetails.lxr").setAttribute("href", lxrUrl + testPath); getElementById(failureHtml, "failureDetails.lxr.text").setTextContent(testPath); getElementById(html.getDocumentElement(), "retestList.text").setTextContent( getElementById(html.getDocumentElement(), "retestList.text").getTextContent() + testPath + "\n" ); getElementById(html.getDocumentElement(), "failureDetails").appendChild(failureHtml); } } } private static class XmlStatus extends ShellTest.Status { private Element target; private Date start; XmlStatus(String path, Element root) { this.target = root.getOwnerDocument().createElement("test"); this.target.setAttribute("path", path); root.appendChild(target); } void running(File file) { this.start = new Date(); } private Element createElement(Element parent, String name) { Element rv = parent.getOwnerDocument().createElement(name); parent.appendChild(rv); return rv; } private void finish() { Date end = new Date(); long elapsed = end.getTime() - start.getTime(); this.target.setAttribute("elapsed", String.valueOf(elapsed)); } private void setTextContent(Element e, String content) { e.setTextContent( newlineLineEndings(content) ); } void exitCodesWere(int expected, int actual) { finish(); Element exit = createElement(target, "exit"); exit.setAttribute("expected", String.valueOf(expected)); exit.setAttribute("actual", String.valueOf(actual)); } void timedOut() { finish(); createElement(target, "timedOut"); } void failed(String s) { finish(); Element failed = createElement(target, "failed"); setTextContent(failed, s); } void outputWas(String message) { finish(); Element output = createElement(target, "output"); setTextContent(output, message); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -