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 
24 package org.openoffice;
25 
26 import share.LogWriter;
27 import stats.InternalLogWriter;
28 import lib.TestParameters;
29 import util.DynamicClassLoader;
30 import base.TestBase;
31 import helper.ClParser;
32 import helper.CfgParser;
33 import com.sun.star.beans.XPropertyAccess;
34 import com.sun.star.beans.PropertyValue;
35 import com.sun.star.task.XJob;
36 import com.sun.star.uno.XInterface;
37 import com.sun.star.comp.loader.FactoryHelper;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.lang.XServiceInfo;
40 import com.sun.star.lang.XSingleServiceFactory;
41 import com.sun.star.lang.XTypeProvider;
42 import com.sun.star.uno.Type;
43 import com.sun.star.registry.XRegistryKey;
44 import com.sun.star.beans.NamedValue;
45 
46 import java.util.Vector;
47 
48 /**
49  * The main class, will call ClParser and CfgParser to <br>
50  * fill the TestParameters.<br>
51  * Will then call the appropriate Testbase to run the tests.
52  */
53 public class RunnerService implements XJob, XServiceInfo,
54                                             XTypeProvider, XPropertyAccess {
55 
56     static public final String __serviceName = "org.openoffice.Runner";
57     static public final String __implName = "org.openoffice.RunnerService";
58     static private XMultiServiceFactory xMSF = null;
59 
60     /**
61      * ct'tor
62      * Construct an own office provider for tests
63      */
RunnerService(XMultiServiceFactory xMSF)64     public RunnerService(XMultiServiceFactory xMSF) {
65     }
66 
execute(NamedValue[] args)67     public Object execute(NamedValue[] args) {
68         // construct valid arguments from the given stuff
69         int arg_length=args.length;
70         String[] arguments = new String[arg_length*2];
71         for ( int i=0; i< arg_length; i++ ) {
72             arguments[i*2] = args[i].Name;
73             Object o = args[i].Value;
74             arguments[i*2+1] = o.toString();
75         }
76 
77         TestParameters param = new TestParameters();
78         DynamicClassLoader dcl = new DynamicClassLoader();
79 
80 
81         // take the standard log writer
82         String standardLogWriter = param.LogWriter;
83         String standardOutProducer = param.OutProducer;
84 
85         ClParser cli = new ClParser();
86 
87         //parse the arguments if an ini-parameter is given
88         String iniFile = cli.getIniPath(arguments);
89 
90         //initialize cfgParser with ini-path
91         CfgParser ini = new CfgParser(iniFile);
92 
93         //parse ConfigFile
94         ini.getIniParameters(param);
95 
96 
97         //parse the commandline arguments if an runnerprops-parameter is given
98         String runnerIniFile = cli.getRunnerIniPath(arguments);
99 
100         //initialize cfgParser with ini-path
101         CfgParser runnerIni = new CfgParser(runnerIniFile);
102 
103         //parse ConfigFile
104         runnerIni.getIniParameters(param);
105 
106         //parse the commandline arguments
107         cli.getCommandLineParameter(param,arguments);
108 
109         // now compare the standard log writer with the parameters:
110         // if we have a new one, use the new, else use the internal
111         // log writer
112         if (((String)param.get("LogWriter")).equals(standardLogWriter))
113             param.put("LogWriter", "stats.InternalLogWriter");
114         if (((String)param.get("OutProducer")).equals(standardOutProducer))
115             param.put("OutProducer", "stats.InternalLogWriter");
116         LogWriter log = (LogWriter) dcl.getInstance(
117                                             (String)param.get("LogWriter"));
118 
119         param.put("ServiceFactory", xMSF);
120 
121         param.ServiceFactory = xMSF; //(XMultiServiceFactory)
122                                      //       appProvider.getManager(param);
123 
124         log.println("TestJob: "+param.get("TestJob"));
125 
126         TestBase toExecute = (TestBase)dcl.getInstance("base.java_fat_service");
127 
128         boolean worked = toExecute.executeTest(param);
129         if (!worked)
130             log.println("Test did not execute correctly.");
131 
132         String returnString = "";
133         if (log instanceof InternalLogWriter)
134             returnString = ((InternalLogWriter)log).getLog();
135         return returnString;
136     }
137 
138     /**
139      * This function provides the service name
140      * @return the service name
141      */
getServiceName()142     public String getServiceName() {
143         return __serviceName;
144     }
145 
146     /**
147      * Get all implemented types of this class.
148      * @return An array of implemented interface types.
149      * @see com.sun.star.lang.XTypeProvider
150      */
getTypes()151     public Type[] getTypes() {
152         Type[] type = new Type[5];
153         type[0] = new Type(XInterface.class);
154         type[1] = new Type(XTypeProvider.class);
155         type[2] = new Type(XJob.class);
156         type[3] = new Type(XServiceInfo.class);
157         type[4] = new Type(XPropertyAccess.class);
158         return type;
159     }
160 
161     /**
162      * Get the implementation id.
163      * @return An empty implementation id.
164      * @see com.sun.star.lang.XTypeProvider
165      */
getImplementationId()166     public byte[] getImplementationId() {
167         return new byte[0];
168     }
169     /**
170      * Function for reading the implementation name.
171      *
172      * @return the implementation name
173      * @see com.sun.star.lang.XServiceInfo
174      */
getImplementationName()175     public String getImplementationName() {
176         return __implName;
177     }
178 
179     /**
180      * Does the implementation support this service?
181      *
182      * @param serviceName The name of the service in question
183      * @return true, if service is supported, false otherwise
184      * @see com.sun.star.lang.XServiceInfo
185      */
supportsService(String serviceName)186     public boolean supportsService(String serviceName) {
187         if(serviceName.equals(__serviceName))
188             return true;
189         return false;
190     }
191 
192     /**
193      * Function for reading all supported services
194      *
195      * @return An aaray with all supported service names
196      * @see com.sun.star.lang.XServiceInfo
197      */
getSupportedServiceNames()198     public String[] getSupportedServiceNames() {
199         String[] supServiceNames = {__serviceName};
200         return supServiceNames;
201     }
202 
203     /**
204      * Return all valid testcases from the object descriptions
205      * @return The valid testcases as property values
206      */
getPropertyValues()207     public PropertyValue[] getPropertyValues() {
208         PropertyValue[] pVal = null;
209         java.net.URL url = this.getClass().getResource("/objdsc");
210         if (url == null) {
211            pVal = new PropertyValue[1];
212            pVal[0] = new PropertyValue();
213            pVal[0].Name = "Error";
214            pVal[0].Value = "OOoRunner.jar file doesn't contain object " +
215                            "descriptions: don't know what to test.";
216            return pVal;
217         }
218 
219         Vector v = new Vector(600);
220         try {
221             // open connection to  Jar
222             java.net.JarURLConnection con =
223                                 (java.net.JarURLConnection)url.openConnection();
224             // get Jar file from connection
225             java.util.jar.JarFile f = con.getJarFile();
226             // Enumerate over all entries
227             java.util.Enumeration aEnum = f.entries();
228 
229             while (aEnum.hasMoreElements()) {
230                 String entry = aEnum.nextElement().toString();
231                 if (entry.endsWith(".csv")) {
232 
233                     String module = null;
234                     String object = null;
235 
236                     int startIndex = entry.indexOf("objdsc/") + 7;
237                     int endIndex = entry.lastIndexOf('/');
238 /*                    int endIndex = entry.indexOf('.');
239                     module = entry.substring(startIndex, endIndex);
240                     startIndex = 0;
241                     endIndex = module.lastIndexOf('/'); */
242                     module = entry.substring(startIndex, endIndex);
243 
244                     // special cases
245                     if (entry.indexOf("/file/") != -1 || entry.indexOf("/xmloff/") != -1) {
246                         endIndex = entry.indexOf(".csv");
247                         object = entry.substring(0, endIndex);
248                         endIndex = object.lastIndexOf('.');
249                         startIndex = object.indexOf('.');
250                         while (startIndex != endIndex) {
251                             object = object.substring(startIndex+1);
252                             startIndex = object.indexOf('.');
253                             endIndex = object.lastIndexOf('.');
254                         }
255                     }
256 /*                    else if (entry.indexOf("/xmloff/") != -1) {
257                         endIndex = entry.indexOf(".csv");
258                         object = entry.substring(0, endIndex);
259                         endIndex = entry.lastIndexOf('.');
260                         while (object.indexOf('.') != endIndex) {
261                             object = object.substring(object.indexOf('.')+1);
262                         }
263                     } */
264                     else {
265                         startIndex = 0;
266                         endIndex = entry.indexOf(".csv");
267                         object = entry.substring(startIndex, endIndex);
268                         startIndex = object.lastIndexOf('.');
269                         object = object.substring(startIndex+1);
270                     }
271                     v.add(module+"."+object);
272                 }
273             }
274         }
275         catch(java.io.IOException e) {
276            e.printStackTrace();
277         }
278 
279         int size = v.size();
280 
281         String[] sTestCases = new String[size];
282         v.toArray(sTestCases);
283         java.util.Arrays.sort(sTestCases);
284 
285         pVal = new PropertyValue[size];
286         for (int i=0; i<size; i++) {
287             pVal[i] = new PropertyValue();
288             pVal[i].Name = "TestCase"+i;
289             pVal[i].Value = sTestCases[i];
290         }
291         return pVal;
292    }
293 
294 
295    /**
296    *
297    * Gives a factory for creating the service.
298    * This method is called by the <code>JavaLoader</code>
299    * <p>
300    * @return  returns a <code>XSingleServiceFactory</code> for creating the component
301    * @param   implName     the name of the implementation for which a service is desired
302    * @param   multiFactory the service manager to be used if needed
303    * @param   regKey       the registryKey
304    * @see                  com.sun.star.comp.loader.JavaLoader
305    */
__getServiceFactory(String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)306     public static XSingleServiceFactory __getServiceFactory(String implName,
307                     XMultiServiceFactory multiFactory, XRegistryKey regKey)
308     {
309         XSingleServiceFactory xSingleServiceFactory = null;
310 
311         if (implName.equals(RunnerService.class.getName()))
312             xSingleServiceFactory = FactoryHelper.getServiceFactory(
313                 RunnerService.class, __serviceName, multiFactory, regKey);
314         xMSF = multiFactory;
315         return xSingleServiceFactory;
316     }
317 
318   /**
319    * Writes the service information into the given registry key.
320    * This method is called by the <code>JavaLoader</code>
321    * <p>
322    * @return  returns true if the operation succeeded
323    * @param   regKey       the registryKey
324    * @see                  com.sun.star.comp.loader.JavaLoader
325    */
__writeRegistryServiceInfo(XRegistryKey regKey)326     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
327         return FactoryHelper.writeRegistryServiceInfo(RunnerService.class.getName(),
328         __serviceName, regKey);
329     }
330 
331     /**
332      * empty: not needed here.
333      */
setPropertyValues(PropertyValue[] propertyValue)334     public void setPropertyValues(PropertyValue[] propertyValue)
335                         throws com.sun.star.beans.UnknownPropertyException,
336                                com.sun.star.beans.PropertyVetoException,
337                                com.sun.star.lang.IllegalArgumentException,
338                                com.sun.star.lang.WrappedTargetException {
339       // empty implementation
340     }
341 
342 }
343 
344 
345 
346 
347 
348 
349 
350 
351 
352 
353 
354 
355 
356 
357 
358 
359 
360 
361 
362 
363 
364 
365 
366 
367 
368 
369 
370 
371 
372 
373 
374 
375 
376 
377 
378 
379 
380 
381 
382 
383 
384 
385 
386 
387 
388 
389 
390 
391 
392 
393 
394