1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package org.openoffice; 28 29 import java.util.Enumeration; 30 import java.util.Properties; 31 import java.util.StringTokenizer; 32 import lib.TestParameters; 33 import util.DynamicClassLoader; 34 import base.TestBase; 35 import helper.ClParser; 36 import helper.CfgParser; 37 38 /** 39 * The main class, will call ClParser and CfgParser to <br> 40 * fill the TestParameters.<br> 41 * Will then call the appropriate Testbase to run the tests. 42 */ 43 public class Runner 44 { 45 46 private static long m_nStartTime; 47 48 public static long getRunnerStartTime() 49 { 50 return m_nStartTime; 51 } 52 /* 53 simple helper functions to start/stop a timer, to know how long a process need in milliseconds 54 */ 55 56 private static long getTime() 57 { 58 return System.currentTimeMillis(); 59 } 60 61 private static void setStartTime(long _nStartTime) 62 { 63 m_nStartTime = _nStartTime; 64 } 65 66 /* 67 return the time, which is done until last startTime() 68 */ 69 public static long meanTime(long _nCurrentTimer) 70 { 71 if (_nCurrentTimer == 0) 72 { 73 System.out.println("Forgotten to initialise a start timer?"); 74 return 0; 75 } 76 long nMeanTime = getTime(); 77 return nMeanTime - _nCurrentTimer; 78 } 79 80 private static String beautifyTime(long _nTime) 81 { 82 long sec = (_nTime / 1000) % 60; 83 long min = (_nTime / (60 * 1000)) % 60; 84 long hour = _nTime / (60 * 60 * 1000); 85 StringBuffer aTime = new StringBuffer(); 86 aTime.append(helper.StringHelper.createValueString((int) hour, 2)). 87 append(':'). 88 append(helper.StringHelper.createValueString((int) min, 2)). 89 append(':'). 90 append(helper.StringHelper.createValueString((int) sec, 2)); 91 return aTime.toString(); 92 } 93 94 /** 95 Helper to check if there are problems with Cygwin Path variables. 96 */ 97 private static boolean checkVariableForCygwin(String _sVariable) 98 { 99 if (_sVariable == null) 100 { 101 return false; 102 } 103 if (_sVariable.startsWith("/cygdrive")) 104 { 105 return true; 106 } 107 return false; 108 } 109 110 private static boolean checkPathVariable(String _sPath, String delim) 111 { 112 String sPath = System.getProperty(_sPath); 113 if (sPath != null) 114 { 115 StringTokenizer aTokenEnum = new StringTokenizer(sPath, delim); 116 while (aTokenEnum.hasMoreElements()) 117 { 118 String sToken = (String) aTokenEnum.nextElement(); 119 if (checkVariableForCygwin(sToken)) 120 { 121 System.err.println("ERROR: OOoRunner detect cygwin path in '" + _sPath + "'"); 122 return true; 123 } 124 } 125 } 126 return false; 127 } 128 129 private static void checkAllVariablesForCygwinPath(TestParameters _aParams) 130 { 131 // ----- check all System.getProperty(key) variables ----- 132 String sOsName = System.getProperty("os.name"); 133 if (!sOsName.toLowerCase().startsWith("windows")) 134 { 135 // we need to check only on windows 136 return; 137 } 138 139 Properties aProps = System.getProperties(); 140 Enumeration aEnum = aProps.propertyNames(); 141 // Enumeration aEnum = aProps.elements(); // these are only the values 142 boolean bEmergencyStop = false; 143 144 while (aEnum.hasMoreElements()) 145 { 146 String sKey = (String) aEnum.nextElement(); 147 String sValue = System.getProperty(sKey); 148 149 if (checkVariableForCygwin(sValue)) 150 { 151 System.err.println("ERROR: OOoRunner detect cygwin path in '" + sKey + ":=" + sValue + "'"); 152 bEmergencyStop = true; 153 } 154 } 155 156 // ----- check path variables separatly ----- 157 String sDelim = System.getProperty("path.separator"); 158 bEmergencyStop |= checkPathVariable("java.library.path", sDelim); 159 bEmergencyStop |= checkPathVariable("java.class.path", sDelim); 160 bEmergencyStop |= checkPathVariable("sun.boot.class.path", sDelim); 161 162 // ----- check all TestParameters ----- 163 aEnum = _aParams.keys(); 164 while (aEnum.hasMoreElements()) 165 { 166 String sKey = (String) aEnum.nextElement(); 167 if (_aParams.get(sKey) instanceof String) 168 { 169 String sValue = (String) _aParams.get(sKey); 170 171 if (checkVariableForCygwin(sValue)) 172 { 173 System.err.println("ERROR: OOoRunner detect cygwin path in '" + sKey + ":=" + sValue + "'"); 174 bEmergencyStop = true; 175 } 176 } 177 } 178 179 if (bEmergencyStop) 180 { 181 System.exit(-1); 182 } 183 } 184 185 public static boolean run(String... args) 186 { 187 System.out.println("OOoRunner Main() version from 20101118 (yyyymmdd)"); 188 189 setStartTime(getTime()); 190 191 DynamicClassLoader dcl = new DynamicClassLoader(); 192 193 // get a class for test parameters 194 TestParameters param = new TestParameters(); 195 196 ClParser cli = new ClParser(); 197 198 //parse the commandline arguments if an ini-parameter is given 199 String iniFile = cli.getIniPath(args); 200 201 //initialize cfgParser with ini-path 202 CfgParser ini = new CfgParser(iniFile); 203 204 //parse ConfigFile 205 ini.getIniParameters(param); 206 207 208 //parse the commandline arguments if an runnerprops-parameter is given 209 String runnerIniFile = cli.getRunnerIniPath(args); 210 211 //initialize cfgParser with ini-path 212 CfgParser runnerIni = new CfgParser(runnerIniFile); 213 214 //parse ConfigFile 215 runnerIni.getIniParameters(param); 216 217 //parse the commandline arguments 218 // TODO: no right error message, if no parameter given! 219 cli.getCommandLineParameter(param, args); 220 221 Object tj = param.get("TestJob"); 222 223 if (tj == null) 224 { 225 System.out.println("=========================================================================="); 226 System.out.println("No TestJob given, please make sure that you "); 227 System.out.println("a.) called the OOoRunner with the paramter -o <job> or -sce <scenarioFile>"); 228 System.out.println("or"); 229 System.out.println("b.) have an entry called TestJob in your used properties file"); 230 System.out.println("=========================================================================="); 231 System.exit(-1); 232 } 233 234 System.out.println("TestJob: " + tj); 235 String sName = "base." + (String) param.get("TestBase"); 236 TestBase toExecute = (TestBase) dcl.getInstance(sName); 237 238 checkAllVariablesForCygwinPath(param); 239 240 boolean worked = toExecute.executeTest(param); 241 long nTime = meanTime(getRunnerStartTime()); 242 String sBeautifyTime = beautifyTime(nTime); 243 244 System.out.println("Job run took: " + nTime + "ms " + " [" + sBeautifyTime + "]"); 245 246 if (!worked) 247 { 248 System.out.println("Job " + param.get("TestJob") + " failed"); 249 } 250 else 251 { 252 System.out.println("Job " + param.get("TestJob") + " done"); 253 } 254 return worked; 255 } 256 257 public static void main(String[] args) 258 { 259 System.exit(run(args) ? 0 : -1); 260 } 261 } 262