Home | History | Annotate | Download | only in share
      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  */
     26 package org.apache.harmony.jpda.tests.jdwp.share;
     27 
     28 import org.apache.harmony.jpda.tests.framework.TestErrorException;
     29 import org.apache.harmony.jpda.tests.share.JPDALogWriter;
     30 import org.apache.harmony.jpda.tests.share.JPDATestOptions;
     31 
     32 import 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  */
     45 public 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     @Override
    118     protected void setUp() throws Exception {
    119         super.setUp();
    120 
    121         settings = createTestOptions();
    122         settings.setDebuggeeClassName(getDebuggeeClassName());
    123 
    124         logWriter = new JPDALogWriter(System.out, null, settings.isVerbose());
    125 
    126         logWriter.println("\n=====================================>>>");
    127         logWriter.println("Run: " + getClass().getName() + "." + getName());
    128         logWriter.println("----------------------------------------");
    129 
    130         try {
    131             internalSetUp();
    132             logWriter.println("----------------------------------------");
    133         } catch (Throwable e) {
    134             logWriter.printError(e);
    135             logWriter.println("----------------------------------------");
    136             internalTearDown();
    137             throw new TestErrorException(e);
    138         }
    139     }
    140 
    141     /**
    142      * Creates wrapper object for accessing test options;
    143      */
    144     protected JPDATestOptions createTestOptions() {
    145         return new JPDATestOptions();
    146     }
    147 
    148     /**
    149      * Overrides inherited JUnit method to provide cleanup and invocation of
    150      * internalTearDown() method.
    151      */
    152     @Override
    153     protected void tearDown() throws Exception {
    154         logWriter.println("----------------------------------------");
    155         internalTearDown();
    156         logWriter.println("<<<=====================================\n");
    157 
    158         super.tearDown();
    159     }
    160 
    161     /**
    162      * Helper method to print a message prefixed by the current test name.
    163      * @param msg the message to print
    164      */
    165     protected void printTestLog(String msg) {
    166         String testName = getName();
    167         logWriter.println(">> " + testName + ": " + msg);
    168     }
    169 }
    170