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 .
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
;
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;
37 protected LogWriter log
= null;
39 * The method name which will be written into f.e. the data base
41 private String mTestMethodName
= null;
43 private boolean m_bBeforeCalled
;
46 * is called before the real test starts
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());
73 message
= "Exception in before() method.\n\r" + t
.getMessage();
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'
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
];
107 // set all test methods on failed, if 'before()' did not work.
108 subEntry
.State
= message
;
109 subEntry
.hasErrorMsg
= true;
110 subEntry
.ErrorMsg
= message
;
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 });
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
);
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.
150 int sleepingStep
= 1000;
153 while (th
.isAlive() && (lastPing
!= newPing
|| factor
* sleepingStep
< nThreadTimeOut
))
155 Thread
.sleep(sleepingStep
);
157 // if a test starts the office itself it the watcher is a
159 share
.Watcher ow
= (share
.Watcher
) param
.get("Watcher");
163 newPing
= ow
.getPing();
168 catch (InterruptedException e
)
173 log
.println("Destroy " + mTestMethodName
);
175 subEntry
.State
= "Test did sleep for " + (nThreadTimeOut
/ 1000) + " seconds and has been killed!";
176 subEntry
.hasErrorMsg
= true;
177 subEntry
.ErrorMsg
= subEntry
.State
;
182 log
.println("Finished " + mTestMethodName
);
183 if (th
.hasErrorMessage())
185 subEntry
.State
= th
.getErrorMessage();
186 subEntry
.hasErrorMsg
= true;
187 subEntry
.ErrorMsg
= subEntry
.State
;
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
);
203 subEntry
.State
= (bSuccessful ?
"COMPLETED.OK" : message
);
204 subEntry
.hasErrorMsg
= !bSuccessful
;
205 subEntry
.ErrorMsg
= message
;
210 * after() is called after the test is done
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
)
226 catch (java
.lang
.IllegalAccessException e
)
230 catch (java
.lang
.reflect
.InvocationTargetException e
)
232 Throwable t
= e
.getTargetException();
233 if (!(t
instanceof StatusException
))
235 log
.println(t
.toString());
238 message
= "Exception in after() method.\n\r" + t
.getMessage();
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
270 // start with the before() method
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();