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