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