KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > controller > RunTests


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.controller;
66
67 import com.jcorporate.expresso.core.controller.ControllerException;
68 import com.jcorporate.expresso.core.controller.ControllerRequest;
69 import com.jcorporate.expresso.core.controller.ControllerResponse;
70 import com.jcorporate.expresso.core.controller.DBController;
71 import com.jcorporate.expresso.core.controller.Input;
72 import com.jcorporate.expresso.core.controller.NonHandleableException;
73 import com.jcorporate.expresso.core.controller.Output;
74 import com.jcorporate.expresso.core.controller.State;
75 import com.jcorporate.expresso.core.controller.Transition;
76 import com.jcorporate.expresso.core.db.DBException;
77 import com.jcorporate.expresso.core.misc.DateTime;
78 import com.jcorporate.expresso.core.misc.EventHandler;
79 import com.jcorporate.expresso.core.misc.StringUtil;
80 import com.jcorporate.expresso.ext.dbobj.PerfTestResult;
81 import com.jcorporate.expresso.ext.dbobj.PerfTestSet;
82 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
83 import com.jcorporate.expresso.services.dbobj.Event;
84
85 import java.util.Enumeration JavaDoc;
86 import java.util.Iterator JavaDoc;
87 import java.util.StringTokenizer JavaDoc;
88 import java.util.Vector JavaDoc;
89
90
91 /**
92  * RunTests is a stand-alone application that allows sets of
93  * tests to be run
94  *
95  * @author Michael Nash
96  */

97 public class RunTests
98         extends DBController {
99
100     public RunTests() {
101         State prompt = new State("prompt", "Prompt for Command");
102         addState(prompt);
103         setInitialState("prompt");
104
105         State runtests = new State("runtests", "Run Tests");
106         runtests.addRequiredParameter("sets");
107         runtests.addRequiredParameter("repeats");
108         runtests.addRequiredParameter("threads");
109         addState(runtests);
110         this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class);
111     }
112
113     /**
114      * Transition to a new state
115      *
116      * @param newState The new state to transition into
117      */

118     public ControllerResponse newState(String JavaDoc newState,
119                                        ControllerRequest params)
120             throws ControllerException,
121             NonHandleableException {
122         ControllerResponse myResponse = super.newState(newState, params);
123
124         if (newState.equals("prompt")) {
125             promptState(myResponse, params);
126         } else if (newState.equals("runtests")) {
127             runTestsState(myResponse, params);
128         }
129
130         return myResponse;
131     } /* newState() */
132
133
134     private void promptState(ControllerResponse myResponse,
135                              ControllerRequest params)
136             throws ControllerException {
137         myResponse.addInput(new Input("sets",
138                 "Test Sets To Run (Comma separator)"));
139         myResponse.addInput(new Input("repeats", "Repeats"));
140         myResponse.addInput(new Input("threads", "Threads"));
141
142         Transition runTests = new Transition("Run Tests", getClass().getName());
143         runTests.setName("runtests");
144         runTests.addParam(STATE_PARAM_KEY, "runtests");
145         myResponse.addTransition(runTests);
146     }
147
148     /**
149      * @return java.lang.String The Title of the controller
150      */

151     public String JavaDoc getTitle() {
152         return "Run Performance Test Sets";
153     }
154
155     /**
156      * Perform a series of tests to try to determine if all of the
157      * components of the system are operating correctly
158      *
159      * @param args[]
160      */

161     private void runTestsState(ControllerResponse myResponse,
162                                ControllerRequest params)
163             throws ControllerException {
164         try {
165             FastStringBuffer mailMessage = new FastStringBuffer(1024);
166             mailMessage.append("Tests Start at " + DateTime.getDateTimeString() +
167                     "\n");
168             myResponse.addOutput(new Output("Test Start - db '" + params.getDataContext() +
169                     "'"));
170
171             StringBuffer JavaDoc message = new StringBuffer JavaDoc("RunTests\n");
172             message.append("\nRunTests at " + DateTime.getDateTimeForDB(params.getDataContext()) + "\n");
173             message.append("For database/context '" + params.getDataContext() +
174                     "'\n\n");
175
176             String JavaDoc setsToRunString = StringUtil.notNull((String JavaDoc) params.getParameter("sets"));
177
178             if (setsToRunString.equals("")) {
179                 setsToRunString = "all";
180             }
181
182             String JavaDoc repeatsStr = StringUtil.notNull((String JavaDoc) params.getParameter("repeats"));
183
184             if (repeatsStr.equals("")) {
185                 repeatsStr = "1";
186             }
187
188             String JavaDoc threadCountStr = StringUtil.notNull((String JavaDoc) params.getParameter("threads"));
189
190             if (threadCountStr.equals("")) {
191                 threadCountStr = "1";
192             }
193
194             int repeats = 1;
195
196             try {
197                 repeats = new Integer JavaDoc(repeatsStr).intValue();
198             } catch (NumberFormatException JavaDoc ne) {
199                 myResponse.addOutput(new Output("Number of repeats '" + repeatsStr +
200                         "' not valid. Running only once."));
201             }
202
203             int threadCount = 1;
204
205             try {
206                 threadCount = new Integer JavaDoc(threadCountStr).intValue();
207             } catch (NumberFormatException JavaDoc ne) {
208                 myResponse.addOutput(new Output("Number of threads '" + threadCountStr +
209                         "' not valid. Running only one thread."));
210             }
211
212             Vector JavaDoc setsToRun = new Vector JavaDoc();
213             boolean noProblems = true;
214             PerfTestResult overallResult = new PerfTestResult();
215
216             try {
217                 if (setsToRunString.equals("all")) {
218                     PerfTestSet ps = new PerfTestSet();
219                     ps.setDataContext(params.getDataContext());
220
221                     PerfTestSet oneSet = null;
222
223                     for (Iterator JavaDoc eps = ps.searchAndRetrieveList().iterator();
224                          eps.hasNext();) {
225                         oneSet = (PerfTestSet) eps.next();
226                         setsToRun.addElement(oneSet.getField("SetNumber"));
227                     }
228                 } else {
229                     StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(setsToRunString, ",");
230
231                     while (stk.hasMoreTokens()) {
232                         setsToRun.addElement(stk.nextElement());
233                     }
234                 }
235
236                 myResponse.addOutput(new Output("Running tests " + repeats +
237                         " time(s)"));
238                 message.append("\nRunning " + setsToRun.size() + " test sets.");
239
240                 int activeThreads = 0;
241                 Vector JavaDoc threadVector = new Vector JavaDoc();
242
243                 for (int i = 1; i <= threadCount; i++) {
244                     RunTestThread rt = new RunTestThread();
245                     threadVector.addElement(rt);
246                     rt.setDBName(params.getDataContext());
247                     rt.setTests((Vector JavaDoc) setsToRun.clone());
248                     rt.setThreadNumber(i);
249                     rt.setRepeats(repeats);
250                     rt.start();
251                     activeThreads++;
252                 } /* for each repitition */
253
254
255                 myResponse.addOutput(new Output("" + activeThreads +
256                         " threads started"));
257
258                 RunTestThread oneThread = null;
259
260                 while (activeThreads > 0) {
261
262                     //yield();
263
//sleep(5000);
264
// Got to make a utility class here to do delays, now that Controller no longer inherits from Thread
265
activeThreads = 0;
266
267                     for (Enumeration JavaDoc te = threadVector.elements();
268                          te.hasMoreElements();) {
269                         oneThread = (RunTestThread) te.nextElement();
270
271                         if (!oneThread.isAlive()) {
272                             myResponse.addOutput(new Output("Thread " + oneThread.getThreadNumber() +
273                                     " completed."));
274
275                             if (activeThreads > 0) {
276                                 activeThreads--;
277                             }
278                         } else {
279                             activeThreads++;
280                         }
281                     }
282
283                     System.gc();
284
285                     //myResponse.addOutput(new Output("" + activeThreads + " threads running"));
286
}
287                 for (Enumeration JavaDoc te = threadVector.elements();
288                      te.hasMoreElements();) {
289                     oneThread = (RunTestThread) te.nextElement();
290                     overallResult.add(oneThread.getResults());
291                 }
292
293                 message.append(overallResult.getMessage());
294                 message.append("\nThere were ");
295
296                 if (overallResult.getFailureCount() > 0) {
297                     if (overallResult.getFailureCount() == 1) {
298                         message.append("was 1 failure, ");
299                     } else {
300                         message.append("were " + overallResult.getFailureCount() +
301                                 " failures, ");
302                     }
303                 } else {
304                     message.append("no failures, ");
305                 }
306                 if (overallResult.getWarningCount() > 0) {
307                     if (overallResult.getWarningCount() == 1) {
308                         message.append("1 warning, ");
309                     } else {
310                         message.append("" + overallResult.getWarningCount() +
311                                 " warnings, ");
312                     }
313                 } else {
314                     message.append("no warnings, ");
315                 }
316
317                 message.append("and ");
318
319                 if (overallResult.getCautionCount() > 0) {
320                     if (overallResult.getCautionCount() == 1) {
321                         message.append("1 caution, ");
322                     } else {
323                         message.append("were " + overallResult.getCautionCount() +
324                                 " cautions, ");
325                     }
326                 } else {
327                     message.append("no cautions.");
328                 }
329
330                 message.append("\n");
331                 message.append("There were " + overallResult.getCurrentUsers() +
332                         " users logged in during tests\n");
333                 message.append("Total execution time: " +
334                         overallResult.getRunTime() + " milliseconds");
335
336                 if ((overallResult.getFailureCount() > 0) ||
337                         (overallResult.getWarningCount() > 0) ||
338                         (overallResult.getCautionCount() > 0)) {
339                     noProblems = false;
340                 }
341                 try {
342                     new Event(params.getDataContext(), "SYSERROR", message.toString(),
343                             noProblems);
344                     EventHandler.flush();
345                 } catch (Exception JavaDoc me) {
346                     myResponse.addOutput(new Output("WARNING: Unble to send email of test results:" + me.getMessage()));
347                     me.printStackTrace(System.out);
348                 }
349
350                 myResponse.addOutput(new Output(message.toString()));
351             } catch (Exception JavaDoc de) {
352                 de.printStackTrace(System.err);
353                 throw new ControllerException(de);
354             }
355
356             myResponse.addOutput(new Output("All Tests Complete."));
357         } catch (DBException dbe) {
358             throw new ControllerException(dbe);
359         }
360     } /* runTestsState() */
361
362
363 } /* RunTests */
364
Popular Tags