Skip to content

Commit b470018

Browse files
authored
Merge pull request #405 from vlsi/apply_test
test: add test for applying org.jsonschema2dataclass before java plugin
2 parents 4cea25f + 5603535 commit b470018

File tree

4 files changed

+85
-7
lines changed

4 files changed

+85
-7
lines changed

src/main/kotlin/org/jsonschema2dataclass/js2p/Js2pPlugin.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import org.gradle.api.Task
77
import org.gradle.api.file.DirectoryProperty
88
import org.gradle.api.file.FileCollection
99
import org.gradle.api.tasks.TaskProvider
10+
import org.gradle.kotlin.dsl.apply
11+
import org.gradle.kotlin.dsl.the
1012
import org.gradle.util.GradleVersion
1113
import org.jsonschema2dataclass.js2p.support.applyInternalAndroid
1214
import org.jsonschema2dataclass.js2p.support.applyInternalJava
@@ -33,21 +35,33 @@ class Js2pPlugin : Plugin<Project> {
3335

3436
for (pluginId in listOf("java", "java-library")) {
3537
project.plugins.withId(pluginId) {
36-
project.afterEvaluate {
37-
applyInternalJava(pluginExtension, project)
38-
}
38+
project.apply<Js2pJavaPlugin>()
3939
}
4040
}
4141
for (pluginId in androidPlugins) {
4242
project.plugins.withId(pluginId) {
43-
project.afterEvaluate {
44-
applyInternalAndroid(pluginExtension, project)
45-
}
43+
project.apply<Js2pAndroidPlugin>()
4644
}
4745
}
4846
}
4947
}
5048

49+
internal class Js2pJavaPlugin : Plugin<Project> {
50+
override fun apply(project: Project) {
51+
project.afterEvaluate {
52+
applyInternalJava(the(), project)
53+
}
54+
}
55+
}
56+
57+
internal class Js2pAndroidPlugin : Plugin<Project> {
58+
override fun apply(project: Project) {
59+
project.afterEvaluate {
60+
applyInternalAndroid(the(), project)
61+
}
62+
}
63+
}
64+
5165
internal fun setupConfigExecutions(
5266
extension: Js2pExtension,
5367
defaultSourcePath: Path?,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.jsonschema2dataclass.js2p
2+
3+
import org.gradle.testkit.runner.TaskOutcome
4+
import org.junit.jupiter.api.Assertions
5+
import org.junit.jupiter.api.DisplayName
6+
import org.junit.jupiter.api.io.TempDir
7+
import org.junit.jupiter.params.ParameterizedTest
8+
import org.junit.jupiter.params.provider.MethodSource
9+
import org.junit.jupiter.params.provider.NullSource
10+
import java.nio.file.Path
11+
12+
class LateInitializationTest {
13+
@TempDir
14+
lateinit var testProjectDir: Path
15+
16+
@ParameterizedTest(name = "[{index}]({argumentsWithNames}) {displayName}")
17+
@NullSource
18+
@MethodSource("org.jsonschema2dataclass.js2p.TestGradleVersionHolder#gradleReleasesForTests")
19+
@DisplayName("java-library applied after org.jsonschema2dataclass")
20+
fun withoutExtension(gradleVersion: String?) {
21+
writeBuildGradle(
22+
testProjectDir,
23+
"""
24+
plugins {
25+
id 'org.jsonschema2dataclass'
26+
}
27+
// apply plugin: 'org.jsonschema2dataclass'
28+
// Even though it is not recommended to use apply syntax, we use it here
29+
// to ensure java-library plugin is applied after org.jsonschema2dataclass
30+
apply plugin: 'java-library'
31+
""".trimIndent()
32+
)
33+
copyAddressJSON(testProjectDir)
34+
35+
val result = executeRunner(gradleVersion, testProjectDir)
36+
37+
Assertions.assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0)?.outcome)
38+
addressJavaExists(
39+
testProjectDir,
40+
TARGET_FOLDER_DEFAULT,
41+
DEFAULT_EXECUTION_NAME,
42+
PACKAGE_EMPTY
43+
)
44+
}
45+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.jsonschema2dataclass.js2p
2+
3+
import org.gradle.kotlin.dsl.apply
4+
import org.gradle.testfixtures.ProjectBuilder
5+
import org.junit.jupiter.api.Test
6+
7+
class PluginDslTest {
8+
@Test
9+
fun `plugin applies even without java plugin`() {
10+
ProjectBuilder.builder().build().run {
11+
apply(plugin = "org.jsonschema2dataclass")
12+
}
13+
}
14+
}

src/test/kotlin/org/jsonschema2dataclass/js2p/TestCasesGenerator.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jsonschema2dataclass.js2p
22

3+
import org.intellij.lang.annotations.Language
34
import java.io.File
45
import java.nio.file.Files
56
import java.nio.file.Path
@@ -46,6 +47,10 @@ fun writeBuildFiles(testProjectDir: Path, shouldCopyAddressJSON: Boolean, suffix
4647
}
4748
}
4849

50+
fun writeBuildGradle(testProjectDir: Path, @Language("Groovy") string: String) {
51+
testProjectDir.resolve("build.gradle").toFile().writeText(string)
52+
}
53+
4954
fun createBuildFilesSingleSimple(testProjectDir: Path, shouldCopyAddressJSON: Boolean) {
5055
writeBuildFiles(
5156
testProjectDir, shouldCopyAddressJSON,
@@ -122,7 +127,7 @@ fun createBuildFilesSingleSourceInherit(testProjectDir: Path, shouldCopyAddressJ
122127
Files.write(testProjectDir.resolve("settings.gradle"), ByteArray(0))
123128
}
124129

125-
private fun copyAddressJSON(testProjectDir: Path) {
130+
internal fun copyAddressJSON(testProjectDir: Path) {
126131
val jsonDir = testProjectDir.resolve("src/main/resources/json")
127132
File(jsonDir.toString()).mkdirs()
128133
jsonDir.resolve("address.json").toFile().writeText(ADDRESS_JSON)

0 commit comments

Comments
 (0)