Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / tests / java / complex / unoapi / CheckModuleAPI.java
blobbf05553bcb6d2c8b2c798f1d1d6175bea5ee0894
1 /*
2 * ************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 * **********************************************************************
29 /** complex tests to check the UNO-API
30 **/
31 package complex.unoapi;
33 // imports
34 import base.java_complex;
35 import complexlib.ComplexTestCase;
36 import helper.OfficeProvider;
37 import helper.ParameterNotFoundException;
38 import helper.ProcessHandler;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import helper.BuildEnvTools;
41 import helper.ComplexDescGetter;
42 import helper.CwsDataExchangeImpl;
43 import java.io.File;
44 // import java.io.FileFilter;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.Iterator;
48 import java.util.Set;
49 import share.DescEntry;
50 import util.PropertyName;
51 import util.utils;
53 /**
54 * This Complex Test will test the UNO-API by calling dmake in <B>$MODULE</B>/qa/unoapi<p>
55 * This test depends on some requirments:
56 * The <B>TestJob</B> is <b>-o complex.unoapi.CheckModuleAPI::module(<CODE>MODULE</CODE>)</b><p> where <CODE>MODULE</CODE>
57 * could be the following:<p>
58 * <ul>
59 * <li><b>all</b> iterates over <CODE>SRC_ROOT</CODE> and call <CODE>dmake</CODE> in all qa/unoapi folder</li><p>
60 * <li><b>$module</b> call dmake in $module/qa/unoapi folder</li><p>
61 * <li><b>$module1,$module2,...</b> call dmake in $module1/qa/unoapi folder then in $module2/qa/unoapi folder and so on</li><p>
62 * </ul><p>
64 * Also you have to fill the following parameter:
65 * <ul>
66 * <li><b>SHELL</b>: fill this parameter with a shell</li>
67 * </ul>
70 public class CheckModuleAPI extends ComplexTestCase
73 private static String mSRC_ROOT = null;
74 private static boolean mIsInitialized = false;
75 private final static boolean mContinue = true;
76 private static boolean mDebug = false;
77 private static BuildEnvTools bet = null;
79 /**
80 * Initialize the test environment.
81 * This method checks for all neccesarry parameter and exit if not all parameter are set.
83 * Further this method starts an office instance and gets the office some more time to start. This is because
84 * some freshly installed offices don not have such a user tree. The office will create it on its first start,
85 * but this will take some time.
86 * Note: This funktionality is only reasonable with parameter <CODE>-noOffice true</CODE>
88 public void before()
91 if (!mIsInitialized)
93 mIsInitialized = true;
95 try
98 bet = new BuildEnvTools(param, log);
101 catch (ParameterNotFoundException ex)
103 this.failed(ex.toString(), false);
106 mSRC_ROOT = bet.getSrcRoot();
108 mDebug = param.getBool(PropertyName.DEBUG_IS_ACTIVE);
114 * let API tests run.
115 * @param module
117 public void checkModule(String module)
119 log.println(utils.getDateTime() + ": start testing module '" + module + "'");
121 log.println(utils.getDateTime() + "start new Office instance...");
123 // TODO: is Office started with this program?
124 final OfficeProvider officeProvider = new OfficeProvider();
126 log.println(utils.getDateTime() + "Receiving the ServiceManager of the Office ");
127 final XMultiServiceFactory msf = (XMultiServiceFactory) officeProvider.getManager(param);
128 assure("couldnot get ServiceFarcotry", msf != null, mContinue);
129 param.put("ServiceFactory", msf);
131 final String sep = System.getProperty("file.separator");
132 final String sUnoapi = getModulePath(module);
133 final File fUnoapi = new File(sUnoapi);
134 final String sMakeFile = sUnoapi + sep + "makefile.mk";
135 final File fMakeFile = new File(sMakeFile);
136 assure("ERROR: could not find makefile: '" + sMakeFile + "'", fMakeFile.exists(), mContinue);
138 final String[] commands = getDmakeCommands(sUnoapi);
140 final ProcessHandler procHdl = bet.runCommandsInEnvironmentShell(commands, fUnoapi, 0);
141 log.println("exit code of dmake: " + procHdl.getExitCode());
142 String test = procHdl.getOutputText();
143 test += procHdl.getErrorText();
144 // if (mDebug) {
145 // log.println("---> Output of dmake:");
146 // log.println(procHdl.getOutputText());
147 // log.println("<--- Output of dmake:");
148 // log.println("---> Error output of dmake:");
149 // log.println(procHdl.getErrorText());
150 // log.println("<--- Error output of dmake:");
151 // }
152 assure("module '" + module + "' failed", verifyOutput(test), mContinue);
153 log.println(utils.getDateTime() + " module '" + module + "': kill existing office...");
155 // TODO: how to check if the office is not started with this process.
156 boolean bNoOffice = param.getBool("NoOffice");
157 if (!bNoOffice)
161 officeProvider.closeExistingOffice(param, true);
163 catch (java.lang.UnsatisfiedLinkError exception)
165 log.println("Office seems not to be running");
169 private String getQaUnoApiPath(String srcRoot, String _sModul)
171 File aFile = new File(srcRoot);
172 if (!aFile.exists())
174 System.out.println("ERROR: srcRoot '" + srcRoot + "' does not exist.");
175 return null;
177 String sModulePath = srcRoot;
178 sModulePath += File.separator;
179 sModulePath += _sModul;
181 File aModulePath = new File(sModulePath);
182 if (! aModulePath.exists())
184 aModulePath = new File(sModulePath + ".lnk");
185 if (! aModulePath.exists())
187 aModulePath = new File(sModulePath + ".link");
188 if (! aModulePath.exists())
190 // module does not exist.
191 return null;
195 sModulePath = aModulePath.getAbsolutePath();
196 sModulePath += File.separator;
197 sModulePath += "qa";
198 sModulePath += File.separator;
199 sModulePath += "unoapi";
200 File aModulePathQaUnoApi = new File(sModulePath);
201 if (aModulePathQaUnoApi.exists())
203 return aModulePathQaUnoApi.getAbsolutePath();
205 return null;
207 private boolean doesQaUnoApiFolderExist(String srcRoot, String _sModul)
209 if (getQaUnoApiPath(srcRoot, _sModul) != null)
211 return true;
213 return false;
216 private boolean doesQaUnoApiFolderExist(File srcRoot)
218 final FolderFilter qaFilter = new FolderFilter("qa");
219 final File[] qaTree = srcRoot.listFiles(qaFilter);
220 if (qaTree != null)
222 for (int j = 0; j < qaTree.length; j++)
224 final File qaFolder = qaTree[j];
225 final FolderFilter apiFilter = new FolderFilter("unoapi");
226 final File[] apiTree = qaFolder.listFiles(apiFilter);
227 if (apiTree != null && apiTree.length > 0)
229 return true;
233 return false;
237 private String[] getAllModuleCommand()
239 String[] checkModules;
241 final String[] names = getModulesFromSourceRoot();
242 checkModules = getCheckModuleCommand(names);
244 return checkModules;
247 private String[] getCheckModuleCommand(String[] names)
249 String[] checkModules;
250 checkModules = new String[names.length];
252 for (int i = 0; i < names.length; i++)
254 // if a module is not added to a cws it contains a dot in its name (forms.lnk)
255 if (names[i].indexOf(".") != -1)
257 checkModules[i] = "checkModule(" + names[i].substring(0, names[i].indexOf(".")) + ")";
259 else
261 checkModules[i] = "checkModule(" + names[i] + ")";
264 return checkModules;
267 private String[] getDmakeCommands(String sUnoapi)
270 String[] cmdLines = null;
271 final String platform = (String) param.get(PropertyName.OPERATING_SYSTEM);
272 log.println("prepare command for platform " + platform);
274 if (platform.equals(PropertyName.WNTMSCI))
276 if (param.getBool(PropertyName.CYGWIN))
278 // cygwin stuff
279 cmdLines = new String[]
281 "cd `cygpath '" + sUnoapi.replaceAll("\\\\", "\\\\\\\\") + "'`",
282 "dmake"
285 else
287 // old 4NT
288 cmdLines = new String[]
290 "cdd " + sUnoapi,
291 "dmake"
295 else
297 // unix
298 cmdLines = new String[]
300 "cd " + sUnoapi,
301 "dmake"
304 return cmdLines;
307 private String[] getCwsModuleCommand()
309 String[] checkModules;
310 final String version = (String) param.get(PropertyName.VERSION);
311 String[] names = null;
312 if (version.startsWith("cws_"))
316 // cws version: all added modules must be tested
317 final String cws = version.substring(4, version.length());
318 final CwsDataExchangeImpl cde = new CwsDataExchangeImpl(cws, param, log);
319 final ArrayList addedModules = cde.getModules();
321 final ArrayList moduleNames = new ArrayList();
322 Iterator iterator = addedModules.iterator();
323 while (iterator.hasNext())
325 String sModuleName = (String) iterator.next();
326 // String sFilename = mSRC_ROOT; // + File.separator + sModuleName;
327 // final File sourceRoot = new File(sFilename);
328 if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
330 moduleNames.add(sModuleName);
333 names = (String[]) moduleNames.toArray(new String[0]);
335 catch (ParameterNotFoundException ex)
337 this.failed(ex.toString(), false);
342 else
344 // major version: all modules must be tested
345 names = getModulesFromSourceRoot();
347 checkModules = getCheckModuleCommand(names);
349 return checkModules;
352 private String[] getDefinedModuleCommand(String module)
354 String[] checkModules = null;
355 // list of modules to test: (sw,sc,sd)
356 if (module.indexOf(",") != -1)
358 final String[] names = module.split(",");
359 checkModules = new String[names.length];
360 for (int i = 0; i < names.length; i++)
362 final String moduleName = names[i].trim();
364 // File sourceRoot = new File(mSRC_ROOT + File.separator + moduleName);
365 // if (!sourceRoot.exists())
366 // {
367 // sourceRoot = new File(mSRC_ROOT + File.separator + moduleName + ".lnk");
368 // }
370 if (doesQaUnoApiFolderExist(mSRC_ROOT, moduleName))
372 checkModules[i] = "checkModule(" + moduleName + ")";
376 else
378 // File sourceRoot = new File(mSRC_ROOT + File.separator + module);
379 // if (!sourceRoot.exists())
380 // {
381 // sourceRoot = new File(mSRC_ROOT + File.separator + module + ".lnk");
382 // }
383 if (doesQaUnoApiFolderExist(mSRC_ROOT, module))
385 checkModules = new String[]
387 "checkModule(" + module + ")"
391 return checkModules;
394 private String getModulePath(String module)
397 // String sUnoapi = null;
398 // final String sep = System.getProperty("file.separator");
399 // final File srcRoot = new File(mSRC_ROOT);
401 // final FolderFilter qaFilter = new FolderFilter(module);
402 // final File[] moduleTree = srcRoot.listFiles(qaFilter);
403 // if (moduleTree != null)
404 // {
405 // if (mDebug)
406 // {
407 // log.println("moduleTree length:" + moduleTree.length);
408 // log.println("moduleTree: " + moduleTree[0].getAbsolutePath());
409 // }
410 // if (moduleTree != null)
411 // {
412 // sUnoapi = moduleTree[0].getAbsolutePath() + sep + "qa" + sep + "unoapi";
413 // }
414 // }
415 String sUnoapi = getQaUnoApiPath(mSRC_ROOT, module);
416 return sUnoapi;
420 Some modules contains more the one project. This methods translates given project paramater to the
421 * correspind module name.
423 * fwk -> framework
424 * fwl -> framework
425 * sch -> chart2
426 * lnn -> lingu
427 * lng -> linguistic
428 * sfx -> sfx2
429 * sm -> starmath
431 private String getTranslatedNames(String module)
434 final HashMap aModuleHashMap = new HashMap();
436 aModuleHashMap.put("fwk", "framework");
437 aModuleHashMap.put("fwl", "framework");
438 aModuleHashMap.put("sch", "chart2");
439 aModuleHashMap.put("lnn", "lingu");
440 aModuleHashMap.put("lng", "linguistic");
441 aModuleHashMap.put("sfx", "sfx2");
442 aModuleHashMap.put("sm", "starmath");
444 // it could the that the parameter looks like "fwk,fwl". This results in double "famework,framework".
445 // The following loop correct this to only one "framework"
447 final Set keys = aModuleHashMap.keySet();
448 final Iterator iterator = keys.iterator();
449 while (iterator.hasNext())
452 final String key = (String) iterator.next();
453 final String value = (String) aModuleHashMap.get(key);
455 module = module.replaceAll(key, value);
457 final int count = module.split(value).length;
458 if (count > 2)
460 for (int i = 2; i < count; i++)
462 module.replaceFirst("," + value, "");
467 return module;
470 private boolean verifyOutput(String output)
473 log.println("verify output...");
474 boolean ok = false;
475 final String[] outs = output.split("\n");
477 for (int i = 0; i < outs.length; i++)
479 final String line = outs[i];
480 if (line.matches("[0-9]+? of [0-9]+? tests failed"))
482 log.println("mached line: " + line);
483 if (line.matches("0 of [0-9]+? tests failed"))
485 ok = true;
486 log.println("Module passed OK");
488 else
490 log.println("Module passed FAILED");
495 if (!ok)
497 log.println("ERROR: could not find '0 of [0-9]+? tests failed' in output");
500 return ok;
503 private String[] getModulesFromSourceRoot()
505 log.println("**** run module tests over all modules ****");
507 log.println("search for qa/unoapi foldres in all modules based in ");
508 log.println("'" + mSRC_ROOT + "'");
510 final ArrayList moduleNames = new ArrayList();
511 final File sourceRoot = new File(mSRC_ROOT);
512 final File[] sourceTree = sourceRoot.listFiles();
514 // assure("Could not find any files in SOURCE_ROOT=" + mSRC_ROOT, sourceTree != null, false);
516 for (int i = 0; i < sourceTree.length; i++)
518 final File moduleName = sourceTree[i];
519 String sModuleName = moduleName.getName(); // (String)moduleNames.get(i);
520 if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
522 // addIfQaUnoApiFolderExist(moduleName, moduleNames);
523 moduleNames.add(sModuleName);
527 final String[] names = (String[]) moduleNames.toArray(new String[0]);
528 return names;
532 * This function generates a list of modules to test and call <CODE>checkModule</CODE> for every module.
533 * <p>
535 * @param module names to test. This could be
536 * <ul>
537 * <li>a comma separated list of modules like 'sw,sc,sd'</li>
538 * <li>'all' to test all modules </li>
539 * <li>'auto' to check only modules which are added to the ChildWorkSpace</li>
540 * </ul>
542 public void module(String module)
545 String[] checkModules;
546 final ComplexDescGetter desc = new ComplexDescGetter();
547 DescEntry entry = null;
548 module = module.trim();
551 all: check all modules which contains a qa/unoapi folder
552 auto: check all modules which contains a qa/unoapi folder except the module is not added
554 if (module.equals("all"))
556 checkModules = getAllModuleCommand();
558 else if (module.equals("auto"))
560 checkModules = getCwsModuleCommand();
562 else
564 module = getTranslatedNames(module);
565 checkModules = getDefinedModuleCommand(module);
568 if (checkModules != null && checkModules.length > 0)
571 entry = desc.createTestDesc("complex.unoapi.CheckModuleAPI", "complex.unoapi.CheckModuleAPI", checkModules,
572 log);
574 final java_complex complex = new java_complex();
576 log.println("********** start test *************");
577 final boolean result = complex.executeTest(param, new DescEntry[] { entry });
578 log.println("********** end test *************");
580 assure("CheckModuleAPI.module(" + module + ") PASSED.FAILED", result);
583 else
585 log.println("No modules containing qa/unoapi folder found => OK");
586 state = true;
589 setUnoApiCwsStatus(state);
593 private void setUnoApiCwsStatus(boolean status)
596 if (!param.getBool(PropertyName.NO_CWS_ATTACH))
599 final String version = (String) param.get(PropertyName.VERSION);
600 if (version.startsWith("cws_"))
605 // cws version: all added modules must be tested
606 final String cws = version.substring(4, version.length());
607 final CwsDataExchangeImpl cde = new CwsDataExchangeImpl(cws, param, log);
608 cde.setUnoApiCwsStatus(status);
610 catch (ParameterNotFoundException ex)
612 log.println("ERROR: could not wirte status to EIS database: " + ex.toString());
618 public String[] getTestMethodNames()
620 return new String[]
622 "module(all)"
626 // class _FolderFilter implements FileFilter
627 // {
629 // private String mFolderName;
631 // public FolderFilter(String folderName)
632 // {
633 // mFolderName = folderName;
634 // }
636 // public boolean accept_(File pathname)
637 // {
639 // boolean found = false;
640 // if (pathname.isDirectory())
641 // {
642 // if (pathname.getName().equals(mFolderName))
643 // {
644 // found = true;
645 // }
646 // else if (pathname.getName().equals(mFolderName + ".lnk"))
647 // {
648 // found = true;
649 // }
650 // }
651 // return found;
652 // }
653 // }