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