File: BootstrapLoggerAPIsTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (286 lines) | stat: -rw-r--r-- 12,258 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
/*
 * Copyright (c) 2015, 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.PrintStream;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.Enumeration;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import jdk.internal.logger.BootstrapLogger;
import jdk.internal.logger.LazyLoggers;

/*
 * @test
 * @bug     8144460 8144214
 * @summary Cover the logXX and LogEvent.valueOf APIs of BootstrapLogger
 *          and logXX APIs of SimpleConsoleLogger.
 * @modules java.base/jdk.internal.logger:+open
 *          java.base/sun.util.logging
 * @build BootstrapLoggerUtils LogStream
 * @run main/othervm BootstrapLoggerAPIsTest
 */

public class BootstrapLoggerAPIsTest {

    private static final LogStream ERR = new LogStream();

    public static void main(String[] args) throws Exception {

        final ContentManager MGR = new ContentManager();

        // private reflection hook that allows us to simulate a non booted VM
        final AtomicBoolean VM_BOOTED = new AtomicBoolean(false);

        BootstrapLoggerUtils.setBootedHook(() -> VM_BOOTED.get());

        // We replace System.err to check the messages that have been logged
        // by the JUL ConsoleHandler and default SimpleConsoleLogger
        // implementaion
        System.setErr(new PrintStream(ERR));

        VM_BOOTED.getAndSet(false);
        if (BootstrapLogger.isBooted()) {
            throw new RuntimeException("VM should not be booted!");
        }

        final Logger LOGGER =
                LazyLoggers.getLogger("foo.bar", Thread.class.getModule());
        final sun.util.logging.PlatformLogger.Level PLATFORM_LEVEL =
                sun.util.logging.PlatformLogger.Level.SEVERE;
        final MyResources BUNDLE = new MyResources();

        /*
         * Test logXX APIs for interface java.lang.System.Logger. Log content
         * before VM is booted should be retained. Log content after VM was
         * booted should be flushed instantly. VM is not booted in first round
         * of loop, VM is booted in second round of loop.
         */
        for (int i = 0; i < 2; i++) {
            boolean booted = BootstrapLogger.isBooted();

            // make sure there is no [remaining] content in the LogStream.
            MGR.failLog("xyz", "throwable #", "MyClass_#", "MyMethod_#");

            /*
             * test logXX APIs for interface java.lang.System.Logger.
             */
            // void log(java.lang.System$Logger$Level,java.util.ResourceBundle,
            //          java.lang.String,java.lang.Throwable)
            LOGGER.log(Level.ERROR, BUNDLE, "abc #0", new RuntimeException("throwable #0"));
            MGR.checkLog(booted, "xyz #0", "throwable #0");

            // void log(java.lang.System$Logger$Level,java.util.ResourceBundle,
            //          java.lang.String,java.lang.Object[])
            LOGGER.log(Level.ERROR, BUNDLE, "abc #1");
            MGR.checkLog(booted, "xyz #1");

            // void log(java.lang.System$Logger$Level,java.lang.String,java.lang.Object[])
            LOGGER.log(Level.ERROR, BUNDLE, "abc {0}", "#2");
            MGR.checkLog(booted, "xyz #2");

            // void log(java.lang.System$Logger$Level,java.lang.String,java.lang.Throwable)
            LOGGER.log(Level.ERROR, "xyz #3", new RuntimeException("throwable #3"));
            MGR.checkLog(booted, "xyz #3", "throwable #3");

            // void log(java.lang.System$Logger$Level,java.util.function.Supplier)
            LOGGER.log(Level.ERROR, () -> "xyz #4");
            MGR.checkLog(booted, "xyz #4");

            // void log(java.lang.System$Logger$Level,java.lang.Object)
            LOGGER.log(Level.ERROR, new MyObject("xyz #5"));
            MGR.checkLog(booted, "xyz #5");

            // void log(java.lang.System$Logger$Level,java.util.function.Supplier,
            //          java.lang.Throwable)
            LOGGER.log(Level.ERROR, () -> "xyz #6", new RuntimeException("throwable #6"));
            MGR.checkLog(booted, "xyz #6", "throwable #6");


            /*
             * test logXX APIs for interface
             * sun.util.logging.PlatformLogger.Bridge.
             */
            sun.util.logging.PlatformLogger.Bridge bridge =
                    (sun.util.logging.PlatformLogger.Bridge) LOGGER;

            // void log(sun.util.logging.PlatformLogger$Level,java.lang.String)
            bridge.log(PLATFORM_LEVEL, "xyz #7");
            MGR.checkLog(booted, "xyz #7");

            // void log(sun.util.logging.PlatformLogger$Level,java.lang.String,java.lang.Throwable)
            bridge.log(PLATFORM_LEVEL, "xyz #8", new RuntimeException("throwable #8"));
            MGR.checkLog(booted, "xyz #8", "throwable #8");

            // void log(sun.util.logging.PlatformLogger$Level,java.lang.String,java.lang.Object[])
            bridge.log(PLATFORM_LEVEL, "xyz {0}", "#9");
            MGR.checkLog(booted, "xyz #9");

            // void log(sun.util.logging.PlatformLogger$Level,java.util.function.Supplier)
            bridge.log(PLATFORM_LEVEL, () -> "xyz #10");
            MGR.checkLog(booted, "xyz #10");

            // void log(sun.util.logging.PlatformLogger$Level,
            //        java.lang.Throwable,java.util.function.Supplier)
            bridge.log(PLATFORM_LEVEL, new RuntimeException("throwable #11"), () -> "xyz #11");
            MGR.checkLog(booted, "xyz #11", "throwable #11");

            // void logp(sun.util.logging.PlatformLogger$Level,java.lang.String,
            //          java.lang.String,java.lang.String)
            bridge.logp(PLATFORM_LEVEL, "MyClass_#12", "MyMethod_#12", "xyz #12");
            MGR.checkLog(booted, "xyz #12", "MyClass_#12", "MyMethod_#12");

            // void logp(sun.util.logging.PlatformLogger$Level,java.lang.String,
            //          java.lang.String,java.util.function.Supplier)
            bridge.logp(PLATFORM_LEVEL, "MyClass_#13", "MyMethod_#13", () -> "xyz #13");
            MGR.checkLog(booted, "xyz #13", "MyClass_#13", "MyMethod_#13");

            // void logp(sun.util.logging.PlatformLogger$Level,java.lang.String,
            //          java.lang.String,java.lang.String,java.lang.Object[])
            bridge.logp(PLATFORM_LEVEL, "MyClass_#14", "MyMethod_#14", "xyz {0}", "#14");
            MGR.checkLog(booted, "xyz #14", "MyClass_#14", "MyMethod_#14");

            // void logp(sun.util.logging.PlatformLogger$Level,java.lang.String,
            //          java.lang.String,java.lang.String,java.lang.Throwable)
            bridge.logp(PLATFORM_LEVEL, "MyClass_#15", "MyMethod_#15",
                    "xyz #15", new RuntimeException("throwable #15"));
            MGR.checkLog(booted, "xyz #15", "throwable #15", "MyClass_#15", "MyMethod_#15");

            // void logp(sun.util.logging.PlatformLogger$Level,java.lang.String,
            //          java.lang.String,java.lang.Throwable,java.util.function.Supplier)
            bridge.logp(PLATFORM_LEVEL, "MyClass_#16", "MyMethod_#16",
                    new RuntimeException("throwable #16"), () -> "xyz #16");
            MGR.checkLog(booted, "xyz #16", "throwable #16", "MyClass_#16", "MyMethod_#16");

            // void logrb(sun.util.logging.PlatformLogger$Level,java.lang.String,java.lang.String,
            //          java.util.ResourceBundle,java.lang.String,java.lang.Object[])
            bridge.logrb(PLATFORM_LEVEL, "MyClass_#17", "MyMethod_#17",
                    BUNDLE, "abc {0}", "#17");
            MGR.checkLog(booted, "xyz #17", "MyClass_#17", "MyMethod_#17");

            // void logrb(sun.util.logging.PlatformLogger$Level,java.lang.String,java.lang.String,
            //          java.util.ResourceBundle,java.lang.String,java.lang.Throwable)
            bridge.logrb(PLATFORM_LEVEL, "MyClass_#18", "MyMethod_#18",
                    BUNDLE, "abc #18", new RuntimeException("throwable #18"));
            MGR.checkLog(booted, "xyz #18", "throwable #18", "MyClass_#18", "MyMethod_#18");

            // void logrb(sun.util.logging.PlatformLogger$Level,java.util.ResourceBundle,
            //          java.lang.String,java.lang.Object[])
            bridge.logrb(PLATFORM_LEVEL, BUNDLE, "abc {0}", "#19");
            MGR.checkLog(booted, "xyz #19");

            // void logrb(sun.util.logging.PlatformLogger$Level,java.util.ResourceBundle,
            //          java.lang.String,java.lang.Throwable)
            bridge.logrb(PLATFORM_LEVEL, BUNDLE, "abc #20",
                        new RuntimeException("throwable #20"));
            MGR.checkLog(booted, "xyz #20", "throwable #20");

            /*
             * retained log content should be flushed after VM is booted.
             */
            if (!booted) {
                VM_BOOTED.getAndSet(true);
                // trigger the flush, make sure to call LOGGER.log(...)
                // after VM_BOOTED.getAndSet(true) and before MGR.assertCachedLog()
                LOGGER.log(Level.ERROR, "VM was just booted! This log should flush the cached logs.");
                MGR.assertCachedLog();
            }
        }
    }

    private static class ContentManager {
        final List<String[]> cached = new ArrayList<String[]>();
        String[] last;

        public void cache() {
            cached.add(last);
        }

        public ContentManager failLog(String... nonexistent) {
            last = nonexistent;
            for (String c : nonexistent) {
                if (ERR.drain().contains(c)) {
                    throw new RuntimeException("Content \"" + nonexistent
                            + "\" should not exist in the log!");
                }
            }
            return this;
        }

        public void assertLog(String... logs) {
            String log = ERR.drain();
            for (String str : logs) {
                if (!log.contains(str)) {
                    throw new RuntimeException("Content \"" + str + "\" does not exist in the log!");
                }
            }
        }

        public void checkLog(boolean booted, String... logs) {
            if (!booted) {
                failLog(logs).cache();
            } else {
                assertLog(logs);
            }
        }

        public void assertCachedLog() {
            String log = ERR.drain();
            for (String[] arr : cached) {
                for (String c : arr) {
                    if (!log.contains(c)) {
                        throw new RuntimeException("Content \"" + c + "\" does not exist in the log!");
                    }
                }
            }
        }
    }

    private static class MyObject {
        String str;

        public MyObject(String str) {
            this.str = str;
        }

        public String toString() {
            return str;
        }
    }

    private static class MyResources extends ResourceBundle {
        public Object handleGetObject(String key) {
            if (key.contains("abc #") || key.contains("abc {")) {
                return key.replaceAll("abc ", "xyz ");
            }
            return null;
        }

        public Enumeration<String> getKeys() {
            return null;
        }
    }
}