xref: /aoo41x/main/qadevOOo/runner/base/java_fat.java (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package base;
28 
29 import com.sun.star.lang.XMultiServiceFactory;
30 
31 import helper.APIDescGetter;
32 import helper.AppProvider;
33 
34 import java.io.BufferedReader;
35 import java.io.FileReader;
36 import java.io.PrintWriter;
37 
38 import java.util.Vector;
39 
40 import lib.MultiMethodTest;
41 import lib.TestCase;
42 import lib.TestEnvironment;
43 import lib.TestParameters;
44 import lib.TestResult;
45 
46 import share.DescEntry;
47 import share.DescGetter;
48 import share.LogWriter;
49 
50 import stats.OutProducerFactory;
51 import stats.Summarizer;
52 
53 import util.DynamicClassLoader;
54 
55 /**
56  *
57  * this class handles tests written in java and running on a fat Office
58  */
59 public class java_fat implements TestBase
60 {
61 
62     private static boolean m_isDebug = false;
63     private static boolean keepdocument = false;
64     private static boolean logging = true;
65     private static boolean newOffice = false;
66     private DynamicClassLoader m_aDynamicClassLoader = null;
67 
68     private lib.TestParameters m_aParams;
69     private AppProvider m_aOffice;
70 
71     public boolean executeTest(lib.TestParameters _aParams)
72         {
73             m_aParams = _aParams;
74 
75             m_aDynamicClassLoader = new DynamicClassLoader();
76 
77             DescGetter dg = new APIDescGetter();
78             String job = (String) m_aParams.get("TestJob");
79             String ExclusionFile = (String) m_aParams.get("ExclusionList");
80             Vector exclusions = null;
81             boolean retValue = true;
82             m_isDebug = m_aParams.getBool("DebugIsActive");
83             logging = m_aParams.getBool("LoggingIsActive");
84             keepdocument = m_aParams.getBool("KeepDocument");
85             newOffice = m_aParams.getBool(util.PropertyName.NEW_OFFICE_INSTANCE);
86             if (keepdocument)
87             {
88                 System.setProperty("KeepDocument", "true");
89             }
90             if (ExclusionFile != null)
91             {
92                 exclusions = getExclusionList(ExclusionFile, m_isDebug);
93             }
94             //get Job-Descriptions
95             // System.out.println("Getting Descriptions for Job: " + job);
96 
97             String sDescriptionPath = (String) m_aParams.get("DescriptionPath");
98             DescEntry[] entries = dg.getDescriptionFor(job, sDescriptionPath, m_isDebug);
99 
100             // System.out.println();
101 
102             if (entries == null)
103             {
104                 System.out.println("Couldn't get Description for Job: " + job);
105 
106                 return false;
107             }
108 
109 //        String officeProviderName = (String) m_aParams.get("OfficeProvider");
110 //        AppProvider office = (AppProvider) m_aDynamicClassLoader.getInstance(officeProviderName);
111 //
112 //        if (office == null) {
113 //            System.out.println("ERROR: Wrong parameter 'OfficeProvider', " +
114 //                               " it cannot be instantiated.");
115 //            System.exit(-1);
116 //        }
117 
118             m_aOffice = startOffice(m_aParams);
119 
120             boolean firstRun = true;
121 
122             // Run through all entries (e.g. sw.SwXBookmark.*)
123 
124             for (int l = 0; l < entries.length; l++)
125             {
126                 DescEntry entry = entries[l];
127 
128                 if (entry == null)
129                 {
130                     continue;
131                 }
132 
133                 if (entry.hasErrorMsg)
134                 {
135                     System.out.println(entries[l].ErrorMsg);
136                     retValue = false;
137                     continue;
138                 }
139 
140                 if (!firstRun && newOffice)
141                 {
142                     if (!m_aOffice.closeExistingOffice(m_aParams, true))
143                     {
144                         m_aOffice.disposeManager(m_aParams);
145                     }
146                     startOffice(m_aParams);
147                 }
148                 firstRun = false;
149 
150                 XMultiServiceFactory msf = (XMultiServiceFactory) m_aParams.getMSF();
151 
152                 if (msf == null)
153                 {
154                     retValue = false;
155                     continue;
156                 }
157 
158                 //get some helper classes
159                 Summarizer sumIt = new Summarizer();
160                 TestCase tCase = getTestCase(entry);
161                 if (tCase == null)
162                 {
163                     continue;
164                 }
165 
166 //                if (m_isDebug)
167 //                {
168 //                    System.out.println("sleeping 2 seconds..");
169 //                }
170                 util.utils.shortWait(2000);
171 
172                 System.out.println("Creating: " + entry.entryName);
173 
174                 LogWriter log = (LogWriter) m_aDynamicClassLoader.getInstance((String) m_aParams.get("LogWriter"));
175                 log.initialize(entry, logging);
176                 entry.UserDefinedParams = m_aParams;
177 
178                 tCase.setLogWriter((PrintWriter) log);
179                 tCase.initializeTestCase(m_aParams);
180 
181                 TestEnvironment tEnv = getTestEnvironment(tCase, entry);
182                 if (tEnv == null)
183                 {
184                     continue;
185                 }
186 
187                 final String sObjectName = tCase.getObjectName();
188                 // System.out.println(sObjectName + " recreated ");
189 
190                 for (int j = 0; j < entry.SubEntryCount; j++)
191                 {
192                     DescEntry aSubEntry = entry.SubEntries[j];
193                     final boolean bIsToTest = aSubEntry.isToTest;
194                     if (!bIsToTest)
195                     {
196                         Summarizer.summarizeDown(aSubEntry, "not part of the job");
197                         continue;
198                     }
199 
200                     // final String sEntryName = aSubEntry.entryName;
201                     final String sLongEntryName = aSubEntry.longName;
202 
203                     if ((exclusions != null) && (exclusions.contains(sLongEntryName)))
204                     {
205                         Summarizer.summarizeDown(aSubEntry, "known issue");
206                         continue;
207                     }
208 
209                     // System.out.println("running: '" + sLongEntryName + "' testcode: [" + sEntryName + "]");
210                     // this will shown in test itself
211 
212                     LogWriter ifclog = (LogWriter) m_aDynamicClassLoader.getInstance( (String) m_aParams.get("LogWriter"));
213 
214                     ifclog.initialize(aSubEntry, logging);
215                     aSubEntry.UserDefinedParams = m_aParams;
216                     aSubEntry.Logger = ifclog;
217 
218                     if ((tEnv == null) || tEnv.isDisposed())
219                     {
220                         closeExistingOffice();
221                         tEnv = getEnv(entry, m_aParams);
222                         tCase = tEnv.getTestCase();
223                     }
224 
225                     // MultiMethodTest ifc = null;
226                     lib.TestResult res = null;
227 
228                     // run the interface test twice if it failed.
229                     int countInterfaceTestRun = 0;
230                     boolean finished = false;
231                     while (!finished)
232                     {
233                         try
234                         {
235                             countInterfaceTestRun++;
236                             finished = true;
237                             res = executeInterfaceTest(aSubEntry, tEnv, m_aParams);
238                         }
239                         catch (IllegalArgumentException iae)
240                         {
241                             System.out.println("Couldn't load class " + aSubEntry.entryName);
242                             System.out.println("**** " + iae.getMessage() + " ****");
243                             Summarizer.summarizeDown(aSubEntry, iae.getMessage());
244                         }
245                         catch (java.lang.NoClassDefFoundError iae)
246                         {
247                             System.out.println("Couldn't load class " + aSubEntry.entryName);
248                             System.out.println("**** " + iae.getMessage() + " ****");
249                             Summarizer.summarizeDown(aSubEntry, iae.getMessage());
250                         }
251                         catch (java.lang.RuntimeException e)
252                         {
253                             closeExistingOffice();
254                             tEnv = getEnv(entry, m_aParams);
255                             tCase = tEnv.getTestCase();
256                             if (countInterfaceTestRun < 2)
257                             {
258                                 finished = false;
259                             }
260                             else
261                             {
262                                 Summarizer.summarizeDown(aSubEntry, e.toString() + ".FAILED");
263                             }
264                         }
265                     }
266                     setState(aSubEntry, res);
267 
268                     sumIt.summarizeUp(aSubEntry);
269 
270                     LogWriter sumIfc = OutProducerFactory.createOutProducer(m_aParams);
271                     aSubEntry.UserDefinedParams = m_aParams;
272                     sumIfc.initialize(aSubEntry, logging);
273                     sumIfc.summary(aSubEntry);
274                 }
275 
276                 try
277                 {
278                     if (!keepdocument)
279                     {
280                         tCase.cleanupTestCase(m_aParams);
281                     }
282                 }
283                 catch (Exception e)
284                 {
285                     System.err.println( "couldn't cleanup:" + e.toString() );
286                 }
287                 catch (java.lang.NoClassDefFoundError e)
288                 {
289                     System.err.println( "couldn't cleanup:" + e.toString() );
290                 }
291 
292                 sumIt.summarizeUp(entry);
293 
294                 LogWriter sumObj = OutProducerFactory.createOutProducer(m_aParams);
295 
296                 sumObj.initialize(entry, logging);
297                 sumObj.summary(entry);
298             }
299 
300             if (entries.length > 0)
301             {
302                 System.out.println();
303 
304                 int counter = 0;
305                 System.out.println("Failures that appeared during scenario execution:");
306 
307                 for (int i = 0; i < entries.length; i++)
308                 {
309                     final String sState = entries[i].State;
310                     if (!sState.endsWith("OK"))
311                     {
312                         System.out.println("\t " + entries[i].longName);
313                         counter++;
314                     }
315                 }
316 
317                 System.out.println(counter + " of " + entries.length + " tests failed");
318                 if (counter != 0) {
319                     retValue = false;
320                 }
321             }
322 
323             closeExistingOffice();
324             return retValue;
325         }
326 
327 //
328     private TestEnvironment getTestEnvironment(TestCase tCase, DescEntry entry)
329     {
330         TestEnvironment tEnv = null;
331 
332         try
333         {
334             tEnv = tCase.getTestEnvironment(m_aParams);
335         }
336         catch (Exception e)
337         {
338             System.out.println("Exception while creating " + tCase.getObjectName());
339             System.out.println("Message " + e.getMessage());
340             e.printStackTrace();
341             tEnv = null;
342         }
343         catch (java.lang.UnsatisfiedLinkError e)
344         {
345             System.out.println("Exception while creating " + tCase.getObjectName());
346             System.out.println("Message " + e.getMessage());
347             tEnv = null;
348         }
349         catch (java.lang.NoClassDefFoundError e)
350         {
351             System.out.println("Exception while creating " + tCase.getObjectName());
352             System.out.println("Message " + e.getMessage());
353             tEnv = null;
354         }
355 
356         if (tEnv == null)
357         {
358             Summarizer.summarizeDown(entry, "Couldn't create " + tCase.getObjectName());
359 
360             LogWriter sumObj = OutProducerFactory.createOutProducer(m_aParams);
361             entry.UserDefinedParams = m_aParams;
362             sumObj.initialize(entry, logging);
363             sumObj.summary(entry);
364         }
365         return tEnv;
366     }
367     // -------------------------------------------------------------------------
368     private TestCase getTestCase(DescEntry _aEntry)
369     {
370         TestCase tCase = null;
371 
372         try
373         {
374             tCase = (TestCase) m_aDynamicClassLoader.getInstance("mod._" + _aEntry.entryName);
375         }
376         catch (java.lang.IllegalArgumentException ie)
377         {
378             _aEntry.ErrorMsg = ie.getMessage();
379             _aEntry.hasErrorMsg = true;
380         }
381         catch (java.lang.NoClassDefFoundError ie)
382         {
383             _aEntry.ErrorMsg = ie.getMessage();
384             _aEntry.hasErrorMsg = true;
385         }
386 
387         if (tCase == null)
388         {
389             Summarizer.summarizeDown(_aEntry, _aEntry.ErrorMsg);
390 
391             LogWriter sumObj = OutProducerFactory.createOutProducer(m_aParams);
392             _aEntry.UserDefinedParams = m_aParams;
393             sumObj.initialize(_aEntry, logging);
394             sumObj.summary(_aEntry);
395         }
396         return tCase;
397 }
398 
399     private void setState(DescEntry aSubEntry, lib.TestResult res)
400     {
401         if (res != null)
402         {
403             for (int k = 0; k < aSubEntry.SubEntryCount; k++)
404             {
405                 DescEntry aSubSubEntry = aSubEntry.SubEntries[k];
406                 if (res.hasMethod( aSubSubEntry.entryName))
407                 {
408                     aSubSubEntry.State = res.getStatusFor(aSubSubEntry.entryName).toString();
409                 }
410             }
411         }
412     }
413 
414     private TestEnvironment getEnv(DescEntry entry, TestParameters param)
415         {
416 //        if (m_aDynamicClassLoader == null)
417 //            m_aDynamicClassLoader = new DynamicClassLoader();
418 //        String officeProviderName = (String) m_aParams.get("OfficeProvider");
419 //        AppProvider office = (AppProvider) m_aDynamicClassLoader.getInstance(officeProviderName);
420 //
421 //        if (office == null) {
422 //            System.out.println("ERROR: Wrong parameter 'OfficeProvider', " +
423 //                               " it cannot be instantiated.");
424 //            System.exit(-1);
425 //        }
426 //
427 //        XMultiServiceFactory msf = (XMultiServiceFactory) office.getManager(
428 //                                           m_aParams);
429 //
430 //        if (msf == null) {
431 //            return null;
432 //        }
433 //
434 //        m_aParams.put("ServiceFactory", msf);
435 
436             // AppProvider office = startOffice(m_aParams);
437 
438             TestCase tCase = null;
439 
440             try
441             {
442                 tCase = (TestCase) m_aDynamicClassLoader.getInstance("mod._" + entry.entryName);
443             }
444             catch (java.lang.IllegalArgumentException ie)
445             {
446                 entry.ErrorMsg = ie.getMessage();
447                 entry.hasErrorMsg = true;
448             }
449             catch (java.lang.NoClassDefFoundError ie)
450             {
451                 entry.ErrorMsg = ie.getMessage();
452                 entry.hasErrorMsg = true;
453             }
454 
455             System.out.println("Creating: " + entry.entryName);
456 
457             entry.UserDefinedParams = param;
458 
459             LogWriter log = (LogWriter) m_aDynamicClassLoader.getInstance((String) param.get("LogWriter"));
460             log.initialize(entry, logging);
461             tCase.setLogWriter((PrintWriter) log);
462 
463             try
464             {
465                 tCase.initializeTestCase(param);
466                 return tCase.getTestEnvironment(param);
467             }
468             catch (com.sun.star.lang.DisposedException de)
469             {
470                 System.out.println("Office disposed");
471                 closeExistingOffice();
472                 throw de;
473             }
474             catch (lib.StatusException e)
475             {
476                 System.out.println(e.getMessage());
477 
478                 closeExistingOffice();
479 
480                 entry.ErrorMsg = e.getMessage();
481                 entry.hasErrorMsg = true;
482                 throw e;
483             }
484         }
485 
486     private void closeExistingOffice()
487         {
488             helper.ProcessHandler ph = (helper.ProcessHandler) m_aParams.get("AppProvider");
489 
490             if (ph != null)
491             {
492                 m_aOffice.closeExistingOffice(m_aParams, true);
493                 util.utils.shortWait(5000);
494             }
495 
496         }
497 
498 //    private void shortWait(int millis)
499 //        {
500 //            try
501 //            {
502 //                Thread.sleep(millis);
503 //            }
504 //            catch (java.lang.InterruptedException ie)
505 //            {
506 //            }
507 //        }
508 
509     private Vector getExclusionList(String url, boolean debug)
510         {
511             Vector entryList = new Vector();
512             String line = "#";
513             BufferedReader exclusion = null;
514 
515             try
516             {
517                 exclusion = new BufferedReader(new FileReader(url));
518             }
519             catch (java.io.FileNotFoundException fnfe)
520             {
521                 if (debug)
522                 {
523                     System.out.println("Couldn't find file " + url);
524                 }
525 
526                 return entryList;
527             }
528 
529             while (line != null)
530             {
531                 try
532                 {
533                     if (!line.startsWith("#") && (line.length() > 1))
534                     {
535                         entryList.add(line.trim());
536                     }
537 
538                     line = exclusion.readLine();
539                 }
540                 catch (java.io.IOException ioe)
541                 {
542                     if (debug)
543                     {
544                         System.out.println("Exception while reading exclusion list");
545                     }
546 
547                     return entryList;
548                 }
549             }
550 
551             try
552             {
553                 exclusion.close();
554             }
555             catch (java.io.IOException ioe)
556             {
557                 if (debug)
558                 {
559                     System.out.println("Couldn't close file " + url);
560                 }
561 
562                 return entryList;
563             }
564 
565             return entryList;
566         }
567 
568     private TestResult executeInterfaceTest(
569         DescEntry entry, TestEnvironment tEnv, TestParameters param)
570         throws IllegalArgumentException, java.lang.NoClassDefFoundError
571         {
572             MultiMethodTest ifc = (MultiMethodTest) m_aDynamicClassLoader.getInstance(entry.entryName);
573             return ifc.run(entry, tEnv, param);
574         }
575 
576     private AppProvider startOffice(lib.TestParameters param)
577         {
578 
579             if (m_aDynamicClassLoader == null)
580             {
581                 m_aDynamicClassLoader = new DynamicClassLoader();
582             }
583 
584             String officeProviderName = (String) param.get("OfficeProvider");
585             AppProvider office = (AppProvider) m_aDynamicClassLoader.getInstance(officeProviderName);
586 
587             if (office == null)
588             {
589                 System.out.println("ERROR: Wrong parameter 'OfficeProvider', " + " it cannot be instantiated.");
590                 System.exit(-1);
591             }
592 
593             XMultiServiceFactory msf = (XMultiServiceFactory) office.getManager(param);
594 
595             if (msf != null)
596             {
597                 param.put("ServiceFactory", msf);
598             }
599 
600             return office;
601         }
602 }
603