Skip to content

Commit 16ba916

Browse files
committed
GROOVY-11997: Add @ForkedJvm and @ExpectedToFail JUnit extensions to groovy-test-junit6
1 parent 19826e1 commit 16ba916

13 files changed

Lines changed: 1054 additions & 253 deletions

File tree

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ dependencies {
125125
testImplementation projects.groovyTest
126126
testImplementation projects.groovyMacro
127127
testImplementation projects.groovyDateutil
128+
testImplementation projects.groovyTestJunit6 // for groovy.junit6.plugin.ForkedJvm
128129
testImplementation "net.jcip:jcip-annotations:${versions.jcipAnnotations}"
129130
testImplementation "com.thoughtworks.qdox:qdox:${versions.qdox}"
130131
testImplementation "com.fasterxml.jackson.core:jackson-databind:${versions.jackson}"

src/test/groovy/groovy/ValDisabledTest.groovy

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,24 @@
1818
*/
1919
package groovy
2020

21+
import groovy.junit6.plugin.ForkedJvm
2122
import org.junit.jupiter.api.Test
2223

23-
import static org.codehaus.groovy.runtime.m12n.ExtensionModuleHelperForTests.doInFork
24+
import static groovy.test.GroovyAssert.assertScript
2425

2526
/**
2627
* Tests that when {@code groovy.val.enabled=false}, GEP-16 breaking
2728
* changes are resolved and {@code val} behaves as a regular identifier.
28-
*
29-
* Each test runs in a freshly forked JVM (compile + execution) with the
30-
* property set, so the lexer's {@code static final VAL_ENABLED} is
31-
* initialised to {@code false}.
29+
* <p>
30+
* Each test runs in a freshly forked JVM with the property set, so the
31+
* lexer's {@code static final VAL_ENABLED} is initialised to {@code false}.
3232
*/
33+
@ForkedJvm(systemProperties = ['groovy.val.enabled=false'])
3334
final class ValDisabledTest {
3435

35-
private static final List<String> JVM_ARGS = ['-Dgroovy.val.enabled=false']
36-
37-
private static void doInForkWithValDisabled(String script) {
38-
// Wrap each snippet in assertScript so top-level class declarations work
39-
// (the snippet is otherwise placed inside a method body where local
40-
// classes are not supported).
41-
doInFork('java.lang.Object', "assertScript '''${script.replace("'", "\\'")}'''", JVM_ARGS)
42-
}
43-
4436
@Test
4537
void testFieldNamedValBeforeMethod() {
46-
doInForkWithValDisabled '''
38+
assertScript '''
4739
class Foo {
4840
def val
4941
void doSomething() {}
@@ -56,16 +48,16 @@ final class ValDisabledTest {
5648

5749
@Test
5850
void testValAsCastExpression() {
59-
doInForkWithValDisabled '''
51+
assertScript '''
6052
def val = 42
6153
def result = val as String
62-
assert result == "42"
54+
assert result == '42'
6355
'''
6456
}
6557

6658
@Test
6759
void testClassNamedVal() {
68-
doInForkWithValDisabled '''
60+
assertScript '''
6961
class val {
7062
int x
7163
}
@@ -76,7 +68,7 @@ final class ValDisabledTest {
7668

7769
@Test
7870
void testValAsMethodReturnType() {
79-
doInForkWithValDisabled '''
71+
assertScript '''
8072
class val {
8173
int x
8274
}
@@ -90,7 +82,7 @@ final class ValDisabledTest {
9082

9183
@Test
9284
void testValAsExplicitType() {
93-
doInForkWithValDisabled '''
85+
assertScript '''
9486
class val {
9587
int x
9688
}
@@ -101,15 +93,15 @@ final class ValDisabledTest {
10193

10294
@Test
10395
void testDefValAssignment() {
104-
doInForkWithValDisabled '''
96+
assertScript '''
10597
def val = 1
10698
assert val == 1
10799
'''
108100
}
109101

110102
@Test
111103
void testValReassignment() {
112-
doInForkWithValDisabled '''
104+
assertScript '''
113105
def val = 1
114106
val = 2
115107
assert val == 2
@@ -118,18 +110,18 @@ final class ValDisabledTest {
118110

119111
@Test
120112
void testValAsMapKey() {
121-
doInForkWithValDisabled '''
113+
assertScript '''
122114
def m = [val: 42]
123115
assert m.val == 42
124116
'''
125117
}
126118

127119
@Test
128120
void testValPropertyAccess() {
129-
doInForkWithValDisabled '''
130-
class Foo { def val = "hello" }
121+
assertScript '''
122+
class Foo { def val = 'hello' }
131123
def f = new Foo()
132-
assert f.val == "hello"
124+
assert f.val == 'hello'
133125
'''
134126
}
135127
}

src/test/groovy/groovy/transform/stc/STCExtensionMethodsTest.groovy

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*/
1919
package groovy.transform.stc
2020

21+
import groovy.junit6.plugin.ForkedJvm
22+
import org.codehaus.groovy.runtime.m12n.ExtensionModuleRegistry
2123
import org.junit.jupiter.api.Test
2224

23-
import static org.codehaus.groovy.runtime.m12n.ExtensionModuleHelperForTests.doInFork
24-
2525
/**
2626
* Unit tests for static type checking : extension methods.
2727
*/
@@ -53,33 +53,32 @@ class STCExtensionMethodsTest extends StaticTypeCheckingTestCase {
5353
* @see org.codehaus.groovy.runtime.m12n.TestStaticStringExtension
5454
*/
5555
@Test
56+
@ForkedJvm
5657
void testShouldFindExtensionMethodWithGrab() {
57-
doInFork 'groovy.transform.stc.StaticTypeCheckingTestCase', '''
58-
def impl = new MetaClassImpl(String)
59-
impl.initialize()
60-
String.metaClass = impl
61-
try {
62-
ExtensionModuleRegistry registry = GroovySystem.metaClassRegistry.moduleRegistry
63-
// ensure that the module isn't loaded
64-
assert !registry.modules.any { it.name == 'Test module for Grab' && it.version == '1.4' }
65-
66-
def jarURL = this.class.getResource('/jars')
67-
assert jarURL
68-
69-
assertScript """@GrabResolver(name='local',root='$jarURL')
70-
@Grab('module-test:module-test:1.4;changing=true')
71-
import org.codehaus.groovy.runtime.m12n.*
72-
73-
// the following methods are added by the Grab test module
74-
def str = 'This is a string'
75-
assert str.reverseToUpperCase2() == str.toUpperCase().reverse()
76-
// a static method added to String thanks to a @Grab extension
77-
assert String.answer2() == 42
78-
"""
79-
} finally {
80-
String.metaClass = null
81-
}
82-
'''
58+
def impl = new MetaClassImpl(String)
59+
impl.initialize()
60+
String.metaClass = impl
61+
try {
62+
ExtensionModuleRegistry registry = GroovySystem.metaClassRegistry.moduleRegistry
63+
// ensure that the module isn't loaded
64+
assert !registry.modules.any { it.name == 'Test module for Grab' && it.version == '1.4' }
65+
66+
def jarURL = this.class.getResource('/jars')
67+
assert jarURL
68+
69+
assertScript """@GrabResolver(name='local',root='$jarURL')
70+
@Grab('module-test:module-test:1.4;changing=true')
71+
import org.codehaus.groovy.runtime.m12n.*
72+
73+
// the following methods are added by the Grab test module
74+
def str = 'This is a string'
75+
assert str.reverseToUpperCase2() == str.toUpperCase().reverse()
76+
// a static method added to String thanks to a @Grab extension
77+
assert String.answer2() == 42
78+
"""
79+
} finally {
80+
String.metaClass = null
81+
}
8382
}
8483

8584
/**

src/test/groovy/org/codehaus/groovy/runtime/m12n/ExtensionModuleHelperForTests.groovy

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)