tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / qadevOOo / runner / complexlib / ComplexTestCase.java
blobab95d1eed03fb3cb316cee83feb0ff29a0d1b4d2
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 TestParameters param = null;
36 /** Log writer **/
37 protected LogWriter log = null;
38 /**
39 * The method name which will be written into f.e. the data base
40 **/
41 private String mTestMethodName = null;
43 private boolean m_bBeforeCalled;
45 /**
46 * is called before the real test starts
48 private void before()
50 try
52 Method before = this.getClass().getMethod("before", new Class[] {} );
53 before.invoke(this, new Object[] {} );
55 m_bBeforeCalled = true;
57 catch (java.lang.NoSuchMethodException e)
59 m_bBeforeCalled = true;
61 catch (java.lang.IllegalAccessException e)
63 log.println("Cannot access the 'before()' method, although it" + " is there. Is this ok?");
65 catch (java.lang.reflect.InvocationTargetException e)
67 Throwable t = e.getTargetException();
68 if (!(t instanceof RuntimeException) || bSuccessful)
70 log.println(t.toString());
71 if (message == null)
73 message = "Exception in before() method.\n\r" + t.getMessage();
75 bSuccessful = false;
76 t.printStackTrace((PrintWriter) log);
82 /** Description entry **/
84 private void test_method(DescEntry _entry)
87 /* Maximal time one method is allowed to execute
88 * Can be set with parameter 'ThreadTimeOut'
89 **/
90 int nThreadTimeOut = param.getInt("ThreadTimeOut");
91 if (nThreadTimeOut == 0)
93 nThreadTimeOut = 300000;
96 for (int i = 0; i < _entry.SubEntries.length; i++)
99 DescEntry subEntry = _entry.SubEntries[i];
100 if (m_bBeforeCalled)
102 bSuccessful = true;
103 message = "";
105 else
107 // set all test methods on failed, if 'before()' did not work.
108 subEntry.State = message;
109 subEntry.hasErrorMsg = true;
110 subEntry.ErrorMsg = message;
111 continue;
113 Method testMethod = null;
116 String entryName = subEntry.entryName;
117 Object[] parameter = null;
119 int posLeftParenthesis = entryName.indexOf('(');
120 if (posLeftParenthesis != -1)
122 String sParameter = entryName.substring(posLeftParenthesis + 1, entryName.indexOf(')'));
123 mTestMethodName = entryName;
124 parameter = new String[] { sParameter };
125 entryName = entryName.substring(0, posLeftParenthesis);
126 testMethod = this.getClass().getMethod(entryName, new Class[] { String.class });
128 else
130 testMethod = this.getClass().getMethod(entryName, new Class[] {} );
131 mTestMethodName = entryName;
134 MethodThread th = new MethodThread(testMethod, this, parameter, (java.io.PrintWriter) log);
135 log.println("Starting " + mTestMethodName);
136 th.start();
140 // some tests are very dynamic in their execution time so that
141 // a threadTimeOut fails. In this cases the logging mechanism
142 // is a useful way to detect that an office respective a test
143 // is running and not death.
144 // But way ThreadTimeOut?
145 // There exists a complex test which uses no office. Therefore
146 // a logging mechanism to detect a stalled test.
147 int lastPing = -1;
148 int newPing = 0;
150 int sleepingStep = 1000;
151 int factor = 0;
153 while (th.isAlive() && (lastPing != newPing || factor * sleepingStep < nThreadTimeOut))
155 Thread.sleep(sleepingStep);
156 factor++;
157 // if a test starts the office itself it the watcher is a
158 // new one.
159 share.Watcher ow = (share.Watcher) param.get("Watcher");
160 if (ow != null)
162 lastPing = newPing;
163 newPing = ow.getPing();
164 factor = 0;
168 catch (InterruptedException e)
171 if (th.isAlive())
173 log.println("Destroy " + mTestMethodName);
174 th.stopRunning();
175 subEntry.State = "Test did sleep for " + (nThreadTimeOut / 1000) + " seconds and has been killed!";
176 subEntry.hasErrorMsg = true;
177 subEntry.ErrorMsg = subEntry.State;
178 continue;
180 else
182 log.println("Finished " + mTestMethodName);
183 if (th.hasErrorMessage())
185 subEntry.State = th.getErrorMessage();
186 subEntry.hasErrorMsg = true;
187 subEntry.ErrorMsg = subEntry.State;
188 continue;
192 catch (java.lang.Exception e)
194 log.println(e.getClass().getName());
195 String msg = e.getMessage();
196 log.println("Message: " + msg);
197 e.printStackTrace((PrintWriter) log);
198 subEntry.State = "SKIPPED.FAILED";
199 subEntry.hasErrorMsg = true;
200 subEntry.ErrorMsg = (msg == null ? "" : msg);
201 continue;
203 subEntry.State = (bSuccessful ? "COMPLETED.OK" : message);
204 subEntry.hasErrorMsg = !bSuccessful;
205 subEntry.ErrorMsg = message;
210 * after() is called after the test is done
212 private void after()
214 if (m_bBeforeCalled)
216 // the after() method
219 Method after = this.getClass().getMethod("after", new Class[] {});
220 after.invoke(this, new Object[] {} );
222 catch (java.lang.NoSuchMethodException e)
224 // simply ignore
226 catch (java.lang.IllegalAccessException e)
228 // simply ignore
230 catch (java.lang.reflect.InvocationTargetException e)
232 Throwable t = e.getTargetException();
233 if (!(t instanceof StatusException))
235 log.println(t.toString());
236 if (message == null)
238 message = "Exception in after() method.\n\r" + t.getMessage();
240 else
242 message += "Exception in \'after()\' method.\n\r" + t.getMessage();
244 log.println("Message: " + message);
245 t.printStackTrace((PrintWriter) log);
255 * Call test. It is expected, that an environment is
256 * given to this test.
258 * @param entry The name of the test method that should be called.
259 * @param environment The environment for the test.
261 public void executeMethods(DescEntry entry, TestParameters environment)
263 m_bBeforeCalled = false;
265 // get the environment
266 param = environment;
267 log = entry.Logger;
270 // start with the before() method
271 before();
273 //executeMethodTests
274 test_method(entry);
276 // cleanup
277 after();
282 * Implement this method in the Complex test.
283 * @return All test method names.
285 public abstract String[] getTestMethodNames();
288 * Return a name for the test or tested object.
289 * Override to give an own name.
290 * @return As default, the name of this class.
292 public String getTestObjectName()
294 return this.getClass().getName();