Path: blob/master/test/jdk/java/lang/instrument/ATestCaseScaffold.java
41149 views
/*1* Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Copyright 2003 Wily Technology, Inc.25*/2627/**28* This class serves as a bridge between the Wily JUnit-style tests and the29* Sun test framework.30*31* This is a replacement for the JUnit TestCase base class. Provides surrogate32* functionality for the setup/teardown calls, and for all the verification and33* assertion services.34*35* The Sun framework relies on each test case being a separate class with a separate main,36* which throws if the test fails and does not throw if the test succeeds.37*/383940public abstract class ATestCaseScaffold {41private String fName;42private boolean fVerbose;434445protected46ATestCaseScaffold(String name) {47fName = name;48fVerbose = false;49}5051public final void52runTest()53throws Throwable {54Throwable toRethrow = null;5556setUp();5758try {59doRunTest();60}61finally {62tearDown();63}6465}6667protected void68setUp()69throws Exception {70}7172protected void73tearDown()74throws Exception {75}7677protected abstract void78doRunTest()79throws Throwable;8081/**82* Be verbose: print out what happens after this83*/84public void85beVerbose()86{87fVerbose = true;88}8990/**91* Print a string, if and only if verbose printing is enabled.92*/93public void94verbosePrint(String message)95{96if (fVerbose)97{98System.out.println("Debugging message: " + message);99}100}101102/*103* Replacement verification methods104* Shaped the same as the JUnit ones to make reusing the JUnit test possible105* Didn't implement them all, only the ones our existing tests use.106*/107108public final void109fail() {110throw new TestCaseScaffoldException();111}112113public final void114fail(String message) {115throw new TestCaseScaffoldException(message);116}117118public final void119assertTrue(boolean condition) {120if ( !condition ) {121fail();122}123}124125public final void126assertTrue(String message, boolean condition) {127if ( !condition ) {128fail(message);129}130}131132public final void133assertNotNull(Object o) {134assertTrue(o != null);135}136137public final void138assertNotNull(String message, Object o) {139assertTrue(message, o != null);140}141142public final void143assertEquals(String message, Object expected, Object actual) {144if ( (expected == null) && (actual == null) ) {145return;146}147else if ( (expected != null) && (expected.equals(actual)) ) {148return;149}150else {151throw new TestCaseScaffoldException(message + ". Expected: '" + expected +152"'. Actual: '" + actual + "'.");153}154}155156public final void157assertEquals(Object expected, Object actual) {158assertEquals(null, expected, actual);159}160161public final void162assertEquals(String message, int expected, int actual) {163assertEquals(message, new Integer(expected), new Integer(actual));164}165166public final void167assertEquals(int expected, int actual) {168assertEquals("Expected equality", expected, actual);169}170171public static final class172TestCaseScaffoldException extends RuntimeException {173public174TestCaseScaffoldException() {175super();176}177178public179TestCaseScaffoldException(String m) {180super(m);181}182183}184185}186187188