Skip to content

Commit c80a635

Browse files
committed
Test on various supported gradle versions, drop hamcrest dependency
1 parent d20175e commit c80a635

File tree

4 files changed

+87
-52
lines changed

4 files changed

+87
-52
lines changed

build.gradle

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,15 @@ gradlePlugin {
3333
}
3434

3535
dependencies {
36-
runtimeOnly(gradleApi())
37-
3836
implementation 'org.codehaus.groovy:groovy-all:3.0.7'
3937
implementation 'org.jsonschema2pojo:jsonschema2pojo-core:1.1.0'
40-
38+
4139
testImplementation platform('org.junit:junit-bom:5.7.1')
42-
testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
4340

4441
testImplementation("org.junit.jupiter:junit-jupiter-api")
45-
testImplementation gradleTestKit()
46-
42+
testImplementation("org.junit.jupiter:junit-jupiter-params")
4743
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
44+
testImplementation gradleTestKit()
4845
}
4946

5047
test {
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Sat Mar 13 18:16:58 CET 2021
2+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
13
distributionBase=GRADLE_USER_HOME
24
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
4-
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6+
zipStoreBase=GRADLE_USER_HOME

src/main/groovy/com/github/eirnym/js2p/JsonSchemaPlugin.groovy

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import org.gradle.api.Plugin
2020
import org.gradle.api.Project
2121
import org.gradle.api.file.ConfigurableFileCollection
2222
import org.gradle.api.plugins.JavaPluginConvention
23-
import org.gradle.api.tasks.SourceSet
2423

2524
import java.nio.file.Paths
2625

@@ -30,7 +29,6 @@ import java.nio.file.Paths
3029
class JsonSchemaPlugin implements Plugin<Project> {
3130
private static String BASE_FOLDER = 'generated/sources/js2d'
3231
public static String TASK_NAME = 'generateJsonSchema2DataClass'
33-
public static String COLON_TASK_NAME = ':' + TASK_NAME
3432

3533
@Override
3634
void apply(Project project) {
Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
package com.github.eirnym.js2p
22

3+
import org.gradle.testkit.runner.BuildResult
34
import org.gradle.testkit.runner.GradleRunner
45
import org.gradle.testkit.runner.TaskOutcome
56
import org.junit.jupiter.api.BeforeEach
67
import org.junit.jupiter.api.DisplayName
7-
import org.junit.jupiter.api.Test
88
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
913

1014
import java.nio.file.Files
1115
import java.nio.file.Path
1216
import java.nio.file.Paths
17+
import java.util.stream.Stream
1318

14-
import static com.github.eirnym.js2p.JsonSchemaPlugin.COLON_TASK_NAME
1519
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.*
2121

2222
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+
2329
@TempDir
2430
public Path testProjectDir
2531
private Path buildFile
@@ -29,68 +35,59 @@ class JavaTaskFunctionalTest {
2935
buildFile = testProjectDir.resolve("build.gradle")
3036
}
3137

32-
@Test
33-
void withoutExtension() {
38+
@ParameterizedTest
39+
@NullSource
40+
@MethodSource('gradleReleases')
41+
void withoutExtension(String gradleVersion) {
3442
createBuildFiles()
3543
copyAddressJSON()
3644

37-
def result = GradleRunner.create()
38-
.withPluginClasspath()
39-
.withProjectDir(testProjectDir.toFile())
40-
.withArguments(TASK_NAME, "--info")
41-
.build()
45+
def result = executeRunner(gradleVersion, testProjectDir)
4246

4347
def js2pDir = testProjectDir.resolve("build/generated/sources/js2d")
4448
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())
4751
}
4852

49-
@Test
53+
54+
@ParameterizedTest
55+
@NullSource
56+
@MethodSource('gradleReleases')
5057
@DisplayName("compileJava task depends task even when project has no java code")
51-
void noJavaCode() {
58+
void noJavaCode(String gradleVersion) {
5259
createBuildFiles()
5360
copyAddressJSON()
5461

55-
def result = GradleRunner.create()
56-
.withPluginClasspath()
57-
.withProjectDir(testProjectDir.toFile())
58-
.withArguments("compileJava", "--info")
59-
.build()
62+
def result = executeRunner(gradleVersion, testProjectDir, "compileJava")
6063

6164
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME).outcome)
6265
}
6366

64-
@Test
67+
@ParameterizedTest
68+
@NullSource
69+
@MethodSource('gradleReleases')
6570
@DisplayName('task is cache-able')
66-
void taskIsCachable() {
71+
void taskIsCachable(String gradleVersion) {
6772
createBuildFiles()
6873
copyAddressJSON()
6974

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)
8079

8180
assertEquals(TaskOutcome.UP_TO_DATE, result.task(COLON_TASK_NAME).outcome)
8281
}
8382

84-
@Test
83+
@ParameterizedTest
84+
@NullSource
85+
@MethodSource('gradleReleases')
8586
@DisplayName('task skips if no json file exists')
86-
void noJsonFiles() {
87+
void noJsonFiles(String gradleVersion) {
8788
createBuildFiles()
8889

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)
9491

9592
assertEquals(TaskOutcome.NO_SOURCE, result.task(COLON_TASK_NAME).outcome)
9693
}
@@ -117,4 +114,46 @@ class JavaTaskFunctionalTest {
117114
Files.copy(Paths.get("demo", "java", "src", "main", "resources", "json", "address.json"), addressJson)
118115
}
119116

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+
}
120159
}

0 commit comments

Comments
 (0)