⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsdriver.java

📁 這是一個javascript 的 interpreter是了解 web browser的好材料
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				void threw(Throwable t) {			finish();			Element threw = createElement(target, "threw");			setTextContent(threw, ShellTest.getStackTrace(t));		}	}    private static class Results {        private ShellContextFactory factory;        private Arguments arguments;        private File output;        private boolean trace;        private Document html;        private Element failureHtml;				private Document xml;		private Date start;        private int tests;        private int failures;        Results(ShellContextFactory factory, Arguments arguments, boolean trace) {            this.factory = factory;            this.arguments = arguments;						File output = arguments.getOutputFile();			if (output == null) {				output = new File("rhino-test-results." + new java.text.SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()) + ".html");			}			this.output = output;            this.trace = trace;        }        private Document parse(InputStream in) {            try {                javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();                factory.setValidating(false);                javax.xml.parsers.DocumentBuilder dom = factory.newDocumentBuilder();                return dom.parse(in);            } catch (Throwable t) {                throw new RuntimeException("Parser failure", t);            }        }        private Document getTemplate() {            return parse(getClass().getResourceAsStream("results.html"));        }        private void write(Document template, boolean xml) {            try {				File output = this.output;				javax.xml.transform.TransformerFactory factory = javax.xml.transform.TransformerFactory.newInstance();				javax.xml.transform.Transformer xform = factory.newTransformer();				if (xml) {					xform.setOutputProperty(javax.xml.transform.OutputKeys.METHOD, "xml");					xform.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");					output = new File(output.getCanonicalPath() + ".xml");				}                xform.transform(                    new javax.xml.transform.dom.DOMSource(template),                    new javax.xml.transform.stream.StreamResult( new FileOutputStream(output) )                );            } catch (IOException e) {                arguments.getConsole().println("Could not write results file to " + output + ": ");                e.printStackTrace(System.err);            } catch (javax.xml.transform.TransformerConfigurationException e) {                throw new RuntimeException("Parser failure", e);            } catch (javax.xml.transform.TransformerException e) {                throw new RuntimeException("Parser failure", e);            }        }				void start() {            this.html = getTemplate();            this.failureHtml = getElementById(html.getDocumentElement(), "failureDetails.prototype");            if (this.failureHtml == null) {                try {                    javax.xml.transform.TransformerFactory.newInstance().newTransformer().transform(                        new javax.xml.transform.dom.DOMSource(html),                        new javax.xml.transform.stream.StreamResult(System.err)                    );                } catch (Throwable t) {                    throw new RuntimeException(t);                }                throw new RuntimeException("No");            }            this.failureHtml.getParentNode().removeChild(this.failureHtml);						try {				this.xml = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder()					.getDOMImplementation().createDocument(null, "results", null)				;				xml.getDocumentElement().setAttribute("timestamp", String.valueOf(new Date().getTime()));				xml.getDocumentElement().setAttribute("optimization", String.valueOf(arguments.getOptimizationLevel()));                xml.getDocumentElement().setAttribute("strict", String.valueOf(arguments.isStrict()));                xml.getDocumentElement().setAttribute("timeout", String.valueOf(arguments.getTimeout()));			} catch (javax.xml.parsers.ParserConfigurationException e) {				throw new RuntimeException(e);			}						this.start = new Date();		}        void run(Tests.Script script, ShellTest.Parameters parameters) {			String path = script.getPath();			File test = script.getFile();            ConsoleStatus cStatus = new ConsoleStatus(arguments.getConsole(), trace);            HtmlStatus hStatus = new HtmlStatus(arguments.getLxrUrl(), arguments.getBugUrl(), path, html, (Element)failureHtml.cloneNode(true));			XmlStatus xStatus = new XmlStatus(path, this.xml.getDocumentElement());            ShellTest.Status status = ShellTest.Status.compose(new ShellTest.Status[] { cStatus, hStatus, xStatus });            try {                ShellTest.run(factory, test, parameters, status);            } catch (Exception e) {                throw new RuntimeException(e);            }            tests++;            if (hStatus.failed()) {                failures++;            }            hStatus.finish();        }		        private void set(Document document, String id, String value) {            getElementById(document.getDocumentElement(), id).setTextContent(value);        }        void finish() {			Date end = new Date();            long elapsedMs = end.getTime() - start.getTime();            set(html, "results.testlist", join(arguments.getTestList()));            set(html, "results.skiplist", join(arguments.getSkipList()));            String pct = new java.text.DecimalFormat("##0.00").format( (double)failures / (double)tests * 100.0 );            set(html, "results.results", "Tests attempted: " + tests + " Failures: " + failures + " (" + pct + "%)");            set(html, "results.platform", "java.home=" + System.getProperty("java.home")                + "\n" + "java.version=" + System.getProperty("java.version")                + "\n" + "os.name=" + System.getProperty("os.name")            );            set(html, "results.classpath", System.getProperty("java.class.path").replace(File.pathSeparatorChar, ' '));            int elapsedSeconds = (int)(elapsedMs / 1000);            int elapsedMinutes = elapsedSeconds / 60;            elapsedSeconds = elapsedSeconds % 60;            String elapsed = "" + elapsedMinutes + " minutes, " + elapsedSeconds + " seconds";            set(html, "results.elapsed", elapsed);            set(html, "results.time", new java.text.SimpleDateFormat("MMMM d yyyy h:mm:ss aa").format(new java.util.Date()));            write(html, false);			write(xml, true);        }    }    private static class ShellTestParameters extends ShellTest.Parameters {        private int timeout;        ShellTestParameters(int timeout) {            this.timeout = timeout;        }        int getTimeoutMilliseconds() {            return timeout;        }    }    void run(Arguments arguments) throws Throwable {        if (arguments.help()) {            System.out.println("See mozilla/js/tests/README-jsDriver.html; note that some options are not supported.");            System.out.println("Consult the Java source code at testsrc/org/mozilla/javascript/JsDriver.java for details.");            System.exit(0);        }        ShellContextFactory factory = new ShellContextFactory();        factory.setOptimizationLevel(arguments.getOptimizationLevel());        factory.setStrictMode(arguments.isStrict());        File path = arguments.getTestsPath();        if (path == null) {            path = new File("../tests");        }        if (!path.exists()) {            throw new RuntimeException("JavaScript tests not found at " + path.getCanonicalPath());        }        Tests tests = new Tests(path, arguments.getTestList(), arguments.getSkipList());        Tests.Script[] all = tests.getFiles();        arguments.getConsole().println("Running " + all.length + " tests.");        Results results = new Results(factory, arguments, arguments.trace());				results.start();        for (int i=0; i<all.length; i++) {            results.run(all[i], new ShellTestParameters(arguments.getTimeout()));        }		results.finish();    }    public static void main(Arguments arguments) throws Throwable {        JsDriver driver = new JsDriver();        driver.run(arguments);    }    private static class Arguments {        private ArrayList options = new ArrayList();        private Option bugUrl = new Option("b", "bugurl", false, false, "http://bugzilla.mozilla.org/show_bug.cgi?id=");        private Option optimizationLevel = new Option("o", "optimization", false, false, "-1");        private Option strict = new Option(null, "strict", false, true, null);                private Option outputFile = new Option("f", "file", false, false, null);        private Option help = new Option("h", "help", false, true, null);        private Option logFailuresToConsole = new Option("k", "confail", false, true, null);        private Option testList = new Option("l", "list", true, false, null);        private Option skipList = new Option("L", "neglist", true, false, null);        private Option testsPath = new Option("p", "testpath", false, false, null);        private Option trace = new Option("t", "trace", false, true, null);        private Option lxrUrl = new Option("u", "lxrurl", false, false, "http://lxr.mozilla.org/mozilla/source/js/tests/");        private Option timeout = new Option(null, "timeout", false, false, "60000");               public static class Console {          public void print(String message) {            System.out.print(message);          }          public void println(String message) {            System.out.println(message);          }        }        private Console console = new Console();        private class Option {            private String letterOption;            private String wordOption;            private boolean array;            private boolean flag;            private boolean ignored;            private ArrayList values = new ArrayList();            //    array: can this option have multiple values?            //    flag: is this option a simple true/false switch?            Option(String letterOption, String wordOption, boolean array, boolean flag, String unspecified) {                this.letterOption = letterOption;                this.wordOption = wordOption;                this.flag = flag;                this.array = array;                if (!flag && !array) {                    this.values.add(unspecified);                }                options.add(this);            }            Option ignored() {                this.ignored = true;                return this;            }            int getInt() {                return Integer.parseInt( getValue() );            }            String getValue() {                return (String)values.get(0);            }            boolean getSwitch() {                return values.size() > 0;            }            File getFile() {                if (getValue() == null) return null;                return new File(getValue());            }            String[] getValues() {                return (String[])values.toArray(new String[0]);            }            void process(List arguments) {                String option = (String)arguments.get(0);                String dashLetter = (letterOption == null) ? (String)null : "-" + letterOption;                if (option.equals(dashLetter) || option.equals("--" + wordOption)) {                    arguments.remove(0);                    if (flag) {                        values.add(0, (String)null );                    } else if (array) {                        while( arguments.size() > 0 && !( (String)arguments.get(0) ).startsWith("-") ) {                            values.add(arguments.remove(0));                        }                    } else {                        values.set(0, arguments.remove(0));                    }                    if (ignored) {                        System.err.println("WARNING: " + option + " is ignored in the Java version of the test driver.");                    }                }            }        }        //    -b URL, --bugurl=URL        public String getBugUrl() {            return bugUrl.getValue();        }                       //    -c PATH, --classpath=PATH        //    Does not apply; we will use the VM's classpath        //    -e TYPE ..., --engine=TYPE ...        //    Does not apply; was used to select between SpiderMonkey and Rhino        //    Not in jsDriver.pl        public int getOptimizationLevel() {            return optimizationLevel.getInt();        }                //    --strict        public boolean isStrict() {          return strict.getSwitch();        }        //    -f FILE, --file=FILE        public File getOutputFile() {            return outputFile.getFile();        }                //    -h, --help        public boolean help() {            return help.getSwitch();        }        //    -j PATH, --javapath=PATH        //    Does not apply; we will use this JVM        //    -k, --confail        //    TODO    Currently this is ignored; not clear precisely what it means (perhaps we should not be logging ordinary        //            pass/fail to the console currently?)        public boolean logFailuresToConsole() {            return logFailuresToConsole.getSwitch();        }        //    -l FILE,... or --list=FILE,...        public String[] getTestList() {            return testList.getValues();        }        //    -L FILE,... or --neglist=FILE,...        public String[] getSkipList() {            return skipList.getValues();        }        //    -p PATH, --testpath=PATH        public File getTestsPath() {            return testsPath.getFile();        }        //    -s PATH, --shellpath=PATH        //    Does not apply; we will use the Rhino shell with any classes given on the classpath        //    -t, --trace        public boolean trace() {            return trace.getSwitch();        }        //    -u URL, --lxrurl=URL        public String getLxrUrl() {            return lxrUrl.getValue();        }        //        //    New arguments        //        //    --timeout        //    Milliseconds to wait for each test        public int getTimeout() {            return timeout.getInt();        }        public Console getConsole() {            return console;        }                void process(List arguments) {            while(arguments.size() > 0) {                String option = (String)arguments.get(0);                if (option.startsWith("--")) {                    //    preprocess --name=value options into --name value                    if (option.indexOf("=") != -1) {                        arguments.set(0, option.substring(option.indexOf("=")));                        arguments.add(1, option.substring(option.indexOf("=") + 1));                    }                } else if (option.startsWith("-")) {                    //    could be multiple single-letter options, e.g. -kht, so preprocess them into -k -h -t                    if (option.length() > 2) {                        for (int i=2; i<option.length(); i++) {                            arguments.add(1, "-" + option.substring(i,i+1));                        }                        arguments.set(0, option.substring(0,2));                    }                }                int lengthBefore = arguments.size();                for (int i=0; i<options.size(); i++) {                    if (arguments.size() > 0) {                        ((Option)options.get(i)).process(arguments);                    }                }                                if (arguments.size() == lengthBefore) {					System.err.println("WARNING: ignoring unrecognized option " + arguments.remove(0));                }            }        }    }    public static void main(String[] args) throws Throwable {        ArrayList arguments = new ArrayList();        arguments.addAll(Arrays.asList(args));        Arguments clArguments = new Arguments();        clArguments.process(arguments);        main(clArguments);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -