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 complexlib.ComplexTestCase;
26 import util.DynamicClassLoader;
27 import share.DescGetter;
28 import stats.OutProducerFactory;
29 import helper.ComplexDescGetter;
30 import helper.AppProvider;
31 import helper.CfgParser;
32 import share.DescEntry;
33 import share.LogWriter;
34 import stats.Summarizer;
35 import lib.TestParameters;
36 import util.PropertyName;
37 
38 /**
39  * Test base for executing a java complex test.
40  * @see base.TestBase
41  */
42 public class java_complex implements TestBase
43 {
44 
45     /**
46      * This function executes the complex tests given as parameter "-o" or "TestJob". It querys for the correspond class
47      * and crates the JobDescription.
48      * @param param
49      * @return true of all tests run successfuly, else false
50      */
51     public boolean executeTest(TestParameters param)
52     {
53 
54         // is there an ini file for the complex tests defined?
55         String complexIniFileName = ((String) param.get("ComplexIni"));
56         if (complexIniFileName != null)
57         {
58             CfgParser ini = new CfgParser(complexIniFileName);
59             ini.getIniParameters(param);
60         }
61 
62         // get the test job
63         String testJob = ((String) param.get("TestJob"));
64 
65         DescGetter descGetter = new ComplexDescGetter();
66         // get the test jobs
67         DescEntry[] entries = descGetter.getDescriptionFor(testJob, null, true);
68         return executeTest(param, entries);
69 
70     }
71 
72     /**
73      * This function run the given DescEntry[] as ComplexTest
74      * @param param
75      * @param entries
76      * @return true of all tests run successfuly, else false
77      */
78     public boolean executeTest(TestParameters param, DescEntry[] entries)
79     {
80         // is there an ini file for the complex tests defined?
81         String complexIniFileName = ((String) param.get("ComplexIni"));
82         if (complexIniFileName != null)
83         {
84             CfgParser ini = new CfgParser(complexIniFileName);
85             ini.getIniParameters(param);
86         }
87 
88         DynamicClassLoader dcl = new DynamicClassLoader();
89         ComplexTestCase testClass = null;
90         boolean returnVal = true;
91 
92 //        the concept of the TimeOut depends on runner logs. If the runner log,
93 //        for example to start a test method, the timeout was reset. This is not
94 //        while the test itself log something like "open docuent...".
95 //        An property of complex test could be that it have only one test method
96 //        which works for serveral minutes. Ih this case the TimeOut get not trigger
97 //        and the office was killed.
98 //        In complex tests just use "ThreadTimeOut" as timout.
99 
100         // param.put("TimeOut", new Integer(0));
101 
102         for (int i = 0; i < entries.length; i++)
103         {
104 
105             if (entries[i] == null)
106             {
107                 continue;
108             }
109             String iniName = entries[i].longName;
110             iniName = iniName.replace('.', '/');
111             CfgParser ini = new CfgParser(iniName + ".props");
112             ini.getIniParameters(param);
113 
114             LogWriter log = (LogWriter) dcl.getInstance((String) param.get("LogWriter"));
115 
116             AppProvider office = null;
117             if (!param.getBool("NoOffice"))
118             {
119                 try
120                 {
121                     office = (AppProvider) dcl.getInstance("helper.OfficeProvider");
122                     Object msf = office.getManager(param);
123                     if (msf == null)
124                     {
125                         returnVal = false;
126                         continue;
127                     }
128                     param.put("ServiceFactory", msf);
129                 }
130                 catch (IllegalArgumentException e)
131                 {
132                     office = null;
133                 }
134             }
135             log.initialize(entries[i], param.getBool(PropertyName.LOGGING_IS_ACTIVE));
136             entries[i].Logger = log;
137 
138             // create an instance
139             try
140             {
141                 testClass = (ComplexTestCase) dcl.getInstance(entries[i].longName);
142             }
143             catch (java.lang.Exception e)
144             {
145                 e.printStackTrace();
146                 return false;
147             }
148             testClass.executeMethods(entries[i], param);
149 
150             Summarizer sum = new Summarizer();
151             sum.summarizeUp(entries[i]);
152 
153             if (office != null)
154             {
155                 office.closeExistingOffice(param, false);
156             }
157 
158             LogWriter out = OutProducerFactory.createOutProducer(param);
159 
160             out.initialize(entries[i], true);
161             out.summary(entries[i]);
162             returnVal &= entries[i].State.endsWith("OK");
163         }
164         return returnVal;
165     }
166 }
167