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