jdk/test/java/lang/instrument/ATestCaseScaffold.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * Copyright 2003 Wily Technology, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  This class serves as a bridge between the Wily JUnit-style tests and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  Sun test framework.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  This is a replacement for the JUnit TestCase base class. Provides surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  functionality for the setup/teardown calls, and for all the verification and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  assertion services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *  The Sun framework relies on each test case being a separate class with a separate main,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *  which throws if the test fails and does not throw if the test succeeds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public abstract class ATestCaseScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private String      fName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private boolean     fVerbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    ATestCaseScaffold(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        fName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        fVerbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    runTest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Throwable toRethrow = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        setUp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            doRunTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            tearDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    setUp()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    tearDown()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected abstract void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    doRunTest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        throws Throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Be verbose: print out what happens after this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    beVerbose()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        fVerbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Print a string, if and only if verbose printing is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    verbosePrint(String message)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (fVerbose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            System.out.println("Debugging message: " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *  Replacement verification methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *  Shaped the same as the JUnit ones to make reusing the JUnit test possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *  Didn't implement them all, only the ones our existing tests use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    fail() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        throw new TestCaseScaffoldException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    fail(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        throw new TestCaseScaffoldException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    assertTrue(boolean condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if ( !condition ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            fail();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    assertTrue(String message, boolean condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if ( !condition ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            fail(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    assertNotNull(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        assertTrue(o != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    assertNotNull(String message, Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        assertTrue(message, o != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    assertEquals(String message, Object expected, Object actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if ( (expected == null) && (actual == null) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        else if ( (expected != null) && (expected.equals(actual)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new TestCaseScaffoldException(message + ". Expected: '" + expected +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                                "'. Actual: '" + actual + "'.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    assertEquals(Object expected, Object actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        assertEquals(null, expected, actual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    assertEquals(String message, int expected, int actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        assertEquals(message, new Integer(expected), new Integer(actual));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    assertEquals(int expected, int actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        assertEquals("Expected equality", expected, actual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public final static class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    TestCaseScaffoldException extends RuntimeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        TestCaseScaffoldException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        TestCaseScaffoldException(String m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            super(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}