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 
30 import basicrunner.BasicIfcTest;
31 import basicrunner.BasicTestCase;
32 
33 import com.sun.star.lang.XMultiServiceFactory;
34 
35 import helper.APIDescGetter;
36 import helper.AppProvider;
37 import helper.OfficeProvider;
38 import helper.OfficeWatcher;
39 
40 import java.io.BufferedReader;
41 import java.io.FileReader;
42 import java.io.PrintWriter;
43 
44 import java.util.Vector;
45 
46 import lib.TestCase;
47 import lib.TestEnvironment;
48 import lib.TestParameters;
49 
50 import share.DescEntry;
51 import share.DescGetter;
52 import share.LogWriter;
53 
54 import stats.OutProducerFactory;
55 import stats.Summarizer;
56 
57 import util.DynamicClassLoader;
58 
59 /**
60  * The testbase for executing basic tests.
61  *  @see lib.TestBase
62  */
63 public class basic_fat implements TestBase
64 {
65 
66     public static boolean debug = false;
67 
68     /**
69      *  Execute a test.
70      *  @param param The test parameters.
71      * @return True, if the test was executed.
72      */
73     public boolean executeTest(TestParameters param)
74     {
75         DescGetter dg = new APIDescGetter();
76         String job = (String) param.get("TestJob");
77         OfficeProvider office = null;
78         debug = param.getBool("DebugIsActive");
79 
80 
81         //get Job-Descriptions
82         System.out.print("Getting Descriptions for Job: " + job + " from ");
83 
84         DescEntry[] entries = dg.getDescriptionFor(job,
85                 (String) param.get(
86                 "DescriptionPath"),
87                 debug);
88 
89         if (entries == null)
90         {
91             System.out.println("Couldn't get Description for Job");
92 
93             return false;
94         }
95 
96         String ExclusionFile = (String) param.get("ExclusionList");
97         Vector exclusions = null;
98 
99         if (ExclusionFile != null)
100         {
101             exclusions = getExclusionList(ExclusionFile, debug);
102         }
103 
104         String conStr = (String) param.get("ConnectionString");
105         System.out.println("");
106         System.out.print("> Connecting the Office ");
107         System.out.println("With " + conStr);
108 
109         for (int l = 0; l < entries.length; l++)
110         {
111             if (entries[l].hasErrorMsg)
112             {
113                 System.out.println(entries[l].ErrorMsg);
114 
115                 continue;
116             }
117 
118             office = new OfficeProvider();
119 
120             XMultiServiceFactory msf = (XMultiServiceFactory) office.getManager(
121                     param);
122 
123             if (msf == null)
124             {
125                 return false;
126             }
127 
128             param.put("ServiceFactory", msf);
129 
130             DescEntry entry = entries[l];
131 
132             //get some helper classes
133             Summarizer sumIt = new Summarizer();
134             DynamicClassLoader dcl = new DynamicClassLoader();
135 
136             TestCase tCase = null;
137 
138             tCase = (TestCase) new BasicTestCase(entry);
139 
140             if (tCase == null)
141             {
142                 sumIt.summarizeDown(entry, entry.ErrorMsg);
143 
144                 LogWriter sumObj = OutProducerFactory.createOutProducer(param);
145                 sumObj.initialize(entry, true);
146                 sumObj.summary(entry);
147 
148                 continue;
149             }
150 
151             System.out.println("Creating: " + tCase.getObjectName());
152 
153             LogWriter log = (LogWriter) dcl.getInstance(
154                     (String) param.get("LogWriter"));
155             log.initialize(entry, true);
156             entry.UserDefinedParams = param;
157             tCase.setLogWriter((PrintWriter) log);
158 
159             try
160             {
161                 tCase.initializeTestCase(param);
162             }
163             catch (RuntimeException e)
164             {
165                 helper.ProcessHandler ph = (helper.ProcessHandler) param.get(
166                         "AppProvider");
167 
168                 if (ph != null)
169                 {
170                     OfficeWatcher ow = (OfficeWatcher) param.get("Watcher");
171 
172                     if ((ow != null) && ow.isAlive())
173                     {
174                         ow.finish = true;
175                     }
176 
177                     ph.kill();
178                     shortWait(5000);
179                 }
180 
181                 continue;
182             }
183 
184             TestEnvironment tEnv = tCase.getTestEnvironment(param);
185 
186             if (tEnv == null)
187             {
188                 sumIt.summarizeDown(entry, "Unable to create testcase");
189 
190                 LogWriter sumObj = OutProducerFactory.createOutProducer(param);
191                 sumObj.initialize(entry, true);
192                 sumObj.summary(entry);
193 
194                 helper.ProcessHandler ph = (helper.ProcessHandler) param.get(
195                         "AppProvider");
196 
197                 if (ph != null)
198                 {
199                     OfficeWatcher ow = (OfficeWatcher) param.get("Watcher");
200 
201                     if ((ow != null) && ow.isAlive())
202                     {
203                         ow.finish = true;
204                     }
205 
206                     ph.kill();
207                     shortWait(5000);
208                 }
209 
210                 continue;
211             }
212 
213             System.out.println("created " + tCase.getObjectName());
214 
215             for (int j = 0; j < entry.SubEntryCount; j++)
216             {
217                 if (!entry.SubEntries[j].isToTest)
218                 {
219                     Summarizer.summarizeDown(entry.SubEntries[j],
220                             "not part of the job");
221 
222                     continue;
223                 }
224 
225                 if ((exclusions != null) &&
226                         (exclusions.contains(entry.SubEntries[j].longName)))
227                 {
228                     Summarizer.summarizeDown(entry.SubEntries[j],
229                             "known issue");
230 
231                     continue;
232                 }
233 
234                 System.out.println("running: " +
235                         entry.SubEntries[j].entryName);
236 
237                 LogWriter ifclog = (LogWriter) dcl.getInstance(
238                         (String) param.get("LogWriter"));
239                 ifclog.initialize(entry.SubEntries[j], true);
240                 entry.SubEntries[j].UserDefinedParams = param;
241                 entry.SubEntries[j].Logger = ifclog;
242 
243                 if ((tEnv == null) || tEnv.isDisposed())
244                 {
245                     helper.ProcessHandler ph = (helper.ProcessHandler) param.get(
246                             "AppProvider");
247 
248                     if (ph != null)
249                     {
250                         office.closeExistingOffice(param, true);
251                         shortWait(5000);
252                     }
253 
254                     tEnv = getEnv(entry, param);
255                 }
256 
257                 BasicIfcTest ifc = null;
258                 lib.TestResult res = null;
259                 ifc = new BasicIfcTest(entry.SubEntries[j].longName);
260                 res = ifc.run(entry.SubEntries[j], tEnv, param);
261 
262                 sumIt.summarizeUp(entry.SubEntries[j]);
263 
264                 LogWriter sumIfc = OutProducerFactory.createOutProducer(param);
265 
266                 sumIfc.initialize(entry.SubEntries[j], true);
267                 sumIfc.summary(entry.SubEntries[j]);
268             }
269 
270             try
271             {
272                 tCase.cleanupTestCase(param);
273             }
274             catch (Exception e)
275             {
276                 log.println("TestCase already gone");
277 
278                 helper.ProcessHandler ph = (helper.ProcessHandler) param.get(
279                         "AppProvider");
280 
281                 if (ph != null)
282                 {
283                     office.closeExistingOffice(param, true);
284                     shortWait(5000);
285                 }
286             }
287 
288             sumIt.summarizeUp(entry);
289 
290             LogWriter sumObj = OutProducerFactory.createOutProducer(param);
291             sumObj.initialize(entry, true);
292             sumObj.summary(entry);
293         }
294 
295         if (entries.length > 1)
296         {
297             System.out.println();
298 
299             int counter = 0;
300             System.out.println(
301                     "Failures that appeared during scenario execution:");
302 
303             for (int i = 0; i < entries.length; i++)
304             {
305                 if (!entries[i].State.endsWith("OK"))
306                 {
307                     System.out.println("\t " + entries[i].longName);
308                     counter++;
309                 }
310             }
311 
312             System.out.println(counter + " of " + entries.length +
313                     " tests failed");
314         }
315 
316         helper.ProcessHandler ph = (helper.ProcessHandler) param.get(
317                 "AppProvider");
318 
319         if (ph != null)
320         {
321             office.closeExistingOffice(param, true);
322         }
323 
324         return true;
325     }
326 
327     protected TestEnvironment getEnv(DescEntry entry, TestParameters param)
328     {
329         DynamicClassLoader dcl = new DynamicClassLoader();
330         String officeProviderName = (String) param.get("OfficeProvider");
331         AppProvider office = (AppProvider) dcl.getInstance(officeProviderName);
332 
333         if (office == null)
334         {
335             System.out.println("ERROR: Wrong parameter 'OfficeProvider', " +
336                     " it cannot be instantiated.");
337             System.exit(-1);
338         }
339 
340         XMultiServiceFactory msf = (XMultiServiceFactory) office.getManager(
341                 param);
342 
343         if (msf == null)
344         {
345             return null;
346         }
347 
348         param.put("ServiceFactory", msf);
349 
350         TestCase tCase = (TestCase) new BasicTestCase(entry);
351 
352         System.out.println("Creating: " + tCase.getObjectName());
353 
354         LogWriter log = (LogWriter) dcl.getInstance(
355                 (String) param.get("LogWriter"));
356         log.initialize(entry, true);
357         entry.UserDefinedParams = param;
358         tCase.setLogWriter((PrintWriter) log);
359 
360         TestEnvironment tEnv = null;
361 
362         try
363         {
364             tCase.initializeTestCase(param);
365             tEnv = tCase.getTestEnvironment(param);
366         }
367         catch (java.lang.RuntimeException e)
368         {
369             System.out.println(e.getMessage());
370 
371             helper.ProcessHandler ph = (helper.ProcessHandler) param.get(
372                     "AppProvider");
373 
374             if (ph != null)
375             {
376                 office.closeExistingOffice(param, true);
377                 shortWait(5000);
378             }
379 
380             entry.ErrorMsg = e.getMessage();
381             entry.hasErrorMsg = true;
382         }
383 
384         return tEnv;
385     }
386 
387     protected void shortWait(int millis)
388     {
389         try
390         {
391             Thread.sleep(millis);
392         }
393         catch (java.lang.InterruptedException ie)
394         {
395         }
396     }
397 
398     protected Vector getExclusionList(String url, boolean debug)
399     {
400         Vector entryList = new Vector();
401         String line = "#";
402         BufferedReader exclusion = null;
403 
404         try
405         {
406             exclusion = new BufferedReader(new FileReader(url));
407         }
408         catch (java.io.FileNotFoundException fnfe)
409         {
410             if (debug)
411             {
412                 System.out.println("Couldn't find file " + url);
413             }
414 
415             return entryList;
416         }
417 
418         while (line != null)
419         {
420             try
421             {
422                 if (!line.startsWith("#"))
423                 {
424                     entryList.add(line);
425                 }
426 
427                 line = exclusion.readLine();
428             }
429             catch (java.io.IOException ioe)
430             {
431                 if (debug)
432                 {
433                     System.out.println(
434                             "Exception while reading exclusion list");
435                 }
436 
437                 return entryList;
438             }
439         }
440 
441         try
442         {
443             exclusion.close();
444         }
445         catch (java.io.IOException ioe)
446         {
447             if (debug)
448             {
449                 System.out.println("Couldn't close file " + url);
450             }
451 
452             return entryList;
453         }
454 
455         return entryList;
456     }
457 }
458