001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.harness.T_Generic
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.harness;
023:
024: import org.apache.derby.iapi.services.monitor.ModuleControl;
025: import org.apache.derby.iapi.services.monitor.Monitor;
026:
027: import org.apache.derbyTesting.unitTests.harness.UnitTest;
028: import org.apache.derbyTesting.unitTests.harness.UnitTestConstants;
029: import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
030: import org.apache.derby.iapi.services.sanity.SanityManager;
031: import org.apache.derby.iapi.error.StandardException;
032: import org.apache.derby.iapi.services.sanity.SanityManager;
033:
034: import java.util.Properties;
035:
036: /**
037: Abstract class which executes a unit test.
038:
039: <P>To write a test, extend this class with a class which implements the two
040: abstract methods:
041: <UL>
042: <LI>runTests
043: <LI>setUp
044: </UL>
045: @see UnitTest
046: @see ModuleControl
047: */
048: public abstract class T_Generic implements UnitTest, ModuleControl {
049: /**
050: The unqualified name for the module to test. This is set by the generic
051: code.
052: */
053: protected String shortModuleToTestName;
054:
055: /**
056: The start parameters for your test. This is set by generic code.
057: */
058: protected Properties startParams;
059:
060: /**
061: The HeaderPrintWriter for test output. This is set by the
062: generic code.
063: */
064: protected HeaderPrintWriter out;
065:
066: protected T_Generic() {
067: }
068:
069: /*
070: ** Public methods of ModuleControl
071: */
072:
073: /**
074: ModuleControl.start
075:
076: @see ModuleControl#boot
077: @exception StandardException Module cannot be started.
078: */
079: public void boot(boolean create, Properties startParams)
080: throws StandardException {
081: shortModuleToTestName = getModuleToTestProtocolName()
082: .substring(
083: getModuleToTestProtocolName().lastIndexOf('.') + 1);
084:
085: this .startParams = startParams;
086: }
087:
088: /**
089: ModuleControl.stop
090:
091: @see ModuleControl#stop
092: */
093: public void stop() {
094: }
095:
096: /*
097: ** Public methods of UnitTest
098: */
099: /**
100: UnitTest.Execute
101:
102: @see UnitTest#Execute
103: */
104: public boolean Execute(HeaderPrintWriter out) {
105: this .out = out;
106:
107: String myClass = this .getClass().getName();
108: String testName = myClass
109: .substring(myClass.lastIndexOf('.') + 1);
110:
111: System.out.println("-- Unit Test " + testName + " starting");
112:
113: try {
114: runTests();
115: }
116:
117: catch (Throwable t) {
118:
119: while (t != null) {
120: FAIL(t.toString());
121: t.printStackTrace(out.getPrintWriter());
122: if (t instanceof StandardException) {
123: t = ((StandardException) t).getNestedException();
124: continue;
125: }
126: break;
127: }
128: return false;
129: }
130:
131: System.out.println("-- Unit Test " + testName + " finished");
132:
133: return true;
134: }
135:
136: /**
137: UnitTest.UnitTestDuration
138:
139: @return UnitTestConstants.DURATION_MICRO
140: @see UnitTest#UnitTestDuration
141: @see UnitTestConstants
142: */
143: public int UnitTestDuration() {
144: return UnitTestConstants.DURATION_MICRO;
145: }
146:
147: /**
148: UnitTest.UnitTestType
149:
150: @return UnitTestConstants.TYPE_COMMON
151: @see UnitTest#UnitTestType
152: @see UnitTestConstants
153: */
154: public int UnitTestType() {
155: return UnitTestConstants.TYPE_COMMON;
156: }
157:
158: /**
159: Emit a message indicating why the test failed.
160:
161: RESOLVE: Should this be localized?
162:
163: @param msg the message.
164: @return false
165: */
166: protected boolean FAIL(String msg) {
167: out.println("[" + Thread.currentThread().getName()
168: + "] FAIL - " + msg);
169: return false;
170: }
171:
172: /**
173: Emit a message saying the test passed.
174: You may use this to emit messages indicating individual test cases
175: within a unit test passed.
176:
177: <P>RESOLVE:Localize this.
178: @param test the test which passed.
179: @return true
180: */
181: protected boolean PASS(String testName) {
182: out.println("[" + Thread.currentThread().getName()
183: + "] Pass - " + shortModuleToTestName + " " + testName);
184: return true;
185: }
186:
187: /**
188: Emit a message during a unit test run, indent the message
189: to allow the PASS/FAIL messages to stand out.
190: */
191: public void REPORT(String msg) {
192: out.println("[" + Thread.currentThread().getName() + "] "
193: + msg);
194: }
195:
196: /**
197: Abstract methods to implement for your test.
198: */
199:
200: /**
201: Run the test. The test should raise an exception if it
202: fails. runTests should return if the tests pass.
203:
204: @exception Exception Test code throws these
205: */
206: protected abstract void runTests() throws Exception;
207:
208: /**
209: Get the name of the protocol for the module to test.
210: This is the 'factory.MODULE' variable.
211:
212: 'moduleName' to the name of the module to test.
213:
214: @param testConfiguration the configuration for this test.
215: */
216: protected abstract String getModuleToTestProtocolName();
217: }
|