1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 package base;
19 
20 import complexlib.ComplexTestCase;
21 import util.DynamicClassLoader;
22 import share.DescGetter;
23 import stats.OutProducerFactory;
24 import helper.ComplexDescGetter;
25 import helper.AppProvider;
26 import helper.CfgParser;
27 import share.DescEntry;
28 import share.LogWriter;
29 import stats.Summarizer;
30 import lib.TestParameters;
31 import util.PropertyName;
32 import java.io.UnsupportedEncodingException;
33 
34 /**
35  * Test base for executing a java complex test.
36  * @see base.TestBase
37  */
38 public class java_complex implements TestBase
39 {
40 
41     /**
42      * This function executes the complex tests given as parameter "-o" or "TestJob". It queries for the correspond class
43      * and creates the JobDescription.
44      * @return true of all tests run successfully, else false
45      */
executeTest(TestParameters param)46     public boolean executeTest(TestParameters param)
47     {
48         // get the test job
49         String testJob = ((String) param.get("TestJob"));
50 
51         DescGetter descGetter = new ComplexDescGetter();
52         // get the test jobs
53         DescEntry[] entries = descGetter.getDescriptionFor(testJob, null, true);
54 
55         if (entries == null) {
56             System.out.println("Couldn't get Description for Job: " + testJob);
57 
58             return false;
59         }
60 
61         return executeTest(param, entries);
62     }
63 
64     /**
65      * This function run the given DescEntry[] as ComplexTest
66      * @return true of all tests run successfully, else false
67      */
executeTest(TestParameters param, DescEntry[] entries)68     public boolean executeTest(TestParameters param, DescEntry[] entries)
69     {
70         DynamicClassLoader dcl = new DynamicClassLoader();
71         ComplexTestCase testClass = null;
72         boolean returnVal = true;
73 
74 //        the concept of the TimeOut depends on runner logs. If the runner log,
75 //        for example to start a test method, the timeout was reset. This is not
76 //        while the test itself log something like "open document...".
77 //        A property of complex test could be that it has only one test method
78 //        which works for several minutes. In this case the TimeOut get not trigger
79 //        and the office was killed.
80 //        In complex tests just use "ThreadTimeOut" as timeout.
81 
82         for (int i = 0; i < entries.length; i++)
83         {
84 
85             if (entries[i] == null)
86             {
87                 continue;
88             }
89             String iniName = entries[i].longName;
90             iniName = iniName.replace('.', '/');
91             CfgParser ini = new CfgParser(iniName + ".props");
92             ini.getIniParameters(param);
93 
94             LogWriter log = (LogWriter) dcl.getInstance((String) param.get("LogWriter"));
95 
96             AppProvider office = null;
97             if (!param.getBool("NoOffice"))
98             {
99                 try
100                 {
101                     office = (AppProvider) dcl.getInstance("helper.OfficeProvider");
102                     Object msf = office.getManager(param);
103                     if (msf == null)
104                     {
105                         returnVal = false;
106                         continue;
107                     }
108                     param.put("ServiceFactory", msf);
109                 }
110                 catch (IllegalArgumentException e)
111                 {
112                     office = null;
113                 }
114                 catch (UnsupportedEncodingException e)
115                 {
116                     office = null;
117                 }
118             }
119             log.initialize(entries[i], param.getBool(PropertyName.LOGGING_IS_ACTIVE));
120             entries[i].Logger = log;
121 
122             // create an instance
123             try
124             {
125                 testClass = (ComplexTestCase) dcl.getInstance(entries[i].longName);
126             }
127             catch (java.lang.Exception e)
128             {
129                 e.printStackTrace();
130                 return false;
131             }
132             testClass.executeMethods(entries[i], param);
133 
134             Summarizer sum = new Summarizer();
135             sum.summarizeUp(entries[i]);
136 
137             if (office != null)
138             {
139                 office.closeExistingOffice(param, false);
140             }
141 
142             LogWriter out = OutProducerFactory.createOutProducer(param);
143 
144             out.initialize(entries[i], true);
145             out.summary(entries[i]);
146             returnVal &= entries[i].State.endsWith("OK");
147         }
148         return returnVal;
149     }
150 }
151