xref: /aoo42x/main/qadevOOo/runner/lib/TestCase.java (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package lib;
29 
30 import java.io.PrintWriter;
31 
32 import lib.TestParameters;
33 /**
34  * <code>TestCase</code> represent a factory for <code>TestEnvironment</code>s
35  * creation and disposing for a given implementation object. The
36  * <code>TestEnvironment</code> contains an instance of the implementation
37  * object and all additional objects needed to perform tests on the object.
38  *
39  * <p>The <code>TestCase</code> provides four methods for its subclasses to
40  * define its functionality: <code>initialize()</code>, <code>cleanup()</code>,
41  * <code>createTestEnvironment()</code> and <code>disposeTestEnvironment()</code>.
42  * The first two are intended to initialize and cleanup common objects shared
43  * among all instances of <code>TestEnvironment</code> produced by the
44  * <code>TestCase</code>, and they are called at the beginning and at the end of
45  * the <code>TestCase</code> lifecycle accordingly.
46  *
47  * <p>The other two are intended to produce and dispose
48  * <code>TestEnvironment</code> instances. The
49  * <code>createTestEnvironment()</code> is called to create a
50  * <code>TestEnvironment</code> instance and the
51  * <code>disposeTestEnvironment()</code> is called when the instane is not used
52  * anymore.
53  *
54  * @see lib.TestEnvironment
55  */
56 public abstract class TestCase {
57 
58     /**
59      * Specifies the PrintWriter to log information.
60      */
61     public PrintWriter log;
62 
63     //public static TestCase tCase;
64 
65     /**
66      * Sets the log to write information during testing.
67      */
68     public void setLogWriter( PrintWriter log ) {
69         this.log = log;
70     }
71 
72     /**
73      * Initializes the <code>TestCase</code>. Calls <code>initialize()</code>
74      * method.
75      *
76      * @param tParam test parameters.
77      */
78     public void initializeTestCase( TestParameters tParam ) {
79         initialize( tParam, log );
80     }
81 
82     /**
83      * Called while the <code>TestCase</code> initialization. In the
84      * implementation does nothing. Subclasses can override to initialize
85      * objects shared among all <code>TestEnvironment</code>s.
86      *
87      * @param tParam test parameters
88      * @param log writer to log information while testing
89      *
90      * @see #initializeTestCase()
91      */
92     protected void initialize( TestParameters tParam, PrintWriter log ) {
93     }
94 
95 
96     /**
97      * Cleans up the <code>TestCase</code>. Calls <code>cleanup()</code>.
98      *
99      * @param tParam test parameters
100      */
101     public void cleanupTestCase( TestParameters tParam ) {
102         cleanup( tParam, log );
103     }
104 
105     /**
106      * Called while the <code>TestCase</code> cleanup. In the implementation
107      * does nothing. Subclasses can override to cleanup objects shared among
108      * all <code>TestEnvironment</code>s.
109      *
110      * @param tParam test parameters
111      * @param log writer to log information while testing
112      *
113      * @see #cleanupTestCase
114      */
115     protected void cleanup( TestParameters tParam, PrintWriter log ) {
116     }
117 
118     /**
119      * Creates a <code>TestEnvironment</code> containing an instance of the
120      * implementation object and related objects needed to perform test.
121      *
122      * @param tParam test parameters
123      *
124      * @return the created <code>TestEnvironment</code>
125      *
126      * @see #createTestEnvironment()
127      * @see lib.TestEnvironment
128      */
129     public synchronized TestEnvironment getTestEnvironment( TestParameters tParam ) {
130         TestEnvironment tEnv = null;
131         try {
132             tEnv = createTestEnvironment( tParam, log );
133             System.out.println("Environment created");
134             if (tEnv != null) {
135                 tEnv.setTestCase(this);
136             }
137         } catch (Exception e) {
138             String message = e.getMessage();
139             if (message == null)
140                 message = e.toString();
141             System.out.println("Exception while getting Environment "+message);
142             e.printStackTrace();
143             cleanup(tParam, log);
144         }
145         return tEnv;
146     }
147 
148     /**
149      * Disposes the <code>TestEnvironment</code> when it is not needed anymore.
150      *
151      * @param tEnv the environment to dispose
152      * @param tParam test parameters
153      */
154     public synchronized void disposeTestEnvironment( TestEnvironment tEnv,
155         TestParameters tParam ) {
156         cleanup( tParam, log );
157     }
158 
159     /**
160      * Called to create an instance of <code>TestEnvironment</code> with an
161      * object to test and related objects. Subclasses should implement this
162      * method to provide the implementation and related objects. The method is
163      * called from <code>getTestEnvironment()</code>.
164      *
165      * @param tParam test parameters
166      * @param log writer to log information while testing
167      *
168      * @see TestEnvironment
169      * @see #getTestEnvironment()
170      */
171     protected abstract TestEnvironment createTestEnvironment(
172             TestParameters tParam, PrintWriter log );
173 
174     /**
175      * @return the name of the object
176      */
177     public String getObjectName() {
178         String clName = this.getClass().getName();
179         return clName.substring( clName.lastIndexOf('.') + 1 );
180     }
181 
182 }
183