merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / runner / complexlib / ComplexTestCase.java
blobc5663d23bed977a02af47db532496954bcba7318
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ComplexTestCase.java,v $
10 * $Revision: 1.16.2.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package complexlib;
32 import java.lang.reflect.Method;
33 import share.DescEntry;
34 import lib.TestParameters;
35 import lib.StatusException;
36 import share.LogWriter;
37 import share.ComplexTest;
38 import java.io.PrintWriter;
40 /**
41 * Base class for all complex tests.
43 public abstract class ComplexTestCase extends Assurance implements ComplexTest
46 /** The test parameters **/
47 protected static TestParameters param = null;
48 /** Log writer **/
49 protected static LogWriter log = null;
50 /** Description entry **/
51 protected DescEntry subEntry = null;
52 /**
53 * The method name which will be written into f.e. the data base
54 **/
55 protected String mTestMethodName = null;
56 /** Maximal time one method is allowed to execute
57 * Can be set with parameter 'ThreadTimeOut'
58 **/
59 protected int mThreadTimeOut = 0;
60 /** Continue a test even if it did fail **/
61 // public static final boolean CONTINUE = true;
63 /** End a test if it did fail **/
64 public static final boolean BREAK = true;
66 /**
67 * Call test. It is expected, that an environment is
68 * given to this test.
70 * @param entry The name of the test method that should be called.
71 * @param environment The environment for the test.
73 public void executeMethods(DescEntry entry, TestParameters environment)
76 // get the environment
77 param = environment;
78 log = entry.Logger;
80 mThreadTimeOut = param.getInt("ThreadTimeOut");
81 if (mThreadTimeOut == 0)
83 mThreadTimeOut = 300000;
85 // start with the before() method
86 boolean beforeWorked = true;
87 try
89 Method before = this.getClass().getMethod("before", new Class[] {} );
90 before.invoke(this, new Object[] {} );
92 catch (java.lang.NoSuchMethodException e)
94 // simply ignore
96 catch (java.lang.IllegalAccessException e)
98 log.println("Cannot access the 'before()' method, although it" + " is there. Is this ok?");
100 catch (java.lang.reflect.InvocationTargetException e)
102 beforeWorked = false;
103 Throwable t = e.getTargetException();
104 if (!(t instanceof RuntimeException) || state)
106 log.println(t.toString());
107 if (message == null)
109 message = "Exception in before() method.\n\r" + t.getMessage();
111 state = false;
112 t.printStackTrace((PrintWriter) log);
117 //executeMethodTests
118 for (int i = 0; i < entry.SubEntries.length; i++)
120 subEntry = entry.SubEntries[i];
121 if (beforeWorked)
123 state = true;
124 message = "";
126 else
128 // set all test methods on failed, if 'before()' did not work.
129 subEntry.State = message;
130 subEntry.hasErrorMsg = true;
131 subEntry.ErrorMsg = message;
132 continue;
134 Method testMethod = null;
137 String entryName = subEntry.entryName;
138 Object[] parameter = null;
140 if (entryName.indexOf("(") != -1)
142 String sParameter = (entryName.substring(entryName.indexOf("(") + 1, entryName.indexOf(")")));
143 mTestMethodName = entryName;
144 parameter = new String[]
146 sParameter
148 entryName = entryName.substring(0, entryName.indexOf("("));
149 testMethod = this.getClass().getMethod(entryName, new Class[] { String.class });
151 else
153 testMethod = this.getClass().getMethod(entryName, new Class[] {} );
154 mTestMethodName = entryName;
157 MethodThread th = new MethodThread(testMethod, this, parameter, (java.io.PrintWriter) log);
158 log.println("Starting " + mTestMethodName);
159 th.start();
163 // some tests are very dynamic in its exceution time so that
164 // a threadTimeOut fials. In this cases the logging mechanisim
165 // is a usefull way to detect that a office respective a test
166 // is running and not death.
167 // But way ThreadTimeOut?
168 // There exeitsts a complex test which uses no office. Therefore
169 // a logging mechanisim to detect a stalled test.
170 int lastPing = -1;
171 int newPing = 0;
173 int sleepingStep = 1000;
174 int factor = 0;
176 while (th.isAlive() && (lastPing != newPing || factor * sleepingStep < mThreadTimeOut))
178 Thread.sleep(sleepingStep);
179 factor++;
180 // if a test starts the office itself it the watcher is a
181 // new one.
182 share.Watcher ow = (share.Watcher) param.get("Watcher");
183 if (ow != null)
185 lastPing = newPing;
186 newPing = ow.getPing();
187 //System.out.println("lastPing: '" + lastPing + "' newPing '" + newPing + "'");
188 factor = 0;
192 catch (InterruptedException e)
195 if (th.isAlive())
197 log.println("Destroy " + mTestMethodName);
198 th.destroy();
199 subEntry.State = "Test did sleep for " + (mThreadTimeOut / 1000) + " seconds and has been killed!";
200 subEntry.hasErrorMsg = true;
201 subEntry.ErrorMsg = subEntry.State;
202 continue;
204 else
206 log.println("Finished " + mTestMethodName);
207 if (th.hasErrorMessage())
209 subEntry.State = th.getErrorMessage();
210 subEntry.hasErrorMsg = true;
211 subEntry.ErrorMsg = subEntry.State;
212 continue;
216 catch (java.lang.Exception e)
218 log.println(e.getClass().getName());
219 String msg = e.getMessage();
220 log.println("Message: " + msg);
221 e.printStackTrace((PrintWriter) log);
222 subEntry.State = "SKIPPED.FAILED";
223 subEntry.hasErrorMsg = true;
224 subEntry.ErrorMsg = (msg == null ? "" : msg);
225 continue;
227 subEntry.State = (state ? "PASSED.OK" : message);
228 subEntry.hasErrorMsg = !state;
229 subEntry.ErrorMsg = message;
232 if (beforeWorked)
234 // the after() method
237 Method after = this.getClass().getMethod("after", new Class[] {});
238 after.invoke(this, new Object[] {} );
240 catch (java.lang.NoSuchMethodException e)
242 // simply ignore
244 catch (java.lang.IllegalAccessException e)
246 // simply ignore
248 catch (java.lang.reflect.InvocationTargetException e)
250 Throwable t = e.getTargetException();
251 if (!(t instanceof StatusException))
253 log.println(t.toString());
254 if (message == null)
256 message = "Exception in after() method.\n\r" + t.getMessage();
258 else
260 message += "Exception in \'after()\' method.\n\r" + t.getMessage();
262 log.println("Message: " + message);
263 t.printStackTrace((PrintWriter) log);
270 * Implement this method in the Complex test.
271 * @return All test method names.
273 public abstract String[] getTestMethodNames();
276 * Return a name for the test or tested object.
277 * Override to give an own name.
278 * @return As default, the name of this class.
280 public String getTestObjectName()
282 return this.getClass().getName();