1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package lib; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.io.PrintWriter; 27cdf0e10cSrcweir 28cdf0e10cSrcweir import lib.TestParameters; 29cdf0e10cSrcweir /** 30cdf0e10cSrcweir * <code>TestCase</code> represent a factory for <code>TestEnvironment</code>s 31cdf0e10cSrcweir * creation and disposing for a given implementation object. The 32cdf0e10cSrcweir * <code>TestEnvironment</code> contains an instance of the implementation 33cdf0e10cSrcweir * object and all additional objects needed to perform tests on the object. 34cdf0e10cSrcweir * 35cdf0e10cSrcweir * <p>The <code>TestCase</code> provides four methods for its subclasses to 36cdf0e10cSrcweir * define its functionality: <code>initialize()</code>, <code>cleanup()</code>, 37cdf0e10cSrcweir * <code>createTestEnvironment()</code> and <code>disposeTestEnvironment()</code>. 38cdf0e10cSrcweir * The first two are intended to initialize and cleanup common objects shared 39cdf0e10cSrcweir * among all instances of <code>TestEnvironment</code> produced by the 40cdf0e10cSrcweir * <code>TestCase</code>, and they are called at the beginning and at the end of 41cdf0e10cSrcweir * the <code>TestCase</code> lifecycle accordingly. 42cdf0e10cSrcweir * 43cdf0e10cSrcweir * <p>The other two are intended to produce and dispose 44cdf0e10cSrcweir * <code>TestEnvironment</code> instances. The 45cdf0e10cSrcweir * <code>createTestEnvironment()</code> is called to create a 46cdf0e10cSrcweir * <code>TestEnvironment</code> instance and the 47cdf0e10cSrcweir * <code>disposeTestEnvironment()</code> is called when the instane is not used 48cdf0e10cSrcweir * anymore. 49cdf0e10cSrcweir * 50cdf0e10cSrcweir * @see lib.TestEnvironment 51cdf0e10cSrcweir */ 52cdf0e10cSrcweir public abstract class TestCase { 53cdf0e10cSrcweir 54cdf0e10cSrcweir /** 55cdf0e10cSrcweir * Specifies the PrintWriter to log information. 56cdf0e10cSrcweir */ 57cdf0e10cSrcweir public PrintWriter log; 58cdf0e10cSrcweir 59cdf0e10cSrcweir //public static TestCase tCase; 60cdf0e10cSrcweir 61cdf0e10cSrcweir /** 62cdf0e10cSrcweir * Sets the log to write information during testing. 63cdf0e10cSrcweir */ 64cdf0e10cSrcweir public void setLogWriter( PrintWriter log ) { 65cdf0e10cSrcweir this.log = log; 66cdf0e10cSrcweir } 67cdf0e10cSrcweir 68cdf0e10cSrcweir /** 69cdf0e10cSrcweir * Initializes the <code>TestCase</code>. Calls <code>initialize()</code> 70cdf0e10cSrcweir * method. 71cdf0e10cSrcweir * 72cdf0e10cSrcweir * @param tParam test parameters. 73cdf0e10cSrcweir */ 74cdf0e10cSrcweir public void initializeTestCase( TestParameters tParam ) { 75cdf0e10cSrcweir initialize( tParam, log ); 76cdf0e10cSrcweir } 77cdf0e10cSrcweir 78cdf0e10cSrcweir /** 79cdf0e10cSrcweir * Called while the <code>TestCase</code> initialization. In the 80cdf0e10cSrcweir * implementation does nothing. Subclasses can override to initialize 81cdf0e10cSrcweir * objects shared among all <code>TestEnvironment</code>s. 82cdf0e10cSrcweir * 83cdf0e10cSrcweir * @param tParam test parameters 84cdf0e10cSrcweir * @param log writer to log information while testing 85cdf0e10cSrcweir * 86cdf0e10cSrcweir * @see #initializeTestCase() 87cdf0e10cSrcweir */ 88cdf0e10cSrcweir protected void initialize( TestParameters tParam, PrintWriter log ) { 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir 92cdf0e10cSrcweir /** 93cdf0e10cSrcweir * Cleans up the <code>TestCase</code>. Calls <code>cleanup()</code>. 94cdf0e10cSrcweir * 95cdf0e10cSrcweir * @param tParam test parameters 96cdf0e10cSrcweir */ 97cdf0e10cSrcweir public void cleanupTestCase( TestParameters tParam ) { 98cdf0e10cSrcweir cleanup( tParam, log ); 99cdf0e10cSrcweir } 100cdf0e10cSrcweir 101cdf0e10cSrcweir /** 102cdf0e10cSrcweir * Called while the <code>TestCase</code> cleanup. In the implementation 103cdf0e10cSrcweir * does nothing. Subclasses can override to cleanup objects shared among 104cdf0e10cSrcweir * all <code>TestEnvironment</code>s. 105cdf0e10cSrcweir * 106cdf0e10cSrcweir * @param tParam test parameters 107cdf0e10cSrcweir * @param log writer to log information while testing 108cdf0e10cSrcweir * 109cdf0e10cSrcweir * @see #cleanupTestCase 110cdf0e10cSrcweir */ 111cdf0e10cSrcweir protected void cleanup( TestParameters tParam, PrintWriter log ) { 112cdf0e10cSrcweir } 113cdf0e10cSrcweir 114cdf0e10cSrcweir /** 115cdf0e10cSrcweir * Creates a <code>TestEnvironment</code> containing an instance of the 116cdf0e10cSrcweir * implementation object and related objects needed to perform test. 117cdf0e10cSrcweir * 118cdf0e10cSrcweir * @param tParam test parameters 119cdf0e10cSrcweir * 120cdf0e10cSrcweir * @return the created <code>TestEnvironment</code> 121cdf0e10cSrcweir * 122cdf0e10cSrcweir * @see #createTestEnvironment() 123cdf0e10cSrcweir * @see lib.TestEnvironment 124cdf0e10cSrcweir */ 125cdf0e10cSrcweir public synchronized TestEnvironment getTestEnvironment( TestParameters tParam ) { 126cdf0e10cSrcweir TestEnvironment tEnv = null; 127cdf0e10cSrcweir try { 128cdf0e10cSrcweir tEnv = createTestEnvironment( tParam, log ); 129cdf0e10cSrcweir System.out.println("Environment created"); 130cdf0e10cSrcweir if (tEnv != null) { 131cdf0e10cSrcweir tEnv.setTestCase(this); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir } catch (Exception e) { 134cdf0e10cSrcweir String message = e.getMessage(); 135cdf0e10cSrcweir if (message == null) 136cdf0e10cSrcweir message = e.toString(); 137cdf0e10cSrcweir System.out.println("Exception while getting Environment "+message); 138cdf0e10cSrcweir e.printStackTrace(); 139cdf0e10cSrcweir cleanup(tParam, log); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir return tEnv; 142cdf0e10cSrcweir } 143cdf0e10cSrcweir 144cdf0e10cSrcweir /** 145cdf0e10cSrcweir * Disposes the <code>TestEnvironment</code> when it is not needed anymore. 146cdf0e10cSrcweir * 147cdf0e10cSrcweir * @param tEnv the environment to dispose 148cdf0e10cSrcweir * @param tParam test parameters 149cdf0e10cSrcweir */ 150cdf0e10cSrcweir public synchronized void disposeTestEnvironment( TestEnvironment tEnv, 151cdf0e10cSrcweir TestParameters tParam ) { 152cdf0e10cSrcweir cleanup( tParam, log ); 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir /** 156cdf0e10cSrcweir * Called to create an instance of <code>TestEnvironment</code> with an 157cdf0e10cSrcweir * object to test and related objects. Subclasses should implement this 158cdf0e10cSrcweir * method to provide the implementation and related objects. The method is 159cdf0e10cSrcweir * called from <code>getTestEnvironment()</code>. 160cdf0e10cSrcweir * 161cdf0e10cSrcweir * @param tParam test parameters 162cdf0e10cSrcweir * @param log writer to log information while testing 163cdf0e10cSrcweir * 164cdf0e10cSrcweir * @see TestEnvironment 165cdf0e10cSrcweir * @see #getTestEnvironment() 166cdf0e10cSrcweir */ 167cdf0e10cSrcweir protected abstract TestEnvironment createTestEnvironment( 168cdf0e10cSrcweir TestParameters tParam, PrintWriter log ); 169cdf0e10cSrcweir 170cdf0e10cSrcweir /** 171cdf0e10cSrcweir * @return the name of the object 172cdf0e10cSrcweir */ 173cdf0e10cSrcweir public String getObjectName() { 174cdf0e10cSrcweir String clName = this.getClass().getName(); 175cdf0e10cSrcweir return clName.substring( clName.lastIndexOf('.') + 1 ); 176cdf0e10cSrcweir } 177cdf0e10cSrcweir 178cdf0e10cSrcweir } 179