📄 basetest.java
字号:
* arguments (which are assumed to have been processed by the subclass). */ protected void commandline(String[] args, int offset) { commandLine = true; try { parseOptions(args, offset); test(); waitForExit(); System.exit(TestException.COMPLETE_SUCCESS); } catch (TestException e) { status.println(e.getMessage()); waitForExit(); System.exit(e.getErrorCode()); } } /** * Parses a command-line option. If the option is not recognised, * <code>super.parseOption(option)</code> should be called. * <p> * Case should generally not be significant in option names. */ protected void parseOption(String option) throws TestException { if (option.equalsIgnoreCase("-verbose")) setVerbose(true); else if (option.equalsIgnoreCase("-gui")) setGuiEnabled(true); else { System.err.println(describeUsage()); throw new TestException("Unrecognised option: '" + option + "'", TestException.ILLEGAL_ARGUMENTS); } } /** * Processes command-line arguments from <i>args</i>, starting at index * <i>offset</i>. */ public void parseOptions(String[] args, int offset) throws TestException { for (int i = offset; i < args.length; i++) parseOption(args[i]); } /** Describes the command-line usage of this class. */ public String describeUsage() { return "Usage:\n" + " java " + getClass().getName() + " [options...]\n" + describeOptions(); } /** Describes the command-line options of this class. */ public String describeOptions() { return "Options:\n" + " -verbose: print output even if all tests pass.\n"; } /** * Sets the name of this test (as shown to the user). The default is the name of * the class. */ public void setName(String n) { name = n; } /** Gets the name of this test (as shown to the user). */ public String getName() { return name; } /** * Sets the PrintWriter to which output is to be sent. In most cases this * does not need to be set by the test class; the <code>commandline</code> * method will do that automatically. */ public void setOutput(PrintWriter pw) { status = pw; } /** Sets whether output is to be printed even if all tests pass. */ public void setVerbose(boolean flag) { verbose = flag; } /** Returns true iff output is to be printed even if all tests pass. */ public boolean isVerbose() { return verbose; } /** * Runs an instance of Maker. First use the methods setIncludeSource, * setResourcesOnly, setProvider, etc. to initialize any properties for which * the defaults are not as required. * * @param options an array of string options. * @param name a name for this test. * @exception cryptix.util.test.TestException if the test fails. * @since Cryptix 3.0.4 */ /** * Begins the test proper. This method is called automatically by * <code>commandline</code>, but it can also be called directly. */ public void test() throws TestException { if (verbose) { sw = null; out = status; } else { sw = new StringWriter(); out = new PrintWriter(sw); } try { failures = errors = passes = skipped = expectedPasses = 0; overallPass = false; status.print("Running tests for " + getName()); if (verbose) status.println(); else status.flush(); engineTest(); } catch (Throwable e) { error(e); } finally { String andSkipped = (skipped > 0) ? " and skipped tests" : ""; if (passes + skipped < expectedPasses) error("Number of passes" + andSkipped + " is less than expected"); else if (passes < 1) error("At least one pass is required"); else if (0 < expectedPasses && expectedPasses < passes + skipped) error("Number of passes" + andSkipped + " is more than expected\n" + "(therefore the expected number is wrong)"); report(); if (failures > 0 || errors > 0) { if (passes == 0) throw new TestException(getName() + " failed completely", TestException.COMPLETE_FAILURE); throw new TestException(getName() + " failed partially", TestException.PARTIAL_FAILURE); } overallPass = true; } } /** * Reports a failure, with the given message. */ protected void fail(String msg) { failures++; out.println("\nFailed: " + msg); if (sw != null) switchStream(); } /** * Reports an error, with the given message. */ protected void error(String msg) { errors++; out.println("\nError: " + msg); if (sw != null) switchStream(); } /** * Reports a skipped test, with the given message. */ protected void skip(String msg) { skipped++; out.println("\nTest skipped: " + msg); if (sw != null) switchStream(); } private void switchStream() { out.flush(); out = status; out.println(); out.print(sw.getBuffer()); out.flush(); sw = null; } /** * Reports an error due to an unexpected exception. */ protected void error(Exception e) { error("Exception Unexpected " + e.getClass().getName()); e.printStackTrace(out); } protected void error(Throwable e) { error("Throwable Unexpected " + e.getClass().getName()); e.printStackTrace(out); } /** * Reports a pass, with the given message. */ protected void pass(String msg) { passes++; out.println("\nPassed: " + msg); if (sw != null) { status.print("."); status.flush(); } } /** * Reports a pass if <i>pass</i> is true, or a failure if it is false. * In either case, the given message is used. */ protected void passIf(boolean pass, String msg) { if (pass) pass(msg); else fail(msg); } /** * Sets the number of expected passes for this test class. This can be * called at any time by the <code>engineTest</code> method, but should * normally be called at the start of that method. */ protected void setExpectedPasses(int n) { if (n < 0) throw new IllegalArgumentException("n < 0"); expectedPasses = n; } /** * Forces a report of the number of passes, failures, errors, and expected * passes so far. */ protected void report() { status.println(SEPARATOR); status.println("Number of passes: " + passes); status.println("Number of failures: " + failures); if (errors > 0) status.println("Number of errors: " + errors); if (skipped > 0) status.println("Number of skipped tests: " + skipped); status.println("Expected passes: " + (expectedPasses > 0 ? Integer.toString(expectedPasses) : "unknown")); } /** Returns the number of failures so far. */ public int getFailures() { return failures; } /** Returns the number of errors so far. */ public int getErrors() { return errors; } /** Returns the number of passes so far. */ public int getPasses() { return passes; } /** Returns the number of skipped tests so far. */ public int getSkipped() { return skipped; } /** * Returns the number of expected passes, or 0 if this has not yet been * set, or is unknown. */ public int getExpectedPasses() { return expectedPasses; } /** Returns true iff all the tests have completed successfully. */ public boolean isOverallPass() { return overallPass; } /** * This method should be overridden by test subclasses, to perform the * actual testing. */ protected abstract void engineTest() throws Exception;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -