bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / tests / java / complex / unoapi / CheckModuleAPI.java
blobd5b16eca17e5a7254fa4a0fc854bb95316f60b7d
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 // imports
24 import base.java_complex;
25 import complexlib.ComplexTestCase;
26 import helper.OfficeProvider;
27 import helper.ParameterNotFoundException;
28 import helper.ProcessHandler;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import helper.BuildEnvTools;
31 import helper.ComplexDescGetter;
32 import helper.CwsDataExchangeImpl;
33 import java.io.File;
34 // import java.io.FileFilter;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.Set;
39 import share.DescEntry;
40 import util.PropertyName;
41 import util.utils;
43 /**
44 * This Complex Test will test the UNO-API by calling dmake in <B>$MODULE</B>/qa/unoapi<p>
45 * This test depends on some requirments:
46 * The <B>TestJob</B> is <b>-o complex.unoapi.CheckModuleAPI::module(<CODE>MODULE</CODE>)</b><p> where <CODE>MODULE</CODE>
47 * could be the following:<p>
48 * <ul>
49 * <li><b>all</b> iterates over <CODE>SRC_ROOT</CODE> and call <CODE>dmake</CODE> in all qa/unoapi folder</li><p>
50 * <li><b>$module</b> call dmake in $module/qa/unoapi folder</li><p>
51 * <li><b>$module1,$module2,...</b> call dmake in $module1/qa/unoapi folder then in $module2/qa/unoapi folder and so on</li><p>
52 * </ul><p>
54 * Also you have to fill the following parameter:
55 * <ul>
56 * <li><b>SHELL</b>: fill this parameter with a shell</li>
57 * </ul>
60 public class CheckModuleAPI extends ComplexTestCase
63 private static String mSRC_ROOT = null;
64 private static boolean mIsInitialized = false;
65 private final static boolean mContinue = true;
66 private static BuildEnvTools bet = null;
68 /**
69 * Initialize the test environment.
70 * This method checks for all neccesarry parameter and exit if not all parameter are set.
72 * Further this method starts an office instance and gets the office some more time to start. This is because
73 * some freshly installed offices don not have such a user tree. The office will create it on its first start,
74 * but this will take some time.
75 * Note: This funktionality is only reasonable with parameter <CODE>-noOffice true</CODE>
77 public void before()
80 if (!mIsInitialized)
82 mIsInitialized = true;
84 try
87 bet = new BuildEnvTools(param, log);
90 catch (ParameterNotFoundException ex)
92 this.failed(ex.toString(), false);
95 mSRC_ROOT = bet.getSrcRoot();
99 /**
100 * let API tests run.
101 * @param module
103 public void checkModule(String module)
105 log.println(utils.getDateTime() + ": start testing module '" + module + "'");
107 log.println(utils.getDateTime() + "start new Office instance...");
109 // TODO: is Office started with this program?
110 final OfficeProvider officeProvider = new OfficeProvider();
112 log.println(utils.getDateTime() + "Receiving the ServiceManager of the Office ");
113 final XMultiServiceFactory msf = (XMultiServiceFactory) officeProvider.getManager(param);
114 assure("couldnot get ServiceFarcotry", msf != null, mContinue);
115 param.put("ServiceFactory", msf);
117 final String sep = System.getProperty("file.separator");
118 final String sUnoapi = getModulePath(module);
119 final File fUnoapi = new File(sUnoapi);
120 final String sMakeFile = sUnoapi + sep + "makefile.mk";
121 final File fMakeFile = new File(sMakeFile);
122 assure("ERROR: could not find makefile: '" + sMakeFile + "'", fMakeFile.exists(), mContinue);
124 final String[] commands = getDmakeCommands(sUnoapi);
126 final ProcessHandler procHdl = bet.runCommandsInEnvironmentShell(commands, fUnoapi, 0);
127 log.println("exit code of dmake: " + procHdl.getExitCode());
128 String test = procHdl.getOutputText();
129 test += procHdl.getErrorText();
130 // if (mDebug) {
131 // log.println("---> Output of dmake:");
132 // log.println(procHdl.getOutputText());
133 // log.println("<--- Output of dmake:");
134 // log.println("---> Error output of dmake:");
135 // log.println(procHdl.getErrorText());
136 // log.println("<--- Error output of dmake:");
137 // }
138 assure("module '" + module + "' failed", verifyOutput(test), mContinue);
139 log.println(utils.getDateTime() + " module '" + module + "': kill existing office...");
141 // TODO: how to check if the office is not started with this process.
142 boolean bNoOffice = param.getBool("NoOffice");
143 if (!bNoOffice)
147 officeProvider.closeExistingOffice(param, true);
149 catch (java.lang.UnsatisfiedLinkError exception)
151 log.println("Office seems not to be running");
155 private String getQaUnoApiPath(String srcRoot, String _sModul)
157 File aFile = new File(srcRoot);
158 if (!aFile.exists())
160 System.out.println("ERROR: srcRoot '" + srcRoot + "' does not exist.");
161 return null;
163 String sModulePath = srcRoot;
164 sModulePath += File.separator;
165 sModulePath += _sModul;
167 File aModulePath = new File(sModulePath);
168 if (! aModulePath.exists())
170 aModulePath = new File(sModulePath + ".lnk");
171 if (! aModulePath.exists())
173 aModulePath = new File(sModulePath + ".link");
174 if (! aModulePath.exists())
176 // module does not exist.
177 return null;
181 sModulePath = aModulePath.getAbsolutePath();
182 sModulePath += File.separator;
183 sModulePath += "qa";
184 sModulePath += File.separator;
185 sModulePath += "unoapi";
186 File aModulePathQaUnoApi = new File(sModulePath);
187 if (aModulePathQaUnoApi.exists())
189 return aModulePathQaUnoApi.getAbsolutePath();
191 return null;
193 private boolean doesQaUnoApiFolderExist(String srcRoot, String _sModul)
195 if (getQaUnoApiPath(srcRoot, _sModul) != null)
197 return true;
199 return false;
202 private boolean doesQaUnoApiFolderExist(File srcRoot)
204 final FolderFilter qaFilter = new FolderFilter("qa");
205 final File[] qaTree = srcRoot.listFiles(qaFilter);
206 if (qaTree != null)
208 for (int j = 0; j < qaTree.length; j++)
210 final File qaFolder = qaTree[j];
211 final FolderFilter apiFilter = new FolderFilter("unoapi");
212 final File[] apiTree = qaFolder.listFiles(apiFilter);
213 if (apiTree != null && apiTree.length > 0)
215 return true;
219 return false;
223 private String[] getAllModuleCommand()
225 String[] checkModules;
227 final String[] names = getModulesFromSourceRoot();
228 checkModules = getCheckModuleCommand(names);
230 return checkModules;
233 private String[] getCheckModuleCommand(String[] names)
235 String[] checkModules;
236 checkModules = new String[names.length];
238 for (int i = 0; i < names.length; i++)
240 // if a module is not added to a cws it contains a dot in its name (forms.lnk)
241 if (names[i].indexOf(".") != -1)
243 checkModules[i] = "checkModule(" + names[i].substring(0, names[i].indexOf(".")) + ")";
245 else
247 checkModules[i] = "checkModule(" + names[i] + ")";
250 return checkModules;
253 private String[] getDmakeCommands(String sUnoapi)
256 String[] cmdLines = null;
257 final String platform = (String) param.get(PropertyName.OPERATING_SYSTEM);
258 log.println("prepare command for platform " + platform);
260 if (platform.equals(PropertyName.WNTMSCI))
262 if (param.getBool(PropertyName.CYGWIN))
264 // cygwin stuff
265 cmdLines = new String[]
267 "cd `cygpath '" + sUnoapi.replaceAll("\\\\", "\\\\\\\\") + "'`",
268 "dmake"
271 else
273 // old 4NT
274 cmdLines = new String[]
276 "cdd " + sUnoapi,
277 "dmake"
281 else
283 // unix
284 cmdLines = new String[]
286 "cd " + sUnoapi,
287 "dmake"
290 return cmdLines;
293 private String[] getCwsModuleCommand()
295 String[] checkModules;
296 final String version = (String) param.get(PropertyName.VERSION);
297 String[] names = null;
298 if (version.startsWith("cws_"))
302 // cws version: all added modules must be tested
303 final String cws = version.substring(4, version.length());
304 final CwsDataExchangeImpl cde = new CwsDataExchangeImpl(cws, param, log);
305 final ArrayList<String> addedModules = cde.getModules();
307 final ArrayList<String> moduleNames = new ArrayList<String>();
308 Iterator<String> iterator = addedModules.iterator();
309 while (iterator.hasNext())
311 String sModuleName = iterator.next();
312 // String sFilename = mSRC_ROOT; // + File.separator + sModuleName;
313 // final File sourceRoot = new File(sFilename);
314 if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
316 moduleNames.add(sModuleName);
319 names = moduleNames.toArray(new String[0]);
321 catch (ParameterNotFoundException ex)
323 this.failed(ex.toString(), false);
328 else
330 // major version: all modules must be tested
331 names = getModulesFromSourceRoot();
333 checkModules = getCheckModuleCommand(names);
335 return checkModules;
338 private String[] getDefinedModuleCommand(String module)
340 String[] checkModules = null;
341 // list of modules to test: (sw,sc,sd)
342 if (module.indexOf(",") != -1)
344 final String[] names = module.split(",");
345 checkModules = new String[names.length];
346 for (int i = 0; i < names.length; i++)
348 final String moduleName = names[i].trim();
350 // File sourceRoot = new File(mSRC_ROOT + File.separator + moduleName);
351 // if (!sourceRoot.exists())
352 // {
353 // sourceRoot = new File(mSRC_ROOT + File.separator + moduleName + ".lnk");
354 // }
356 if (doesQaUnoApiFolderExist(mSRC_ROOT, moduleName))
358 checkModules[i] = "checkModule(" + moduleName + ")";
362 else
364 // File sourceRoot = new File(mSRC_ROOT + File.separator + module);
365 // if (!sourceRoot.exists())
366 // {
367 // sourceRoot = new File(mSRC_ROOT + File.separator + module + ".lnk");
368 // }
369 if (doesQaUnoApiFolderExist(mSRC_ROOT, module))
371 checkModules = new String[]
373 "checkModule(" + module + ")"
377 return checkModules;
380 private String getModulePath(String module)
382 String sUnoapi = getQaUnoApiPath(mSRC_ROOT, module);
383 return sUnoapi;
387 Some modules contains more the one project. This methods translates given project parameter to the
388 * correspind module name.
390 * fwk -> framework
391 * fwl -> framework
392 * sch -> chart2
393 * lnn -> lingu
394 * lng -> linguistic
395 * sfx -> sfx2
396 * sm -> starmath
398 private String getTranslatedNames(String module)
401 final HashMap<String,String> aModuleHashMap = new HashMap<String,String>();
403 aModuleHashMap.put("fwk", "framework");
404 aModuleHashMap.put("fwl", "framework");
405 aModuleHashMap.put("sch", "chart2");
406 aModuleHashMap.put("lnn", "lingu");
407 aModuleHashMap.put("lng", "linguistic");
408 aModuleHashMap.put("sfx", "sfx2");
409 aModuleHashMap.put("sm", "starmath");
411 // it could the that the parameter looks like "fwk,fwl". This results in double "famework,framework".
412 // The following loop correct this to only one "framework"
414 final Set<String> keys = aModuleHashMap.keySet();
415 final Iterator<String> iterator = keys.iterator();
416 while (iterator.hasNext())
419 final String key = iterator.next();
420 final String value = aModuleHashMap.get(key);
422 module = module.replaceAll(key, value);
424 final int count = module.split(value).length;
425 if (count > 2)
427 for (int i = 2; i < count; i++)
429 module.replaceFirst("," + value, "");
434 return module;
437 private boolean verifyOutput(String output)
440 log.println("verify output...");
441 boolean ok = false;
442 final String[] outs = output.split("\n");
444 for (int i = 0; i < outs.length; i++)
446 final String line = outs[i];
447 if (line.matches("[0-9]+? of [0-9]+? tests failed"))
449 log.println("mached line: " + line);
450 if (line.matches("0 of [0-9]+? tests failed"))
452 ok = true;
453 log.println("Module passed OK");
455 else
457 log.println("Module passed FAILED");
462 if (!ok)
464 log.println("ERROR: could not find '0 of [0-9]+? tests failed' in output");
467 return ok;
470 private String[] getModulesFromSourceRoot()
472 log.println("**** run module tests over all modules ****");
474 log.println("search for qa/unoapi foldres in all modules based in ");
475 log.println("'" + mSRC_ROOT + "'");
477 final ArrayList<String> moduleNames = new ArrayList<String>();
478 final File sourceRoot = new File(mSRC_ROOT);
479 final File[] sourceTree = sourceRoot.listFiles();
481 // assure("Could not find any files in SOURCE_ROOT=" + mSRC_ROOT, sourceTree != null, false);
483 for (int i = 0; i < sourceTree.length; i++)
485 final File moduleName = sourceTree[i];
486 String sModuleName = moduleName.getName(); // (String)moduleNames.get(i);
487 if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
489 // addIfQaUnoApiFolderExist(moduleName, moduleNames);
490 moduleNames.add(sModuleName);
494 final String[] names = moduleNames.toArray(new String[0]);
495 return names;
499 * This function generates a list of modules to test and call <CODE>checkModule</CODE> for every module.
500 * <p>
502 * @param module names to test. This could be
503 * <ul>
504 * <li>a comma separated list of modules like 'sw,sc,sd'</li>
505 * <li>'all' to test all modules </li>
506 * <li>'auto' to check only modules which are added to the ChildWorkSpace</li>
507 * </ul>
509 public void module(String module)
512 String[] checkModules;
513 final ComplexDescGetter desc = new ComplexDescGetter();
514 DescEntry entry = null;
515 module = module.trim();
518 all: check all modules which contains a qa/unoapi folder
519 auto: check all modules which contains a qa/unoapi folder except the module is not added
521 if (module.equals("all"))
523 checkModules = getAllModuleCommand();
525 else if (module.equals("auto"))
527 checkModules = getCwsModuleCommand();
529 else
531 module = getTranslatedNames(module);
532 checkModules = getDefinedModuleCommand(module);
535 if (checkModules != null && checkModules.length > 0)
538 entry = desc.createTestDesc("complex.unoapi.CheckModuleAPI", "complex.unoapi.CheckModuleAPI", checkModules,
539 log);
541 final java_complex complex = new java_complex();
543 log.println("********** start test *************");
544 final boolean result = complex.executeTest(param, new DescEntry[] { entry });
545 log.println("********** end test *************");
547 assure("CheckModuleAPI.module(" + module + ") PASSED.FAILED", result);
550 else
552 log.println("No modules containing qa/unoapi folder found => OK");
553 state = true;
556 setUnoApiCwsStatus(state);
560 private void setUnoApiCwsStatus(boolean status)
563 if (!param.getBool(PropertyName.NO_CWS_ATTACH))
566 final String version = (String) param.get(PropertyName.VERSION);
567 if (version.startsWith("cws_"))
572 // cws version: all added modules must be tested
573 final String cws = version.substring(4, version.length());
574 final CwsDataExchangeImpl cde = new CwsDataExchangeImpl(cws, param, log);
575 cde.setUnoApiCwsStatus(status);
577 catch (ParameterNotFoundException ex)
579 log.println("ERROR: could not wirte status to EIS database: " + ex.toString());
585 public String[] getTestMethodNames()
587 return new String[]
589 "module(all)"
593 // class _FolderFilter implements FileFilter
594 // {
596 // private String mFolderName;
598 // public FolderFilter(String folderName)
599 // {
600 // mFolderName = folderName;
601 // }
603 // public boolean accept_(File pathname)
604 // {
606 // boolean found = false;
607 // if (pathname.isDirectory())
608 // {
609 // if (pathname.getName().equals(mFolderName))
610 // {
611 // found = true;
612 // }
613 // else if (pathname.getName().equals(mFolderName + ".lnk"))
614 // {
615 // found = true;
616 // }
617 // }
618 // return found;
619 // }
620 // }