Skip to content

Commit 2979a8d

Browse files
Migrate tests to JUnit5
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent aa5fe88 commit 2979a8d

23 files changed

+1249
-989
lines changed

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>org.jenkins-ci.plugins</groupId>
66
<artifactId>plugin</artifactId>
7-
<version>5.24</version>
7+
<version>5.26</version>
88
<relativePath/>
99
</parent>
1010

@@ -21,6 +21,7 @@
2121
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
2222
<hpi.strictBundledArtifacts>true</hpi.strictBundledArtifacts>
2323
<hpi.bundledArtifacts>groovy-sandbox</hpi.bundledArtifacts>
24+
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
2425
</properties>
2526
<licenses>
2627
<license>
@@ -87,5 +88,11 @@
8788
<artifactId>test-harness</artifactId>
8889
<scope>test</scope>
8990
</dependency>
91+
<dependency>
92+
<groupId>org.awaitility</groupId>
93+
<artifactId>awaitility</artifactId>
94+
<version>4.3.0</version>
95+
<scope>test</scope>
96+
</dependency>
9097
</dependencies>
9198
</project>

src/test/java/org/jenkinsci/plugins/scriptsecurity/sandbox/groovy/GroovyCallSiteSelectorTest.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,24 @@
4343
import org.codehaus.groovy.runtime.GStringImpl;
4444
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.EnumeratingWhitelist;
4545
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.EnumeratingWhitelistTest;
46-
import static org.junit.Assert.*;
47-
import org.junit.Test;
46+
47+
import org.junit.jupiter.api.Test;
48+
import static org.junit.jupiter.api.Assertions.*;
49+
4850
import org.jvnet.hudson.test.Issue;
4951

50-
public class GroovyCallSiteSelectorTest {
52+
class GroovyCallSiteSelectorTest {
5153

52-
@Test public void arrays() throws Exception {
54+
@Test
55+
void arrays() throws Exception {
5356
Method m = EnumeratingWhitelistTest.C.class.getDeclaredMethod("m", Object[].class);
54-
assertEquals("literal call", m, GroovyCallSiteSelector.method(new EnumeratingWhitelistTest.C(), "m", new Object[] {new Object[] {"a", "b"}}));
55-
assertNull("we assume the interceptor has dealt with varargs", GroovyCallSiteSelector.method(new EnumeratingWhitelistTest.C(), "m", new Object[]{"a", "b"}));
56-
assertEquals("array cast", m, GroovyCallSiteSelector.method(new EnumeratingWhitelistTest.C(), "m", new Object[] {new String[] {"a", "b"}}));
57+
assertEquals(m, GroovyCallSiteSelector.method(new EnumeratingWhitelistTest.C(), "m", new Object[] {new Object[] {"a", "b"}}), "literal call");
58+
assertNull(GroovyCallSiteSelector.method(new EnumeratingWhitelistTest.C(), "m", new Object[]{"a", "b"}), "we assume the interceptor has dealt with varargs");
59+
assertEquals(m, GroovyCallSiteSelector.method(new EnumeratingWhitelistTest.C(), "m", new Object[] {new String[] {"a", "b"}}), "array cast");
5760
}
5861

59-
@Test public void overloads() throws Exception {
62+
@Test
63+
void overloads() throws Exception {
6064
PrintWriter receiver = new PrintWriter(new OutputStream() {
6165
@Override
6266
public void write(int b) throws IOException {
@@ -69,33 +73,38 @@ public void write(int b) throws IOException {
6973
}
7074

7175
@Issue("JENKINS-29541")
72-
@Test public void methodsOnGString() throws Exception {
76+
@Test
77+
void methodsOnGString() throws Exception {
7378
GStringImpl gString = new GStringImpl(new Object[0], new String[] {"x"});
7479
assertEquals(String.class.getMethod("substring", int.class), GroovyCallSiteSelector.method(gString, "substring", new Object[] {99}));
7580
assertEquals(GString.class.getMethod("getValues"), GroovyCallSiteSelector.method(gString, "getValues", new Object[0]));
7681
assertEquals(GString.class.getMethod("getStrings"), GroovyCallSiteSelector.method(gString, "getStrings", new Object[0]));
7782
}
7883

7984
@Issue("JENKINS-31701")
80-
@Test public void primitives() throws Exception {
85+
@Test
86+
void primitives() throws Exception {
8187
assertEquals(Primitives.class.getMethod("m1", long.class), GroovyCallSiteSelector.staticMethod(Primitives.class, "m1", new Object[] {Long.MAX_VALUE}));
8288
assertEquals(Primitives.class.getMethod("m1", long.class), GroovyCallSiteSelector.staticMethod(Primitives.class, "m1", new Object[] {99}));
8389
assertEquals(Primitives.class.getMethod("m2", long.class), GroovyCallSiteSelector.staticMethod(Primitives.class, "m2", new Object[] {Long.MAX_VALUE}));
8490
assertEquals(Primitives.class.getMethod("m2", int.class), GroovyCallSiteSelector.staticMethod(Primitives.class, "m2", new Object[] {99}));
8591
}
92+
8693
public static class Primitives {
8794
public static void m1(long x) {}
8895
public static void m2(int x) {}
8996
public static void m2(long x) {}
9097
}
9198

92-
@Test public void staticMethodsCannotBeOverridden() throws Exception {
99+
@Test
100+
void staticMethodsCannotBeOverridden() throws Exception {
93101
assertEquals(Jenkins.class.getMethod("getInstance"), GroovyCallSiteSelector.staticMethod(Jenkins.class, "getInstance", new Object[0]));
94102
assertEquals(Hudson.class.getMethod("getInstance"), GroovyCallSiteSelector.staticMethod(Hudson.class, "getInstance", new Object[0]));
95103
}
96104

97105
@Issue("JENKINS-45117")
98-
@Test public void constructorVarargs() throws Exception {
106+
@Test
107+
void constructorVarargs() throws Exception {
99108
assertEquals(EnvVars.class.getConstructor(), GroovyCallSiteSelector.constructor(EnvVars.class, new Object[0]));
100109
assertEquals(EnvVars.class.getConstructor(String[].class), GroovyCallSiteSelector.constructor(EnvVars.class, new Object[]{"x"}));
101110
List<ParameterValue> params = new ArrayList<>();
@@ -115,7 +124,7 @@ public static void m2(long x) {}
115124

116125
@Issue("JENKINS-47159")
117126
@Test
118-
public void varargsFailureCases() throws Exception {
127+
void varargsFailureCases() {
119128
// If there's a partial match, we should get a ClassCastException
120129
final ClassCastException e = assertThrows(ClassCastException.class,
121130
() -> assertNull(GroovyCallSiteSelector.constructor(ParametersAction.class,
@@ -128,7 +137,7 @@ public void varargsFailureCases() throws Exception {
128137

129138
@Issue("JENKINS-37257")
130139
@Test
131-
public void varargsArrayElementTypeMismatch() throws Exception {
140+
void varargsArrayElementTypeMismatch() throws Exception {
132141
List<String> l = Arrays.asList("a", "b", "c");
133142
assertEquals(String.class.getMethod("join", CharSequence.class, Iterable.class),
134143
GroovyCallSiteSelector.staticMethod(String.class, "join", new Object[]{",", l}));

src/test/java/org/jenkinsci/plugins/scriptsecurity/sandbox/groovy/GroovyLanguageCoverageTest.java

Lines changed: 119 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -2,151 +2,142 @@
22

33
import org.jenkinsci.plugins.scriptsecurity.sandbox.Whitelist;
44
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.GenericWhitelist;
5-
import org.junit.Ignore;
6-
import org.junit.Rule;
7-
import org.junit.Test;
8-
import org.junit.rules.ErrorCollector;
5+
import org.junit.jupiter.api.Disabled;
6+
import org.junit.jupiter.api.Test;
97
import org.jvnet.hudson.test.Issue;
108

11-
public class GroovyLanguageCoverageTest {
9+
class GroovyLanguageCoverageTest {
1210

13-
@Rule
14-
public ErrorCollector errors = new ErrorCollector();
15-
16-
private void assertEvaluate(Whitelist whitelist, Object expected, String script) {
17-
SandboxInterceptorTest.assertEvaluate(whitelist, expected, script, errors);
18-
}
19-
20-
@Ignore("This fails on m.\"fruit${bKey}\" returning null due to JENKINS-46327")
11+
@Disabled("This fails on m.\"fruit${bKey}\" returning null due to JENKINS-46327")
2112
@Issue("JENKINS-46327")
2213
@Test
23-
public void quotedIdentifiers() throws Exception {
14+
void quotedIdentifiers() throws Exception {
2415
// See http://groovy-lang.org/syntax.html#_quoted_identifiers
25-
assertEvaluate(new GenericWhitelist(),
26-
true,
27-
"def m = [fruitA: 'apple', fruitB: 'banana']\n" +
28-
"assert m.fruitA == 'apple'\n" +
29-
"assert m.'fruitA' == 'apple'\n" +
30-
"assert m.\"fruitA\" == 'apple'\n" +
31-
"assert m.'''fruitA''' == 'apple'\n" +
32-
"assert m.\"\"\"fruitA\"\"\" == 'apple'\n" +
33-
"assert m./fruitA/ == 'apple'\n" +
34-
"assert m.$/fruitA/$ == 'apple'\n" +
35-
"def bKey = 'B'\n" +
36-
"assert m.\"fruit${bKey}\" == 'banana'\n" +
37-
"assert m.\"\"\"fruit${bKey}\"\"\" == 'banana'" +
38-
"assert m./fruit${bKey}/ == 'banana'\n" +
39-
"assert m.$/fruit${bKey}/$ == 'banana'\n" +
40-
"return true\n"
41-
);
16+
Whitelist whitelist = new GenericWhitelist();
17+
SandboxInterceptorTest.assertEvaluate(whitelist, true, """
18+
def m = [fruitA: 'apple', fruitB: 'banana']
19+
assert m.fruitA == 'apple'
20+
assert m.'fruitA' == 'apple'
21+
assert m."fruitA" == 'apple'
22+
assert m.'''fruitA''' == 'apple'
23+
assert m.""\"fruitA""\" == 'apple'
24+
assert m./fruitA/ == 'apple'
25+
assert m.$/fruitA/$ == 'apple'
26+
def bKey = 'B'
27+
assert m."fruit${bKey}" == 'banana'
28+
assert m.""\"fruit${bKey}""\" == 'banana'\
29+
assert m./fruit${bKey}/ == 'banana'
30+
assert m.$/fruit${bKey}/$ == 'banana'
31+
return true
32+
""");
4233
}
4334

4435
@Test
45-
public void gStringWithClosure() throws Exception {
36+
void gStringWithClosure() throws Exception {
4637
// see http://groovy-lang.org/syntax.html#_special_case_of_interpolating_closure_expressions
47-
assertEvaluate(new GenericWhitelist(),
48-
true,
49-
"def number = 1\n" +
50-
"def eagerGString = \"value == ${number}\"\n" +
51-
"def lazyGString = \"value == ${ -> number }\"\n" +
52-
"assert eagerGString == 'value == 1'\n" +
53-
"assert lazyGString.toString() == 'value == 1'\n" +
54-
"number = 2\n" +
55-
"assert eagerGString == 'value == 1'\n" +
56-
"assert lazyGString == 'value == 2'\n" +
57-
"return true\n"
58-
);
38+
Whitelist whitelist = new GenericWhitelist();
39+
SandboxInterceptorTest.assertEvaluate(whitelist, true, """
40+
def number = 1
41+
def eagerGString = "value == ${number}"
42+
def lazyGString = "value == ${ -> number }"
43+
assert eagerGString == 'value == 1'
44+
assert lazyGString.toString() == 'value == 1'
45+
number = 2
46+
assert eagerGString == 'value == 1'
47+
assert lazyGString == 'value == 2'
48+
return true
49+
""");
5950
}
6051

6152
@Test
62-
public void arithmeticOperators() throws Exception {
53+
void arithmeticOperators() throws Exception {
6354
// see http://groovy-lang.org/operators.html#_arithmetic_operators
64-
assertEvaluate(new GenericWhitelist(),
65-
true,
66-
"assert 1 + 2 == 3\n" +
67-
"assert 4 - 3 == 1\n" +
68-
"assert 3 * 5 == 15\n" +
69-
"assert 3 / 2 == 1.5\n" +
70-
"assert 10 % 3 == 1\n" +
71-
"assert 2 ** 3 == 8\n" +
72-
"assert +3 == 3\n" +
73-
"assert -4 == 0 - 4\n" +
74-
"assert -(-1) == 1 \n" +
75-
"def a = 2\n" +
76-
"def b = a++ * 3\n" +
77-
"assert a == 3 && b == 6\n" +
78-
"def c = 3\n" +
79-
"def d = c-- * 2\n" +
80-
"assert c == 2 && d == 6\n" +
81-
"def e = 1\n" +
82-
"def f = ++e + 3\n" +
83-
"assert e == 2 && f == 5\n" +
84-
"def g = 4\n" +
85-
"def h = --g + 1\n" +
86-
"assert g == 3 && h == 4\n" +
87-
"def a2 = 4\n" +
88-
"a2 += 3\n" +
89-
"assert a2 == 7\n" +
90-
"def b2 = 5\n" +
91-
"b2 -= 3\n" +
92-
"assert b2 == 2\n" +
93-
"def c2 = 5\n" +
94-
"c2 *= 3\n" +
95-
"assert c2 == 15\n" +
96-
"def d2 = 10\n" +
97-
"d2 /= 2\n" +
98-
"assert d2 == 5\n" +
99-
"def e2 = 10\n" +
100-
"e2 %= 3\n" +
101-
"assert e2 == 1\n" +
102-
"def f2 = 3\n" +
103-
"f2 **= 2\n" +
104-
"assert f2 == 9\n" +
105-
"return true\n"
106-
);
55+
Whitelist whitelist = new GenericWhitelist();
56+
SandboxInterceptorTest.assertEvaluate(whitelist, true, """
57+
assert 1 + 2 == 3
58+
assert 4 - 3 == 1
59+
assert 3 * 5 == 15
60+
assert 3 / 2 == 1.5
61+
assert 10 % 3 == 1
62+
assert 2 ** 3 == 8
63+
assert +3 == 3
64+
assert -4 == 0 - 4
65+
assert -(-1) == 1 \s
66+
def a = 2
67+
def b = a++ * 3
68+
assert a == 3 && b == 6
69+
def c = 3
70+
def d = c-- * 2
71+
assert c == 2 && d == 6
72+
def e = 1
73+
def f = ++e + 3
74+
assert e == 2 && f == 5
75+
def g = 4
76+
def h = --g + 1
77+
assert g == 3 && h == 4
78+
def a2 = 4
79+
a2 += 3
80+
assert a2 == 7
81+
def b2 = 5
82+
b2 -= 3
83+
assert b2 == 2
84+
def c2 = 5
85+
c2 *= 3
86+
assert c2 == 15
87+
def d2 = 10
88+
d2 /= 2
89+
assert d2 == 5
90+
def e2 = 10
91+
e2 %= 3
92+
assert e2 == 1
93+
def f2 = 3
94+
f2 **= 2
95+
assert f2 == 9
96+
return true
97+
""");
10798
}
10899

109100
@Test
110-
public void bigDecimalOperators() throws Exception {
101+
void bigDecimalOperators() throws Exception {
111102
// see http://groovy-lang.org/operators.html#_arithmetic_operators
112-
assertEvaluate(new GenericWhitelist(),
113-
true,
114-
"assert 1.0 + 2.0 == 3.0\n" +
115-
"assert 4.0 - 3.0 == 1.0\n" +
116-
"assert 3.0 * 5.0 == 15.0\n" +
117-
"assert 3.0 / 2.0 == 1.5\n" +
118-
"assert 2.0 ** 3.0 == 8.0\n" +
119-
"assert +3.0 == 3.0\n" +
120-
"assert -4.0 == 0 - 4.0\n" +
121-
"assert -(-1.0) == 1.0\n" +
122-
"def a = 2.0\n" +
123-
"def b = a++ * 3.0\n" +
124-
"assert a == 3.0 && b == 6.0\n" +
125-
"def c = 3.0\n" +
126-
"def d = c-- * 2.0\n" +
127-
"assert c == 2.0 && d == 6.0\n" +
128-
"def e = 1.0\n" +
129-
"def f = ++e + 3.0\n" +
130-
"assert e == 2.0 && f == 5.0\n" +
131-
"def g = 4.0\n" +
132-
"def h = --g + 1.0\n" +
133-
"assert g == 3.0 && h == 4.0\n" +
134-
"def a2 = 4.0\n" +
135-
"a2 += 3.0\n" +
136-
"assert a2 == 7.0\n" +
137-
"def b2 = 5.0\n" +
138-
"b2 -= 3.0\n" +
139-
"assert b2 == 2.0\n" +
140-
"def c2 = 5.0\n" +
141-
"c2 *= 3.0\n" +
142-
"assert c2 == 15.0\n" +
143-
"def d2 = 10.0\n" +
144-
"d2 /= 2.0\n" +
145-
"assert d2 == 5.0\n" +
146-
"def f2 = 3.0\n" +
147-
"f2 **= 2.0\n" +
148-
"assert f2 == 9.0\n" +
149-
"return true\n"
150-
);
103+
Whitelist whitelist = new GenericWhitelist();
104+
SandboxInterceptorTest.assertEvaluate(whitelist, true, """
105+
assert 1.0 + 2.0 == 3.0
106+
assert 4.0 - 3.0 == 1.0
107+
assert 3.0 * 5.0 == 15.0
108+
assert 3.0 / 2.0 == 1.5
109+
assert 2.0 ** 3.0 == 8.0
110+
assert +3.0 == 3.0
111+
assert -4.0 == 0 - 4.0
112+
assert -(-1.0) == 1.0
113+
def a = 2.0
114+
def b = a++ * 3.0
115+
assert a == 3.0 && b == 6.0
116+
def c = 3.0
117+
def d = c-- * 2.0
118+
assert c == 2.0 && d == 6.0
119+
def e = 1.0
120+
def f = ++e + 3.0
121+
assert e == 2.0 && f == 5.0
122+
def g = 4.0
123+
def h = --g + 1.0
124+
assert g == 3.0 && h == 4.0
125+
def a2 = 4.0
126+
a2 += 3.0
127+
assert a2 == 7.0
128+
def b2 = 5.0
129+
b2 -= 3.0
130+
assert b2 == 2.0
131+
def c2 = 5.0
132+
c2 *= 3.0
133+
assert c2 == 15.0
134+
def d2 = 10.0
135+
d2 /= 2.0
136+
assert d2 == 5.0
137+
def f2 = 3.0
138+
f2 **= 2.0
139+
assert f2 == 9.0
140+
return true
141+
""");
151142
}
152143
}

0 commit comments

Comments
 (0)