1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 /** complex tests to check the UNO-API
25  **/
26 package complex.unoapi;
27 
28 // imports
29 import base.java_complex;
30 import complexlib.ComplexTestCase;
31 import helper.OfficeProvider;
32 import helper.ParameterNotFoundException;
33 import helper.ProcessHandler;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import helper.BuildEnvTools;
36 import helper.ComplexDescGetter;
37 import helper.CwsDataExchangeImpl;
38 import java.io.File;
39 // import java.io.FileFilter;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.Set;
44 import share.DescEntry;
45 import util.PropertyName;
46 import util.utils;
47 
48 /**
49  * This Complex Test will test the UNO-API by calling dmake in <B>$MODULE</B>/qa/unoapi<p>
50  * This test depends on some requirements:
51  * The <B>TestJob</B> is <b>-o complex.unoapi.CheckModuleAPI::module(<CODE>MODULE</CODE>)</b><p> where <CODE>MODULE</CODE>
52  * could be the following:<p>
53  * <ul>
54  *  <li><b>all</b>  iterates over <CODE>SRC_ROOT</CODE> and call <CODE>dmake</CODE> in all qa/unoapi folder</li><p>
55  *  <li><b>$module</b>  call dmake in $module/qa/unoapi folder</li><p>
56  *  <li><b>$module1,$module2,...</b>    call dmake in $module1/qa/unoapi folder then in $module2/qa/unoapi folder and so on</li><p>
57  * </ul><p>
58  *
59  * Also you have to fill the following parameter:
60  * <ul>
61  *  <li><b>SHELL</b>:      fill this parameter with a shell</li>
62  * </ul>
63  *
64  */
65 public class CheckModuleAPI extends ComplexTestCase
66 {
67 
68     private static String mSRC_ROOT = null;
69     private static boolean mIsInitialized = false;
70     private final static boolean mContinue = true;
71     private static boolean mDebug = false;
72     private static BuildEnvTools bet = null;
73 
74     /**
75      * Initialize the test environment.
76      * This method checks for all necessary parameter and exit if not all parameter are set.
77      *
78      * Further this method starts an office instance and gets the office some more time to start. This is because
79      * some freshly installed offices don not have such a user tree. The office will create it on its first start,
80      * but this will take some time.
81      * Note: This functionality is only reasonable with parameter <CODE>-noOffice true</CODE>
82      */
before()83     public void before()
84     {
85 
86         if (!mIsInitialized)
87         {
88             mIsInitialized = true;
89 
90             try
91             {
92 
93                 bet = new BuildEnvTools(param, log);
94 
95             }
96             catch (ParameterNotFoundException ex)
97             {
98                 this.failed(ex.toString(), false);
99             }
100 
101             mSRC_ROOT = bet.getSrcRoot();
102 
103             mDebug = param.getBool(PropertyName.DEBUG_IS_ACTIVE);
104 
105         }
106     }
107 
108     /**
109      * let API tests run.
110      * @param module
111      */
checkModule(String module)112     public void checkModule(String module)
113     {
114         log.println(utils.getDateTime() + ": start testing module '" + module + "'");
115 
116         log.println(utils.getDateTime() + "start new Office instance...");
117 
118         // TODO: is Office started with this program?
119         final OfficeProvider officeProvider = new OfficeProvider();
120 
121         log.println(utils.getDateTime() + "Receiving the ServiceManager of the Office ");
122         final XMultiServiceFactory msf = (XMultiServiceFactory) officeProvider.getManager(param);
123         assure("couldnot get ServiceFarcotry", msf != null, mContinue);
124         param.put("ServiceFactory", msf);
125 
126         final String sep = System.getProperty("file.separator");
127         final String sUnoapi = getModulePath(module);
128         final File fUnoapi = new File(sUnoapi);
129         final String sMakeFile = sUnoapi + sep + "makefile.mk";
130         final File fMakeFile = new File(sMakeFile);
131         assure("ERROR: could not find makefile: '" + sMakeFile + "'", fMakeFile.exists(), mContinue);
132 
133         final String[] commands = getDmakeCommands(sUnoapi);
134 
135         final ProcessHandler procHdl = bet.runCommandsInEnvironmentShell(commands, fUnoapi, 0);
136         log.println("exit code of dmake: " + procHdl.getExitCode());
137         String test = procHdl.getOutputText();
138         test += procHdl.getErrorText();
139 //        if (mDebug) {
140 //            log.println("---> Output of dmake:");
141 //            log.println(procHdl.getOutputText());
142 //            log.println("<--- Output of dmake:");
143 //            log.println("---> Error output of dmake:");
144 //            log.println(procHdl.getErrorText());
145 //            log.println("<--- Error output of dmake:");
146 //        }
147         assure("module '" + module + "' failed", verifyOutput(test), mContinue);
148         log.println(utils.getDateTime() + " module '" + module + "': kill existing office...");
149 
150         // TODO: how to check if the office is not started with this process.
151         boolean bNoOffice = param.getBool("NoOffice");
152         if (!bNoOffice)
153         {
154             try
155             {
156                 officeProvider.closeExistingOffice(param, true);
157             }
158             catch (java.lang.UnsatisfiedLinkError exception)
159             {
160                 log.println("Office seems not to be running");
161             }
162         }
163     }
getQaUnoApiPath(String srcRoot, String _sModul)164     private String getQaUnoApiPath(String srcRoot, String _sModul)
165     {
166         File aFile = new File(srcRoot);
167         if (!aFile.exists())
168         {
169             System.out.println("ERROR: srcRoot '" + srcRoot + "' does not exist.");
170             return null;
171         }
172         String sModulePath = srcRoot;
173         sModulePath += File.separator;
174         sModulePath += _sModul;
175 
176         File aModulePath = new File(sModulePath);
177         if (! aModulePath.exists())
178         {
179             aModulePath = new File(sModulePath + ".lnk");
180             if (! aModulePath.exists())
181             {
182                 aModulePath = new File(sModulePath + ".link");
183                 if (! aModulePath.exists())
184                 {
185                     // module does not exist.
186                     return null;
187                 }
188             }
189         }
190         sModulePath = aModulePath.getAbsolutePath();
191         sModulePath += File.separator;
192         sModulePath += "qa";
193         sModulePath += File.separator;
194         sModulePath += "unoapi";
195         File aModulePathQaUnoApi = new File(sModulePath);
196         if (aModulePathQaUnoApi.exists())
197         {
198             return aModulePathQaUnoApi.getAbsolutePath();
199         }
200         return null;
201     }
doesQaUnoApiFolderExist(String srcRoot, String _sModul)202     private boolean doesQaUnoApiFolderExist(String srcRoot, String _sModul)
203     {
204         if (getQaUnoApiPath(srcRoot, _sModul) != null)
205         {
206             return true;
207         }
208         return false;
209     }
210 /*
211  private boolean doesQaUnoApiFolderExist(File srcRoot)
212     {
213         final FolderFilter qaFilter = new FolderFilter("qa");
214         final File[] qaTree = srcRoot.listFiles(qaFilter);
215         if (qaTree != null)
216         {
217             for (int j = 0; j < qaTree.length; j++)
218             {
219                 final File qaFolder = qaTree[j];
220                 final FolderFilter apiFilter = new FolderFilter("unoapi");
221                 final File[] apiTree = qaFolder.listFiles(apiFilter);
222                 if (apiTree != null && apiTree.length > 0)
223                 {
224                     return true;
225                 }
226             }
227         }
228         return false;
229     }
230 */
231 
getAllModuleCommand()232     private String[] getAllModuleCommand()
233     {
234         String[] checkModules;
235 
236         final String[] names = getModulesFromSourceRoot();
237         checkModules = getCheckModuleCommand(names);
238 
239         return checkModules;
240     }
241 
getCheckModuleCommand(String[] names)242     private String[] getCheckModuleCommand(String[] names)
243     {
244         String[] checkModules;
245         checkModules = new String[names.length];
246 
247         for (int i = 0; i < names.length; i++)
248         {
249             // if a module is not added to a cws it contains a dot in its name (forms.lnk)
250             if (names[i].indexOf(".") != -1)
251             {
252                 checkModules[i] = "checkModule(" + names[i].substring(0, names[i].indexOf(".")) + ")";
253             }
254             else
255             {
256                 checkModules[i] = "checkModule(" + names[i] + ")";
257             }
258         }
259         return checkModules;
260     }
261 
getDmakeCommands(String sUnoapi)262     private String[] getDmakeCommands(String sUnoapi)
263     {
264 
265         String[] cmdLines = null;
266         final String platform = (String) param.get(PropertyName.OPERATING_SYSTEM);
267         log.println("prepare command for platform " + platform);
268 
269         if (platform.equals(PropertyName.WNTMSCI))
270         {
271             if (param.getBool(PropertyName.CYGWIN))
272             {
273                 // cygwin stuff
274                 cmdLines = new String[]
275                         {
276                             "cd `cygpath '" + sUnoapi.replaceAll("\\\\", "\\\\\\\\") + "'`",
277                             "dmake"
278                         };
279             }
280             else
281             {
282                 // old 4NT
283                 cmdLines = new String[]
284                         {
285                             "cdd " + sUnoapi,
286                             "dmake"
287                         };
288             }
289         }
290         else
291         {
292             // unix
293             cmdLines = new String[]
294                     {
295                         "cd " + sUnoapi,
296                         "dmake"
297                     };
298         }
299         return cmdLines;
300     }
301 
getCwsModuleCommand()302     private String[] getCwsModuleCommand()
303     {
304         String[] checkModules;
305         final String version = (String) param.get(PropertyName.VERSION);
306         String[] names = null;
307         if (version.startsWith("cws_"))
308         {
309             try
310             {
311                 // cws version: all added modules must be tested
312                 final String cws = version.substring(4, version.length());
313                 final CwsDataExchangeImpl cde = new CwsDataExchangeImpl(cws, param, log);
314                 final ArrayList addedModules = cde.getModules();
315 
316                 final ArrayList moduleNames = new ArrayList();
317                 Iterator iterator = addedModules.iterator();
318                 while (iterator.hasNext())
319                 {
320                     String sModuleName = (String) iterator.next();
321                     // String sFilename = mSRC_ROOT; //  + File.separator + sModuleName;
322                     // final File sourceRoot = new File(sFilename);
323                     if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
324                     {
325                         moduleNames.add(sModuleName);
326                     }
327                 }
328                 names = (String[]) moduleNames.toArray(new String[0]);
329             }
330             catch (ParameterNotFoundException ex)
331             {
332                 this.failed(ex.toString(), false);
333             }
334 
335 
336         }
337         else
338         {
339             // major version: all modules must be tested
340             names = getModulesFromSourceRoot();
341         }
342         checkModules = getCheckModuleCommand(names);
343 
344         return checkModules;
345     }
346 
getDefinedModuleCommand(String module)347     private String[] getDefinedModuleCommand(String module)
348     {
349         String[] checkModules = null;
350         // list of modules to test: (sw,sc,sd)
351         if (module.indexOf(",") != -1)
352         {
353             final String[] names = module.split(",");
354             checkModules = new String[names.length];
355             for (int i = 0; i < names.length; i++)
356             {
357                 final String moduleName = names[i].trim();
358 
359 //                File sourceRoot = new File(mSRC_ROOT + File.separator + moduleName);
360 //                if (!sourceRoot.exists())
361 //                {
362 //                    sourceRoot = new File(mSRC_ROOT + File.separator + moduleName + ".lnk");
363 //                }
364 
365                 if (doesQaUnoApiFolderExist(mSRC_ROOT, moduleName))
366                 {
367                     checkModules[i] = "checkModule(" + moduleName + ")";
368                 }
369             }
370         }
371         else
372         {
373 //            File sourceRoot = new File(mSRC_ROOT + File.separator + module);
374 //            if (!sourceRoot.exists())
375 //            {
376 //                sourceRoot = new File(mSRC_ROOT + File.separator + module + ".lnk");
377 //            }
378             if (doesQaUnoApiFolderExist(mSRC_ROOT, module))
379             {
380                 checkModules = new String[]
381                         {
382                             "checkModule(" + module + ")"
383                         };
384             }
385         }
386         return checkModules;
387     }
388 
getModulePath(String module)389     private String getModulePath(String module)
390     {
391 
392         // String sUnoapi = null;
393         // final String sep = System.getProperty("file.separator");
394         // final File srcRoot = new File(mSRC_ROOT);
395 
396         // final FolderFilter qaFilter = new FolderFilter(module);
397         // final File[] moduleTree = srcRoot.listFiles(qaFilter);
398 //        if (moduleTree != null)
399 //        {
400 //            if (mDebug)
401 //            {
402 //                log.println("moduleTree length:" + moduleTree.length);
403 //                log.println("moduleTree: " + moduleTree[0].getAbsolutePath());
404 //            }
405 //            if (moduleTree != null)
406 //            {
407 //                sUnoapi = moduleTree[0].getAbsolutePath() + sep + "qa" + sep + "unoapi";
408 //            }
409 //        }
410         String sUnoapi = getQaUnoApiPath(mSRC_ROOT, module);
411         return sUnoapi;
412     }
413 
414     /**
415     Some modules contain more than one project. This method translates given project parameter to the
416      *  corresponding module name.
417      *
418      * fwk -> framework
419      * fwl -> framework
420      * sch -> chart2
421      * lnn -> lingu
422      * lng -> linguistic
423      * sfx -> sfx2
424      * sm -> starmath
425      */
getTranslatedNames(String module)426     private String getTranslatedNames(String module)
427     {
428 
429         final HashMap aModuleHashMap = new HashMap();
430 
431         aModuleHashMap.put("fwk", "framework");
432         aModuleHashMap.put("fwl", "framework");
433         aModuleHashMap.put("sch", "chart2");
434         aModuleHashMap.put("lnn", "lingu");
435         aModuleHashMap.put("lng", "linguistic");
436         aModuleHashMap.put("sfx", "sfx2");
437         aModuleHashMap.put("sm", "starmath");
438 
439         // it could the that the parameter looks like "fwk,fwl". This results in double "framework,framework".
440         // The following loop correct this to only one "framework"
441 
442         final Set keys = aModuleHashMap.keySet();
443         final Iterator iterator = keys.iterator();
444         while (iterator.hasNext())
445         {
446 
447             final String key = (String) iterator.next();
448             final String value = (String) aModuleHashMap.get(key);
449 
450             module = module.replaceAll(key, value);
451 
452             final int count = module.split(value).length;
453             if (count > 2)
454             {
455                 for (int i = 2; i < count; i++)
456                 {
457                     module.replaceFirst("," + value, "");
458                 }
459 
460             }
461         }
462         return module;
463     }
464 
verifyOutput(String output)465     private boolean verifyOutput(String output)
466     {
467 
468         log.println("verify output...");
469         boolean ok = false;
470         final String[] outs = output.split("\n");
471 
472         for (int i = 0; i < outs.length; i++)
473         {
474             final String line = outs[i];
475             if (line.matches("[0-9]+? of [0-9]+? tests failed"))
476             {
477                 log.println("mached line: " + line);
478                 if (line.matches("0 of [0-9]+? tests failed"))
479                 {
480                     ok = true;
481                     log.println("Module passed OK");
482                 }
483                 else
484                 {
485                     log.println("Module passed FAILED");
486                 }
487             }
488         }
489 
490         if (!ok)
491         {
492             log.println("ERROR: could not find '0 of [0-9]+? tests failed' in output");
493         }
494 
495         return ok;
496     }
497 
getModulesFromSourceRoot()498     private String[] getModulesFromSourceRoot()
499     {
500         log.println("**** run module tests over all modules ****");
501 
502         log.println("search for qa/unoapi foldres in all modules based in ");
503         log.println("'" + mSRC_ROOT + "'");
504 
505         final ArrayList moduleNames = new ArrayList();
506         final File sourceRoot = new File(mSRC_ROOT);
507         final File[] sourceTree = sourceRoot.listFiles();
508 
509 //        assure("Could not find any files in SOURCE_ROOT=" + mSRC_ROOT, sourceTree != null, false);
510 
511         for (int i = 0; i < sourceTree.length; i++)
512         {
513             final File moduleName = sourceTree[i];
514             String sModuleName = moduleName.getName(); // (String)moduleNames.get(i);
515             if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
516             {
517                 // addIfQaUnoApiFolderExist(moduleName, moduleNames);
518                 moduleNames.add(sModuleName);
519             }
520         }
521 
522         final String[] names = (String[]) moduleNames.toArray(new String[0]);
523         return names;
524     }
525 
526     /**
527      * This function generates a list of modules to test and call <CODE>checkModule</CODE> for every module.
528      * <p>
529      *
530      * @param module names to test. This could be
531      * <ul>
532      * <li>a comma separated list of modules like 'sw,sc,sd'</li>
533      * <li>'all' to test all modules </li>
534      * <li>'auto' to check only modules which are added to the ChildWorkSpace</li>
535      * </ul>
536      */
module(String module)537     public void module(String module)
538     {
539 
540         String[] checkModules;
541         final ComplexDescGetter desc = new ComplexDescGetter();
542         DescEntry entry = null;
543         module = module.trim();
544 
545         /*
546         all: check all modules which contains a qa/unoapi folder
547         auto: check all modules which contains a qa/unoapi folder except the module is not added
548          */
549         if (module.equals("all"))
550         {
551             checkModules = getAllModuleCommand();
552         }
553         else if (module.equals("auto"))
554         {
555             checkModules = getCwsModuleCommand();
556         }
557         else
558         {
559             module = getTranslatedNames(module);
560             checkModules = getDefinedModuleCommand(module);
561         }
562 
563         if (checkModules != null && checkModules.length > 0)
564         {
565 
566             entry = desc.createTestDesc("complex.unoapi.CheckModuleAPI", "complex.unoapi.CheckModuleAPI", checkModules,
567                     log);
568 
569             final java_complex complex = new java_complex();
570 
571             log.println("********** start test *************");
572             final boolean result = complex.executeTest(param, new DescEntry[] { entry });
573             log.println("********** end test *************");
574 
575             assure("CheckModuleAPI.module(" + module + ") PASSED.FAILED", result);
576 
577         }
578         else
579         {
580             log.println("No modules containing qa/unoapi folder found => OK");
581             state = true;
582         }
583 
584         setUnoApiCwsStatus(state);
585 
586     }
587 
setUnoApiCwsStatus(boolean status)588     private void setUnoApiCwsStatus(boolean status)
589     {
590 
591         if (!param.getBool(PropertyName.NO_CWS_ATTACH))
592         {
593 
594             final String version = (String) param.get(PropertyName.VERSION);
595             if (version.startsWith("cws_"))
596             {
597                 try
598                 {
599 
600                     // cws version: all added modules must be tested
601                     final String cws = version.substring(4, version.length());
602                     final CwsDataExchangeImpl cde = new CwsDataExchangeImpl(cws, param, log);
603                     cde.setUnoApiCwsStatus(status);
604                 }
605                 catch (ParameterNotFoundException ex)
606                 {
607                     log.println("ERROR: could not wirte status to EIS database: " + ex.toString());
608                 }
609             }
610         }
611     }
612 
getTestMethodNames()613     public String[] getTestMethodNames()
614     {
615         return new String[]
616                 {
617                     "module(all)"
618                 };
619     }
620 
621 //    class _FolderFilter implements FileFilter
622 //    {
623 //
624 //        private String mFolderName;
625 //
626 //        public FolderFilter(String folderName)
627 //        {
628 //            mFolderName = folderName;
629 //        }
630 //
631 //        public boolean accept_(File pathname)
632 //        {
633 //
634 //            boolean found = false;
635 //            if (pathname.isDirectory())
636 //            {
637 //                if (pathname.getName().equals(mFolderName))
638 //                {
639 //                    found = true;
640 //                }
641 //                else if (pathname.getName().equals(mFolderName + ".lnk"))
642 //                {
643 //                    found = true;
644 //                }
645 //            }
646 //            return found;
647 //        }
648 //    }
649 }
650