-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathTestRunner.java
203 lines (185 loc) · 8.53 KB
/
TestRunner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.impl.javase;
import com.codename1.testing.TestReporting;
import java.io.DataInputStream;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.StringTokenizer;
/**
* The test runner allows running Codename One unit tests on a specific Codename One application within the
* simulator.
*
* @author Shai Almog
*/
public class TestRunner {
private static final int VERSION = 1;
private String mainClass;
private String[] testCases;
private String[] skins;
private boolean quietMode;
private boolean cleanMode = true;
private boolean stopOnFail;
private TestRunner() {
}
private void printUsage() {
System.out.println("Usage: TestRunner mainClass [-testCases testCase1,testCase2...] [-skins skin1,skin2...] [-quiet] [-noCleanMode]"
+ "[-stopOnFail]\n\n"
+ "mainClass - the main application class which is being tested, this is the full name for the lifecycle class.\n"
+ "testCases - optional set of test cases to run using the full package/class name syntax and comma delimited. If "
+ "ommitted all test cases will be executed.\n"
+ "skins - the skins on which the test cases should be executed. If ommitted the default simulator skin is used.\n"
+ "quietMode - when set the skin running the tests will not appear and the tests will be executed in the background\n"
+ "noCleanMode - when set executes every test without isolation from the previous test (by default, without this flag, it executes every test in complete isolation from the previous test restarting the Classloader)"
+ "completely. Clean mode can't be used on the device so its only useful for debugging\n"
+ "stopOnFail - indicates that execution should stop the moment a failure occured");
}
private void init(String[] argv) {
try {
if(argv[0].startsWith("-") || argv[0].startsWith("/")) {
printUsage();
return;
}
try {
mainClass = argv[0];
int pos = 1;
while(pos < argv.length) {
String s = argv[pos];
if(s.equalsIgnoreCase("-testCases")) {
pos++;
testCases = argv[pos].split(",");
pos++;
continue;
}
if(s.equalsIgnoreCase("-skins")) {
pos++;
skins = argv[pos].split(",");
pos++;
continue;
}
if(s.equalsIgnoreCase("-quietMode")) {
quietMode = true;
pos++;
continue;
}
if(s.equalsIgnoreCase("-noCleanMode")) {
cleanMode = false;
pos++;
continue;
}
System.out.println("Unrecognized argument: " + s);
printUsage();
System.exit(1);
return;
}
} catch(Exception err) {
err.printStackTrace();
printUsage();
return;
}
String[] tests;
if(testCases == null || testCases.length == 0) {
InputStream is = getClass().getResourceAsStream("/tests.dat");
if(is == null) {
System.err.println("Test data not found in the file, make sure the ant task was executed in full");
System.exit(2);
return;
}
DataInputStream di = new DataInputStream(is);
int version = di.readInt();
if(version > VERSION) {
System.err.println("Tests were built with a new version of Codename One and can't be executed with this runner");
System.exit(4);
return;
}
tests = new String[di.readInt()];
for(int iter = 0 ; iter < tests.length ; iter++) {
tests[iter] = di.readUTF();
}
di.close();
} else {
tests = testCases;
}
System.out.println("Preparing to execute " + tests.length + " tests");
StringTokenizer t = new StringTokenizer(System.getProperty("java.class.path"), File.pathSeparator);
File[] files = new File[t.countTokens()];
for (int iter = 0; iter < files.length; iter++) {
files[iter] = new File(t.nextToken());
}
int passedTests = 0;
int failedTests = 0;
if(cleanMode) {
for(String currentTestClass : tests) {
ClassLoader ldr = new ClassPathLoader(files);
Class c = Class.forName("com.codename1.impl.javase.TestExecuter", true, ldr);
Method m = c.getDeclaredMethod("runTest", String.class, String.class, Boolean.TYPE);
Boolean passed = (Boolean)m.invoke(null, mainClass, currentTestClass, quietMode);
if(passed.booleanValue()) {
passedTests++;
} else {
failedTests++;
if(stopOnFail) {
System.exit(100);
return;
}
}
}
} else {
ClassLoader ldr = new ClassPathLoader(files);
Class c = Class.forName("com.codename1.impl.javase.TestExecuter", true, ldr);
for(String currentTestClass : tests) {
Method m = c.getDeclaredMethod("runTest", String.class, String.class, Boolean.TYPE);
Boolean passed = (Boolean)m.invoke(null, mainClass, currentTestClass, quietMode);
if(passed.booleanValue()) {
System.out.println(currentTestClass + " passed!");
passedTests++;
} else {
System.out.println(currentTestClass + " failed!");
failedTests++;
if(stopOnFail) {
System.exit(100);
return;
}
}
}
}
TestReporting.getInstance().testExecutionFinished();
if(failedTests > 0) {
System.out.println("Test execution finished, some failed tests occured. Passed: " + passedTests + " tests. Failed: " + failedTests + " tests.");
} else {
System.out.println("All tests passed. Total " + passedTests + " tests passed");
}
System.exit(0);
} catch (Exception ex) {
ex.printStackTrace();
System.exit(3);
}
}
/**
* The main method accepts several arguments of which only the main class is a requirement
*/
public static void main(String[] argv) {
new TestRunner().init(argv);
}
}