Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / complex / unoapi / CheckModuleAPI.java
blob40e75d4c7434d2f6fb6de8a7b29443e68a266978
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 .
19 /** complex tests to check the UNO-API
20 **/
21 package complex.unoapi;
23 import base.java_complex;
24 import complexlib.ComplexTestCase;
25 import helper.OfficeProvider;
26 import helper.ParameterNotFoundException;
27 import helper.ProcessHandler;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import helper.BuildEnvTools;
30 import helper.ComplexDescGetter;
31 import helper.CwsDataExchangeImpl;
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.Set;
37 import share.DescEntry;
38 import util.PropertyName;
39 import util.utils;
41 /**
42 * This Complex Test will test the UNO-API by calling dmake in <B>$MODULE</B>/qa/unoapi<p>
43 * This test depends on some requirments:
44 * The <B>TestJob</B> is <b>-o complex.unoapi.CheckModuleAPI::module(<CODE>MODULE</CODE>)</b><p> where <CODE>MODULE</CODE>
45 * could be the following:<p>
46 * <ul>
47 * <li><b>all</b> iterates over <CODE>SRC_ROOT</CODE> and call <CODE>dmake</CODE> in all qa/unoapi folder</li><p>
48 * <li><b>$module</b> call dmake in $module/qa/unoapi folder</li><p>
49 * <li><b>$module1,$module2,...</b> call dmake in $module1/qa/unoapi folder then in $module2/qa/unoapi folder and so on</li><p>
50 * </ul><p>
52 * Also you have to fill the following parameter:
53 * <ul>
54 * <li><b>SHELL</b>: fill this parameter with a shell</li>
55 * </ul>
58 public class CheckModuleAPI extends ComplexTestCase
61 private static String mSRC_ROOT = null;
62 private static boolean mIsInitialized = false;
63 private final static boolean mContinue = true;
64 private static BuildEnvTools bet = null;
66 /**
67 * Initialize the test environment.
68 * This method checks for all neccesarry parameter and exit if not all parameter are set.
70 * Further this method starts an office instance and gets the office some more time to start. This is because
71 * some freshly installed offices don not have such a user tree. The office will create it on its first start,
72 * but this will take some time.
73 * Note: This funktionality is only reasonable with parameter <CODE>-noOffice true</CODE>
75 public void before()
78 if (!mIsInitialized)
80 mIsInitialized = true;
82 try
85 bet = new BuildEnvTools(param, log);
88 catch (ParameterNotFoundException ex)
90 this.failed(ex.toString(), false);
93 mSRC_ROOT = bet.getSrcRoot();
97 /**
98 * let API tests run.
100 public void checkModule(String module)
102 log.println(utils.getDateTime() + ": start testing module '" + module + "'");
104 log.println(utils.getDateTime() + "start new Office instance...");
106 // TODO: is Office started with this program?
107 final OfficeProvider officeProvider = new OfficeProvider();
109 log.println(utils.getDateTime() + "Receiving the ServiceManager of the Office ");
110 final XMultiServiceFactory msf = (XMultiServiceFactory) officeProvider.getManager(param);
111 assure("couldnot get ServiceFarcotry", msf != null, mContinue);
112 param.put("ServiceFactory", msf);
114 final String sep = System.getProperty("file.separator");
115 final String sUnoapi = getModulePath(module);
116 final File fUnoapi = new File(sUnoapi);
117 final String sMakeFile = sUnoapi + sep + "makefile.mk";
118 final File fMakeFile = new File(sMakeFile);
119 assure("ERROR: could not find makefile: '" + sMakeFile + "'", fMakeFile.exists(), mContinue);
121 final String[] commands = getDmakeCommands(sUnoapi);
123 final ProcessHandler procHdl = bet.runCommandsInEnvironmentShell(commands, fUnoapi, 0);
124 log.println("exit code of dmake: " + procHdl.getExitCode());
125 String test = procHdl.getOutputText();
126 test += procHdl.getErrorText();
127 assure("module '" + module + "' failed", verifyOutput(test), mContinue);
128 log.println(utils.getDateTime() + " module '" + module + "': kill existing office...");
130 // TODO: how to check if the office is not started with this process.
131 boolean bNoOffice = param.getBool("NoOffice");
132 if (!bNoOffice)
136 officeProvider.closeExistingOffice(param, true);
138 catch (java.lang.UnsatisfiedLinkError exception)
140 log.println("Office seems not to be running");
144 private String getQaUnoApiPath(String srcRoot, String _sModul)
146 File aFile = new File(srcRoot);
147 if (!aFile.exists())
149 System.out.println("ERROR: srcRoot '" + srcRoot + "' does not exist.");
150 return null;
152 String sModulePath = srcRoot;
153 sModulePath += File.separator;
154 sModulePath += _sModul;
156 File aModulePath = new File(sModulePath);
157 if (! aModulePath.exists())
159 aModulePath = new File(sModulePath + ".lnk");
160 if (! aModulePath.exists())
162 aModulePath = new File(sModulePath + ".link");
163 if (! aModulePath.exists())
165 // module does not exist.
166 return null;
170 sModulePath = aModulePath.getAbsolutePath();
171 sModulePath += File.separator;
172 sModulePath += "qa";
173 sModulePath += File.separator;
174 sModulePath += "unoapi";
175 File aModulePathQaUnoApi = new File(sModulePath);
176 if (aModulePathQaUnoApi.exists())
178 return aModulePathQaUnoApi.getAbsolutePath();
180 return null;
182 private boolean doesQaUnoApiFolderExist(String srcRoot, String _sModul)
184 if (getQaUnoApiPath(srcRoot, _sModul) != null)
186 return true;
188 return false;
191 private boolean doesQaUnoApiFolderExist(File srcRoot)
193 final FolderFilter qaFilter = new FolderFilter("qa");
194 final File[] qaTree = srcRoot.listFiles(qaFilter);
195 if (qaTree != null)
197 for (int j = 0; j < qaTree.length; j++)
199 final File qaFolder = qaTree[j];
200 final FolderFilter apiFilter = new FolderFilter("unoapi");
201 final File[] apiTree = qaFolder.listFiles(apiFilter);
202 if (apiTree != null && apiTree.length > 0)
204 return true;
208 return false;
212 private String[] getAllModuleCommand()
214 String[] checkModules;
216 final String[] names = getModulesFromSourceRoot();
217 checkModules = getCheckModuleCommand(names);
219 return checkModules;
222 private String[] getCheckModuleCommand(String[] names)
224 String[] checkModules;
225 checkModules = new String[names.length];
227 for (int i = 0; i < names.length; i++)
229 // if a module is not added to a cws it contains a dot in its name (forms.lnk)
230 if (names[i].indexOf(".") != -1)
232 checkModules[i] = "checkModule(" + names[i].substring(0, names[i].indexOf(".")) + ")";
234 else
236 checkModules[i] = "checkModule(" + names[i] + ")";
239 return checkModules;
242 private String[] getDmakeCommands(String sUnoapi)
245 String[] cmdLines = null;
246 final String platform = (String) param.get(PropertyName.OPERATING_SYSTEM);
247 log.println("prepare command for platform " + platform);
249 if (platform.equals(PropertyName.WNTMSCI))
251 if (param.getBool(PropertyName.CYGWIN))
253 // cygwin stuff
254 cmdLines = new String[]
256 "cd `cygpath '" + sUnoapi.replaceAll("\\\\", "\\\\\\\\") + "'`",
257 "dmake"
260 else
262 // old 4NT
263 cmdLines = new String[]
265 "cdd " + sUnoapi,
266 "dmake"
270 else
272 // unix
273 cmdLines = new String[]
275 "cd " + sUnoapi,
276 "dmake"
279 return cmdLines;
282 private String[] getCwsModuleCommand()
284 String[] checkModules;
285 final String version = (String) param.get(PropertyName.VERSION);
286 String[] names = null;
287 if (version.startsWith("cws_"))
291 final CwsDataExchangeImpl cde = new CwsDataExchangeImpl(param, log);
292 final ArrayList<String> addedModules = cde.getModules();
294 final ArrayList<String> moduleNames = new ArrayList<String>();
295 Iterator<String> iterator = addedModules.iterator();
296 while (iterator.hasNext())
298 String sModuleName = iterator.next();
299 if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
301 moduleNames.add(sModuleName);
304 names = moduleNames.toArray(new String[moduleNames.size()]);
306 catch (ParameterNotFoundException ex)
308 this.failed(ex.toString(), false);
313 else
315 // major version: all modules must be tested
316 names = getModulesFromSourceRoot();
318 checkModules = getCheckModuleCommand(names);
320 return checkModules;
323 private String[] getDefinedModuleCommand(String module)
325 String[] checkModules = null;
326 // list of modules to test: (sw,sc,sd)
327 if (module.indexOf(",") != -1)
329 final String[] names = module.split(",");
330 checkModules = new String[names.length];
331 for (int i = 0; i < names.length; i++)
333 final String moduleName = names[i].trim();
335 if (doesQaUnoApiFolderExist(mSRC_ROOT, moduleName))
337 checkModules[i] = "checkModule(" + moduleName + ")";
341 else
343 if (doesQaUnoApiFolderExist(mSRC_ROOT, module))
345 checkModules = new String[]
347 "checkModule(" + module + ")"
351 return checkModules;
354 private String getModulePath(String module)
356 String sUnoapi = getQaUnoApiPath(mSRC_ROOT, module);
357 return sUnoapi;
361 Some modules contains more the one project. This methods translates given project parameter to the
362 * correspind module name.
364 * fwk -> framework
365 * fwl -> framework
366 * sch -> chart2
367 * lnn -> lingu
368 * lng -> linguistic
369 * sfx -> sfx2
370 * sm -> starmath
372 private String getTranslatedNames(String module)
375 final HashMap<String,String> aModuleHashMap = new HashMap<String,String>();
377 aModuleHashMap.put("fwk", "framework");
378 aModuleHashMap.put("fwl", "framework");
379 aModuleHashMap.put("sch", "chart2");
380 aModuleHashMap.put("lnn", "lingu");
381 aModuleHashMap.put("lng", "linguistic");
382 aModuleHashMap.put("sfx", "sfx2");
383 aModuleHashMap.put("sm", "starmath");
385 // it could the that the parameter looks like "fwk,fwl". This results in double "famework,framework".
386 // The following loop correct this to only one "framework"
388 final Set<String> keys = aModuleHashMap.keySet();
389 final Iterator<String> iterator = keys.iterator();
390 while (iterator.hasNext())
393 final String key = iterator.next();
394 final String value = aModuleHashMap.get(key);
396 module = module.replaceAll(key, value);
398 final int count = module.split(value).length;
399 if (count > 2)
401 for (int i = 2; i < count; i++)
403 module.replaceFirst("," + value, "");
408 return module;
411 private boolean verifyOutput(String output)
414 log.println("verify output...");
415 boolean ok = false;
416 final String[] outs = output.split("\n");
418 for (int i = 0; i < outs.length; i++)
420 final String line = outs[i];
421 if (line.matches("[0-9]+? of [0-9]+? tests failed"))
423 log.println("mached line: " + line);
424 if (line.matches("0 of [0-9]+? tests failed"))
426 ok = true;
427 log.println("Module passed OK");
429 else
431 log.println("Module passed FAILED");
436 if (!ok)
438 log.println("ERROR: could not find '0 of [0-9]+? tests failed' in output");
441 return ok;
444 private String[] getModulesFromSourceRoot()
446 log.println("**** run module tests over all modules ****");
448 log.println("search for qa/unoapi foldres in all modules based in ");
449 log.println("'" + mSRC_ROOT + "'");
451 final ArrayList<String> moduleNames = new ArrayList<String>();
452 final File sourceRoot = new File(mSRC_ROOT);
453 final File[] sourceTree = sourceRoot.listFiles();
455 for (int i = 0; i < sourceTree.length; i++)
457 final File moduleName = sourceTree[i];
458 String sModuleName = moduleName.getName(); // (String)moduleNames.get(i);
459 if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
461 moduleNames.add(sModuleName);
465 final String[] names = moduleNames.toArray(new String[moduleNames.size()]);
466 return names;
470 * This function generates a list of modules to test and call <CODE>checkModule</CODE> for every module.
471 * <p>
473 * @param module names to test. This could be
474 * <ul>
475 * <li>a comma separated list of modules like 'sw,sc,sd'</li>
476 * <li>'all' to test all modules </li>
477 * <li>'auto' to check only modules which are added to the ChildWorkSpace</li>
478 * </ul>
480 public void module(String module)
483 String[] checkModules;
484 final ComplexDescGetter desc = new ComplexDescGetter();
485 DescEntry entry = null;
486 module = module.trim();
489 all: check all modules which contains a qa/unoapi folder
490 auto: check all modules which contains a qa/unoapi folder except the module is not added
492 if (module.equals("all"))
494 checkModules = getAllModuleCommand();
496 else if (module.equals("auto"))
498 checkModules = getCwsModuleCommand();
500 else
502 module = getTranslatedNames(module);
503 checkModules = getDefinedModuleCommand(module);
506 if (checkModules != null && checkModules.length > 0)
509 entry = desc.createTestDesc("complex.unoapi.CheckModuleAPI", "complex.unoapi.CheckModuleAPI", checkModules,
510 log);
512 final java_complex complex = new java_complex();
514 log.println("********** start test *************");
515 final boolean result = complex.executeTest(param, new DescEntry[] { entry });
516 log.println("********** end test *************");
518 assure("CheckModuleAPI.module(" + module + ") PASSED.FAILED", result);
521 else
523 log.println("No modules containing qa/unoapi folder found => OK");
524 state = true;
527 setUnoApiCwsStatus(state);
531 private void setUnoApiCwsStatus(boolean status)
533 final String version = (String) param.get(PropertyName.VERSION);
534 if (version.startsWith("cws_"))
539 final CwsDataExchangeImpl cde = new CwsDataExchangeImpl(param, log);
540 cde.setUnoApiCwsStatus(status);
542 catch (ParameterNotFoundException ex)
544 log.println("ERROR: could not wirte status to EIS database: " + ex.toString());
549 @Override
550 public String[] getTestMethodNames()
552 return new String[]
554 "module(all)"