|
| 1 | +package de.ids_mannheim.korapxmltools |
| 2 | + |
| 3 | +import org.junit.After |
| 4 | +import org.junit.Before |
| 5 | +import java.io.ByteArrayOutputStream |
| 6 | +import java.io.File |
| 7 | +import java.io.PrintStream |
| 8 | +import java.net.URL |
| 9 | +import kotlin.test.Ignore |
| 10 | +import kotlin.test.Test |
| 11 | +import kotlin.test.assertEquals |
| 12 | +import kotlin.test.assertTrue |
| 13 | +import kotlin.test.assertFalse |
| 14 | + |
| 15 | +class FoundryOverrideTest { |
| 16 | + private val outContent = ByteArrayOutputStream(10000000) |
| 17 | + private val errContent = ByteArrayOutputStream() |
| 18 | + private val originalOut: PrintStream = System.out |
| 19 | + private val originalErr: PrintStream = System.err |
| 20 | + |
| 21 | + @Before |
| 22 | + fun setUpStreams() { |
| 23 | + System.setOut(PrintStream(outContent)) |
| 24 | + System.setErr(PrintStream(errContent)) |
| 25 | + } |
| 26 | + |
| 27 | + @After |
| 28 | + fun restoreStreams() { |
| 29 | + System.setOut(originalOut) |
| 30 | + System.setErr(originalErr) |
| 31 | + } |
| 32 | + |
| 33 | + private fun loadResource(path: String): URL { |
| 34 | + val resource = Thread.currentThread().contextClassLoader.getResource(path) |
| 35 | + requireNotNull(resource) { "Resource $path not found" } |
| 36 | + return resource |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun testFoundryOverrideWithTagger() { |
| 41 | + val outputDir = File.createTempFile("foundry_override_test", "").apply { |
| 42 | + delete() |
| 43 | + mkdirs() |
| 44 | + } |
| 45 | + try { |
| 46 | + val baseZip = loadResource("wud24_sample.zip").path |
| 47 | + val args = arrayOf( |
| 48 | + "-f", |
| 49 | + "-T", "spacy", |
| 50 | + "-q", |
| 51 | + "-D", outputDir.path, |
| 52 | + "-F", "xyz", |
| 53 | + "-t", "zip", |
| 54 | + baseZip |
| 55 | + ) |
| 56 | + |
| 57 | + // Using debug() which calls KorapXmlTool().call() |
| 58 | + val exitCode = debug(args) |
| 59 | + assertEquals(0, exitCode, "Tool execution should succeed") |
| 60 | + |
| 61 | + // Check if the correct file exists |
| 62 | + val expectedFile = File(outputDir, "wud24_sample.xyz.zip") |
| 63 | + assertTrue(expectedFile.exists(), "Output file should be named wud24_sample.xyz.zip") |
| 64 | + |
| 65 | + // Check if the wrong file does NOT exist |
| 66 | + val wrongFile = File(outputDir, "wud24_sample.spacy.zip") |
| 67 | + assertFalse(wrongFile.exists(), "Output file wud24_sample.spacy.zip should NOT exist") |
| 68 | + |
| 69 | + // Check content of the zip |
| 70 | + val zipEntries = org.apache.commons.compress.archivers.zip.ZipFile.builder() |
| 71 | + .setFile(expectedFile) |
| 72 | + .get() |
| 73 | + .use { zip -> |
| 74 | + zip.entries.asSequence().map { it.name }.toList() |
| 75 | + } |
| 76 | + |
| 77 | + assertTrue(zipEntries.any { it.contains("/xyz/morpho.xml") }, "Zip should contain entries with 'xyz' foundry") |
| 78 | + assertFalse(zipEntries.any { it.contains("/spacy/morpho.xml") }, "Zip should NOT contain entries with 'spacy' foundry") |
| 79 | + |
| 80 | + } finally { |
| 81 | + outputDir.deleteRecursively() |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Ignore("This test is ignored until -o is fixed") |
| 86 | + fun testOutputOptionHasPriority() { |
| 87 | + val outputDir = File.createTempFile("output_option_test", "").apply { |
| 88 | + delete() |
| 89 | + mkdirs() |
| 90 | + } |
| 91 | + try { |
| 92 | + val baseZip = loadResource("wud24_sample.zip").path |
| 93 | + val explicitOutputFile = File(outputDir, "my_custom_output.zip") |
| 94 | + val args = arrayOf( |
| 95 | + "-f", |
| 96 | + "-q", |
| 97 | + "-T", "spacy", |
| 98 | + "-o", explicitOutputFile.path, |
| 99 | + "-t", "zip", |
| 100 | + baseZip |
| 101 | + ) |
| 102 | + |
| 103 | + val exitCode = debug(args) |
| 104 | + assertEquals(0, exitCode, "Tool execution should succeed") |
| 105 | + |
| 106 | + assertTrue(explicitOutputFile.exists(), "Explicit output file should exist: ${explicitOutputFile.path}") |
| 107 | + |
| 108 | + // Ensure the default-named file does NOT exist |
| 109 | + val defaultFile = File(outputDir, "wud24_sample.spacy.zip") |
| 110 | + assertFalse(defaultFile.exists(), "Default output file should NOT exist when -o is used") |
| 111 | + |
| 112 | + } finally { |
| 113 | + outputDir.deleteRecursively() |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments