Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / qadevOOo / runner / base / java_fat.java
blobc85103508cbebc9a0744651ba6f8c4c188121ae8
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package base;
20 import helper.APIDescGetter;
21 import helper.AppProvider;
23 import java.io.BufferedReader;
24 import java.io.FileInputStream;
25 import java.io.InputStreamReader;
26 import java.io.PrintWriter;
27 import java.io.UnsupportedEncodingException;
28 import java.util.ArrayList;
29 import lib.MultiMethodTest;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 import lib.TestResult;
34 import share.DescEntry;
35 import share.DescGetter;
36 import share.LogWriter;
37 import stats.OutProducerFactory;
38 import stats.Summarizer;
39 import util.DynamicClassLoader;
41 import com.sun.star.lang.XMultiServiceFactory;
43 /**
45 * this class handles tests written in java and running on a fat Office
47 public class java_fat implements TestBase {
49 private boolean logging = true;
50 private DynamicClassLoader m_aDynamicClassLoader = null;
52 private lib.TestParameters m_aParams;
53 private AppProvider m_aOffice;
55 public boolean executeTest(lib.TestParameters _aParams) throws Exception {
56 m_aParams = _aParams;
58 m_aDynamicClassLoader = new DynamicClassLoader();
60 DescGetter dg = new APIDescGetter();
61 String job = (String) m_aParams.get("TestJob");
62 String ExclusionFile = (String) m_aParams.get("ExclusionList");
63 ArrayList<String> exclusions = null;
64 boolean retValue = true;
65 boolean isDebug = m_aParams.getBool("DebugIsActive");
66 logging = m_aParams.getBool("LoggingIsActive");
67 boolean keepdocument = m_aParams.getBool("KeepDocument");
68 boolean newOffice = m_aParams
69 .getBool(util.PropertyName.NEW_OFFICE_INSTANCE);
70 if (keepdocument) {
71 System.setProperty("KeepDocument", "true");
73 if (ExclusionFile != null) {
74 exclusions = getExclusionList(ExclusionFile, isDebug);
77 String sDescriptionPath = (String) m_aParams.get("DescriptionPath");
78 DescEntry[] entries = dg.getDescriptionFor(job, sDescriptionPath,
79 isDebug);
81 if (entries == null) {
82 System.out.println("Couldn't get Description for Job: " + job);
84 return false;
87 m_aOffice = startOffice(m_aParams);
89 boolean firstRun = true;
91 // Run through all entries (e.g. sw.SwXBookmark.*)
93 for (int l = 0; l < entries.length; l++) {
94 DescEntry entry = entries[l];
96 if (entry == null) {
97 continue;
100 if (entry.hasErrorMsg) {
101 System.out.println(entries[l].ErrorMsg);
102 retValue = false;
103 continue;
106 if (!firstRun && newOffice) {
107 if (!m_aOffice.closeExistingOffice(m_aParams, true)) {
108 m_aOffice.disposeManager(m_aParams);
110 startOffice(m_aParams);
112 firstRun = false;
114 XMultiServiceFactory msf = m_aParams.getMSF();
116 if (msf == null) {
117 retValue = false;
118 continue;
121 // get some helper classes
122 Summarizer sumIt = new Summarizer();
123 TestCase tCase = getTestCase(entry);
124 if (tCase == null) {
125 continue;
128 util.utils.pause(2000);
130 System.out.println("Creating: " + entry.entryName);
132 LogWriter log = (LogWriter) m_aDynamicClassLoader
133 .getInstance((String) m_aParams.get("LogWriter"));
134 log.initialize(entry, logging);
135 entry.UserDefinedParams = m_aParams;
137 tCase.setLogWriter((PrintWriter) log);
138 tCase.initializeTestCase(m_aParams);
140 TestEnvironment tEnv = getTestEnvironment(tCase, entry);
141 if (tEnv == null) {
142 continue;
145 tCase.getObjectName();
147 for (int j = 0; j < entry.SubEntryCount; j++) {
148 DescEntry aSubEntry = entry.SubEntries[j];
149 final boolean bIsToTest = aSubEntry.isToTest;
150 if (!bIsToTest) {
151 Summarizer.summarizeDown(aSubEntry, "not part of the job");
152 continue;
155 final String sLongEntryName = aSubEntry.longName;
157 if ((exclusions != null)
158 && (exclusions.contains(sLongEntryName))) {
159 Summarizer.summarizeDown(aSubEntry, "known issue");
160 continue;
163 // this will shown in test itself
165 LogWriter ifclog = (LogWriter) m_aDynamicClassLoader
166 .getInstance((String) m_aParams.get("LogWriter"));
168 ifclog.initialize(aSubEntry, logging);
169 aSubEntry.UserDefinedParams = m_aParams;
170 aSubEntry.Logger = ifclog;
172 if (tEnv.isDisposed()) {
173 closeExistingOffice();
174 tEnv = getEnv(entry, m_aParams);
175 tCase = tEnv.getTestCase();
178 lib.TestResult res = null;
180 // run the interface test twice if it failed.
181 int countInterfaceTestRun = 0;
182 boolean finished = false;
183 while (!finished) {
184 try {
185 countInterfaceTestRun++;
186 finished = true;
187 res = executeInterfaceTest(aSubEntry, tEnv, m_aParams);
188 } catch (IllegalArgumentException iae) {
189 System.out.println("Couldn't load class "
190 + aSubEntry.entryName + ":");
191 iae.printStackTrace(System.out);
192 Summarizer.summarizeDown(aSubEntry, iae.getMessage());
193 } catch (java.lang.NoClassDefFoundError iae) {
194 System.out.println("Couldn't load class "
195 + aSubEntry.entryName + ":");
196 iae.printStackTrace(System.out);
197 Summarizer.summarizeDown(aSubEntry, iae.getMessage());
198 } catch (java.lang.RuntimeException e) {
199 closeExistingOffice();
200 tEnv = getEnv(entry, m_aParams);
201 tCase = tEnv.getTestCase();
202 if (countInterfaceTestRun < 2) {
203 finished = false;
204 } else {
205 Summarizer.summarizeDown(aSubEntry, e.toString()
206 + ".FAILED");
210 setState(aSubEntry, res);
212 sumIt.summarizeUp(aSubEntry);
214 LogWriter sumIfc = OutProducerFactory
215 .createOutProducer(m_aParams);
216 aSubEntry.UserDefinedParams = m_aParams;
217 sumIfc.initialize(aSubEntry, logging);
218 sumIfc.summary(aSubEntry);
221 try {
222 if (!keepdocument) {
223 tCase.cleanupTestCase(m_aParams);
225 } catch (Exception e) {
226 System.err.println("couldn't cleanup:" + e.toString());
227 } catch (java.lang.NoClassDefFoundError e) {
228 System.err.println("couldn't cleanup:" + e.toString());
231 sumIt.summarizeUp(entry);
233 LogWriter sumObj = OutProducerFactory.createOutProducer(m_aParams);
235 sumObj.initialize(entry, logging);
236 sumObj.summary(entry);
239 if (entries.length > 0) {
240 System.out.println();
242 int counter = 0;
243 System.out
244 .println("Failures that appeared during scenario execution:");
246 for (int i = 0; i < entries.length; i++) {
247 final String sState = entries[i].State;
248 if (!sState.endsWith("OK")) {
249 System.out.println("\t " + entries[i].longName);
250 counter++;
254 System.out.println(counter + " of " + entries.length
255 + " tests failed");
256 if (counter != 0) {
257 retValue = false;
261 closeExistingOffice();
262 return retValue;
265 private TestEnvironment getTestEnvironment(TestCase tCase, DescEntry entry) {
266 TestEnvironment tEnv = null;
268 try {
269 tEnv = tCase.getTestEnvironment(m_aParams);
270 } catch (Exception e) {
271 System.out.println("Exception while creating "
272 + tCase.getObjectName());
273 System.out.println("Message " + e.getMessage());
274 e.printStackTrace();
275 tEnv = null;
276 } catch (java.lang.UnsatisfiedLinkError e) {
277 System.out.println("Exception while creating "
278 + tCase.getObjectName());
279 System.out.println("Message " + e.getMessage());
280 tEnv = null;
281 } catch (java.lang.NoClassDefFoundError e) {
282 System.out.println("Exception while creating "
283 + tCase.getObjectName());
284 System.out.println("Message " + e.getMessage());
285 tEnv = null;
288 if (tEnv == null) {
289 Summarizer.summarizeDown(entry,
290 "Couldn't create " + tCase.getObjectName());
292 LogWriter sumObj = OutProducerFactory.createOutProducer(m_aParams);
293 entry.UserDefinedParams = m_aParams;
294 sumObj.initialize(entry, logging);
295 sumObj.summary(entry);
297 return tEnv;
300 private TestCase getTestCase(DescEntry _aEntry) {
301 TestCase tCase = null;
303 try {
304 tCase = (TestCase) m_aDynamicClassLoader.getInstance("mod._"
305 + _aEntry.entryName);
306 } catch (java.lang.IllegalArgumentException ie) {
307 _aEntry.ErrorMsg = ie.getMessage();
308 _aEntry.hasErrorMsg = true;
309 } catch (java.lang.NoClassDefFoundError ie) {
310 _aEntry.ErrorMsg = ie.getMessage();
311 _aEntry.hasErrorMsg = true;
314 if (tCase == null) {
315 Summarizer.summarizeDown(_aEntry, _aEntry.ErrorMsg);
317 LogWriter sumObj = OutProducerFactory.createOutProducer(m_aParams);
318 _aEntry.UserDefinedParams = m_aParams;
319 sumObj.initialize(_aEntry, logging);
320 sumObj.summary(_aEntry);
322 return tCase;
325 private void setState(DescEntry aSubEntry, lib.TestResult res) {
326 if (res != null) {
327 for (int k = 0; k < aSubEntry.SubEntryCount; k++) {
328 DescEntry aSubSubEntry = aSubEntry.SubEntries[k];
329 if (res.hasMethod(aSubSubEntry.entryName)) {
330 aSubSubEntry.State = res.getStatusFor(
331 aSubSubEntry.entryName).toString();
337 private TestEnvironment getEnv(DescEntry entry, TestParameters param) throws Exception {
338 TestCase tCase = null;
340 try {
341 tCase = (TestCase) m_aDynamicClassLoader.getInstance("mod._"
342 + entry.entryName);
343 } catch (java.lang.IllegalArgumentException ie) {
344 entry.ErrorMsg = ie.getMessage();
345 entry.hasErrorMsg = true;
346 } catch (java.lang.NoClassDefFoundError ie) {
347 entry.ErrorMsg = ie.getMessage();
348 entry.hasErrorMsg = true;
351 System.out.println("Creating: " + entry.entryName);
353 entry.UserDefinedParams = param;
355 LogWriter log = (LogWriter) m_aDynamicClassLoader
356 .getInstance((String) param.get("LogWriter"));
357 log.initialize(entry, logging);
358 tCase.setLogWriter((PrintWriter) log);
360 try {
361 tCase.initializeTestCase(param);
362 TestEnvironment tEnv = tCase.getTestEnvironment(param);
363 if (tEnv == null) {
364 throw new Exception("Could not get env for '" + entry.entryName + "'");
366 return tEnv;
367 } catch (com.sun.star.lang.DisposedException de) {
368 System.out.println("Office disposed");
369 closeExistingOffice();
370 throw de;
371 } catch (Exception e) {
372 System.out.println(e.getMessage());
374 closeExistingOffice();
376 entry.ErrorMsg = e.getMessage();
377 entry.hasErrorMsg = true;
378 throw e;
382 private void closeExistingOffice() {
383 helper.ProcessHandler ph = (helper.ProcessHandler) m_aParams
384 .get("AppProvider");
386 if (ph != null) {
387 m_aOffice.closeExistingOffice(m_aParams, true);
388 util.utils.pause(5000);
393 private ArrayList<String> getExclusionList(String url, boolean debug) {
394 ArrayList<String> entryList = new ArrayList<String>();
395 String line = "#";
396 BufferedReader exclusion = null;
398 try {
399 InputStreamReader reader = new InputStreamReader(new FileInputStream(url), "UTF-8");
400 exclusion = new BufferedReader(reader);
401 } catch (java.io.UnsupportedEncodingException uee) {
402 if (debug) {
403 System.out.println("Couldn't decode file " + url);
406 return entryList;
407 } catch (java.io.FileNotFoundException fnfe) {
408 if (debug) {
409 System.out.println("Couldn't find file " + url);
412 return entryList;
415 try {
416 while (line != null) {
417 try {
418 if (!line.startsWith("#") && (line.length() > 1)) {
419 entryList.add(line.trim());
422 line = exclusion.readLine();
423 } catch (java.io.IOException ioe) {
424 if (debug) {
425 System.out
426 .println("Exception while reading exclusion list");
429 return entryList;
432 } finally {
433 try {
434 exclusion.close();
435 } catch (java.io.IOException ioe) {
439 return entryList;
442 private TestResult executeInterfaceTest(DescEntry entry,
443 TestEnvironment tEnv, TestParameters param)
444 throws IllegalArgumentException, java.lang.NoClassDefFoundError {
445 MultiMethodTest ifc = (MultiMethodTest) m_aDynamicClassLoader
446 .getInstance(entry.entryName);
447 return ifc.run(entry, tEnv, param);
450 private AppProvider startOffice(lib.TestParameters param) throws UnsupportedEncodingException {
451 if (m_aDynamicClassLoader == null) {
452 m_aDynamicClassLoader = new DynamicClassLoader();
455 String officeProviderName = (String) param.get("OfficeProvider");
456 AppProvider office = (AppProvider) m_aDynamicClassLoader
457 .getInstance(officeProviderName);
459 if (office == null) {
460 System.out.println("ERROR: Wrong parameter 'OfficeProvider', "
461 + " it cannot be instantiated.");
462 System.exit(-1);
465 XMultiServiceFactory msf = (XMultiServiceFactory) office
466 .getManager(param);
468 if (msf != null) {
469 param.put("ServiceFactory", msf);
472 return office;