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