JDWPRawTestCase.java revision 3e35c96b816be002cbdbfd623f2b2d8300f62816
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 */
18
19/**
20 * @author Vitaly A. Provodin
21 */
22
23/**
24 * Created on 01.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.share;
27
28import org.apache.harmony.jpda.tests.framework.TestErrorException;
29import org.apache.harmony.jpda.tests.share.JPDALogWriter;
30import org.apache.harmony.jpda.tests.share.JPDATestOptions;
31
32import junit.framework.TestCase;
33
34/**
35 * Basic class for all JDWP unit tests based on <code>JUnit</code> framework.
36 * <p>
37 * This class extends JUnit interface <code>TestCase</code> and implements
38 * <code>setUp()</code> and <code>tearDown()</code> being common for all
39 * JDWP tests.
40 * <p>
41 * It also introduces <code>internalSetUp()</code> and
42 * <code>internalTearDown()</code> that can be implemented to supply safe
43 * start up and shut down of debuggee.
44 */
45public abstract class JDWPRawTestCase extends TestCase {
46
47    /** Where to print log messages. */
48    protected JPDALogWriter logWriter;
49
50    /** Test run options. */
51    protected JPDATestOptions settings;
52
53    /**
54     * This method should be overridden in derived classes to return full name
55     * for debuggee class.
56     *
57     * @return full debuggee class name
58     */
59    protected abstract String getDebuggeeClassName();
60
61    /**
62     * Returns the signature of the debuggee class. This is computed based on
63     * {@link #getDebuggeeClassName}.
64     *
65     * @return full debuggee class signature.
66     */
67    protected String getDebuggeeClassSignature() {
68        String debuggeeClassName = getDebuggeeClassName();
69        return getClassSignature(debuggeeClassName);
70    }
71
72    /**
73     * Returns the signature matching the given class name
74     * @param className
75     *          a fully qualified class name
76     * @return the class signature
77     */
78    protected static String getClassSignature(String className) {
79        StringBuilder builder = new StringBuilder();
80        builder.append('L');
81        builder.append(className.replace('.', '/'));
82        builder.append(';');
83        return builder.toString();
84    }
85
86    /**
87     * Returns the signature of the given class.
88     * @param c
89     *          a class
90     * @return the class signature
91     */
92    protected static String getClassSignature(Class<?> c) {
93        String className = c.getName();
94        return getClassSignature(className);
95    }
96
97    /**
98     * This method will be invoked before starting each test.
99     *
100     * @throws Exception
101     *             if any error occurs
102     */
103    protected void internalSetUp() throws Exception {
104    }
105
106    /**
107     * This method will be invoked after each test completed or any error
108     * occurred.
109     */
110    protected void internalTearDown() {
111    }
112
113    /**
114     * Overrides inherited JUnit method to provide initialization and invocation
115     * of internalSetUp() and internalTearDown() methods.
116     */
117    protected void setUp() throws Exception {
118        super.setUp();
119
120        settings = createTestOptions();
121        settings.setDebuggeeClassName(getDebuggeeClassName());
122
123        logWriter = new JPDALogWriter(System.out, null, settings.isVerbose());
124
125        logWriter.println("\n=====================================>>>");
126        logWriter.println("Run: " + getClass().getName() + "." + getName());
127        logWriter.println("----------------------------------------");
128
129        try {
130            internalSetUp();
131            logWriter.println("----------------------------------------");
132        } catch (Throwable e) {
133            logWriter.printError(e);
134            logWriter.println("----------------------------------------");
135            internalTearDown();
136            throw new TestErrorException(e);
137        }
138    }
139
140    /**
141     * Creates wrapper object for accessing test options;
142     */
143    protected JPDATestOptions createTestOptions() {
144        return new JPDATestOptions();
145    }
146
147    /**
148     * Overrides inherited JUnit method to provide cleanup and invocation of
149     * internalTearDown() method.
150     */
151    protected void tearDown() throws Exception {
152        logWriter.println("----------------------------------------");
153        internalTearDown();
154        logWriter.println("<<<=====================================\n");
155
156        super.tearDown();
157    }
158
159    /**
160     * Helper method to print a message prefixed by the current test name.
161     * @param msg the message to print
162     */
163    protected void printTestLog(String msg) {
164        String testName = getName();
165        logWriter.println(">> " + testName + ": " + msg);
166    }
167}
168