1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package complexlib;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import java.lang.reflect.Method;
26cdf0e10cSrcweir import share.DescEntry;
27cdf0e10cSrcweir import lib.TestParameters;
28cdf0e10cSrcweir import lib.StatusException;
29cdf0e10cSrcweir import share.LogWriter;
30cdf0e10cSrcweir import share.ComplexTest;
31cdf0e10cSrcweir import java.io.PrintWriter;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir /**
34cdf0e10cSrcweir  * Base class for all complex tests.
35cdf0e10cSrcweir  */
36cdf0e10cSrcweir public abstract class ComplexTestCase extends Assurance implements ComplexTest
37cdf0e10cSrcweir {
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     /** The test parameters **/
40cdf0e10cSrcweir     protected static TestParameters param = null;
41cdf0e10cSrcweir     /** Log writer **/
42cdf0e10cSrcweir     protected static LogWriter log = null;
43cdf0e10cSrcweir     /**
44cdf0e10cSrcweir      * The method name which will be written into f.e. the data base
45cdf0e10cSrcweir      **/
46cdf0e10cSrcweir     protected String mTestMethodName = null;
47cdf0e10cSrcweir     /** Maximal time one method is allowed to execute
48cdf0e10cSrcweir      * Can be set with parameter 'ThreadTimeOut'
49cdf0e10cSrcweir      **/
50cdf0e10cSrcweir     protected int m_nThreadTimeOut = 0;
51cdf0e10cSrcweir     /** Continue a test even if it did fail **/
52cdf0e10cSrcweir     // public static final boolean CONTINUE = true;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     /** End a test if it did fail **/
55cdf0e10cSrcweir     public static final boolean BREAK = true;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     private boolean m_bBeforeCalled;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     /**
60cdf0e10cSrcweir      * is called before the real test starts
61cdf0e10cSrcweir      */
before()62cdf0e10cSrcweir     private void before()
63cdf0e10cSrcweir     {
64cdf0e10cSrcweir         try
65cdf0e10cSrcweir         {
66cdf0e10cSrcweir             Method before = this.getClass().getMethod("before", new Class[] {} );
67cdf0e10cSrcweir             before.invoke(this, new Object[] {} );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir             // beforeWorked = false;
70cdf0e10cSrcweir             m_bBeforeCalled = true;
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir         catch (java.lang.NoSuchMethodException e)
73cdf0e10cSrcweir         {
74cdf0e10cSrcweir             // simply ignore
75cdf0e10cSrcweir             int dummy = 0;
76cdf0e10cSrcweir             m_bBeforeCalled = true;
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir         catch (java.lang.IllegalAccessException e)
79cdf0e10cSrcweir         {
80cdf0e10cSrcweir             log.println("Cannot access the 'before()' method, although it" + " is there. Is this ok?");
81cdf0e10cSrcweir         }
82cdf0e10cSrcweir         catch (java.lang.reflect.InvocationTargetException e)
83cdf0e10cSrcweir         {
84cdf0e10cSrcweir             Throwable t = e.getTargetException();
85cdf0e10cSrcweir             if (!(t instanceof RuntimeException) || state)
86cdf0e10cSrcweir             {
87cdf0e10cSrcweir                 log.println(t.toString());
88cdf0e10cSrcweir                 if (message == null)
89cdf0e10cSrcweir                 {
90cdf0e10cSrcweir                     message = "Exception in before() method.\n\r" + t.getMessage();
91cdf0e10cSrcweir                 }
92cdf0e10cSrcweir                 state = false;
93cdf0e10cSrcweir                 t.printStackTrace((PrintWriter) log);
94cdf0e10cSrcweir             }
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     /** Description entry **/
100cdf0e10cSrcweir     // protected DescEntry subEntry = null;
101cdf0e10cSrcweir 
test_method(DescEntry _entry)102cdf0e10cSrcweir     private void test_method(DescEntry _entry)
103cdf0e10cSrcweir     {
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         m_nThreadTimeOut = param.getInt("ThreadTimeOut");
106cdf0e10cSrcweir         if (m_nThreadTimeOut == 0)
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             m_nThreadTimeOut = 300000;
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         for (int i = 0; i < _entry.SubEntries.length; i++)
112cdf0e10cSrcweir         {
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             DescEntry subEntry = _entry.SubEntries[i];
115cdf0e10cSrcweir             if (m_bBeforeCalled)
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 state = true;
118cdf0e10cSrcweir                 message = "";
119cdf0e10cSrcweir             }
120cdf0e10cSrcweir             else
121cdf0e10cSrcweir             {
122cdf0e10cSrcweir                 // set all test methods on failed, if 'before()' did not work.
123cdf0e10cSrcweir                 subEntry.State = message;
124cdf0e10cSrcweir                 subEntry.hasErrorMsg = true;
125cdf0e10cSrcweir                 subEntry.ErrorMsg = message;
126cdf0e10cSrcweir                 continue;
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir             Method testMethod = null;
129cdf0e10cSrcweir             try
130cdf0e10cSrcweir             {
131cdf0e10cSrcweir                 String entryName = subEntry.entryName;
132cdf0e10cSrcweir                 Object[] parameter = null;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir                 if (entryName.indexOf("(") != -1)
135cdf0e10cSrcweir                 {
136cdf0e10cSrcweir                     String sParameter = (entryName.substring(entryName.indexOf("(") + 1, entryName.indexOf(")")));
137cdf0e10cSrcweir                     mTestMethodName = entryName;
138cdf0e10cSrcweir                     parameter = new String[] { sParameter };
139cdf0e10cSrcweir                     entryName = entryName.substring(0, entryName.indexOf("("));
140cdf0e10cSrcweir                     testMethod = this.getClass().getMethod(entryName, new Class[] { String.class });
141cdf0e10cSrcweir                 }
142cdf0e10cSrcweir                 else
143cdf0e10cSrcweir                 {
144cdf0e10cSrcweir                     testMethod = this.getClass().getMethod(entryName, new Class[] {} );
145cdf0e10cSrcweir                     mTestMethodName = entryName;
146cdf0e10cSrcweir                 }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir                 MethodThread th = new MethodThread(testMethod, this, parameter, (java.io.PrintWriter) log);
149cdf0e10cSrcweir                 log.println("Starting " + mTestMethodName);
150cdf0e10cSrcweir                 th.start();
151cdf0e10cSrcweir 
152cdf0e10cSrcweir                 try
153cdf0e10cSrcweir                 {
154cdf0e10cSrcweir                     // some tests are very dynamic in its exceution time so that
155cdf0e10cSrcweir                     // a threadTimeOut fials. In this cases the logging mechanisim
156*bb6af6bcSPedro Giffuni                     // is a useful way to detect that a office respective a test
157cdf0e10cSrcweir                     // is running and not death.
158cdf0e10cSrcweir                     // But way ThreadTimeOut?
159cdf0e10cSrcweir                     // There exeitsts a complex test which uses no office. Therefore
160cdf0e10cSrcweir                     // a logging mechanisim to detect a stalled test.
161cdf0e10cSrcweir                     int lastPing = -1;
162cdf0e10cSrcweir                     int newPing = 0;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir                     int sleepingStep = 1000;
165cdf0e10cSrcweir                     int factor = 0;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir                     while (th.isAlive() && (lastPing != newPing || factor * sleepingStep < m_nThreadTimeOut))
168cdf0e10cSrcweir                     {
169cdf0e10cSrcweir                         Thread.sleep(sleepingStep);
170cdf0e10cSrcweir                         factor++;
171cdf0e10cSrcweir                         // if a test starts the office itself it the watcher is a
172cdf0e10cSrcweir                         // new one.
173cdf0e10cSrcweir                         share.Watcher ow = (share.Watcher) param.get("Watcher");
174cdf0e10cSrcweir                         if (ow != null)
175cdf0e10cSrcweir                         {
176cdf0e10cSrcweir                             lastPing = newPing;
177cdf0e10cSrcweir                             newPing = ow.getPing();
178cdf0e10cSrcweir                             //System.out.println("lastPing: '" + lastPing + "' newPing '" + newPing + "'");
179cdf0e10cSrcweir                             factor = 0;
180cdf0e10cSrcweir                         }
181cdf0e10cSrcweir                     }
182cdf0e10cSrcweir                 }
183cdf0e10cSrcweir                 catch (InterruptedException e)
184cdf0e10cSrcweir                 {
185cdf0e10cSrcweir                 }
186cdf0e10cSrcweir                 if (th.isAlive())
187cdf0e10cSrcweir                 {
188cdf0e10cSrcweir                     log.println("Destroy " + mTestMethodName);
189cdf0e10cSrcweir                     th.destroy();
190cdf0e10cSrcweir                     subEntry.State = "Test did sleep for " + (m_nThreadTimeOut / 1000) + " seconds and has been killed!";
191cdf0e10cSrcweir                     subEntry.hasErrorMsg = true;
192cdf0e10cSrcweir                     subEntry.ErrorMsg = subEntry.State;
193cdf0e10cSrcweir                     continue;
194cdf0e10cSrcweir                 }
195cdf0e10cSrcweir                 else
196cdf0e10cSrcweir                 {
197cdf0e10cSrcweir                     log.println("Finished " + mTestMethodName);
198cdf0e10cSrcweir                     if (th.hasErrorMessage())
199cdf0e10cSrcweir                     {
200cdf0e10cSrcweir                         subEntry.State = th.getErrorMessage();
201cdf0e10cSrcweir                         subEntry.hasErrorMsg = true;
202cdf0e10cSrcweir                         subEntry.ErrorMsg = subEntry.State;
203cdf0e10cSrcweir                         continue;
204cdf0e10cSrcweir                     }
205cdf0e10cSrcweir                 }
206cdf0e10cSrcweir             }
207cdf0e10cSrcweir             catch (java.lang.Exception e)
208cdf0e10cSrcweir             {
209cdf0e10cSrcweir                 log.println(e.getClass().getName());
210cdf0e10cSrcweir                 String msg = e.getMessage();
211cdf0e10cSrcweir                 log.println("Message: " + msg);
212cdf0e10cSrcweir                 e.printStackTrace((PrintWriter) log);
213cdf0e10cSrcweir                 subEntry.State = "SKIPPED.FAILED";
214cdf0e10cSrcweir                 subEntry.hasErrorMsg = true;
215cdf0e10cSrcweir                 subEntry.ErrorMsg = (msg == null ? "" : msg);
216cdf0e10cSrcweir                 continue;
217cdf0e10cSrcweir             }
218cdf0e10cSrcweir             subEntry.State = (state ? "PASSED.OK" : message);
219cdf0e10cSrcweir             subEntry.hasErrorMsg = !state;
220cdf0e10cSrcweir             subEntry.ErrorMsg = message;
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir     }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir     /**
225cdf0e10cSrcweir      * after() is called after the test is done
226cdf0e10cSrcweir      */
after()227cdf0e10cSrcweir     private void after()
228cdf0e10cSrcweir     {
229cdf0e10cSrcweir         if (m_bBeforeCalled)
230cdf0e10cSrcweir         {
231cdf0e10cSrcweir             // the after() method
232cdf0e10cSrcweir             try
233cdf0e10cSrcweir             {
234cdf0e10cSrcweir                 Method after = this.getClass().getMethod("after", new Class[] {});
235cdf0e10cSrcweir                 after.invoke(this, new Object[] {} );
236cdf0e10cSrcweir             }
237cdf0e10cSrcweir             catch (java.lang.NoSuchMethodException e)
238cdf0e10cSrcweir             {
239cdf0e10cSrcweir                 // simply ignore
240cdf0e10cSrcweir             }
241cdf0e10cSrcweir             catch (java.lang.IllegalAccessException e)
242cdf0e10cSrcweir             {
243cdf0e10cSrcweir                 // simply ignore
244cdf0e10cSrcweir             }
245cdf0e10cSrcweir             catch (java.lang.reflect.InvocationTargetException e)
246cdf0e10cSrcweir             {
247cdf0e10cSrcweir                 Throwable t = e.getTargetException();
248cdf0e10cSrcweir                 if (!(t instanceof StatusException))
249cdf0e10cSrcweir                 {
250cdf0e10cSrcweir                     log.println(t.toString());
251cdf0e10cSrcweir                     if (message == null)
252cdf0e10cSrcweir                     {
253cdf0e10cSrcweir                         message = "Exception in after() method.\n\r" + t.getMessage();
254cdf0e10cSrcweir                     }
255cdf0e10cSrcweir                     else
256cdf0e10cSrcweir                     {
257cdf0e10cSrcweir                         message += "Exception in \'after()\' method.\n\r" + t.getMessage();
258cdf0e10cSrcweir                     }
259cdf0e10cSrcweir                     log.println("Message: " + message);
260cdf0e10cSrcweir                     t.printStackTrace((PrintWriter) log);
261cdf0e10cSrcweir                 }
262cdf0e10cSrcweir             }
263cdf0e10cSrcweir         }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir     }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     /**
270cdf0e10cSrcweir      * Call test. It is expected, that an environment is
271cdf0e10cSrcweir      * given to this test.
272cdf0e10cSrcweir      *
273cdf0e10cSrcweir      * @param entry The name of the test method that should be called.
274cdf0e10cSrcweir      * @param environment The environment for the test.
275cdf0e10cSrcweir      */
executeMethods(DescEntry entry, TestParameters environment)276cdf0e10cSrcweir     public void executeMethods(DescEntry entry, TestParameters environment)
277cdf0e10cSrcweir     {
278cdf0e10cSrcweir         m_bBeforeCalled = false;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir         // get the environment
281cdf0e10cSrcweir         param = environment;
282cdf0e10cSrcweir         log = entry.Logger;
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 
285cdf0e10cSrcweir         // start with the before() method
286cdf0e10cSrcweir         before();
287cdf0e10cSrcweir 
288cdf0e10cSrcweir         //executeMethodTests
289cdf0e10cSrcweir         test_method(entry);
290cdf0e10cSrcweir 
291cdf0e10cSrcweir         // cleanup
292cdf0e10cSrcweir         after();
293cdf0e10cSrcweir     }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 
296cdf0e10cSrcweir     /**
297cdf0e10cSrcweir      * Implement this method in the Complex test.
298cdf0e10cSrcweir      * @return All test method names.
299cdf0e10cSrcweir      */
getTestMethodNames()300cdf0e10cSrcweir     public abstract String[] getTestMethodNames();
301cdf0e10cSrcweir 
302cdf0e10cSrcweir     /**
303cdf0e10cSrcweir      * Return a name for the test or tested object.
304cdf0e10cSrcweir      * Override to give an own name.
305cdf0e10cSrcweir      * @return As default, the name of this class.
306cdf0e10cSrcweir      */
getTestObjectName()307cdf0e10cSrcweir     public String getTestObjectName()
308cdf0e10cSrcweir     {
309cdf0e10cSrcweir         return this.getClass().getName();
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir }
312