File: LoggerFinderAPITest.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 (498 lines) | stat: -rw-r--r-- 20,748 bytes parent folder | download | duplicates (17)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * Copyright (c) 2015, 2016, 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.
 */

/*
 * @test
 * @bug     8140364
 * @author  danielfuchs
 * @summary JDK implementation specific unit test for JDK internal artifacts.
 *          Tests the consistency of the LoggerFinder and JDK extensions.
 * @modules java.base/sun.util.logging
 *          java.base/jdk.internal.logger
 *          java.logging
 * @run  main LoggerFinderAPITest
 */


import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.function.Supplier;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import sun.util.logging.PlatformLogger;

public class LoggerFinderAPITest {

    static final Class<java.lang.System.Logger> spiLoggerClass
            = java.lang.System.Logger.class;
    static final Class<java.lang.System.Logger> jdkLoggerClass
            = java.lang.System.Logger.class;
    static final Class<sun.util.logging.PlatformLogger.Bridge> bridgeLoggerClass
            = sun.util.logging.PlatformLogger.Bridge.class;
    static final Class<java.util.logging.Logger> julLoggerClass
            = java.util.logging.Logger.class;
    static final Class<sun.util.logging.PlatformLogger.Bridge> julLogProducerClass
            = PlatformLogger.Bridge.class;
    static final Pattern julLogNames = Pattern.compile(
            "^((log(p|rb)?)|severe|warning|info|config|fine|finer|finest|isLoggable)$");
    static final Collection<Method> julLoggerIgnores;
    static {
        List<Method> ignores = new ArrayList<>();
        try {
            ignores.add(julLoggerClass.getDeclaredMethod("log", LogRecord.class));
        } catch (NoSuchMethodException | SecurityException ex) {
            throw new ExceptionInInitializerError(ex);
        }
        julLoggerIgnores = Collections.unmodifiableList(ignores);
    }



    // Don't require LoggerBridge to have a body for those methods
    interface LoggerBridgeMethodsWithNoBody extends
        PlatformLogger.Bridge, java.lang.System.Logger {

        @Override
        public default String getName() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public default boolean isLoggable(PlatformLogger.Level level) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public default void log(sun.util.logging.PlatformLogger.Level level,
                         String msg, Throwable thrown) {
        }
        @Override
        public default void log(sun.util.logging.PlatformLogger.Level level,
                         Throwable thrown, Supplier<String> msgSupplier) {
        }
        @Override
        public default void log(sun.util.logging.PlatformLogger.Level level,
                         Supplier<String> msgSupplier) {
        }
        @Override
        public default void log(sun.util.logging.PlatformLogger.Level level, String msg) {
        }
        @Override
        public default void log(sun.util.logging.PlatformLogger.Level level,
                         String format, Object... params) {
        }
        @Override
        public default void logrb(sun.util.logging.PlatformLogger.Level level,
                         ResourceBundle bundle, String key, Throwable thrown) {
        }
        @Override
        public default void logrb(sun.util.logging.PlatformLogger.Level level,
                         ResourceBundle bundle, String format, Object... params) {
        }

        @Override
        public default void logrb(PlatformLogger.Level level,
                         String sourceClass, String sourceMethod,
                         ResourceBundle bundle, String msg, Throwable thrown) {
        }

        @Override
        public default void logrb(PlatformLogger.Level level, String sourceClass,
                         String sourceMethod, ResourceBundle bundle, String msg,
                         Object... params) {
        }

        @Override
        public default void logp(PlatformLogger.Level level, String sourceClass,
                         String sourceMethod, Supplier<String> msgSupplier) {
        }

        @Override
        public default void logp(PlatformLogger.Level level, String sourceClass,
                         String sourceMethod, String msg, Object... params) {
        }

        @Override
        public default void logp(PlatformLogger.Level level, String sourceClass,
                         String sourceMethod, String msg, Throwable thrown) {
        }

        @Override
        public default void logp(PlatformLogger.Level level, String sourceClass,
                         String sourceMethod, String msg) {
        }

        @Override
        public default void logp(PlatformLogger.Level level, String sourceClass,
                         String sourceMethod, Throwable thrown,
                         Supplier<String> msgSupplier) {
        }

        static boolean requiresDefaultBodyFor(Method m) {
            try {
                Method m2 = LoggerBridgeMethodsWithNoBody.class
                        .getDeclaredMethod(m.getName(),
                        m.getParameterTypes());
                return !m2.isDefault();
            } catch (NoSuchMethodException x) {
                return true;
            }
        }
    }

    final boolean warnDuplicateMappings;
    public LoggerFinderAPITest(boolean verbose) {
        this.warnDuplicateMappings = verbose;
        for (Handler h : Logger.getLogger("").getHandlers()) {
            if (h instanceof ConsoleHandler) {
                Logger.getLogger("").removeHandler(h);
            }
        }
        Logger.getLogger("").addHandler( new Handler() {
            @Override
            public void publish(LogRecord record) {
                StringBuilder builder = new StringBuilder();
                builder.append("GOT LogRecord: ")
                        .append(record.getLevel().getLocalizedName())
                        .append(": [").append(record.getLoggerName())
                        .append("] ").append(record.getSourceClassName())
                        .append('.')
                        .append(record.getSourceMethodName()).append(" -> ")
                        .append(record.getMessage())
                        .append(' ')
                        .append(record.getParameters() == null ? ""
                                : Arrays.toString(record.getParameters()))
                        ;
                System.out.println(builder);
                if (record.getThrown() != null) {
                    record.getThrown().printStackTrace(System.out);
                }
            }
            @Override public void flush() {}
            @Override public void close() {}
        });
    }

    public Stream<Method> getJulLogMethodStream(Class<?> loggerClass) {

        return Stream.of(loggerClass.getMethods()).filter((x) -> {
            final Matcher m = julLogNames.matcher(x.getName());
            return m.matches() ? x.getAnnotation(Deprecated.class) == null : false;
        });
    }

    /**
     * Tells whether a method invocation of 'origin' can be transformed in a
     * method invocation of 'target'.
     * This method only look at the parameter signatures, it doesn't look at
     * the name, nor does it look at the return types.
     * <p>
     * Example:
     * <ul>
     *     <li>java.util.logging.Logger.log(Level, String, Object) can be invoked as<br>
         java.util.logging.spi.Logger.log(Level, String, Object...) because the
         last parameter in 'target' is a varargs.</li>
     *     <li>java.util.logging.Logger.log(Level, String) can also be invoked as<br>
         java.util.logging.spi.Logger.log(Level, String, Object...) for the
         same reason.</li>
     * </ul>
     * <p>
     * The algorithm is tailored for our needs: when the last parameter in the
     * target is a vararg, and when origin & target have the same number of
     * parameters, then we consider that the types of the last parameter *must*
     * match.
     * <p>
     * Similarly - we do not consider that o(X x, Y y, Y y) matches t(X x, Y... y)
     * although strictly speaking, it should...
     *
     * @param origin The method in the original class
     * @param target The correspondent candidate in the target class
     * @return true if a method invocation of 'origin' can be transformed in a
     * method invocation of 'target'.
     */
    public boolean canBeInvokedAs(Method origin, Method target,
                                  Map<Class<?>,Class<?>> substitutes) {
        final Class<?>[] xParams = target.getParameterTypes();
        final Class<?>[] mParams = Stream.of(origin.getParameterTypes())
                .map((x) -> substitutes.getOrDefault(x, x))
                .collect(Collectors.toList()).toArray(new Class<?>[0]);
        if (Arrays.deepEquals(xParams, mParams)) return true;
        if (target.isVarArgs()) {
            if (xParams.length == mParams.length) {
                if (xParams[xParams.length-1].isArray()) {
                    return mParams[mParams.length -1].equals(
                            xParams[xParams.length -1].getComponentType());
                }
            } else if (xParams.length == mParams.length + 1) {
                return Arrays.deepEquals(
                        Arrays.copyOfRange(xParams, 0, xParams.length-1), mParams);
            }
        }
        return false;
    }

    /**
     * Look whether {@code otherClass} has a public method similar to m
     * @param m
     * @param otherClass
     * @return
     */
    public Stream<Method> findInvokable(Method m, Class<?> otherClass) {
        final Map<Class<?>,Class<?>> substitues =
                Collections.singletonMap(java.util.logging.Level.class,
                        sun.util.logging.PlatformLogger.Level.class);
        return Stream.of(otherClass.getMethods())
                .filter((x) -> m.getName().equals(x.getName()))
                .filter((x) -> canBeInvokedAs(m, x, substitues));
    }

    /**
     * Test that the concrete Logger implementation passed as parameter
     * overrides all the methods defined by its interface.
     * @param julLogger A concrete implementation of System.Logger
     *    whose backend is a JUL Logger.
     */
    StringBuilder testDefaultJULLogger(java.lang.System.Logger julLogger) {
        final StringBuilder errors = new StringBuilder();
        if (!bridgeLoggerClass.isInstance(julLogger)) {
            final String errorMsg =
                    "Logger returned by LoggerFactory.getLogger(\"foo\") is not a "
                    + bridgeLoggerClass + "\n\t" + julLogger;
            System.err.println(errorMsg);
            errors.append(errorMsg).append('\n');
        }
        final Class<? extends java.lang.System.Logger> xClass = julLogger.getClass();
        List<Method> notOverridden =
                Stream.of(bridgeLoggerClass.getDeclaredMethods()).filter((m) -> {
            try {
                Method x = xClass.getDeclaredMethod(m.getName(), m.getParameterTypes());
                return x == null;
            } catch (NoSuchMethodException ex) {
                return !Modifier.isStatic(m.getModifiers());
            }
        }).collect(Collectors.toList());
        notOverridden.stream().filter((x) -> {
            boolean shouldOverride = true;
            try {
                final Method m = xClass.getMethod(x.getName(), x.getParameterTypes());
                Method m2 = null;
                try {
                    m2 = jdkLoggerClass.getDeclaredMethod(x.getName(), x.getParameterTypes());
                } catch (Exception e) {

                }
                shouldOverride = m.isDefault() || m2 == null;
            } catch (Exception e) {
                // should override.
            }
            return shouldOverride;
        }).forEach(x -> {
            final String errorMsg = xClass.getName() + " should override\n\t" + x.toString();
            System.err.println(errorMsg);
            errors.append(errorMsg).append('\n');
        });
        if (notOverridden.isEmpty()) {
            System.out.println(xClass + " overrides all methods from " + bridgeLoggerClass);
        }
        return errors;
    }

    public static class ResourceBundeParam extends ResourceBundle {
        Map<String, String> map = Collections.synchronizedMap(new LinkedHashMap<>());
        @Override
        protected Object handleGetObject(String key) {
            map.putIfAbsent(key, "${"+key+"}");
            return map.get(key);
        }

        @Override
        public Enumeration<String> getKeys() {
            return Collections.enumeration(new LinkedHashSet<>(map.keySet()));
        }

    }

    final ResourceBundle bundleParam =
            ResourceBundle.getBundle(ResourceBundeParam.class.getName());

    public static class ResourceBundeLocalized extends ResourceBundle {
        Map<String, String> map = Collections.synchronizedMap(new LinkedHashMap<>());
        @Override
        protected Object handleGetObject(String key) {
            map.putIfAbsent(key, "Localized:${"+key+"}");
            return map.get(key);
        }

        @Override
        public Enumeration<String> getKeys() {
            return Collections.enumeration(new LinkedHashSet<>(map.keySet()));
        }

    }

    final static ResourceBundle bundleLocalized =
            ResourceBundle.getBundle(ResourceBundeLocalized.class.getName());

    final Map<Class<?>, Object> params = new HashMap<>();
    {
        params.put(String.class, "TestString");
        params.put(sun.util.logging.PlatformLogger.Level.class, sun.util.logging.PlatformLogger.Level.WARNING);
        params.put(java.lang.System.Logger.Level.class, java.lang.System.Logger.Level.WARNING);
        params.put(ResourceBundle.class, bundleParam);
        params.put(Throwable.class, new Throwable("TestThrowable (Please ignore it!)"));
        params.put(Object[].class, new Object[] {"One", "Two"});
        params.put(Object.class, new Object() {
            @Override public String toString() { return "I am an object!"; }
        });
    }

    public Object[] getParamsFor(Method m) {
        final Object[] res = new Object[m.getParameterCount()];
        final Class<?>[] sig = m.getParameterTypes();
        if (res.length == 0) {
            return res;
        }
        for (int i=0; i<res.length; i++) {
            Object p = params.get(sig[i]);
            if (p == null && sig[i].equals(Supplier.class)) {
                final String msg = "SuppliedMsg["+i+"]";
                p = (Supplier<String>) () -> msg;
            }
            if (p instanceof String) {
                res[i] = String.valueOf(p)+"["+i+"]";
            } else {
                res[i] = p;
            }
        }
        return res;
    }

    public void invokeOn(java.lang.System.Logger logger, Method m) {
        Object[] p = getParamsFor(m);
        try {
            m.invoke(logger, p);
        } catch (Exception e) {
            throw new RuntimeException("Failed to invoke "+m.toString(), e);
        }
    }

    public void testAllJdkExtensionMethods(java.lang.System.Logger logger) {
        Stream.of(jdkLoggerClass.getDeclaredMethods())
                .filter(m -> !Modifier.isStatic(m.getModifiers()))
                .forEach((m) -> invokeOn(logger, m));
    }

    public void testAllAPIMethods(java.lang.System.Logger logger) {
        Stream.of(spiLoggerClass.getDeclaredMethods())
                .filter(m -> !Modifier.isStatic(m.getModifiers()))
                .forEach((m) -> invokeOn(logger, m));
    }

    public void testAllBridgeMethods(java.lang.System.Logger logger) {
        Stream.of(bridgeLoggerClass.getDeclaredMethods())
                .filter(m -> !Modifier.isStatic(m.getModifiers()))
                .forEach((m) -> invokeOn(logger, m));
    }

    public void testAllLogProducerMethods(java.lang.System.Logger logger) {
        Stream.of(julLogProducerClass.getDeclaredMethods())
                .filter(m -> !Modifier.isStatic(m.getModifiers()))
                .forEach((m) -> invokeOn(logger, m));
    }

    public StringBuilder testGetLoggerOverriddenOnSpi() {
        final StringBuilder errors = new StringBuilder();
        Stream.of(jdkLoggerClass.getDeclaredMethods())
                .filter(m -> Modifier.isStatic(m.getModifiers()))
                .filter(m -> Modifier.isPublic(m.getModifiers()))
                .filter(m -> !m.getName().equals("getLoggerFinder"))
                .filter(m -> {
                    try {
                        final Method x = bridgeLoggerClass.getDeclaredMethod(m.getName(), m.getParameterTypes());
                        return x == null;
                    } catch (NoSuchMethodException ex) {
                        return true;
                    }
                }).forEach(m -> {
                    final String errorMsg = bridgeLoggerClass.getName() + " should override\n\t" + m.toString();
                    System.err.println(errorMsg);
                    errors.append(errorMsg).append('\n');
                });
        if (errors.length() == 0) {
            System.out.println(bridgeLoggerClass + " overrides all static methods from " + jdkLoggerClass);
        } else {
            if (errors.length() > 0) throw new RuntimeException(errors.toString());
        }
        return errors;
    }

    public static void main(String argv[]) throws Exception {
        final LoggerFinderAPITest test = new LoggerFinderAPITest(false);
        final StringBuilder errors = new StringBuilder();
        errors.append(test.testGetLoggerOverriddenOnSpi());
        java.lang.System.Logger julLogger =
                java.lang.System.LoggerFinder.getLoggerFinder()
                        .getLogger("foo", LoggerFinderAPITest.class.getModule());
        errors.append(test.testDefaultJULLogger(julLogger));
        if (errors.length() > 0) throw new RuntimeException(errors.toString());
        java.lang.System.Logger julSystemLogger =
                java.lang.System.LoggerFinder.getLoggerFinder()
                        .getLogger("bar", Thread.class.getModule());
        errors.append(test.testDefaultJULLogger(julSystemLogger));
        if (errors.length() > 0) throw new RuntimeException(errors.toString());
        java.lang.System.Logger julLocalizedLogger =
                (java.lang.System.Logger)
                System.getLogger("baz", bundleLocalized);
        java.lang.System.Logger julLocalizedSystemLogger =
                java.lang.System.LoggerFinder.getLoggerFinder()
                        .getLocalizedLogger("oof", bundleLocalized, Thread.class.getModule());
        final String error = errors.toString();
        if (!error.isEmpty()) throw new RuntimeException(error);
        for (java.lang.System.Logger logger : new java.lang.System.Logger[] {
            julLogger, julSystemLogger, julLocalizedLogger, julLocalizedSystemLogger
        }) {
            test.testAllJdkExtensionMethods(logger);
            test.testAllAPIMethods(logger);
            test.testAllBridgeMethods(logger);
            test.testAllLogProducerMethods(logger);
        }
    }

}