xref: /AOO41X/main/qadevOOo/tests/java/complex/unoapi/CheckModuleAPI.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*
2  * ************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
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.
15  *
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).
21  *
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.
26  *
27  * **********************************************************************
28  */
29 /** complex tests to check the UNO-API
30  **/
31 package complex.unoapi;
32 
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;
52 
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>
63  *
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>
68  *
69  */
70 public class CheckModuleAPI extends ComplexTestCase
71 {
72 
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;
78 
79     /**
80      * Initialize the test environment.
81      * This method checks for all neccesarry parameter and exit if not all parameter are set.
82      *
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>
87      */
88     public void before()
89     {
90 
91         if (!mIsInitialized)
92         {
93             mIsInitialized = true;
94 
95             try
96             {
97 
98                 bet = new BuildEnvTools(param, log);
99 
100             }
101             catch (ParameterNotFoundException ex)
102             {
103                 this.failed(ex.toString(), false);
104             }
105 
106             mSRC_ROOT = bet.getSrcRoot();
107 
108             mDebug = param.getBool(PropertyName.DEBUG_IS_ACTIVE);
109 
110         }
111     }
112 
113     /**
114      * let API tests run.
115      * @param module
116      */
117     public void checkModule(String module)
118     {
119         log.println(utils.getDateTime() + ": start testing module '" + module + "'");
120 
121         log.println(utils.getDateTime() + "start new Office instance...");
122 
123         // TODO: is Office started with this program?
124         final OfficeProvider officeProvider = new OfficeProvider();
125 
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);
130 
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);
137 
138         final String[] commands = getDmakeCommands(sUnoapi);
139 
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...");
154 
155         // TODO: how to check if the office is not started with this process.
156         boolean bNoOffice = param.getBool("NoOffice");
157         if (!bNoOffice)
158         {
159             try
160             {
161                 officeProvider.closeExistingOffice(param, true);
162             }
163             catch (java.lang.UnsatisfiedLinkError exception)
164             {
165                 log.println("Office seems not to be running");
166             }
167         }
168     }
169     private String getQaUnoApiPath(String srcRoot, String _sModul)
170     {
171         File aFile = new File(srcRoot);
172         if (!aFile.exists())
173         {
174             System.out.println("ERROR: srcRoot '" + srcRoot + "' does not exist.");
175             return null;
176         }
177         String sModulePath = srcRoot;
178         sModulePath += File.separator;
179         sModulePath += _sModul;
180 
181         File aModulePath = new File(sModulePath);
182         if (! aModulePath.exists())
183         {
184             aModulePath = new File(sModulePath + ".lnk");
185             if (! aModulePath.exists())
186             {
187                 aModulePath = new File(sModulePath + ".link");
188                 if (! aModulePath.exists())
189                 {
190                     // module does not exist.
191                     return null;
192                 }
193             }
194         }
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())
202         {
203             return aModulePathQaUnoApi.getAbsolutePath();
204         }
205         return null;
206     }
207     private boolean doesQaUnoApiFolderExist(String srcRoot, String _sModul)
208     {
209         if (getQaUnoApiPath(srcRoot, _sModul) != null)
210         {
211             return true;
212         }
213         return false;
214     }
215 /*
216  private boolean doesQaUnoApiFolderExist(File srcRoot)
217     {
218         final FolderFilter qaFilter = new FolderFilter("qa");
219         final File[] qaTree = srcRoot.listFiles(qaFilter);
220         if (qaTree != null)
221         {
222             for (int j = 0; j < qaTree.length; j++)
223             {
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)
228                 {
229                     return true;
230                 }
231             }
232         }
233         return false;
234     }
235 */
236 
237     private String[] getAllModuleCommand()
238     {
239         String[] checkModules;
240 
241         final String[] names = getModulesFromSourceRoot();
242         checkModules = getCheckModuleCommand(names);
243 
244         return checkModules;
245     }
246 
247     private String[] getCheckModuleCommand(String[] names)
248     {
249         String[] checkModules;
250         checkModules = new String[names.length];
251 
252         for (int i = 0; i < names.length; i++)
253         {
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)
256             {
257                 checkModules[i] = "checkModule(" + names[i].substring(0, names[i].indexOf(".")) + ")";
258             }
259             else
260             {
261                 checkModules[i] = "checkModule(" + names[i] + ")";
262             }
263         }
264         return checkModules;
265     }
266 
267     private String[] getDmakeCommands(String sUnoapi)
268     {
269 
270         String[] cmdLines = null;
271         final String platform = (String) param.get(PropertyName.OPERATING_SYSTEM);
272         log.println("prepare command for platform " + platform);
273 
274         if (platform.equals(PropertyName.WNTMSCI))
275         {
276             if (param.getBool(PropertyName.CYGWIN))
277             {
278                 // cygwin stuff
279                 cmdLines = new String[]
280                         {
281                             "cd `cygpath '" + sUnoapi.replaceAll("\\\\", "\\\\\\\\") + "'`",
282                             "dmake"
283                         };
284             }
285             else
286             {
287                 // old 4NT
288                 cmdLines = new String[]
289                         {
290                             "cdd " + sUnoapi,
291                             "dmake"
292                         };
293             }
294         }
295         else
296         {
297             // unix
298             cmdLines = new String[]
299                     {
300                         "cd " + sUnoapi,
301                         "dmake"
302                     };
303         }
304         return cmdLines;
305     }
306 
307     private String[] getCwsModuleCommand()
308     {
309         String[] checkModules;
310         final String version = (String) param.get(PropertyName.VERSION);
311         String[] names = null;
312         if (version.startsWith("cws_"))
313         {
314             try
315             {
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();
320 
321                 final ArrayList moduleNames = new ArrayList();
322                 Iterator iterator = addedModules.iterator();
323                 while (iterator.hasNext())
324                 {
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))
329                     {
330                         moduleNames.add(sModuleName);
331                     }
332                 }
333                 names = (String[]) moduleNames.toArray(new String[0]);
334             }
335             catch (ParameterNotFoundException ex)
336             {
337                 this.failed(ex.toString(), false);
338             }
339 
340 
341         }
342         else
343         {
344             // major version: all modules must be tested
345             names = getModulesFromSourceRoot();
346         }
347         checkModules = getCheckModuleCommand(names);
348 
349         return checkModules;
350     }
351 
352     private String[] getDefinedModuleCommand(String module)
353     {
354         String[] checkModules = null;
355         // list of modules to test: (sw,sc,sd)
356         if (module.indexOf(",") != -1)
357         {
358             final String[] names = module.split(",");
359             checkModules = new String[names.length];
360             for (int i = 0; i < names.length; i++)
361             {
362                 final String moduleName = names[i].trim();
363 
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 //                }
369 
370                 if (doesQaUnoApiFolderExist(mSRC_ROOT, moduleName))
371                 {
372                     checkModules[i] = "checkModule(" + moduleName + ")";
373                 }
374             }
375         }
376         else
377         {
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))
384             {
385                 checkModules = new String[]
386                         {
387                             "checkModule(" + module + ")"
388                         };
389             }
390         }
391         return checkModules;
392     }
393 
394     private String getModulePath(String module)
395     {
396 
397         // String sUnoapi = null;
398         // final String sep = System.getProperty("file.separator");
399         // final File srcRoot = new File(mSRC_ROOT);
400 
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;
417     }
418 
419     /**
420     Some modules contains more the one project. This methods translates given project paramater to the
421      *  correspind module name.
422      *
423      * fwk -> framework
424      * fwl -> framework
425      * sch -> chart2
426      * lnn -> lingu
427      * lng -> linguistic
428      * sfx -> sfx2
429      * sm -> starmath
430      */
431     private String getTranslatedNames(String module)
432     {
433 
434         final HashMap aModuleHashMap = new HashMap();
435 
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");
443 
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"
446 
447         final Set keys = aModuleHashMap.keySet();
448         final Iterator iterator = keys.iterator();
449         while (iterator.hasNext())
450         {
451 
452             final String key = (String) iterator.next();
453             final String value = (String) aModuleHashMap.get(key);
454 
455             module = module.replaceAll(key, value);
456 
457             final int count = module.split(value).length;
458             if (count > 2)
459             {
460                 for (int i = 2; i < count; i++)
461                 {
462                     module.replaceFirst("," + value, "");
463                 }
464 
465             }
466         }
467         return module;
468     }
469 
470     private boolean verifyOutput(String output)
471     {
472 
473         log.println("verify output...");
474         boolean ok = false;
475         final String[] outs = output.split("\n");
476 
477         for (int i = 0; i < outs.length; i++)
478         {
479             final String line = outs[i];
480             if (line.matches("[0-9]+? of [0-9]+? tests failed"))
481             {
482                 log.println("mached line: " + line);
483                 if (line.matches("0 of [0-9]+? tests failed"))
484                 {
485                     ok = true;
486                     log.println("Module passed OK");
487                 }
488                 else
489                 {
490                     log.println("Module passed FAILED");
491                 }
492             }
493         }
494 
495         if (!ok)
496         {
497             log.println("ERROR: could not find '0 of [0-9]+? tests failed' in output");
498         }
499 
500         return ok;
501     }
502 
503     private String[] getModulesFromSourceRoot()
504     {
505         log.println("**** run module tests over all modules ****");
506 
507         log.println("search for qa/unoapi foldres in all modules based in ");
508         log.println("'" + mSRC_ROOT + "'");
509 
510         final ArrayList moduleNames = new ArrayList();
511         final File sourceRoot = new File(mSRC_ROOT);
512         final File[] sourceTree = sourceRoot.listFiles();
513 
514 //        assure("Could not find any files in SOURCE_ROOT=" + mSRC_ROOT, sourceTree != null, false);
515 
516         for (int i = 0; i < sourceTree.length; i++)
517         {
518             final File moduleName = sourceTree[i];
519             String sModuleName = moduleName.getName(); // (String)moduleNames.get(i);
520             if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
521             {
522                 // addIfQaUnoApiFolderExist(moduleName, moduleNames);
523                 moduleNames.add(sModuleName);
524             }
525         }
526 
527         final String[] names = (String[]) moduleNames.toArray(new String[0]);
528         return names;
529     }
530 
531     /**
532      * This function generates a list of modules to test and call <CODE>checkModule</CODE> for every module.
533      * <p>
534      *
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>
541      */
542     public void module(String module)
543     {
544 
545         String[] checkModules;
546         final ComplexDescGetter desc = new ComplexDescGetter();
547         DescEntry entry = null;
548         module = module.trim();
549 
550         /*
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
553          */
554         if (module.equals("all"))
555         {
556             checkModules = getAllModuleCommand();
557         }
558         else if (module.equals("auto"))
559         {
560             checkModules = getCwsModuleCommand();
561         }
562         else
563         {
564             module = getTranslatedNames(module);
565             checkModules = getDefinedModuleCommand(module);
566         }
567 
568         if (checkModules != null && checkModules.length > 0)
569         {
570 
571             entry = desc.createTestDesc("complex.unoapi.CheckModuleAPI", "complex.unoapi.CheckModuleAPI", checkModules,
572                     log);
573 
574             final java_complex complex = new java_complex();
575 
576             log.println("********** start test *************");
577             final boolean result = complex.executeTest(param, new DescEntry[] { entry });
578             log.println("********** end test *************");
579 
580             assure("CheckModuleAPI.module(" + module + ") PASSED.FAILED", result);
581 
582         }
583         else
584         {
585             log.println("No modules containing qa/unoapi folder found => OK");
586             state = true;
587         }
588 
589         setUnoApiCwsStatus(state);
590 
591     }
592 
593     private void setUnoApiCwsStatus(boolean status)
594     {
595 
596         if (!param.getBool(PropertyName.NO_CWS_ATTACH))
597         {
598 
599             final String version = (String) param.get(PropertyName.VERSION);
600             if (version.startsWith("cws_"))
601             {
602                 try
603                 {
604 
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);
609                 }
610                 catch (ParameterNotFoundException ex)
611                 {
612                     log.println("ERROR: could not wirte status to EIS database: " + ex.toString());
613                 }
614             }
615         }
616     }
617 
618     public String[] getTestMethodNames()
619     {
620         return new String[]
621                 {
622                     "module(all)"
623                 };
624     }
625 
626 //    class _FolderFilter implements FileFilter
627 //    {
628 //
629 //        private String mFolderName;
630 //
631 //        public FolderFilter(String folderName)
632 //        {
633 //            mFolderName = folderName;
634 //        }
635 //
636 //        public boolean accept_(File pathname)
637 //        {
638 //
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 //    }
654 }
655