bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / complexlib / ComplexTestCase.java
blob56f320abff0314908c4e4f83d5bda3945dba19d4
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 .
18 package complexlib;
20 import java.lang.reflect.Method;
21 import share.DescEntry;
22 import lib.TestParameters;
23 import lib.StatusException;
24 import share.LogWriter;
25 import share.ComplexTest;
26 import java.io.PrintWriter;
28 /**
29 * Base class for all complex tests.
31 public abstract class ComplexTestCase extends Assurance implements ComplexTest
34 /** The test parameters **/
35 protected static TestParameters param = null;
36 /** Log writer **/
37 protected static LogWriter log = null;
38 /**
39 * The method name which will be written into f.e. the data base
40 **/
41 protected String mTestMethodName = null;
42 /** Maximal time one method is allowed to execute
43 * Can be set with parameter 'ThreadTimeOut'
44 **/
45 protected int m_nThreadTimeOut = 0;
46 /** Continue a test even if it did fail **/
47 // public static final boolean CONTINUE = true;
49 /** End a test if it did fail **/
50 public static final boolean BREAK = true;
52 private boolean m_bBeforeCalled;
54 /**
55 * is called before the real test starts
57 private void before()
59 try
61 Method before = this.getClass().getMethod("before", new Class[] {} );
62 before.invoke(this, new Object[] {} );
64 // beforeWorked = false;
65 m_bBeforeCalled = true;
67 catch (java.lang.NoSuchMethodException e)
69 m_bBeforeCalled = true;
71 catch (java.lang.IllegalAccessException e)
73 log.println("Cannot access the 'before()' method, although it" + " is there. Is this ok?");
75 catch (java.lang.reflect.InvocationTargetException e)
77 Throwable t = e.getTargetException();
78 if (!(t instanceof RuntimeException) || state)
80 log.println(t.toString());
81 if (message == null)
83 message = "Exception in before() method.\n\r" + t.getMessage();
85 state = false;
86 t.printStackTrace((PrintWriter) log);
92 /** Description entry **/
93 // protected DescEntry subEntry = null;
95 private void test_method(DescEntry _entry)
98 m_nThreadTimeOut = param.getInt("ThreadTimeOut");
99 if (m_nThreadTimeOut == 0)
101 m_nThreadTimeOut = 300000;
104 for (int i = 0; i < _entry.SubEntries.length; i++)
107 DescEntry subEntry = _entry.SubEntries[i];
108 if (m_bBeforeCalled)
110 state = true;
111 message = "";
113 else
115 // set all test methods on failed, if 'before()' did not work.
116 subEntry.State = message;
117 subEntry.hasErrorMsg = true;
118 subEntry.ErrorMsg = message;
119 continue;
121 Method testMethod = null;
124 String entryName = subEntry.entryName;
125 Object[] parameter = null;
127 if (entryName.indexOf("(") != -1)
129 String sParameter = (entryName.substring(entryName.indexOf("(") + 1, entryName.indexOf(")")));
130 mTestMethodName = entryName;
131 parameter = new String[] { sParameter };
132 entryName = entryName.substring(0, entryName.indexOf("("));
133 testMethod = this.getClass().getMethod(entryName, new Class[] { String.class });
135 else
137 testMethod = this.getClass().getMethod(entryName, new Class[] {} );
138 mTestMethodName = entryName;
141 MethodThread th = new MethodThread(testMethod, this, parameter, (java.io.PrintWriter) log);
142 log.println("Starting " + mTestMethodName);
143 th.start();
147 // some tests are very dynamic in its exceution time so that
148 // a threadTimeOut fials. In this cases the logging mechanisim
149 // is a useful way to detect that a office respective a test
150 // is running and not death.
151 // But way ThreadTimeOut?
152 // There exeitsts a complex test which uses no office. Therefore
153 // a logging mechanisim to detect a stalled test.
154 int lastPing = -1;
155 int newPing = 0;
157 int sleepingStep = 1000;
158 int factor = 0;
160 while (th.isAlive() && (lastPing != newPing || factor * sleepingStep < m_nThreadTimeOut))
162 Thread.sleep(sleepingStep);
163 factor++;
164 // if a test starts the office itself it the watcher is a
165 // new one.
166 share.Watcher ow = (share.Watcher) param.get("Watcher");
167 if (ow != null)
169 lastPing = newPing;
170 newPing = ow.getPing();
171 //System.out.println("lastPing: '" + lastPing + "' newPing '" + newPing + "'");
172 factor = 0;
176 catch (InterruptedException e)
179 if (th.isAlive())
181 log.println("Destroy " + mTestMethodName);
182 th.stopRunning();
183 subEntry.State = "Test did sleep for " + (m_nThreadTimeOut / 1000) + " seconds and has been killed!";
184 subEntry.hasErrorMsg = true;
185 subEntry.ErrorMsg = subEntry.State;
186 continue;
188 else
190 log.println("Finished " + mTestMethodName);
191 if (th.hasErrorMessage())
193 subEntry.State = th.getErrorMessage();
194 subEntry.hasErrorMsg = true;
195 subEntry.ErrorMsg = subEntry.State;
196 continue;
200 catch (java.lang.Exception e)
202 log.println(e.getClass().getName());
203 String msg = e.getMessage();
204 log.println("Message: " + msg);
205 e.printStackTrace((PrintWriter) log);
206 subEntry.State = "SKIPPED.FAILED";
207 subEntry.hasErrorMsg = true;
208 subEntry.ErrorMsg = (msg == null ? "" : msg);
209 continue;
211 subEntry.State = (state ? "PASSED.OK" : message);
212 subEntry.hasErrorMsg = !state;
213 subEntry.ErrorMsg = message;
218 * after() is called after the test is done
220 private void after()
222 if (m_bBeforeCalled)
224 // the after() method
227 Method after = this.getClass().getMethod("after", new Class[] {});
228 after.invoke(this, new Object[] {} );
230 catch (java.lang.NoSuchMethodException e)
232 // simply ignore
234 catch (java.lang.IllegalAccessException e)
236 // simply ignore
238 catch (java.lang.reflect.InvocationTargetException e)
240 Throwable t = e.getTargetException();
241 if (!(t instanceof StatusException))
243 log.println(t.toString());
244 if (message == null)
246 message = "Exception in after() method.\n\r" + t.getMessage();
248 else
250 message += "Exception in \'after()\' method.\n\r" + t.getMessage();
252 log.println("Message: " + message);
253 t.printStackTrace((PrintWriter) log);
263 * Call test. It is expected, that an environment is
264 * given to this test.
266 * @param entry The name of the test method that should be called.
267 * @param environment The environment for the test.
269 public void executeMethods(DescEntry entry, TestParameters environment)
271 m_bBeforeCalled = false;
273 // get the environment
274 param = environment;
275 log = entry.Logger;
278 // start with the before() method
279 before();
281 //executeMethodTests
282 test_method(entry);
284 // cleanup
285 after();
290 * Implement this method in the Complex test.
291 * @return All test method names.
293 public abstract String[] getTestMethodNames();
296 * Return a name for the test or tested object.
297 * Override to give an own name.
298 * @return As default, the name of this class.
300 public String getTestObjectName()
302 return this.getClass().getName();