KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > test > workbench > JalistoTestEngine


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.test.workbench;
25
26 import junit.extensions.jfunc.AssertListener;
27 import junit.extensions.jfunc.textui.JFuncRunner;
28 import junit.extensions.jfunc.util.ColorWriter;
29 import junit.framework.AssertionFailedError;
30 import junit.framework.Test;
31 import junit.framework.TestResult;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 public class JalistoTestEngine extends JFuncRunner {
37
38     public static void main(String JavaDoc[] args) {
39         String JavaDoc dbProp = null;
40         String JavaDoc nameTestSuite = null;
41
42         if (args.length == 0) {
43             printHelp();
44         }
45
46         List JavaDoc newArgList = new ArrayList JavaDoc();
47
48         for (int i = 0; i < args.length; i++) {
49             if (args[i].equals("--help") || args[i].equals("-h")) {
50                 printHelp();
51             } else if (args[i].equals("-db")) {
52                 i = i + 1;
53                 dbProp = args[i];
54             } else if (args[i].equals("-nt")) {
55                 i = i + 1;
56                 nameTestSuite = args[i];
57             } else {
58                 newArgList.add(args[i]);
59             }
60         }
61
62         JalistoTestEngine aTestRunner = new JalistoTestEngine(dbProp);
63
64         newArgList.add("-c");
65         newArgList.add(nameTestSuite);
66
67         String JavaDoc[] newArgs = (String JavaDoc[]) newArgList.toArray(new String JavaDoc[newArgList.size()]);
68
69         try {
70             TestResult r = aTestRunner.start(newArgs);
71         } catch (Exception JavaDoc e) {
72             e.printStackTrace();
73         }
74     }
75
76     private static void printHelp() {
77         System.out.println("Parameters :");
78         System.out.println(" -db <dbFile> : the name of the database properties file");
79         System.out.println(" -nt <nameTest> : the name of the test class");
80         System.out.println(" -h or --help : diplay this help");
81         System.out.println("Options of jfunc : \n" +
82                            "[--wait] [-v] [--color] testCaseName [args]\n" +
83                            " --version display version\n" +
84                            " --wait wait between tests\n" +
85                            " -v be verbose (show verbose assertions)\n" +
86                            " --color display ANSI colors");
87         System.out.println("All files must be locate in classpath.");
88         System.exit(0);
89     }
90
91     /**************************************************************************************************/
92
93     public JalistoTestEngine(String JavaDoc dbProp) {
94         this.dbPropertiesFilename = dbProp;
95     }
96
97     public String JavaDoc getDBPropertiesFilename() {
98         return dbPropertiesFilename;
99     }
100
101     public void setDbPropertiesFilename(String JavaDoc dbPropertiesFilename) {
102         this.dbPropertiesFilename = dbPropertiesFilename;
103     }
104
105     public TestResult doRun(Test suite, boolean wait) {
106         TestResult result = null;
107
108         try {
109             ((JalistoTestSuite) suite).setTestEngine(this);
110
111             TestResult result11 = super.createTestResult();
112             result11.addListener(new VerboseListener());
113
114             long startTime = System.currentTimeMillis();
115             suite.run(result11);
116
117             long endTime = System.currentTimeMillis();
118             long runTime = endTime - startTime;
119             writer().println();
120
121             writer().println("Time: " + super.elapsedTimeAsString(runTime));
122             print(result11);
123
124             writer().println();
125
126             pause(wait);
127             result = result11;
128         } catch (Exception JavaDoc e) {
129             e.printStackTrace();
130         }
131
132         return result;
133     }
134
135
136
137     public void printErrors(TestResult testResult) {
138         super.printErrors(testResult);
139     }
140
141     protected void testCaseInit(JalistoTestCase testCase) {
142     }
143
144
145     private String JavaDoc dbPropertiesFilename;
146
147     /***************************************************************************************************/
148
149     class VerboseListener implements AssertListener {
150         ColorWriter out = new ColorWriter(writer());
151
152         public VerboseListener() {
153             out.enableColor(useColor());
154         }
155
156         public void addAssert(Test test, String JavaDoc msg, boolean condition) {
157             int color;
158             String JavaDoc passorfail;
159             if (!condition) {
160                 return;
161             }
162             if (condition) {
163                 color = ColorWriter.GREEN;
164                 passorfail = "PASSED";
165             } else {
166                 color = ColorWriter.RED;
167                 passorfail = "FAILED";
168             }
169             status(color, passorfail, msg);
170         }
171
172         public void addError(Test test, Throwable JavaDoc t) {
173             //status(ColorWriter.BLACK, ColorWriter.YELLOW, "ERROR");
174
status(ColorWriter.YELLOW, "ERROR", t.toString());
175             //out.println("error: " + test + " " + t);
176
}
177
178         public void addFailure(Test test, AssertionFailedError t) {
179             status(ColorWriter.RED, "FAILED", t.getMessage());
180             //out.println("failure: " + test + " " + t);
181
}
182
183         public void endTest(Test test) {
184             //out.println("end: " + test);
185
}
186
187         public void startTest(Test test) {
188             out.setColor(ColorWriter.CYAN);
189             out.println(test.toString() + ": ");
190             out.setColor(ColorWriter.DEFAULT);
191         }
192
193         private void status(int color, String JavaDoc coloredMsg, String JavaDoc msg) {
194             out.print(" [ ");
195             out.setColor(color);
196             out.print(coloredMsg);
197             out.setColor(ColorWriter.DEFAULT);
198             out.print(" " + msg);
199             out.println(" ]");
200         }
201     }
202 }
203
Popular Tags