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 
24 package base;
25 
26 
27 import java.io.PrintWriter;
28 
29 import lib.TestParameters;
30 import lib.TestCase;
31 import lib.MultiMethodTest;
32 import lib.TestEnvironment;
33 import util.DynamicClassLoader;
34 
35 import com.sun.star.lang.XMultiServiceFactory;
36 
37 import share.DescEntry;
38 import share.DescGetter;
39 import helper.APIDescGetter;
40 
41 import share.LogWriter;
42 import stats.Summarizer;
43 
44 /**
45  * this class handles tests written in java and running on a fat Office
46  * with the runner as a service.
47  */
48 public class java_fat_service implements TestBase {
49 
50     static protected boolean debug = false;
51     protected LogWriter log = null;
52 
executeTest(lib.TestParameters param)53     public boolean executeTest(lib.TestParameters param) {
54         DynamicClassLoader dcl = new DynamicClassLoader();
55         log = (LogWriter)dcl.getInstance((String)param.get("LogWriter"));
56         debug = ((Boolean) param.get("DebugIsActive")).booleanValue();
57 
58         DescGetter dg = new APIDescGetter();
59         String job = (String) param.get("TestJob");
60         boolean retValue = true;
61 
62         //get Job-Descriptions
63         log.println("Getting Descriptions for Job: "+job);
64         DescEntry[] entries = dg.getDescriptionFor(job,
65                 (String) param.get("DescriptionPath"),debug);
66 
67         if (entries == null ) {
68             log.println("Couldn't get Description for Job");
69             return false;
70         }
71 
72         String conStr = (String) param.get("ConnectionString");
73 
74         XMultiServiceFactory msf = (XMultiServiceFactory)param.getMSF();
75 
76         for (int l=0;l<entries.length;l++) {
77 
78             if (entries[l] == null ) {
79                 continue;
80             }
81 
82             if (entries[l].hasErrorMsg) {
83                 log.println(entries[l].ErrorMsg);
84                 continue;
85             }
86 
87             DescEntry entry = entries[l];
88 
89             //get some helper classes
90             Summarizer sumIt = new Summarizer();
91 
92             TestCase tCase = null;
93             try {
94                 tCase = (TestCase)
95                             dcl.getInstance("mod._"+entry.entryName);
96             } catch (java.lang.IllegalArgumentException ie) {
97                 entry.ErrorMsg=ie.getMessage();
98                 entry.hasErrorMsg=true;
99             }
100 
101             if (tCase == null) {
102                 sumIt.summarizeDown(entry,entry.ErrorMsg);
103                 LogWriter sumObj = (LogWriter)dcl.getInstance(
104                                         (String)param.get("OutProducer"));
105                 sumObj.initialize(entry,true);
106                 entry.UserDefinedParams = param;
107                 sumObj.summary(entry);
108                 continue;
109             }
110 
111             log.println("Creating: "+tCase.getObjectName());
112 
113             log.initialize(entry,true);
114             entry.UserDefinedParams = param;
115             TestEnvironment tEnv = null;
116             try {
117                 tCase.setLogWriter((PrintWriter) log);
118                 tCase.initializeTestCase(param);
119                 tEnv = tCase.getTestEnvironment(param);
120             } catch (Exception e) {
121                 log.println("Exception while creating "+tCase.getObjectName());
122                 log.println("Exception: " + e);
123                 log.println("Message "+e.getMessage());
124                 tEnv = null;
125             }
126             if (tEnv == null) {
127                 sumIt.summarizeDown(entry,"Couldn't create "+tCase.getObjectName());
128                 LogWriter sumObj = (LogWriter)dcl.getInstance(
129                                             (String)param.get("OutProducer"));
130                 sumObj.initialize(entry,true);
131                 entry.UserDefinedParams = param;
132                 sumObj.summary(entry);
133                 continue;
134             }
135             log.println("Created "+tCase.getObjectName()+"\n");
136 
137             for (int j=0;j<entry.SubEntryCount;j++) {
138                 if (!entry.SubEntries[j].isToTest) {
139                     Summarizer.summarizeDown(entry.SubEntries[j],"not part of the job");
140                     continue;
141                 }
142 
143                 log.println("running: "+entry.SubEntries[j].entryName);
144 
145                 LogWriter ifclog = (LogWriter)dcl.getInstance(
146                                             (String)param.get("LogWriter"));
147 
148                 ifclog.initialize(entry.SubEntries[j],true);
149                 entry.SubEntries[j].UserDefinedParams = param;
150                 entry.SubEntries[j].Logger = ifclog;
151 
152                 if (tEnv == null || tEnv.isDisposed()) {
153                     tEnv = getEnv(entry,param);
154                 }
155 
156                 MultiMethodTest ifc = null;
157                 lib.TestResult res = null;
158                 try {
159                     ifc = (MultiMethodTest) dcl.getInstance(
160                                               entry.SubEntries[j].entryName);
161                     res = ifc.run(entry.SubEntries[j],tEnv,param);
162                 } catch (IllegalArgumentException iae) {
163                     log.println("Couldn't load class "+entry.SubEntries[j].entryName);
164                     log.println("**** "+iae.getMessage()+" ****");
165                     Summarizer.summarizeDown(entry.SubEntries[j],iae.getMessage());
166                 } catch (java.lang.RuntimeException e) {
167                     tEnv = getEnv(entry,param);
168                     ifc = (MultiMethodTest) dcl.getInstance(
169                                               entry.SubEntries[j].entryName);
170                     if ((tEnv != null) && (ifc != null)) {
171                         res = ifc.run(entry.SubEntries[j],tEnv,param);
172                     }
173                     else
174                     {
175                         res = null;
176                     }
177                 }
178                 if (res != null) {
179                     for (int k=0;k<entry.SubEntries[j].SubEntryCount;k++) {
180                         if (res.hasMethod(entry.SubEntries[j].SubEntries[k].entryName)) {
181                             entry.SubEntries[j].SubEntries[k].State=
182                                 res.getStatusFor(entry.SubEntries[j].SubEntries[k].entryName).toString();
183                         }
184                     }
185                 }
186                 sumIt.summarizeUp(entry.SubEntries[j]);
187 
188                 LogWriter sumIfc = (LogWriter)dcl.getInstance(
189                                             (String)param.get("OutProducer"));
190 
191                 sumIfc.initialize(entry.SubEntries[j],true);
192                 entry.SubEntries[j].UserDefinedParams = param;
193                 sumIfc.summary(entry.SubEntries[j]);
194             }
195             try {
196                 tCase.cleanupTestCase(param);
197             } catch (Exception e) {
198                 e.printStackTrace((PrintWriter)log);
199             }
200             sumIt.summarizeUp(entry);
201             LogWriter sumObj = (LogWriter)dcl.getInstance(
202                                         (String)param.get("OutProducer"));
203             sumObj.initialize(entry,true);
204             sumObj.summary(entry);
205         }
206         if (entries.length > 1) {
207             log.println("");
208             int counter = 0;
209             log.println("Failures that appeared during scenario execution:");
210             for (int i=0;i<entries.length;i++) {
211                 if (! entries[i].State.endsWith("OK")) {
212                     log.println("\t "+entries[i].longName);
213                     counter++;
214                 }
215             }
216             log.println(counter +" of "+entries.length + " tests failed" );
217         }
218 
219         return retValue;
220     }
221 
getEnv(DescEntry entry, TestParameters param)222     protected TestEnvironment getEnv(DescEntry entry, TestParameters param) {
223             DynamicClassLoader dcl = new DynamicClassLoader();
224             log = (LogWriter)dcl.getInstance((String)param.get("LogWriter"));
225             XMultiServiceFactory msf = (XMultiServiceFactory)param.getMSF();
226 
227             TestCase tCase = null;
228 
229             try
230             {
231                 tCase = (TestCase) dcl.getInstance("mod._"+entry.entryName);
232             }
233             catch (java.lang.IllegalArgumentException ie)
234             {
235                 entry.ErrorMsg=ie.getMessage();
236                 entry.hasErrorMsg=true;
237             }
238 
239             log.println("Creating: "+tCase.getObjectName());
240             // LogWriter log = (LogWriter)dcl.getInstance( (String)param.get("LogWriter"));
241             log.initialize(entry,true);
242             entry.UserDefinedParams = param;
243             tCase.setLogWriter((PrintWriter) log);
244             TestEnvironment tEnv = null;
245             try
246             {
247                 tCase.initializeTestCase(param);
248                 tEnv = tCase.getTestEnvironment(param);
249             }
250             catch (com.sun.star.lang.DisposedException de)
251             {
252                 log.println("Office disposed");
253             }
254             return tEnv;
255     }
256 
257 //    protected void shortWait(int millis) {
258 //        try {
259 //            Thread.sleep(millis);
260 //        } catch (java.lang.InterruptedException ie) {}
261 //    }
262 
263 }
264