Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / qadevOOo / runner / lib / TestCase.java
blobe68eff62e15e481afc8c9551c638eb13a97e424f
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package lib;
21 import java.io.PrintWriter;
23 /**
24 * <code>TestCase</code> represent a factory for <code>TestEnvironment</code>s
25 * creation and disposing for a given implementation object. The
26 * <code>TestEnvironment</code> contains an instance of the implementation
27 * object and all additional objects needed to perform tests on the object.
29 * <p>The <code>TestCase</code> provides four methods for its subclasses to
30 * define its functionality: <code>initialize()</code>, <code>cleanup()</code>,
31 * <code>createTestEnvironment()</code> and <code>disposeTestEnvironment()</code>.
32 * The first two are intended to initialize and cleanup common objects shared
33 * among all instances of <code>TestEnvironment</code> produced by the
34 * <code>TestCase</code>, and they are called at the beginning and at the end of
35 * the <code>TestCase</code> lifecycle accordingly.
37 * <p>The other two are intended to produce and dispose
38 * <code>TestEnvironment</code> instances. The
39 * <code>createTestEnvironment()</code> is called to create a
40 * <code>TestEnvironment</code> instance and the
41 * <code>disposeTestEnvironment()</code> is called when the instane is not used
42 * anymore.
44 * @see lib.TestEnvironment
46 public abstract class TestCase {
48 /**
49 * Specifies the PrintWriter to log information.
51 protected PrintWriter log;
53 /**
54 * Sets the log to write information during testing.
56 public void setLogWriter( PrintWriter log ) {
57 this.log = log;
60 /**
61 * Initializes the <code>TestCase</code>. Calls <code>initialize()</code>
62 * method.
64 * @param tParam test parameters.
66 public final void initializeTestCase( TestParameters tParam ) throws Exception {
67 initialize( tParam, log );
70 /**
71 * Called while the <code>TestCase</code> initialization. In the
72 * implementation does nothing. Subclasses can override to initialize
73 * objects shared among all <code>TestEnvironment</code>s.
75 * @param tParam test parameters
76 * @param log writer to log information while testing
78 * @see #initializeTestCase
80 protected void initialize( TestParameters tParam, PrintWriter log ) throws Exception {
84 /**
85 * Cleans up the <code>TestCase</code>. Calls <code>cleanup()</code>.
87 * @param tParam test parameters
89 public final void cleanupTestCase( TestParameters tParam ) {
90 cleanup( tParam, log );
93 /**
94 * Called while the <code>TestCase</code> cleanup. In the implementation
95 * does nothing. Subclasses can override to cleanup objects shared among
96 * all <code>TestEnvironment</code>s.
98 * @param tParam test parameters
99 * @param log writer to log information while testing
101 * @see #cleanupTestCase
103 protected void cleanup( TestParameters tParam, PrintWriter log ) {
107 * Creates a <code>TestEnvironment</code> containing an instance of the
108 * implementation object and related objects needed to perform test.
110 * @param tParam test parameters
112 * @return the created <code>TestEnvironment</code>
114 * @see #createTestEnvironment
115 * @see lib.TestEnvironment
117 public synchronized TestEnvironment getTestEnvironment( TestParameters tParam ) {
118 TestEnvironment tEnv = null;
119 try {
120 tEnv = createTestEnvironment( tParam, log );
121 System.out.println("Environment created");
122 if (tEnv != null) {
123 tEnv.setTestCase(this);
125 } catch (Exception e) {
126 String message = e.getMessage();
127 if (message == null)
128 message = e.toString();
129 System.out.println("Exception while getting Environment "+message);
130 e.printStackTrace();
131 cleanup(tParam, log);
133 return tEnv;
137 * Disposes the <code>TestEnvironment</code> when it is not needed anymore.
139 * @param tEnv the environment to dispose
140 * @param tParam test parameters
142 public synchronized void disposeTestEnvironment( TestEnvironment tEnv,
143 TestParameters tParam ) {
144 cleanup( tParam, log );
148 * Called to create an instance of <code>TestEnvironment</code> with an
149 * object to test and related objects. Subclasses should implement this
150 * method to provide the implementation and related objects. The method is
151 * called from <code>getTestEnvironment()</code>.
153 * @param tParam test parameters
154 * @param log writer to log information while testing
156 * @see TestEnvironment
157 * @see #getTestEnvironment
159 protected abstract TestEnvironment createTestEnvironment(
160 TestParameters tParam, PrintWriter log ) throws Exception;
163 * @return the name of the object
165 public String getObjectName() {
166 String clName = this.getClass().getName();
167 return clName.substring( clName.lastIndexOf('.') + 1 );