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 .
21 import java
.io
.PrintWriter
;
22 import lib
.TestParameters
;
25 * <code>TestCase</code> represent a factory for <code>TestEnvironment</code>s
26 * creation and disposing for a given implementation object. The
27 * <code>TestEnvironment</code> contains an instance of the implementation
28 * object and all additional objects needed to perform tests on the object.
30 * <p>The <code>TestCase</code> provides four methods for its subclasses to
31 * define its functionality: <code>initialize()</code>, <code>cleanup()</code>,
32 * <code>createTestEnvironment()</code> and <code>disposeTestEnvironment()</code>.
33 * The first two are intended to initialize and cleanup common objects shared
34 * among all instances of <code>TestEnvironment</code> produced by the
35 * <code>TestCase</code>, and they are called at the beginning and at the end of
36 * the <code>TestCase</code> lifecycle accordingly.
38 * <p>The other two are intended to produce and dispose
39 * <code>TestEnvironment</code> instances. The
40 * <code>createTestEnvironment()</code> is called to create a
41 * <code>TestEnvironment</code> instance and the
42 * <code>disposeTestEnvironment()</code> is called when the instane is not used
45 * @see lib.TestEnvironment
47 public abstract class TestCase
{
50 * Specifies the PrintWriter to log information.
52 protected PrintWriter log
;
55 * Sets the log to write information during testing.
57 public void setLogWriter( PrintWriter log
) {
62 * Initializes the <code>TestCase</code>. Calls <code>initialize()</code>
65 * @param tParam test parameters.
67 public final void initializeTestCase( TestParameters tParam
) {
68 initialize( tParam
, log
);
72 * Called while the <code>TestCase</code> initialization. In the
73 * implementation does nothing. Subclasses can override to initialize
74 * objects shared among all <code>TestEnvironment</code>s.
76 * @param tParam test parameters
77 * @param log writer to log information while testing
79 * @see #initializeTestCase
81 protected void initialize( TestParameters tParam
, PrintWriter log
) {
86 * Cleans up the <code>TestCase</code>. Calls <code>cleanup()</code>.
88 * @param tParam test parameters
90 public final void cleanupTestCase( TestParameters tParam
) {
91 cleanup( tParam
, log
);
95 * Called while the <code>TestCase</code> cleanup. In the implementation
96 * does nothing. Subclasses can override to cleanup objects shared among
97 * all <code>TestEnvironment</code>s.
99 * @param tParam test parameters
100 * @param log writer to log information while testing
102 * @see #cleanupTestCase
104 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
108 * Creates a <code>TestEnvironment</code> containing an instance of the
109 * implementation object and related objects needed to perform test.
111 * @param tParam test parameters
113 * @return the created <code>TestEnvironment</code>
115 * @see #createTestEnvironment
116 * @see lib.TestEnvironment
118 public synchronized TestEnvironment
getTestEnvironment( TestParameters tParam
) {
119 TestEnvironment tEnv
= null;
121 tEnv
= createTestEnvironment( tParam
, log
);
122 System
.out
.println("Environment created");
124 tEnv
.setTestCase(this);
126 } catch (Exception e
) {
127 String message
= e
.getMessage();
129 message
= e
.toString();
130 System
.out
.println("Exception while getting Environment "+message
);
132 cleanup(tParam
, log
);
138 * Disposes the <code>TestEnvironment</code> when it is not needed anymore.
140 * @param tEnv the environment to dispose
141 * @param tParam test parameters
143 public synchronized void disposeTestEnvironment( TestEnvironment tEnv
,
144 TestParameters tParam
) {
145 cleanup( tParam
, log
);
149 * Called to create an instance of <code>TestEnvironment</code> with an
150 * object to test and related objects. Subclasses should implement this
151 * method to provide the implementation and related objects. The method is
152 * called from <code>getTestEnvironment()</code>.
154 * @param tParam test parameters
155 * @param log writer to log information while testing
157 * @see TestEnvironment
158 * @see #getTestEnvironment
160 protected abstract TestEnvironment
createTestEnvironment(
161 TestParameters tParam
, PrintWriter log
);
164 * @return the name of the object
166 public String
getObjectName() {
167 String clName
= this.getClass().getName();
168 return clName
.substring( clName
.lastIndexOf('.') + 1 );