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     * This method will be invoked before starting each test.
63     *
64     * @throws Exception
65     *             if any error occurs
66     */
67    protected void internalSetUp() throws Exception {
68    }
69
70    /**
71     * This method will be invoked after each test completed or any error
72     * occurred.
73     */
74    protected void internalTearDown() {
75    }
76
77    /**
78     * Overrides inherited JUnit method to provide initialization and invocation
79     * of internalSetUp() and internalTearDown() methods.
80     */
81    protected void setUp() throws Exception {
82        super.setUp();
83
84        settings = createTestOptions();
85        settings.setDebuggeeClassName(getDebuggeeClassName());
86
87        logWriter = new JPDALogWriter(System.out, null, settings.isVerbose());
88
89        logWriter.println("\n=====================================>>>");
90        logWriter.println("Run: " + getClass().getName() + "." + getName());
91        logWriter.println("----------------------------------------");
92
93        try {
94            internalSetUp();
95            logWriter.println("----------------------------------------");
96        } catch (Throwable e) {
97            logWriter.printError(e);
98            logWriter.println("----------------------------------------");
99            internalTearDown();
100            throw new TestErrorException(e);
101        }
102    }
103
104    /**
105     * Creates wrapper object for accessing test options;
106     */
107    protected JPDATestOptions createTestOptions() {
108        return new JPDATestOptions();
109    }
110
111    /**
112     * Overrides inherited JUnit method to provide cleanup and invocation of
113     * internalTearDown() method.
114     */
115    protected void tearDown() throws Exception {
116        logWriter.println("----------------------------------------");
117        internalTearDown();
118        logWriter.println("<<<=====================================\n");
119
120        super.tearDown();
121    }
122}
123