[12] | 1 | /*
|
---|
| 2 | * TestCase.java
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
| 6 | * Copyright (c) 2002, 2004 EclipseOS2 Team.
|
---|
| 7 | * This file is made available under the terms of the Common Public License v1.0
|
---|
| 8 | * which accompanies this distribution, and is available at
|
---|
| 9 | * http://www.eclipse.org/legal/cpl-v10.html
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | import java.io.PrintWriter;
|
---|
| 13 | import java.io.StringWriter;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Simple test case class that implements the Runnable interface. Its run()
|
---|
| 17 | * method (which is done final for ...) sequentally calls init() to do
|
---|
| 18 | * initialization, exec() to do necessary test actions and done() to cleanup.
|
---|
| 19 | * init() and done() simply do nothing, exec() throws a dummy exception.
|
---|
| 20 | *
|
---|
| 21 | * The test is considered to be failed if an ecxeption is thown, otherwise
|
---|
| 22 | * it is considered to be passed.
|
---|
| 23 | *
|
---|
| 24 | * This class also provides a simple testcase launcher -- the go() method,
|
---|
| 25 | * that handles exceptions and logs them to the console. It is intended to
|
---|
| 26 | * be called from the main() method of the testcase to make it possible to
|
---|
| 27 | * run the testase from the console.
|
---|
| 28 | *
|
---|
| 29 | * Subclasses should redefine the PART, STEP, TEST and DESC data members
|
---|
| 30 | * using the static initializer and override the exec() method to do actual
|
---|
| 31 | * test. They also should provide the main() method which simply creates a
|
---|
| 32 | * class instance and calls go() passing the created instance as its
|
---|
| 33 | * argument (see the main method below) to be able to run the testcase from
|
---|
| 34 | * the console and may override init() and done() to do any initialization
|
---|
| 35 | * and cleanup as required.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | public class TestCase implements Runnable {
|
---|
| 39 |
|
---|
| 40 | static String PART = "PART";
|
---|
| 41 | static String STEP = "XXX";
|
---|
| 42 | static String TEST = "YY";
|
---|
| 43 | static String DESC = "Dummy Test";
|
---|
| 44 |
|
---|
| 45 | static final String DASH_LINE =
|
---|
| 46 | "============================================================";
|
---|
| 47 | static final String DOT_LINE =
|
---|
| 48 | "............................................................";
|
---|
| 49 |
|
---|
| 50 | public static void main (String [] args) {
|
---|
| 51 | go (new TestCase ());
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | static void go (TestCase test) {
|
---|
| 55 | try {
|
---|
| 56 | sayHeader (test);
|
---|
| 57 | test.run ();
|
---|
| 58 | sayFooter (test, true);
|
---|
| 59 |
|
---|
| 60 | } catch (Throwable e) {
|
---|
| 61 | StringWriter es = new StringWriter();
|
---|
| 62 | e.printStackTrace (new PrintWriter (es));
|
---|
| 63 | System.out.print (es);
|
---|
| 64 | sayFooter (test, false);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | protected void init () {}
|
---|
| 69 |
|
---|
| 70 | protected void done () {}
|
---|
| 71 |
|
---|
| 72 | protected void exec () {
|
---|
| 73 | sayObjective ("Dummy");
|
---|
| 74 | throw new RuntimeException ("This is a dummy testcase and it will never pass!");
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | final public void run () {
|
---|
| 78 | init();
|
---|
| 79 | exec();
|
---|
| 80 | done();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | static void say () {
|
---|
| 84 | System.out.println();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | static void say (String text) {
|
---|
| 88 | System.out.println(text);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | static void sayObjective (String objective) {
|
---|
| 92 | say ("\nOBJECTIVE: " + objective);
|
---|
| 93 | say (DASH_LINE+'\n');
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | static void saySubObjective (String subObjective) {
|
---|
| 97 | say (subObjective);
|
---|
| 98 | say (DOT_LINE+'\n');
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | static String getName () {
|
---|
| 102 | String rc = PART + STEP;
|
---|
| 103 | if (TEST != null && TEST.length() > 0) rc += '_' + TEST;
|
---|
| 104 | return rc;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | static String getFullName () {
|
---|
| 108 | if (DESC == null) return getName();
|
---|
| 109 | return getName() + ": " + DESC;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | static void sayHeader (TestCase test) {
|
---|
| 113 | say (DASH_LINE);
|
---|
| 114 | say (test.getFullName ());
|
---|
| 115 | say (DASH_LINE+'\n');
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | static void sayFooter (TestCase test, boolean passed) {
|
---|
| 119 | say ('\n'+DASH_LINE);
|
---|
| 120 | say (test.getName () + ": Testcase " + (passed ? "PASSED OK" : "FAILED!!!"));
|
---|
| 121 | say (DASH_LINE+'\n');
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | }
|
---|
| 125 |
|
---|