1
1
package com.github.eirnym.js2p
2
2
3
+ import org.gradle.testkit.runner.BuildResult
3
4
import org.gradle.testkit.runner.GradleRunner
4
5
import org.gradle.testkit.runner.TaskOutcome
5
6
import org.junit.jupiter.api.BeforeEach
6
7
import org.junit.jupiter.api.DisplayName
7
- import org.junit.jupiter.api.Test
8
8
import org.junit.jupiter.api.io.TempDir
9
+ import org.junit.jupiter.params.ParameterizedTest
10
+ import org.junit.jupiter.params.provider.Arguments
11
+ import org.junit.jupiter.params.provider.MethodSource
12
+ import org.junit.jupiter.params.provider.NullSource
9
13
10
14
import java.nio.file.Files
11
15
import java.nio.file.Path
12
16
import java.nio.file.Paths
17
+ import java.util.stream.Stream
13
18
14
- import static com.github.eirnym.js2p.JsonSchemaPlugin.COLON_TASK_NAME
15
19
import static com.github.eirnym.js2p.JsonSchemaPlugin.TASK_NAME
16
- import static org.hamcrest.CoreMatchers.is
17
- import static org.hamcrest.io.FileMatchers.anExistingDirectory
18
- import static org.hamcrest.MatcherAssert.assertThat
19
- import static org.hamcrest.io.FileMatchers.anExistingFile
20
- import static org.junit.jupiter.api.Assertions.assertEquals
20
+ import static org.junit.jupiter.api.Assertions.*
21
21
22
22
class JavaTaskFunctionalTest {
23
+ private static final String COLON_TASK_NAME = ' :' + TASK_NAME
24
+ private static final List<String > GRADLE_RELEASES = [
25
+ ' 5.6.4' , // 5.x for java8-java11 only
26
+ ' 6.8.3' , ' 6.8' , ' 6.7.1' , ' 6.6.1' , ' 6.5.1' , ' 6.4.1' , ' 6.3' , ' 6.2.2' , ' 6.2.1' , ' 6.1.1' , ' 6.0.1' , // 6.x
27
+ ]
28
+
23
29
@TempDir
24
30
public Path testProjectDir
25
31
private Path buildFile
@@ -29,68 +35,59 @@ class JavaTaskFunctionalTest {
29
35
buildFile = testProjectDir. resolve(" build.gradle" )
30
36
}
31
37
32
- @Test
33
- void withoutExtension () {
38
+ @ParameterizedTest
39
+ @NullSource
40
+ @MethodSource (' gradleReleases' )
41
+ void withoutExtension (String gradleVersion ) {
34
42
createBuildFiles()
35
43
copyAddressJSON()
36
44
37
- def result = GradleRunner . create()
38
- .withPluginClasspath()
39
- .withProjectDir(testProjectDir. toFile())
40
- .withArguments(TASK_NAME , " --info" )
41
- .build()
45
+ def result = executeRunner(gradleVersion, testProjectDir)
42
46
43
47
def js2pDir = testProjectDir. resolve(" build/generated/sources/js2d" )
44
48
assertEquals (TaskOutcome . SUCCESS , result. task(COLON_TASK_NAME ). outcome)
45
- assertThat (js2pDir. toFile(), is(anExistingDirectory() ))
46
- assertThat (js2pDir. resolve(" Address.java" ). toFile(), is(anExistingFile() ))
49
+ assertExists (js2pDir. toFile())
50
+ assertExists (js2pDir. resolve(" Address.java" ). toFile())
47
51
}
48
52
49
- @Test
53
+
54
+ @ParameterizedTest
55
+ @NullSource
56
+ @MethodSource (' gradleReleases' )
50
57
@DisplayName (" compileJava task depends task even when project has no java code" )
51
- void noJavaCode () {
58
+ void noJavaCode (String gradleVersion ) {
52
59
createBuildFiles()
53
60
copyAddressJSON()
54
61
55
- def result = GradleRunner . create()
56
- .withPluginClasspath()
57
- .withProjectDir(testProjectDir. toFile())
58
- .withArguments(" compileJava" , " --info" )
59
- .build()
62
+ def result = executeRunner(gradleVersion, testProjectDir, " compileJava" )
60
63
61
64
assertEquals (TaskOutcome . SUCCESS , result. task(COLON_TASK_NAME ). outcome)
62
65
}
63
66
64
- @Test
67
+ @ParameterizedTest
68
+ @NullSource
69
+ @MethodSource (' gradleReleases' )
65
70
@DisplayName (' task is cache-able' )
66
- void taskIsCachable () {
71
+ void taskIsCachable (String gradleVersion ) {
67
72
createBuildFiles()
68
73
copyAddressJSON()
69
74
70
- GradleRunner . create()
71
- .withPluginClasspath()
72
- .withProjectDir(testProjectDir. toFile())
73
- .withArguments(TASK_NAME )
74
- .build()
75
- def result = GradleRunner . create()
76
- .withPluginClasspath()
77
- .withProjectDir(testProjectDir. toFile())
78
- .withArguments(TASK_NAME , " --info" )
79
- .build()
75
+ // Run our task twice to be sure that results has been cached
76
+
77
+ executeRunner(gradleVersion, testProjectDir)
78
+ def result = executeRunner(gradleVersion, testProjectDir)
80
79
81
80
assertEquals (TaskOutcome . UP_TO_DATE , result. task(COLON_TASK_NAME ). outcome)
82
81
}
83
82
84
- @Test
83
+ @ParameterizedTest
84
+ @NullSource
85
+ @MethodSource (' gradleReleases' )
85
86
@DisplayName (' task skips if no json file exists' )
86
- void noJsonFiles () {
87
+ void noJsonFiles (String gradleVersion ) {
87
88
createBuildFiles()
88
89
89
- def result = GradleRunner . create()
90
- .withPluginClasspath()
91
- .withProjectDir(testProjectDir. toFile())
92
- .withArguments(COLON_TASK_NAME , " --info" )
93
- .build()
90
+ def result = executeRunner(gradleVersion, testProjectDir)
94
91
95
92
assertEquals (TaskOutcome . NO_SOURCE , result. task(COLON_TASK_NAME ). outcome)
96
93
}
@@ -117,4 +114,46 @@ class JavaTaskFunctionalTest {
117
114
Files . copy(Paths . get(" demo" , " java" , " src" , " main" , " resources" , " json" , " address.json" ), addressJson)
118
115
}
119
116
117
+ private static void assertExists (File file ) {
118
+ assertNotNull (file)
119
+ assertTrue (file. exists())
120
+ }
121
+
122
+ private static BuildResult executeRunner (String gradleVersion , Path testProjectDir , String task = COLON_TASK_NAME ) {
123
+ def arguments = GradleRunner . create()
124
+ .withPluginClasspath()
125
+ .withProjectDir(testProjectDir. toFile())
126
+ .withArguments(task, " --info" )
127
+
128
+ if (gradleVersion) {
129
+ arguments. withGradleVersion(gradleVersion)
130
+ }
131
+ return arguments. build()
132
+ }
133
+
134
+ static boolean gradleSupported (String gradleVersion , String javaSpecificationVersion ) {
135
+ def javaVersion = javaSpecificationVersion. toFloat()
136
+ if ( javaVersion < 13 ) { // this includes java '1.8' :)
137
+ return true
138
+ }
139
+
140
+ def parts = gradleVersion. split(/ \. / )
141
+
142
+ if (parts[0 ]. toInteger() < 6 ) {
143
+ return false
144
+ }
145
+
146
+ switch ((int )javaVersion) {
147
+ case 13 : return true
148
+ case 14 : return parts[1 ]. toInteger() >= 3
149
+ case 15 : return parts[1 ]. toInteger() >= 6
150
+ default : return false // no official information on Gradle compatibility with further versions of Java
151
+ }
152
+ }
153
+
154
+ static Stream<Arguments > gradleReleases () {
155
+ String javaSpecificationVersion = System . getProperty(' java.specification.version' )
156
+ return GRADLE_RELEASES . stream(). filter {it -> gradleSupported(it, javaSpecificationVersion) }
157
+ .map {it -> Arguments . of(it)} as Stream<Arguments >
158
+ }
120
159
}
0 commit comments