File: BasicUnitTestManager.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (361 lines) | stat: -rw-r--r-- 9,967 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*

   Derby - Class org.apache.derbyTesting.unitTests.harness.BasicUnitTestManager

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.unitTests.harness;

import org.apache.derby.iapi.services.context.ContextManager;
import org.apache.derby.iapi.services.context.ContextService;
import org.apache.derby.iapi.services.monitor.ModuleFactory;
import org.apache.derby.iapi.services.monitor.ModuleControl;
import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.iapi.services.monitor.Monitor;
import org.apache.derby.shared.common.sanity.SanityManager;
import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.AccessController;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import java.util.Hashtable;

public class BasicUnitTestManager implements UnitTestManager, ModuleControl
{
    private Vector<UnitTest> vectorOfTests;
    private Hashtable<String, String> namesOfTests;

	private	static	boolean	alreadyRun = false;
	private HeaderPrintWriter output;
	private HeaderPrintWriter currentOutput;
	private int testType = UnitTestConstants.TYPE_COMPLETE;
	private int testDuration = UnitTestConstants.DURATION_FOREVER;
	private boolean reportOutputOn = true;
	private boolean performanceReportOn = false;
	private ContextService contextService;
	private boolean runForever = false; 

	/*
	** Constructor
	*/

	public BasicUnitTestManager() {
	}

	/*
	** Methods of ModuleControl
	*/
	public void boot(boolean create, Properties startParams)
		 throws StandardException
	{
		boolean	testStatus = true;

		// startParams should define output, for now
		// use the sytem trace stream. If that doesn't exist
		// then use a null stream.

		output = Monitor.getStream();

		contextService = getContextService();

		this.currentOutput = output;

		vectorOfTests = new Vector<UnitTest>();
		namesOfTests = new Hashtable<String, String>();

		findTests(startParams, startParams);
		try {
			findTests(System.getProperties(), startParams);
		} catch (SecurityException se) {
		}
		findTests(getMonitor().getApplicationProperties(), startParams);

		if ( !alreadyRun )
		{
			testStatus = runTests();
			alreadyRun = true;
		}

		if (!testStatus) {

			// try to print out that the shutdown is occurring.
			System.out.println("Shutting down due to unit test failure.");
			output.printlnWithHeader("Shutting down due to unit test failure, see log for more information.");

			getMonitor().shutdown();
		}
	}

	public void stop(){
		return;
	}

	public synchronized void registerTest(UnitTest objectToTest, String testName){

		// only add the new test if it isn't already there.
		// otherwise you will upset me.
		if ( !namesOfTests.containsKey( testName ) )
		{
			vectorOfTests.addElement(objectToTest);
			namesOfTests.put( testName, testName );
		}
	}

	private void findTests(Properties testList, Properties startParams) {

		if (testList == null)
			return;

		for (Enumeration e = testList.propertyNames(); e.hasMoreElements(); ) {

			String key = (String) e.nextElement();
			if (key.startsWith("derby.module.test.")) {
				String unitTestClass = testList.getProperty(key);

				try {
					Object unitTest =
						bootServiceModule(false, this, unitTestClass,
												  startParams);
					if (unitTest instanceof UnitTest) {
						registerTest((UnitTest) unitTest, unitTestClass);
					} else if (unitTest != null) {
					System.out.println("class does not implement UnitTest " +
									   unitTestClass);
					}
				} catch (StandardException se) {
					System.out.println("exception booting " + unitTestClass);
					System.out.println(se.toString());
					se.printStackTrace(System.out);
				}
			}
		}
	}

	/**
	 *	emitAMessage
	 *
	 *	Convenience routine to emit messages. This routine only works
	 *  for messages provided by this package.
	 *
	 *  @see	UnitTestConstants for supported durations.
	 **/
	private void emitAMessage(String message){

	   currentOutput.printlnWithHeader(message);
	}

	private boolean runATest(UnitTest aTest){

		boolean result;

		String thisTestName = aTest.getClass().getName();
		Date startTime = null, endTime;

		// push a new context manager
		ContextManager cm = null;
		if (contextService != null) {
			cm = contextService.newContextManager();
			contextService.setCurrentContextManager(cm);
		}

		if (performanceReportOn)
			startTime = new Date();

		try{
			emitAMessage("Starting test  '" + thisTestName + "'.");
			result = aTest.Execute(currentOutput);
			if (result == true)
				emitAMessage("Test '" + thisTestName + "' passed");
			else
				emitAMessage("Test '" + thisTestName + "' failed");

		} catch (Throwable t) {
			if (t instanceof ThreadDeath)
			{
				t.printStackTrace(output.getPrintWriter());
				Runtime.getRuntime().exit(1);
			}

			result = false;
			String  msg = t.getMessage();
			if (msg == null) msg = t.getClass().getName();
			emitAMessage("Test '" + thisTestName + "' failed with exception '" + msg +"'.");
			t.printStackTrace(output.getPrintWriter());
		} finally {

			if (contextService != null) {
				//
				//Assure the completed test does not stick around
				//cm.cleanupOnError
				//	(BasicUnitTestDatabaseException.cleanUp());
				contextService.resetCurrentContextManager(cm);
			}
		}

		if (performanceReportOn){
			endTime = new Date();
			emitAMessage("Test '" + thisTestName + "' took " + (endTime.getTime() - startTime.getTime()) + " milliseconds.");
		}

		return result;
	}

	// STUB: Verify its ok this is synchronized.
	public synchronized boolean runTests(){

		boolean result = true;
		int passCount = 0;
		int failCount = 0;
		int skipCount = 0;
		boolean runTests = true;

		if (SanityManager.DEBUG)
		{
			runTests =
				!SanityManager.DEBUG_ON(UnitTestManager.SKIP_UNIT_TESTS);
			runForever =
				SanityManager.DEBUG_ON(UnitTestManager.RUN_FOREVER);
		}
		if (runTests) {

		if (!runForever) T_Bomb.makeBomb();
		for(int ix = vectorOfTests.size() - 1; ix >= 0 ; ix--){

			UnitTest thisTest =
				((UnitTest)vectorOfTests.elementAt(ix));
			if (thisTest.UnitTestDuration() <= this.testDuration &&
				thisTest.UnitTestType() <= this.testType){
				if (runATest(thisTest))
					passCount++;
				else
					failCount++;
				vectorOfTests.removeElementAt(ix);
			}
			else{
				skipCount++;
			}
		}
		emitAMessage("Test Summary - Run " + (passCount+failCount) +
			", Passed " + passCount + ", Failed " + failCount + ", Skipped " + skipCount + ".");
		}
		else {
			emitAMessage("Tests not run.");
		}
		return (failCount == 0);
	}


	public boolean runTests(int testType, int testDuration){
		//STUB: Sanity check for type/duration
		this.testType = testType;
		this.testDuration = testDuration;
		return runTests();
	}


	public void setTestDuration(int testDuration){
		//STUB: Sanity check for type/duration
		this.testDuration = testDuration;
		return;
	}


	public void setTestType(int testType){
		//STUB: Sanity check for type/duration
		this.testType = testType;
		return;
	}


	public void setPerformanceReportOn(boolean performanceReportOn){
		this.performanceReportOn = performanceReportOn;
		return;
	}	
    
    /**
     * Privileged lookup of the ContextService. Must be private so that user code
     * can't call this entry point.
     */
    private  static  ContextService    getContextService()
    {
        return AccessController.doPrivileged
            (
             new PrivilegedAction<ContextService>()
             {
                 public ContextService run()
                 {
                     return ContextService.getFactory();
                 }
             }
             );
    }

    
    /**
     * Privileged Monitor lookup. Must be private so that user code
     * can't call this entry point.
     */
    private  static  ModuleFactory  getMonitor()
    {
        return AccessController.doPrivileged
            (
             new PrivilegedAction<ModuleFactory>()
             {
                 public ModuleFactory run()
                 {
                     return Monitor.getMonitor();
                 }
             }
             );
    }

    /**
     * Privileged startup. Must be private so that user code
     * can't call this entry point.
     */
    private  static  Object bootServiceModule
        (
         final boolean create, final Object serviceModule,
         final String factoryInterface, final Properties properties
         )
        throws StandardException
    {
        try {
            return AccessController.doPrivileged
                (
                 new PrivilegedExceptionAction<Object>()
                 {
                     public Object run()
                         throws StandardException
                     {
                         return Monitor.bootServiceModule( create, serviceModule, factoryInterface, properties );
                     }
                 }
                 );
        } catch (PrivilegedActionException pae)
        {
            throw StandardException.plainWrapException( pae );
        }
    }

}