|
| 1 | +package io.github.androa.gradle.plugin.avro |
| 2 | + |
| 3 | +import io.github.androa.gradle.plugin.avro.compiler.AvroCompiler |
| 4 | +import io.github.androa.gradle.plugin.avro.compiler.CompilerOptions |
| 5 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 6 | +import org.junit.jupiter.api.Assertions.assertThrows |
| 7 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 8 | +import org.junit.jupiter.api.Test |
| 9 | +import org.junit.jupiter.api.io.TempDir |
| 10 | +import java.nio.file.Path |
| 11 | + |
| 12 | +class ErrorHandlingTest { |
| 13 | + @field:TempDir |
| 14 | + lateinit var tempDir: Path |
| 15 | + |
| 16 | + @Test |
| 17 | + fun `compiler handles malformed schema files gracefully`() { |
| 18 | + // Setup |
| 19 | + val compiler = AvroCompiler() |
| 20 | + val outputDir = tempDir.resolve("output").toFile().apply { mkdirs() } |
| 21 | + |
| 22 | + // Create a malformed schema file |
| 23 | + val malformedSchema = tempDir.resolve("malformed.avsc").toFile() |
| 24 | + malformedSchema.writeText( |
| 25 | + """ |
| 26 | + { |
| 27 | + "namespace": "com.example", |
| 28 | + "type": "record", |
| 29 | + "name": "Broken", |
| 30 | + "fields": [ |
| 31 | + { "name": "id", "type": "int" }, |
| 32 | + { "name": "invalid" } // Missing type |
| 33 | + ] |
| 34 | + } |
| 35 | + """.trimIndent(), |
| 36 | + ) |
| 37 | + |
| 38 | + // Execute and verify exception is thrown with appropriate message |
| 39 | + val exception = |
| 40 | + assertThrows(Exception::class.java) { |
| 41 | + compiler.compileSchema(CompilerOptions(), setOf(malformedSchema), outputDir) |
| 42 | + } |
| 43 | + |
| 44 | + // Verify error message contains helpful information |
| 45 | + assertTrue( |
| 46 | + exception.message?.contains("malformed") == true || |
| 47 | + exception.message?.contains("schema") == true || |
| 48 | + exception.message?.contains("parse") == true || |
| 49 | + exception.message?.contains("missing") == true || |
| 50 | + exception.message?.contains("type") == true, |
| 51 | + "Exception message should indicate schema parsing issue", |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `compiler handles non-existent input directory gracefully`() { |
| 57 | + // Setup |
| 58 | + val compiler = AvroCompiler() |
| 59 | + val nonExistentDir = tempDir.resolve("nonexistent").toFile() |
| 60 | + val outputDir = tempDir.resolve("output").toFile().apply { mkdirs() } |
| 61 | + |
| 62 | + // Execute |
| 63 | + compiler.compileSchema(CompilerOptions(), setOf(nonExistentDir), outputDir) |
| 64 | + |
| 65 | + // Verify - should not throw exception, and output dir should be empty |
| 66 | + val outputFiles = outputDir.listFiles() ?: emptyArray() |
| 67 | + assertEquals(0, outputFiles.size, "No output should be generated with empty input") |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `task handles empty input directory`() { |
| 72 | + // Setup task |
| 73 | + val project = |
| 74 | + org.gradle.testfixtures.ProjectBuilder |
| 75 | + .builder() |
| 76 | + .withProjectDir(tempDir.toFile()) |
| 77 | + .build() |
| 78 | + val task = project.tasks.create("generateAvro", GenerateAvroTask::class.java) |
| 79 | + |
| 80 | + // Create an empty directory |
| 81 | + val emptyDir = tempDir.resolve("empty").toFile().apply { mkdirs() } |
| 82 | + val outputDir = tempDir.resolve("output").toFile().apply { mkdirs() } |
| 83 | + |
| 84 | + // Configure task |
| 85 | + task.schemas.setFrom(emptyDir) |
| 86 | + task.outputDir.set(outputDir) |
| 87 | + task.intermediateDir.set(tempDir.resolve("intermediate").toFile()) |
| 88 | + |
| 89 | + // Execute - should not throw exception |
| 90 | + task.generate() |
| 91 | + |
| 92 | + // Verify output dir exists but is empty |
| 93 | + assertTrue(outputDir.exists(), "Output directory should exist") |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun `generate task handles malformed avpr file gracefully`() { |
| 98 | + // Setup task |
| 99 | + val project = |
| 100 | + org.gradle.testfixtures.ProjectBuilder |
| 101 | + .builder() |
| 102 | + .withProjectDir(tempDir.toFile()) |
| 103 | + .build() |
| 104 | + val task = project.tasks.create("generateAvro", GenerateAvroTask::class.java) |
| 105 | + |
| 106 | + // Create a malformed protocol file |
| 107 | + val malformedProtocol = tempDir.resolve("malformed.avpr").toFile() |
| 108 | + malformedProtocol.writeText( |
| 109 | + """ |
| 110 | + { |
| 111 | + "protocol": "BrokenService", |
| 112 | + "namespace": "com.example", |
| 113 | + "types": [ |
| 114 | + { |
| 115 | + "type": "record", |
| 116 | + "name": "Broken", |
| 117 | + "fields": [ |
| 118 | + { "name": "id", "type": "string" }, |
| 119 | + { "name": "data" } // Missing type |
| 120 | + ] |
| 121 | + } |
| 122 | + ] |
| 123 | + } |
| 124 | + """.trimIndent(), |
| 125 | + ) |
| 126 | + |
| 127 | + // Configure task |
| 128 | + task.schemas.setFrom(malformedProtocol) |
| 129 | + task.outputDir.set(tempDir.resolve("output").toFile()) |
| 130 | + task.intermediateDir.set(tempDir.resolve("intermediate").toFile()) |
| 131 | + |
| 132 | + // Execute and verify |
| 133 | + val exception = |
| 134 | + assertThrows(Exception::class.java) { |
| 135 | + task.generate() |
| 136 | + } |
| 137 | + |
| 138 | + // Verify error message is helpful |
| 139 | + assertTrue( |
| 140 | + exception.message?.contains("malformed") == true || |
| 141 | + exception.message?.contains("protocol") == true || |
| 142 | + exception.message?.contains("parse") == true || |
| 143 | + exception.message?.contains("missing") == true || |
| 144 | + exception.message?.contains("type") == true, |
| 145 | + "Exception message should indicate protocol parsing issue", |
| 146 | + ) |
| 147 | + } |
| 148 | +} |
0 commit comments