This is an automated email from the ASF dual-hosted git repository.

tballison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/main by this push:
     new 9d6809a2f1 TIKA-5065: make the external program input tests portable 
-- fix windows (#3633)
9d6809a2f1 is described below

commit 9d6809a2f1fa12e63161778e46dab9931ed47ae6
Author: Tim Allison <[email protected]>
AuthorDate: Tue Nov 17 11:21:44 2026 -0500

    TIKA-5065: make the external program input tests portable -- fix windows 
(#3633)
---
 .../tika/embedder/ExternalEmbedderInputTest.java   |  9 +--
 .../java/org/apache/tika/io/InputFileProbe.java    | 85 ++++++++++++++++++++++
 .../parser/external/ExternalParserInputTest.java   | 18 ++---
 3 files changed, 95 insertions(+), 17 deletions(-)

diff --git 
a/tika-core/src/test/java/org/apache/tika/embedder/ExternalEmbedderInputTest.java
 
b/tika-core/src/test/java/org/apache/tika/embedder/ExternalEmbedderInputTest.java
index 0150586478..dcf3c37fd5 100644
--- 
a/tika-core/src/test/java/org/apache/tika/embedder/ExternalEmbedderInputTest.java
+++ 
b/tika-core/src/test/java/org/apache/tika/embedder/ExternalEmbedderInputTest.java
@@ -23,13 +23,11 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
 import java.io.ByteArrayOutputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.condition.DisabledOnOs;
-import org.junit.jupiter.api.condition.OS;
 import org.junit.jupiter.api.io.TempDir;
 
+import org.apache.tika.io.InputFileProbe;
 import org.apache.tika.io.TikaInputStream;
 import org.apache.tika.metadata.Metadata;
 import org.apache.tika.parser.ParseContext;
@@ -37,17 +35,16 @@ import org.apache.tika.parser.ParseContext;
 public class ExternalEmbedderInputTest {
 
     @Test
-    @DisabledOnOs(OS.WINDOWS)
     public void testInputIsGivenAsInputInItsOwnDirectory(@TempDir Path dir) 
throws Exception {
         Path doc = Files.write(dir.resolve("report.txt"), 
"hello".getBytes(UTF_8));
         ExternalEmbedder embedder = new ExternalEmbedder();
-        embedder.setCommand("echo", ExternalEmbedder.INPUT_FILE_TOKEN);
+        
embedder.setCommand(InputFileProbe.command(ExternalEmbedder.INPUT_FILE_TOKEN));
         embedder.setMetadataCommandArguments(null);
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         try (TikaInputStream tis = TikaInputStream.get(doc)) {
             embedder.embed(new Metadata(), tis, out, new ParseContext());
         }
-        Path given = Paths.get(out.toString(UTF_8).trim());
+        Path given = InputFileProbe.arg(out.toString(UTF_8));
         assertEquals("input", 
given.getFileName().toString().replaceFirst("\\..*", ""));
         assertFalse(given.startsWith(dir));
         assertFalse(Files.exists(given.getParent()));
diff --git a/tika-core/src/test/java/org/apache/tika/io/InputFileProbe.java 
b/tika-core/src/test/java/org/apache/tika/io/InputFileProbe.java
new file mode 100644
index 0000000000..56d7fd04cc
--- /dev/null
+++ b/tika-core/src/test/java/org/apache/tika/io/InputFileProbe.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.io;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Stream;
+
+/**
+ * Stands in for an external program in tests: run in a JVM of its own, it 
reports the file
+ * it was given. No shell, so it behaves the same on every OS.
+ */
+public class InputFileProbe {
+
+    private static final String ARG = "arg=";
+    private static final String ENTRY = "entry=";
+    private static final String CONTENT = "content=";
+
+    public static void main(String[] args) throws IOException {
+        Path input = Paths.get(args[0]);
+        StringBuilder sb = new StringBuilder();
+        sb.append(ARG).append(args[0]).append('\n');
+        try (Stream<Path> entries = Files.list(input.getParent())) {
+            entries.map(p -> p.getFileName().toString()).sorted()
+                    .forEach(name -> 
sb.append(ENTRY).append(name).append('\n'));
+        }
+        sb.append(CONTENT).append(new String(Files.readAllBytes(input), 
UTF_8)).append('\n');
+        System.out.write(sb.toString().getBytes(UTF_8));
+        System.out.flush();
+    }
+
+    /** A command line that runs this class on {@code inputToken}. */
+    public static String[] command(String inputToken) throws 
URISyntaxException {
+        Path classes = 
Paths.get(InputFileProbe.class.getProtectionDomain().getCodeSource()
+                .getLocation().toURI());
+        return new String[]{
+                Paths.get(System.getProperty("java.home"), "bin", 
"java").toString(),
+                "-cp", classes.toString(), InputFileProbe.class.getName(), 
inputToken};
+    }
+
+    /** The path the program was given. */
+    public static Path arg(String stdout) {
+        return Paths.get(values(stdout, ARG).get(0));
+    }
+
+    /** The names in the directory that holds the given path. */
+    public static List<String> entries(String stdout) {
+        return values(stdout, ENTRY);
+    }
+
+    public static String content(String stdout) {
+        return values(stdout, CONTENT).get(0);
+    }
+
+    private static List<String> values(String stdout, String prefix) {
+        List<String> values = new ArrayList<>();
+        for (String line : stdout.split("\r?\n")) {
+            if (line.startsWith(prefix)) {
+                values.add(line.substring(prefix.length()));
+            }
+        }
+        return values;
+    }
+}
diff --git 
a/tika-core/src/test/java/org/apache/tika/parser/external/ExternalParserInputTest.java
 
b/tika-core/src/test/java/org/apache/tika/parser/external/ExternalParserInputTest.java
index 98c729b55a..7b9fcc2258 100644
--- 
a/tika-core/src/test/java/org/apache/tika/parser/external/ExternalParserInputTest.java
+++ 
b/tika-core/src/test/java/org/apache/tika/parser/external/ExternalParserInputTest.java
@@ -21,8 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assumptions.assumeTrue;
 
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -31,13 +29,13 @@ import java.util.List;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 
+import org.apache.tika.io.InputFileProbe;
 import org.apache.tika.io.TikaInputStream;
 import org.apache.tika.metadata.ExternalProcess;
 import org.apache.tika.metadata.HttpHeaders;
 import org.apache.tika.metadata.Metadata;
 import org.apache.tika.parser.ParseContext;
 import org.apache.tika.sax.BodyContentHandler;
-import org.apache.tika.utils.ProcessUtils;
 
 public class ExternalParserInputTest {
 
@@ -45,15 +43,13 @@ public class ExternalParserInputTest {
     @Test
     public void testInputFileNameAndDirectory(@TempDir Path tmp)
             throws Exception {
-        assumeTrue(ProcessUtils.checkCommand(new String[]{"sh", "-c", "exit 
0"}));
         Path document = tmp.resolve("document.bin");
         Files.write(document, "content".getBytes(UTF_8));
         Files.write(tmp.resolve("other.bin"), "other".getBytes(UTF_8));
 
         ExternalParserConfig config = new ExternalParserConfig();
         config.setSupportedTypes(List.of("audio/vnd.wave"));
-        config.setCommandLine(List.of("sh", "-c",
-                "echo \"arg=$0\"; ls \"$(dirname \"$0\")\"; cat \"$0\"", 
"${INPUT_FILE}"));
+        
config.setCommandLine(List.of(InputFileProbe.command(ExternalParser.INPUT_FILE_TOKEN)));
         config.setReturnStdout(true);
         Metadata metadata = new Metadata();
         metadata.set(HttpHeaders.CONTENT_TYPE, "audio/vnd.wave");
@@ -62,11 +58,11 @@ public class ExternalParserInputTest {
                     new ParseContext());
         }
         String stdout = metadata.get(ExternalProcess.STD_OUT);
-        assertTrue(stdout.contains("content"), stdout);
-        assertTrue(stdout.contains("/input.wav\n"), stdout);
-        assertFalse(stdout.contains("document"), stdout);
-        assertFalse(stdout.contains("other"), stdout);
-        assertFalse(stdout.contains(tmp.toString()), stdout);
+        Path given = InputFileProbe.arg(stdout);
+        assertEquals("input.wav", given.getFileName().toString(), stdout);
+        assertFalse(given.startsWith(tmp), stdout);
+        assertEquals(List.of("input.wav"), InputFileProbe.entries(stdout), 
stdout);
+        assertEquals("content", InputFileProbe.content(stdout), stdout);
     }
 
     @Test

Reply via email to