Skip to content

Commit dc6410c

Browse files
hnc-jgleeclaude
authored andcommitted
test(cli): assert friendly message contract on hybrid fail-fast (PDFDLOSP-21)
CodeRabbit nit on PR #511: the regression guarded against stack-trace leakage but didn't lock in the friendly message contract this PR promises. CLIMain emits the hybrid unavailability message via LOGGER.SEVERE, which JUL routes to stderr — out of reach of the existing stdout-only capture helper. Attach a LogRecord handler scoped to the CLIMain logger so the test can assert that the captured SEVERE message contains both 'Hybrid server is not available' and the '--hybrid-fallback' hint, while keeping the existing stdout stack-trace guard intact. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f7100e1 commit dc6410c

1 file changed

Lines changed: 42 additions & 11 deletions

File tree

  • java/opendataloader-pdf-cli/src/test/java/org/opendataloader/pdf/cli

java/opendataloader-pdf-cli/src/test/java/org/opendataloader/pdf/cli/CLIMainTest.java

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,10 @@ private static Path createPasswordProtectedPdf(Path target, String password) thr
371371

372372
/**
373373
* When the hybrid backend is unreachable and {@code --hybrid-fallback} is
374-
* NOT supplied, CLIMain must surface the friendly fail-fast message on
375-
* stdout (including the new {@code --hybrid-fallback} hint) and must never
376-
* leak a Java stack trace. Regression for PDFDLOSP-21.
374+
* NOT supplied, CLIMain must surface the friendly fail-fast message
375+
* (including the new {@code --hybrid-fallback} hint) via the SEVERE log
376+
* record and must never leak a Java stack trace to stdout. Regression for
377+
* PDFDLOSP-21.
377378
*/
378379
@Test
379380
void testHybridUnavailableWithoutFallbackEmitsFriendlyMessageNoStackTrace() throws IOException {
@@ -392,20 +393,50 @@ void testHybridUnavailableWithoutFallbackEmitsFriendlyMessageNoStackTrace() thro
392393
}
393394

394395
String[] stdoutHolder = new String[1];
395-
int exitCode = (int) runCapturingStdout(() -> CLIMain.run(new String[]{
396-
"--hybrid", "docling-fast",
397-
"--hybrid-mode", "full",
398-
"--hybrid-url", "http://127.0.0.1:" + closedPort,
399-
"--output", tempDir.toString(),
400-
testPdf.toString()
401-
}), stdoutHolder);
396+
StringBuilder logCapture = new StringBuilder();
397+
java.util.logging.Logger cliLogger = java.util.logging.Logger.getLogger(
398+
"org.opendataloader.pdf.cli.CLIMain");
399+
java.util.logging.Handler captureHandler = new java.util.logging.Handler() {
400+
@Override public void publish(java.util.logging.LogRecord record) {
401+
logCapture.append(record.getMessage()).append('\n');
402+
Throwable thrown = record.getThrown();
403+
while (thrown != null) {
404+
logCapture.append(thrown).append('\n');
405+
for (StackTraceElement frame : thrown.getStackTrace()) {
406+
logCapture.append("\tat ").append(frame).append('\n');
407+
}
408+
thrown = thrown.getCause();
409+
}
410+
}
411+
@Override public void flush() {}
412+
@Override public void close() {}
413+
};
414+
cliLogger.addHandler(captureHandler);
415+
int exitCode;
416+
try {
417+
exitCode = (int) runCapturingStdout(() -> CLIMain.run(new String[]{
418+
"--hybrid", "docling-fast",
419+
"--hybrid-mode", "full",
420+
"--hybrid-url", "http://127.0.0.1:" + closedPort,
421+
"--output", tempDir.toString(),
422+
testPdf.toString()
423+
}), stdoutHolder);
424+
} finally {
425+
cliLogger.removeHandler(captureHandler);
426+
}
402427

403428
assertNotEquals(0, exitCode,
404429
"Exit code must be non-zero when hybrid backend is unreachable and fallback is off");
405430

406431
String out = stdoutHolder[0];
407432
assertFalse(out.contains("\tat "),
408-
"Output must not contain a Java stack trace ('\\tat '); got: " + out);
433+
"stdout must not contain a Java stack trace ('\\tat '); got: " + out);
434+
435+
String logged = logCapture.toString();
436+
assertTrue(logged.contains("Hybrid server is not available"),
437+
"Log output should explain that the hybrid backend is unreachable; got: " + logged);
438+
assertTrue(logged.contains("--hybrid-fallback"),
439+
"Log output should include the --hybrid-fallback recovery hint; got: " + logged);
409440
}
410441

411442
// --- PDFDLOSP-14: magic-number guard regressions ----------------------

0 commit comments

Comments
 (0)