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