File: DebugLoggerTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (333 lines) | stat: -rw-r--r-- 13,599 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.ref.Reference;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Predicate;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jdk.internal.net.http.common.Utils;

import static java.nio.charset.StandardCharsets.UTF_8;

/*
 * @test
 * @summary Verify the behaviour of the debug logger.
 * @library /test/lib /test/jdk/java/net/httpclient/lib
 * @build jdk.httpclient.test.lib.common.HttpServerAdapters jdk.test.lib.net.SimpleSSLContext
 *        DebugLoggerTest
 * @run main/othervm  DebugLoggerTest
 * @run main/othervm -Djdk.internal.httpclient.debug=errr  DebugLoggerTest
 * @run main/othervm -Djdk.internal.httpclient.debug=err  DebugLoggerTest ERR
 * @run main/othervm -Djdk.internal.httpclient.debug=out  DebugLoggerTest OUT
 * @run main/othervm -Djdk.internal.httpclient.debug=log  DebugLoggerTest LOG
 * @run main/othervm -Djdk.internal.httpclient.debug=true DebugLoggerTest ERR LOG
 * @run main/othervm -Djdk.internal.httpclient.debug=err,OUT  DebugLoggerTest ERR OUT
 * @run main/othervm -Djdk.internal.httpclient.debug=err,out,log DebugLoggerTest ERR OUT LOG
 * @run main/othervm -Djdk.internal.httpclient.debug=true,log DebugLoggerTest ERR LOG
 * @run main/othervm -Djdk.internal.httpclient.debug=true,out DebugLoggerTest ERR OUT LOG
 * @run main/othervm -Djdk.internal.httpclient.debug=err,OUT,foo  DebugLoggerTest ERR OUT
 */
public class DebugLoggerTest {
    static final PrintStream stdErr = System.err;
    static final PrintStream stdOut = System.out;
    static final String LOGGER_NAME = "jdk.internal.httpclient.debug";
    static final String MESSAGE = "May the luck of the Irish be with you!";
    static final String MESSAGE2 = "May the wind be at your back!";
    static final String MESSAGE3 = "May the sun shine warm upon your face!";

    static RecordingPrintStream err = null;
    static RecordingPrintStream out = null;

    /**
     * A {@code RecordingPrintStream} is a {@link PrintStream} that makes
     * it possible to record part of the data stream in memory while still
     * forwarding everything to a delegated {@link OutputStream}.
     * @apiNote
     * For instance, a {@code RecordingPrintStream} might be used as an
     * interceptor to record anything printed on {@code System.err}
     * at specific times. Recording can be started and stopped
     * at any time, and multiple times. For instance, a typical
     * usage might be:
     * <pre>static final PrintStream stderr = System.err;
     * static final RecordingPrintString recorder =
     *     new RecordingPrintStream(stderr, true, UTF_8);
     * static {
     *     System.setErr(recorder);
     * }
     *
     * ...
     *      // ....
     *      recorder.startRecording();
     *      try {
     *          // do something
     *          String str1 = recorder.drainRecordedData();
     *          // do something else
     *          String str2 = recorder.drainRecordedData();
     *          // ....
     *      } finally {
     *          recorder.stopRecording();
     *      }
     *      // ....
     * ...
     * </pre>
     * <p>Though the methods are thread safe, {@link #startRecording()}
     * {@link #stopRecording()} and {@link #drainRecordedData()} must
     * not be called concurrently by different threads without external
     * orchestration, as calling these methods mutate the state of
     * the recorder in a way that can be globally observed by all
     * threads.
     */
    public static final class RecordingPrintStream extends PrintStream {
        private final Charset charset;
        private final ByteArrayOutputStream recordedData;
        private volatile boolean recording;

        /**
         * Creates a new {@code RecordingPrintStream} instance that wraps
         * the provided {@code OutputStream}.
         * @implSpec Calls {@link PrintStream#PrintStream(
         *            OutputStream, boolean, Charset)}.
         * @param out An {@code OutputStream} instance to which all bytes will
         *            be forwarded.
         * @param autoFlush Whether {@linkplain PrintStream#PrintStream(
         *            OutputStream, boolean, Charset) autoFlush} is on.
         * @param charset A {@linkplain Charset} used to transform text to
         *                bytes and bytes to string.
         */
        public RecordingPrintStream(OutputStream out, boolean autoFlush, Charset charset) {
            super(out, autoFlush, charset);
            this.charset = charset;
            recordedData = new ByteArrayOutputStream();
        }

        /**
         * Flushes the stream and starts recording.
         * If recording is already started, this method has no effect beyond
         * {@linkplain PrintStream#flush() flushing} the stream.
         */
        public void startRecording() {
            flush(); // make sure everything that was printed before is flushed
            synchronized (recordedData) {
                recording = true;
            }
        }

        /**
         * Flushes the stream and stops recording.
         * If recording is already stopped, this method has no effect beyond
         * {@linkplain PrintStream#flush() flushing} the stream.
         */
        public void stopRecording() {
            flush(); // make sure everything that was printed before is flushed
            synchronized (recordedData) {
                recording = false;
            }
        }

        /**
         * Flushes the stream, drains the recorded data, convert it
         * to a string, and returns it. This has the effect of
         * flushing any recorded data from memory: the next call
         * to {@code drainRecordedData()} will not return any data
         * previously returned.
         * This method can be called regardless of whether recording
         * is started or stopped.
         */
        public String drainRecordedData() {
            flush(); // make sure everything that was printed before is flushed
            synchronized (recordedData) {
                String data = recordedData.toString(charset);
                recordedData.reset();
                return data;
            }
        }

        @Override
        public void write(int b) {
            super.write(b);
            if (recording) {
                synchronized (recordedData) {
                    if (recording) recordedData.write(b);
                }
            }
        }

        @Override
        public void write(byte[] buf, int off, int len) {
            super.write(buf, off, len);
            if (recording) {
                synchronized (recordedData) {
                    if (recording) recordedData.write(buf, off, len);
                }
            }
        }
    }

    static class TestHandler extends Handler {
        final CopyOnWriteArrayList logs = new CopyOnWriteArrayList();
        TestHandler() {
            setLevel(Level.ALL);
        }
        @Override
        public void publish(LogRecord record) {
            logs.add(record);
        }
        @Override
        public void flush() {
        }
        @Override
        public void close() throws SecurityException {
        }
    }

    enum Destination {OUT, ERR, LOG}

    static Set<Destination> getDestinations(String prop) {
        if (prop == null) return Set.of();
        String[] values = prop.split(",");
        if (values.length == 0) return Set.of();
        Set<Destination> dest = HashSet.newHashSet(3);
        for (var v : values) {
            v = v.trim().toLowerCase(Locale.ROOT);
            if ("log".equals(v)) dest.add(Destination.LOG);
            if ("err".equals(v)) dest.add(Destination.ERR);
            if ("out".equals(v)) dest.add(Destination.OUT);
            if ("true".equals(v)) dest.addAll(EnumSet.of(Destination.ERR, Destination.LOG));
        }
        return Set.copyOf(dest);
    }

    public static void main(String[] args) {
        err =  new RecordingPrintStream(stdErr, true, UTF_8);
        out =  new RecordingPrintStream(stdOut, true, UTF_8);
        System.setErr(err);
        System.setOut(out);
        TestHandler logHandler = new TestHandler();
        Logger julLogger = Logger.getLogger(LOGGER_NAME);
        julLogger.setLevel(Level.ALL);
        julLogger.setUseParentHandlers(false);
        julLogger.addHandler(logHandler);
        System.Logger sysLogger = System.getLogger(LOGGER_NAME);
        var debug = Utils.getDebugLogger(() -> "DebugLoggerTest", Utils.DEBUG);
        String prop = System.getProperty(LOGGER_NAME);
        stdOut.printf("DebugLoggerTest: [\"%s\", %s] start%n", prop, Arrays.asList(args));
        var dest = getDestinations(prop);
        var dest2 = Stream.of(args)
                .map(Destination::valueOf)
                .collect(Collectors.toSet());
        assertEquals(debug.on(), !dest.isEmpty());
        assertEquals(dest, dest2);

        Predicate<LogRecord> matcher1 = (r) -> r.getMessage() != null && r.getMessage().contains(MESSAGE);
        doTest(() -> debug.log(MESSAGE), debug, logHandler, dest, MESSAGE, matcher1);
        Exception thrown = new Exception(MESSAGE3);
        Predicate<LogRecord> matcher2 = (r) -> r.getMessage() != null
                && r.getMessage().contains(MESSAGE2)
                && thrown.equals(r.getThrown());
        doTest(() -> debug.log(MESSAGE2, thrown), debug, logHandler, dest, MESSAGE2, matcher2);
        stdOut.printf("Test [\"%s\", %s] passed%n", prop, Arrays.asList(args));
        Reference.reachabilityFence(julLogger);
    }

    private static void doTest(Runnable test,
                               System.Logger logger,
                               TestHandler logHandler,
                               Set<Destination> dest,
                               String msg,
                               Predicate<LogRecord> logMatcher) {
        logHandler.logs.clear();
        err.drainRecordedData();
        out.drainRecordedData();

        err.startRecording();
        out.startRecording();
        test.run();
        err.stopRecording();
        out.stopRecording();
        String outStr = out.drainRecordedData();
        String errStr = err.drainRecordedData();
        List<LogRecord> logs = logHandler.logs.stream().toList();

        if (!(logger instanceof jdk.internal.net.http.common.Logger debug)) {
            throw new AssertionError("Unexpected logger type for: " + logger);
        }
        assertEquals(debug.on(), !dest.isEmpty(), "Unexpected debug.on() for " + dest);
        assertEquals(debug.isLoggable(System.Logger.Level.DEBUG), !dest.isEmpty());
        if (dest.contains(Destination.ERR)) {
            if (!errStr.contains(msg)) {
                throw new AssertionError("stderr does not contain the expected message");
            }
        } else if (errStr.contains(msg)) {
            throw new AssertionError("stderr should not contain the log message");
        }
        if (dest.contains(Destination.OUT)) {
            if (!outStr.contains(msg)) {
                throw new AssertionError("stdout does not contain the expected message");
            }
        } else if (outStr.contains(msg)) {
            throw new AssertionError("stdout should not contain the log message");
        }
        boolean logMatches = logs.stream().anyMatch(logMatcher);
        if (dest.contains(Destination.LOG)) {
            if (!logMatches) {
                throw new AssertionError("expected message not found in logs");
            }
        } else {
            if (logMatches) {
                throw new AssertionError("logs should not contain the message!");
            }
        }
    }

    static void assertEquals(Object o1, Object o2) {
        if (!Objects.equals(o1, o2)) {
            throw new AssertionError("Not equals: \""
                    + o1 + "\" != \"" + o2 + "\"");
        }
    }
    static void assertEquals(Object o1, Object o2, String message) {
        if (!Objects.equals(o1, o2)) {
            throw new AssertionError(message + ": \""
                    + o1 + "\" != \"" + o2 + "\"");
        }
    }


}