-
Notifications
You must be signed in to change notification settings - Fork 7
/
Tester.groovy
236 lines (197 loc) · 7.89 KB
/
Tester.groovy
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
@Grab(group = 'org.osgi', module = 'osgi.core', version = '6.0.0')
@Grab(group = 'org.testng.testng-remote', module = 'testng-remote', version = '1.5.1')
import org.osgi.framework.Version
import org.testng.remote.strprotocol.JsonMessageSender
//
// global var
//
// need to download the classifier jar for versions <= 5.11
classifierVer = new Version("5.11")
groovyVer = System.getProperty("GROOVY_VERSION") ?: "2.3.11"
ivyVer = System.getProperty("IVY_VERSION") ?: "2.3.0"
testngRemoteVer = System.getProperty("PROJECT_VERSION") ?: "1.5.1"
jcmdVer = "1.48"
slf4jVer = "1.7.32"
workingDir = new File(System.getProperty("user.dir"))
if (System.getProperty("PROJECT_BASEDIR")) {
workingDir = new File(System.getProperty("PROJECT_BASEDIR"))
}
println "\t:::"
println "\t::: workingDir: ${workingDir}"
println "\t::: testng remote version: ${testngRemoteVer}"
println "\t::: groovy version: ${groovyVer}"
println "\t::: ivy version: ${ivyVer}"
println "\t:::"
scriptDir = new File(workingDir.absolutePath + "/src/test/groovy")
mvnRepoDir = System.getenv("HOME") + "/.m2/repository"
groovyJar = "${mvnRepoDir}/org/codehaus/groovy/groovy-all/${groovyVer}/groovy-all-${groovyVer}.jar"
// ivy is required for groovy @Grab annotation
ivyJar = "${mvnRepoDir}/org/apache/ivy/ivy/${ivyVer}/ivy-${ivyVer}.jar"
grapeRepoDir = System.getenv("HOME") + "/.groovy/grapes"
remoteTestngJar = "${grapeRepoDir}/org.testng.testng-remote/testng-remote-dist/jars/testng-remote-dist-${testngRemoteVer}-shaded.jar"
jcmdJar = "${grapeRepoDir}/com.beust/jcommander/jars/jcommander-${jcmdVer}.jar"
slf4jJar = "${grapeRepoDir}/org.slf4j/slf4j-api/jars/slf4j-api-${slf4jVer}.jar"
resultSet = new HashMap<Integer, Set>()
//~~
def startTime = System.currentTimeMillis()
def metadata = new XmlSlurper().parse("https://repo1.maven.org/maven2/org/testng/testng/maven-metadata.xml")
def versionBlackList = ['6.12', '6.13', '6.14.0-RC2', '6.14.0-RC3', '7.0.0-beta2', '7.0.0-beta4', '7.1.0', '7.1.1']
if (System.getProperty('java.version').startsWith('1.7')) {
versionBlackList.add('7.*')
}
metadata.versioning.versions.version.each { version ->
println ">>>>>"
println ">>>>> Testing ${version}"
println ">>>>>"
// skip invalid testng jar
for (def bver : versionBlackList) {
if (version.toString().matches(bver)) {
println "skip testing ${version} \n"
return
}
}
def exitValue = runTestNGTest(version)
def rset = resultSet[exitValue]
if (rset == null) {
rset = new ArrayList()
resultSet[exitValue] = rset
}
rset << version
println "<<<<<"
println ">>>>> Tested ${version}: result=${exitValue}"
println "<<<<<\n"
}
println "\nCompleted in " + (System.currentTimeMillis() - startTime) + " (ms)"
println "\nSummary report:"
resultSet.each {
switch (it.key) {
case 0:
println it.key + " - PASSED:"
break;
case 1:
println it.key + " - unsupported version detected:"
break;
case 2:
println it.key + " - NoClassDefFoundError:"
break;
default:
println it.key + " - OTHERS:"
}
println "\t" + it.value
}
println ""
// failed if there's any OTHER failures
assert resultSet[-1] == null
def minVer = new Version("6.0")
resultSet[1].each {
// no version >= 6.0 will get error 'unsupported version detected'
assert (toVersion(it.toString()).compareTo(minVer) < 0)
}
resultSet[2].each {
// no version >= 6.5.1 will get error 'NoClassDefFoundError'
assert (toVersion(it.toString()).compareTo(minVer) < 0)
}
/**
* run the testng test with groovy in a separate process
*
* @param ver the testng version
* @return 0 - success; 1 - unsupported version detected; 2 - NoClassDefFoundError; -1 - others;
*/
def runTestNGTest(ver) {
if (downloadTestNG(ver) != 0) {
println "failed to download jars for ${ver}, skip testing"
return -1
}
def port = 12345
def msgHub = new JsonMessageSender("localhost", port)
Thread.start {
msgHub.initReceiver()
}
try {
def testngJar = "${grapeRepoDir}/org.testng/testng/jars/testng-${ver}.jar"
if (toVersion(ver.toString()).compareTo(classifierVer) <= 0) {
testngJar = "${grapeRepoDir}/org.testng/testng/jars/testng-${ver}-jdk15.jar"
}
println "classpath: ${groovyJar}:${ivyJar}:${remoteTestngJar}:${testngJar}:${jcmdJar}:${slf4jJar}\n"
def scriptFile = new File(scriptDir, "TestNGTest.groovy")
// run the groovy script via Java executable rather than groovy executable, because:
// 1) groovy has RootLoader loads groovy distributed testng jar, which is in prior to our own jar;
// 2) groovy @Grad can't specify the order of the jar on the classpath,
// while testng-remote need to be on front of testng
// (since some older version of testng jar contains older version of RemoteTestNG)
def output = new StringBuilder()
def process = new ProcessBuilder(
"java",
"-classpath", "${groovyJar}:${ivyJar}:${remoteTestngJar}:${testngJar}:${jcmdJar}:${slf4jJar}",
"-Dtestng.eclipse.verbose",
"-Dtestng.eclipse.debug",
"groovy.ui.GroovyMain",
scriptFile.absolutePath,
"json", "" + port)
.directory(workingDir).redirectErrorStream(true).start()
process.inputStream.eachLine { println it; output << it.toString() }
process.waitFor();
def exitValue = process.exitValue()
if (exitValue == 0) {
return 0
} else {
if (output.contains("is not a supported TestNG version")) {
return 1
} else if (output.contains("java.lang.NoClassDefFoundError")) {
return 2;
} else {
return -1
}
}
} finally {
msgHub.stopReceiver()
}
}
/**
* start a new process to download the dep jars,
* since @Grad can't specify the order of the jars on the classpath
* while we do want to make testng-remote on front of testng jar
* that's why we not put @Grab in the TestNGTest.groovy
*
* @param ver the testng version
* @return 0 - success, otherwise - failure
*/
def downloadTestNG(ver) {
def grabScriptFile = new File(scriptDir, "grabJar_${ver}.groovy")
grabScriptFile.withWriter { w ->
w << "@GrabResolver(name = 'jcenter', root = 'http://jcenter.bintray.com/')" + "\n"
if (toVersion(ver.toString()).compareTo(classifierVer) > 0) {
w << "@Grab(group = 'org.testng', module = 'testng', version = '${ver}')" + "\n"
} else {
w << "@Grab(group = 'org.testng', module = 'testng', version = '${ver}', classifier = 'jdk15')" + "\n"
}
w << "@GrabExclude('com.google.guava:guava')" + "\n"
w << "@Grab(group = 'com.beust', module = 'jcommander', version = '${jcmdVer}')" + "\n"
w << "@Grab(group = 'org.slf4j', module = 'slf4j-api', version = '${slf4jVer}')" + "\n"
w << "@Grab(group = 'org.testng.testng-remote', module = 'testng-remote-dist', version = '${testngRemoteVer}', classifier = 'shaded')" + "\n"
w << "import org.testng.annotations.Test;" + "\n"
}
try {
def grapeProc = new ProcessBuilder(
"java",
"-classpath", "${groovyJar}:${ivyJar}",
"groovy.ui.GroovyMain",
grabScriptFile.absolutePath)
.directory(workingDir).redirectErrorStream(true).start()
grapeProc.inputStream.eachLine { println it }
grapeProc.waitFor();
return grapeProc.exitValue()
} finally {
grabScriptFile.delete()
}
}
def Version toVersion(String ver) {
if ("5.5.m".equals(ver)) {
ver = "5.5"
}
def idx = ver.indexOf("-")
if (idx > 0) {
ver = ver.substring(0, idx)
}
return new Version(ver)
}