|
6 | 6 | import java.io.ByteArrayOutputStream; |
7 | 7 | import java.io.PrintStream; |
8 | 8 |
|
9 | | -/** |
10 | | - * A try-with-resources compatible utility for capturing console output. |
11 | | - * Captures both System.out and System.err streams and provides methods |
12 | | - * to retrieve the captured content as strings. |
13 | | - */ |
14 | 9 | public class ConsoleOutput implements AutoCloseable |
15 | 10 | { |
16 | 11 | private final ByteArrayOutputStream outContent; |
17 | 12 | private final ByteArrayOutputStream errContent; |
18 | 13 | private final PrintStream originalOut; |
19 | 14 | private final PrintStream originalErr; |
20 | | - /** |
21 | | - * Creates a new ConsoleOutput instance and begins capturing console output. |
22 | | - * Both System.out and System.err are redirected to internal buffers. |
23 | | - */ |
| 15 | + |
24 | 16 | public ConsoleOutput() |
25 | 17 | { |
26 | | - // Store original streams |
27 | 18 | originalOut = System.out; |
28 | 19 | originalErr = System.err; |
29 | | - // Create capture streams |
30 | 20 | outContent = new ByteArrayOutputStream(); |
31 | 21 | errContent = new ByteArrayOutputStream(); |
32 | | - // Redirect system streams |
33 | 22 | System.setOut(new PrintStream(outContent)); |
34 | 23 | System.setErr(new PrintStream(errContent)); |
35 | 24 | } |
36 | | - /** |
37 | | - * Returns the captured standard output as a string. |
38 | | - * @return The content written to System.out since this ConsoleOutput was created |
39 | | - */ |
| 25 | + |
40 | 26 | public String getOutput() |
41 | 27 | { |
42 | 28 | return outContent.toString(); |
43 | 29 | } |
44 | | - /** |
45 | | - * Returns the captured standard error as a string. |
46 | | - * @return The content written to System.err since this ConsoleOutput was created |
47 | | - */ |
| 30 | + |
48 | 31 | public String getError() |
49 | 32 | { |
50 | 33 | return errContent.toString(); |
51 | 34 | } |
52 | | - /** |
53 | | - * Verifies the captured standard output using ApprovalTests. |
54 | | - * This is a convenience method that calls Approvals.verify() on the captured output. |
55 | | - */ |
| 35 | + |
56 | 36 | public void verifyOutput() |
57 | 37 | { |
58 | 38 | verifyOutput(new Options()); |
59 | 39 | } |
60 | | - /** |
61 | | - * Verifies the captured standard output using ApprovalTests with options. |
62 | | - * This is a convenience method that calls Approvals.verify() on the captured output. |
63 | | - * @param options The options to use for verification |
64 | | - */ |
| 40 | + |
65 | 41 | public void verifyOutput(Options options) |
66 | 42 | { |
67 | 43 | Approvals.verify(getOutput(), options); |
68 | 44 | } |
69 | | - /** |
70 | | - * Verifies the captured standard error using ApprovalTests. |
71 | | - * This is a convenience method that calls Approvals.verify() on the captured error output. |
72 | | - */ |
| 45 | + |
73 | 46 | public void verifyError() |
74 | 47 | { |
75 | 48 | Approvals.verify(getError()); |
76 | 49 | } |
77 | | - /** |
78 | | - * Verifies the captured standard error using ApprovalTests with options. |
79 | | - * This is a convenience method that calls Approvals.verify() on the captured error output. |
80 | | - * @param options The options to use for verification |
81 | | - */ |
| 50 | + |
82 | 51 | public void verifyError(Options options) |
83 | 52 | { |
84 | 53 | Approvals.verify(getError(), options); |
85 | 54 | } |
86 | | - /** |
87 | | - * Verifies both captured standard output and error using ApprovalTests. |
88 | | - * This is a convenience method that calls Approvals.verify() on both the output and error combined. |
89 | | - */ |
| 55 | + |
90 | 56 | public void verifyAll() |
91 | 57 | { |
92 | 58 | verifyAll(new Options()); |
93 | 59 | } |
94 | | - /** |
95 | | - * Verifies both captured standard output and error using ApprovalTests with options. |
96 | | - * This is a convenience method that calls Approvals.verify() on both the output and error combined. |
97 | | - * @param options The options to use for verification |
98 | | - */ |
| 60 | + |
99 | 61 | public void verifyAll(Options options) |
100 | 62 | { |
101 | 63 | String combined = "Output:\n" + getOutput() + "\nError:\n" + getError(); |
102 | 64 | Approvals.verify(combined, options); |
103 | 65 | } |
104 | | - /** |
105 | | - * Restores the original System.out and System.err streams. |
106 | | - * This method is automatically called when used in a try-with-resources block. |
107 | | - */ |
| 66 | + |
108 | 67 | @Override |
109 | 68 | public void close() |
110 | 69 | { |
111 | | - // Restore original streams |
112 | 70 | System.setOut(originalOut); |
113 | 71 | System.setErr(originalErr); |
114 | 72 | } |
|
0 commit comments