Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / qadevOOo / runner / base / java_fat.java
blob2fa1d05c9722285ef12d9b83b3c3f5e0504254ef
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.util.ArrayList;
28 import lib.MultiMethodTest;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import lib.TestResult;
33 import share.DescEntry;
34 import share.DescGetter;
35 import share.LogWriter;
36 import stats.OutProducerFactory;
37 import stats.Summarizer;
38 import util.DynamicClassLoader;
40 import com.sun.star.lang.XMultiServiceFactory;
42 /**
44 * this class handles tests written in java and running on a fat Office
46 public class java_fat implements TestBase {
48 private boolean logging = true;
49 private DynamicClassLoader m_aDynamicClassLoader = null;
51 private lib.TestParameters m_aParams;
52 private AppProvider m_aOffice;
54 public boolean executeTest(lib.TestParameters _aParams) throws Exception {
55 m_aParams = _aParams;
57 m_aDynamicClassLoader = new DynamicClassLoader();
59 DescGetter dg = new APIDescGetter();
60 String job = (String) m_aParams.get("TestJob");
61 String ExclusionFile = (String) m_aParams.get("ExclusionList");
62 ArrayList<String> exclusions = null;
63 boolean retValue = true;
64 boolean isDebug = m_aParams.getBool("DebugIsActive");
65 logging = m_aParams.getBool("LoggingIsActive");
66 boolean keepdocument = m_aParams.getBool("KeepDocument");
67 boolean newOffice = m_aParams
68 .getBool(util.PropertyName.NEW_OFFICE_INSTANCE);
69 if (keepdocument) {
70 System.setProperty("KeepDocument", "true");
72 if (ExclusionFile != null) {
73 exclusions = getExclusionList(ExclusionFile, isDebug);
76 String sDescriptionPath = (String) m_aParams.get("DescriptionPath");
77 DescEntry[] entries = dg.getDescriptionFor(job, sDescriptionPath,
78 isDebug);
80 if (entries == null) {
81 System.out.println("Couldn't get Description for Job: " + job);
83 return false;
86 m_aOffice = startOffice(m_aParams);
88 boolean firstRun = true;
90 // Run through all entries (e.g. sw.SwXBookmark.*)
92 for (int l = 0; l < entries.length; l++) {
93 DescEntry entry = entries[l];
95 if (entry == null) {
96 continue;
99 if (entry.hasErrorMsg) {
100 System.out.println(entries[l].ErrorMsg);
101 retValue = false;
102 continue;
105 if (!firstRun && newOffice) {
106 if (!m_aOffice.closeExistingOffice(m_aParams, true)) {
107 m_aOffice.disposeManager(m_aParams);
109 startOffice(m_aParams);
111 firstRun = false;
113 XMultiServiceFactory msf = m_aParams.getMSF();
115 if (msf == null) {
116 retValue = false;
117 continue;
120 // get some helper classes
121 Summarizer sumIt = new Summarizer();
122 TestCase tCase = getTestCase(entry);
123 if (tCase == null) {
124 continue;
127 util.utils.pause(2000);
129 System.out.println("Creating: " + entry.entryName);
131 LogWriter log = (LogWriter) m_aDynamicClassLoader
132 .getInstance((String) m_aParams.get("LogWriter"));
133 log.initialize(entry, logging);
134 entry.UserDefinedParams = m_aParams;
136 tCase.setLogWriter((PrintWriter) log);
137 tCase.initializeTestCase(m_aParams);
139 TestEnvironment tEnv = getTestEnvironment(tCase, entry);
140 if (tEnv == null) {
141 continue;
144 tCase.getObjectName();
146 for (int j = 0; j < entry.SubEntryCount; j++) {
147 DescEntry aSubEntry = entry.SubEntries[j];
148 final boolean bIsToTest = aSubEntry.isToTest;
149 if (!bIsToTest) {
150 Summarizer.summarizeDown(aSubEntry, "not part of the job");
151 continue;
154 final String sLongEntryName = aSubEntry.longName;
156 if ((exclusions != null)
157 && (exclusions.contains(sLongEntryName))) {
158 Summarizer.summarizeDown(aSubEntry, "known issue");
159 continue;
162 // this will shown in test itself
164 LogWriter ifclog = (LogWriter) m_aDynamicClassLoader
165 .getInstance((String) m_aParams.get("LogWriter"));
167 ifclog.initialize(aSubEntry, logging);
168 aSubEntry.UserDefinedParams = m_aParams;
169 aSubEntry.Logger = ifclog;
171 if (tEnv.isDisposed()) {
172 closeExistingOffice();
173 tEnv = getEnv(entry, m_aParams);
174 tCase = tEnv.getTestCase();
177 lib.TestResult res = null;
179 // run the interface test twice if it failed.
180 int countInterfaceTestRun = 0;
181 boolean finished = false;
182 while (!finished) {
183 try {
184 countInterfaceTestRun++;
185 finished = true;
186 res = executeInterfaceTest(aSubEntry, tEnv, m_aParams);
187 } catch (IllegalArgumentException iae) {
188 System.out.println("Couldn't load class "
189 + aSubEntry.entryName);
190 System.out
191 .println("**** " + iae.getMessage() + " ****");
192 Summarizer.summarizeDown(aSubEntry, iae.getMessage());
193 } catch (java.lang.NoClassDefFoundError iae) {
194 System.out.println("Couldn't load class "
195 + aSubEntry.entryName);
196 System.out
197 .println("**** " + iae.getMessage() + " ****");
198 Summarizer.summarizeDown(aSubEntry, iae.getMessage());
199 } catch (java.lang.RuntimeException e) {
200 closeExistingOffice();
201 tEnv = getEnv(entry, m_aParams);
202 tCase = tEnv.getTestCase();
203 if (countInterfaceTestRun < 2) {
204 finished = false;
205 } else {
206 Summarizer.summarizeDown(aSubEntry, e.toString()
207 + ".FAILED");
211 setState(aSubEntry, res);
213 sumIt.summarizeUp(aSubEntry);
215 LogWriter sumIfc = OutProducerFactory
216 .createOutProducer(m_aParams);
217 aSubEntry.UserDefinedParams = m_aParams;
218 sumIfc.initialize(aSubEntry, logging);
219 sumIfc.summary(aSubEntry);
222 try {
223 if (!keepdocument) {
224 tCase.cleanupTestCase(m_aParams);
226 } catch (Exception e) {
227 System.err.println("couldn't cleanup:" + e.toString());
228 } catch (java.lang.NoClassDefFoundError e) {
229 System.err.println("couldn't cleanup:" + e.toString());
232 sumIt.summarizeUp(entry);
234 LogWriter sumObj = OutProducerFactory.createOutProducer(m_aParams);
236 sumObj.initialize(entry, logging);
237 sumObj.summary(entry);
240 if (entries.length > 0) {
241 System.out.println();
243 int counter = 0;
244 System.out
245 .println("Failures that appeared during scenario execution:");
247 for (int i = 0; i < entries.length; i++) {
248 final String sState = entries[i].State;
249 if (!sState.endsWith("OK")) {
250 System.out.println("\t " + entries[i].longName);
251 counter++;
255 System.out.println(counter + " of " + entries.length
256 + " tests failed");
257 if (counter != 0) {
258 retValue = false;
262 closeExistingOffice();
263 return retValue;
266 private TestEnvironment getTestEnvironment(TestCase tCase, DescEntry entry) {
267 TestEnvironment tEnv = null;
269 try {
270 tEnv = tCase.getTestEnvironment(m_aParams);
271 } catch (Exception e) {
272 System.out.println("Exception while creating "
273 + tCase.getObjectName());
274 System.out.println("Message " + e.getMessage());
275 e.printStackTrace();
276 tEnv = null;
277 } catch (java.lang.UnsatisfiedLinkError e) {
278 System.out.println("Exception while creating "
279 + tCase.getObjectName());
280 System.out.println("Message " + e.getMessage());
281 tEnv = null;
282 } catch (java.lang.NoClassDefFoundError e) {
283 System.out.println("Exception while creating "
284 + tCase.getObjectName());
285 System.out.println("Message " + e.getMessage());
286 tEnv = null;
289 if (tEnv == null) {
290 Summarizer.summarizeDown(entry,
291 "Couldn't create " + tCase.getObjectName());
293 LogWriter sumObj = OutProducerFactory.createOutProducer(m_aParams);
294 entry.UserDefinedParams = m_aParams;
295 sumObj.initialize(entry, logging);
296 sumObj.summary(entry);
298 return tEnv;
301 private TestCase getTestCase(DescEntry _aEntry) {
302 TestCase tCase = null;
304 try {
305 tCase = (TestCase) m_aDynamicClassLoader.getInstance("mod._"
306 + _aEntry.entryName);
307 } catch (java.lang.IllegalArgumentException ie) {
308 _aEntry.ErrorMsg = ie.getMessage();
309 _aEntry.hasErrorMsg = true;
310 } catch (java.lang.NoClassDefFoundError ie) {
311 _aEntry.ErrorMsg = ie.getMessage();
312 _aEntry.hasErrorMsg = true;
315 if (tCase == null) {
316 Summarizer.summarizeDown(_aEntry, _aEntry.ErrorMsg);
318 LogWriter sumObj = OutProducerFactory.createOutProducer(m_aParams);
319 _aEntry.UserDefinedParams = m_aParams;
320 sumObj.initialize(_aEntry, logging);
321 sumObj.summary(_aEntry);
323 return tCase;
326 private void setState(DescEntry aSubEntry, lib.TestResult res) {
327 if (res != null) {
328 for (int k = 0; k < aSubEntry.SubEntryCount; k++) {
329 DescEntry aSubSubEntry = aSubEntry.SubEntries[k];
330 if (res.hasMethod(aSubSubEntry.entryName)) {
331 aSubSubEntry.State = res.getStatusFor(
332 aSubSubEntry.entryName).toString();
338 private TestEnvironment getEnv(DescEntry entry, TestParameters param) throws Exception {
339 TestCase tCase = null;
341 try {
342 tCase = (TestCase) m_aDynamicClassLoader.getInstance("mod._"
343 + entry.entryName);
344 } catch (java.lang.IllegalArgumentException ie) {
345 entry.ErrorMsg = ie.getMessage();
346 entry.hasErrorMsg = true;
347 } catch (java.lang.NoClassDefFoundError ie) {
348 entry.ErrorMsg = ie.getMessage();
349 entry.hasErrorMsg = true;
352 System.out.println("Creating: " + entry.entryName);
354 entry.UserDefinedParams = param;
356 LogWriter log = (LogWriter) m_aDynamicClassLoader
357 .getInstance((String) param.get("LogWriter"));
358 log.initialize(entry, logging);
359 tCase.setLogWriter((PrintWriter) log);
361 try {
362 tCase.initializeTestCase(param);
363 TestEnvironment tEnv = tCase.getTestEnvironment(param);
364 if (tEnv == null) {
365 throw new Exception("Could not get env for '" + entry.entryName + "'");
367 return tEnv;
368 } catch (com.sun.star.lang.DisposedException de) {
369 System.out.println("Office disposed");
370 closeExistingOffice();
371 throw de;
372 } catch (Exception e) {
373 System.out.println(e.getMessage());
375 closeExistingOffice();
377 entry.ErrorMsg = e.getMessage();
378 entry.hasErrorMsg = true;
379 throw e;
383 private void closeExistingOffice() {
384 helper.ProcessHandler ph = (helper.ProcessHandler) m_aParams
385 .get("AppProvider");
387 if (ph != null) {
388 m_aOffice.closeExistingOffice(m_aParams, true);
389 util.utils.pause(5000);
394 private ArrayList<String> getExclusionList(String url, boolean debug) {
395 ArrayList<String> entryList = new ArrayList<String>();
396 String line = "#";
397 BufferedReader exclusion = null;
399 try {
400 InputStreamReader reader = new InputStreamReader(new FileInputStream(url), "UTF-8");
401 exclusion = new BufferedReader(reader);
402 } catch (java.io.UnsupportedEncodingException uee) {
403 if (debug) {
404 System.out.println("Couldn't decode file " + url);
407 return entryList;
408 } catch (java.io.FileNotFoundException fnfe) {
409 if (debug) {
410 System.out.println("Couldn't find file " + url);
413 return entryList;
416 try {
417 while (line != null) {
418 try {
419 if (!line.startsWith("#") && (line.length() > 1)) {
420 entryList.add(line.trim());
423 line = exclusion.readLine();
424 } catch (java.io.IOException ioe) {
425 if (debug) {
426 System.out
427 .println("Exception while reading exclusion list");
430 return entryList;
433 } finally {
434 try {
435 exclusion.close();
436 } catch (java.io.IOException ioe) {
440 return entryList;
443 private TestResult executeInterfaceTest(DescEntry entry,
444 TestEnvironment tEnv, TestParameters param)
445 throws IllegalArgumentException, java.lang.NoClassDefFoundError {
446 MultiMethodTest ifc = (MultiMethodTest) m_aDynamicClassLoader
447 .getInstance(entry.entryName);
448 return ifc.run(entry, tEnv, param);
451 private AppProvider startOffice(lib.TestParameters param) {
452 if (m_aDynamicClassLoader == null) {
453 m_aDynamicClassLoader = new DynamicClassLoader();
456 String officeProviderName = (String) param.get("OfficeProvider");
457 AppProvider office = (AppProvider) m_aDynamicClassLoader
458 .getInstance(officeProviderName);
460 if (office == null) {
461 System.out.println("ERROR: Wrong parameter 'OfficeProvider', "
462 + " it cannot be instantiated.");
463 System.exit(-1);
466 XMultiServiceFactory msf = (XMultiServiceFactory) office
467 .getManager(param);
469 if (msf != null) {
470 param.put("ServiceFactory", msf);
473 return office;