From 3e38022d5e98c7fa2c6935a7d43ff322b9958a79 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Sat, 9 Aug 2025 21:40:50 +0900 Subject: [PATCH] Sync OutputCapture from Spring Boot See https://github.com/spring-projects/spring-boot/pull/46685 Signed-off-by: Johnny Lim --- .../testsupport/system/OutputCapture.java | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/micrometer-core/src/test/java/io/micrometer/core/testsupport/system/OutputCapture.java b/micrometer-core/src/test/java/io/micrometer/core/testsupport/system/OutputCapture.java index 4dcf380148..9c45869696 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/testsupport/system/OutputCapture.java +++ b/micrometer-core/src/test/java/io/micrometer/core/testsupport/system/OutputCapture.java @@ -48,11 +48,15 @@ class OutputCapture implements CapturedOutput { private final Deque systemCaptures = new ArrayDeque<>(); - private final AtomicReference out = new AtomicReference<>(null); + private final AtomicReference out = new AtomicReference<>(); - private final AtomicReference err = new AtomicReference<>(null); + private final AtomicReference err = new AtomicReference<>(); - private final AtomicReference all = new AtomicReference<>(null); + private final AtomicReference all = new AtomicReference<>(); + + OutputCapture() { + clearExisting(); + } /** * Push a new system capture session onto the stack. @@ -128,20 +132,21 @@ void reset() { } void clearExisting() { - this.out.set(null); - this.err.set(null); - this.all.set(null); + this.out.set(new NoOutput()); + this.err.set(new NoOutput()); + this.all.set(new NoOutput()); } - private String get(AtomicReference existing, Predicate filter) { + private String get(AtomicReference existing, Predicate filter) { Assert.state(!this.systemCaptures.isEmpty(), "No system captures found. Please check your output capture registration."); - String result = existing.get(); - if (result == null) { - result = build(filter); - existing.compareAndSet(null, result); + Object existingOutput = existing.get(); + if (existingOutput instanceof String) { + return (String) existingOutput; } - return result; + String builtOutput = build(filter); + existing.compareAndSet(existingOutput, builtOutput); + return builtOutput; } String build(Predicate filter) { @@ -306,4 +311,8 @@ enum Type { } + static class NoOutput { + + } + }