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
21 package complex
.unoapi
;
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
;
34 // import java.io.FileFilter;
35 import java
.util
.ArrayList
;
36 import java
.util
.HashMap
;
37 import java
.util
.Iterator
;
39 import share
.DescEntry
;
40 import util
.PropertyName
;
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>
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>
54 * Also you have to fill the following parameter:
56 * <li><b>SHELL</b>: fill this parameter with a shell</li>
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;
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>
82 mIsInitialized
= true;
87 bet
= new BuildEnvTools(param
, log
);
90 catch (ParameterNotFoundException ex
)
92 this.failed(ex
.toString(), false);
95 mSRC_ROOT
= bet
.getSrcRoot();
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();
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:");
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");
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
);
160 System
.out
.println("ERROR: srcRoot '" + srcRoot
+ "' does not exist.");
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.
181 sModulePath
= aModulePath
.getAbsolutePath();
182 sModulePath
+= File
.separator
;
184 sModulePath
+= File
.separator
;
185 sModulePath
+= "unoapi";
186 File aModulePathQaUnoApi
= new File(sModulePath
);
187 if (aModulePathQaUnoApi
.exists())
189 return aModulePathQaUnoApi
.getAbsolutePath();
193 private boolean doesQaUnoApiFolderExist(String srcRoot
, String _sModul
)
195 if (getQaUnoApiPath(srcRoot
, _sModul
) != null)
202 private boolean doesQaUnoApiFolderExist(File srcRoot)
204 final FolderFilter qaFilter = new FolderFilter("qa");
205 final File[] qaTree = srcRoot.listFiles(qaFilter);
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)
223 private String
[] getAllModuleCommand()
225 String
[] checkModules
;
227 final String
[] names
= getModulesFromSourceRoot();
228 checkModules
= getCheckModuleCommand(names
);
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(".")) + ")";
247 checkModules
[i
] = "checkModule(" + names
[i
] + ")";
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
))
265 cmdLines
= new String
[]
267 "cd `cygpath '" + sUnoapi
.replaceAll("\\\\", "\\\\\\\\") + "'`",
274 cmdLines
= new String
[]
284 cmdLines
= new String
[]
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);
330 // major version: all modules must be tested
331 names
= getModulesFromSourceRoot();
333 checkModules
= getCheckModuleCommand(names
);
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())
353 // sourceRoot = new File(mSRC_ROOT + File.separator + moduleName + ".lnk");
356 if (doesQaUnoApiFolderExist(mSRC_ROOT
, moduleName
))
358 checkModules
[i
] = "checkModule(" + moduleName
+ ")";
364 // File sourceRoot = new File(mSRC_ROOT + File.separator + module);
365 // if (!sourceRoot.exists())
367 // sourceRoot = new File(mSRC_ROOT + File.separator + module + ".lnk");
369 if (doesQaUnoApiFolderExist(mSRC_ROOT
, module
))
371 checkModules
= new String
[]
373 "checkModule(" + module
+ ")"
380 private String
getModulePath(String module
)
382 String sUnoapi
= getQaUnoApiPath(mSRC_ROOT
, module
);
387 Some modules contains more the one project. This methods translates given project parameter to the
388 * correspind module name.
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
;
427 for (int i
= 2; i
< count
; i
++)
429 module
.replaceFirst("," + value
, "");
437 private boolean verifyOutput(String output
)
440 log
.println("verify output...");
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"))
453 log
.println("Module passed OK");
457 log
.println("Module passed FAILED");
464 log
.println("ERROR: could not find '0 of [0-9]+? tests failed' in output");
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]);
499 * This function generates a list of modules to test and call <CODE>checkModule</CODE> for every module.
502 * @param module names to test. This could be
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>
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();
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
,
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
);
552 log
.println("No modules containing qa/unoapi folder found => OK");
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()
593 // class _FolderFilter implements FileFilter
596 // private String mFolderName;
598 // public FolderFilter(String folderName)
600 // mFolderName = folderName;
603 // public boolean accept_(File pathname)
606 // boolean found = false;
607 // if (pathname.isDirectory())
609 // if (pathname.getName().equals(mFolderName))
613 // else if (pathname.getName().equals(mFolderName + ".lnk"))