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