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