@@ -2,137 +2,81 @@ package io.github.androa.gradle.plugin.avro
22
33import org.gradle.testkit.runner.GradleRunner
44import org.junit.jupiter.api.Assertions.assertTrue
5- import org.junit.jupiter.api.Test
65import org.junit.jupiter.api.io.TempDir
6+ import org.junit.jupiter.params.ParameterizedTest
7+ import org.junit.jupiter.params.provider.ValueSource
78import java.io.File
9+ import java.nio.file.Files
10+ import java.nio.file.StandardCopyOption
811
912class AvroPluginFunctionalTest {
1013 @field:TempDir
1114 lateinit var projectDir: File
1215
13- @Test
14- fun `plugin generates code successfully` () {
15- // Create a minimal settings file so that Gradle recognizes the project.
16+ @ParameterizedTest
17+ @ValueSource(
18+ strings = [
19+ " zero-config.gradle.kts" ,
20+ " default.gradle.kts" ,
21+ " custom-paths.gradle.kts" ,
22+ ],
23+ )
24+ fun `plugin generates code successfully with different build configs` (buildConfigFile : String ) {
25+ // Create a minimal settings file
1626 projectDir.resolve(" settings.gradle" ).writeText(" " )
1727
18- // Create a minimal build.gradle.kts that applies the plugin
19- projectDir.resolve(" build.gradle.kts" ).writeText(
20- """
21- import org.apache.avro.compiler.specific.SpecificCompiler
22-
23- plugins {
24- id("io.github.androa.gradle.plugin.avro")
25- kotlin("jvm") version "2.1.20"
26- }
27-
28- generateAvro {
29- noSetters = true
30- // Both assign and set() is possible
31- addNullSafeAnnotations.set(true)
32- encoding = "UTF-8"
33- fieldVisibility = SpecificCompiler.FieldVisibility.PRIVATE
34-
35- //schemas.from(project.fileTree("src/main/avro"))
36- outputDir.set(layout.buildDirectory.dir("generated-avro"))
37- }
38- """ .trimIndent(),
39- )
28+ // Copy the build config from resources
29+ copyResourceTo(" build-configs/$buildConfigFile " , " build.gradle.kts" )
4030
41- // Create a dummy Avro schema file
42- projectDir.resolve(" src/main/avro/" ).apply {
43- mkdirs()
44- resolve(" schema.avsc" ).writeText(
45- // language=AvroSchema
46- """
47- {
48- "namespace": "com.example",
49- "type": "record",
50- "name": "Dummy",
51- "fields": [
52- { "name": "id", "type": "int" },
53- { "name": "age", "type": [ "null", "int" ] }
54- ]
55- }
56- """ .trimIndent(),
57- )
31+ // For the custom paths test, we need to adjust the directory structure
32+ if (buildConfigFile == " custom-paths.gradle.kts" ) {
33+ setupSchemaFiles(" custom-avro-path" )
34+ runAndVerify(" build/custom-output-dir" )
35+ } else {
36+ setupSchemaFiles()
37+ runAndVerify()
5838 }
39+ }
5940
60- // Create a dummy Avro protocol file
61- projectDir.resolve(" src/main/avro/" ).apply {
62- mkdirs()
63- resolve(" protocol.avpr" ).writeText(
64- // language=AvroSchema
65- """
66- {
67- "protocol": "UserService",
68- "namespace": "com.example.avro",
69- "types": [
70- {
71- "type": "record",
72- "name": "User",
73- "fields": [
74- {
75- "name": "id",
76- "type": "string"
77- },
78- {
79- "name": "name",
80- "type": "string"
81- },
82- {
83- "name": "email",
84- "type": "string",
85- "default": ""
86- }
87- ]
88- }
89- ],
90- "messages": {
91- "getUser": {
92- "request": [
93- {
94- "name": "id",
95- "type": "string"
96- }
97- ],
98- "response": "User"
99- },
100- "createUser": {
101- "request": [
102- {
103- "name": "user",
104- "type": "User"
105- }
106- ],
107- "response": "string"
108- }
109- }
110- }
111- """ .trimIndent(),
112- )
113- }
41+ /* *
42+ * Helper function to copy a resource file to the test project directory
43+ */
44+ private fun copyResourceTo (
45+ resourcePath : String ,
46+ targetPath : String ,
47+ ) {
48+ val inputStream =
49+ javaClass.classLoader.getResourceAsStream(resourcePath)
50+ ? : throw IllegalArgumentException (" Resource not found: $resourcePath " )
11451
115- // Create a dummy Avro IDL file
116- projectDir.resolve(" src/main/avro/" ).apply {
117- mkdirs()
118- resolve(" protocol.avdl" ).writeText(
119- // language=AvroIDL
120- """
121- @namespace("com.example.avro")
122- protocol RecieptService {
123- record Receipt {
124- string id;
125- string name;
126- string email = "";
127- }
52+ val targetFile = projectDir.resolve(targetPath)
53+ targetFile.parentFile.mkdirs()
12854
129- Receipt getReciept(string id);
130- string createReciept(Receipt reciept);
131- }
132- """ .trimIndent(),
133- )
134- }
55+ Files .copy(
56+ inputStream,
57+ targetFile.toPath(),
58+ StandardCopyOption .REPLACE_EXISTING ,
59+ )
60+ }
61+
62+ /* *
63+ * Helper function to set up schema files in the project directory
64+ */
65+ private fun setupSchemaFiles (targetDir : String = "src/main/avro") {
66+ // Copy AVSC schema
67+ copyResourceTo(" schemas/avsc/schema.avsc" , " $targetDir /schema.avsc" )
13568
69+ // Copy AVPR protocol
70+ copyResourceTo(" schemas/avpr/protocol.avpr" , " $targetDir /protocol.avpr" )
71+
72+ // Copy AVDL file
73+ copyResourceTo(" schemas/avdl/protocol.avdl" , " $targetDir /protocol.avdl" )
74+ }
75+
76+ /* *
77+ * Helper function to run the generateAvro task and verify outputs
78+ */
79+ private fun runAndVerify (outputDir : String = "build/generated/sources/avro") {
13680 // Run the generateAvro task
13781 GradleRunner
13882 .create()
@@ -142,19 +86,22 @@ class AvroPluginFunctionalTest {
14286 .build()
14387
14488 // Verify that the output directory was created and contains generated files
145- val outputDir = projectDir.resolve(" build/generated-avro " )
89+ val generatedDir = projectDir.resolve(outputDir )
14690 assertTrue(
147- outputDir .exists() && outputDir .listFiles()?.isNotEmpty() == true ,
148- " Expected generated Avro code in ${outputDir .absolutePath} " ,
91+ generatedDir .exists() && generatedDir .listFiles()?.isNotEmpty() == true ,
92+ " Expected generated Avro code in ${generatedDir .absolutePath} " ,
14993 )
15094
151- with (outputDir.walk().filter { it.extension == " java" }) {
152- assertTrue(any { it.name == " Dummy.java" }, " Expected Dummy.java in ${outputDir.absolutePath} " )
153- assertTrue(any { it.name == " User.java" }, " Expected User.java in ${outputDir.absolutePath} " )
154- assertTrue(any { it.name == " UserService.java" }, " Expected UserService.java in ${outputDir.absolutePath} " )
95+ with (generatedDir.walk().filter { it.extension == " java" }) {
96+ assertTrue(any { it.name == " Dummy.java" }, " Expected Dummy.java in ${generatedDir.absolutePath} " )
97+ assertTrue(any { it.name == " User.java" }, " Expected User.java in ${generatedDir.absolutePath} " )
98+ assertTrue(
99+ any { it.name == " UserService.java" },
100+ " Expected UserService.java in ${generatedDir.absolutePath} " ,
101+ )
155102 assertTrue(
156103 any { it.name == " RecieptService.java" },
157- " Expected RecieptService.java in ${outputDir .absolutePath} " ,
104+ " Expected RecieptService.java in ${generatedDir .absolutePath} " ,
158105 )
159106 }
160107 }
0 commit comments