test/jdk/java/lang/System/LoggerFinder/internal/SystemLoggerInPlatformLoader/SystemLoggerInPlatformLoader.java
author amlu
Fri, 12 Jan 2018 14:09:50 +0800
changeset 48519 482ede6c4936
parent 47216 71c04702a3d5
child 52407 9a9d7c8d9e88
permissions -rw-r--r--
8194959: Correct test tag to move bugid from @test to @bug Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41818
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
     1
/*
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
     4
 *
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    10
 *
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    15
 * accompanied this code).
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    16
 *
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    20
 *
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    23
 * questions.
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    24
 */
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    25
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    26
import java.io.File;
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    27
import java.io.FileInputStream;
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    28
import java.io.IOException;
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    29
import java.io.InputStream;
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    30
import java.lang.reflect.InvocationTargetException;
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    31
import java.lang.reflect.Method;
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    32
42453
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
    33
/*
48519
482ede6c4936 8194959: Correct test tag to move bugid from @test to @bug
amlu
parents: 47216
diff changeset
    34
 * @test
482ede6c4936 8194959: Correct test tag to move bugid from @test to @bug
amlu
parents: 47216
diff changeset
    35
 * @bug 8163162
41818
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    36
 * @summary Checks that LazyLoggers are returned for System.Logger instances
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    37
 *          created by modules in the platform class loader.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41818
diff changeset
    38
 * @modules java.base/java.lang:open
41818
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    39
 * @build systempkg.log.SystemLoggerAccessor SystemLoggerInPlatformLoader
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    40
 * @run main/othervm SystemLoggerInPlatformLoader
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    41
 * @author danielfuchs
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    42
 */
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    43
public class SystemLoggerInPlatformLoader {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    44
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    45
    static final class PlatformClassLoaderChild extends ClassLoader {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    46
        private PlatformClassLoaderChild() {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    47
            super(ClassLoader.getPlatformClassLoader());
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    48
        }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    49
        public Class<?> definePlatformClass(String name) throws IOException {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    50
            String testClasses = System.getProperty("test.classes", "./build/classes");
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    51
            String fname = name.replace('.', '/').concat(".class");
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    52
            try (InputStream is = new FileInputStream(new File(testClasses, fname))) {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    53
                byte[] b = is.readAllBytes();
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    54
                ClassLoader parent = getParent();
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    55
                try {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    56
                    Method m = ClassLoader.class.getDeclaredMethod("defineClass",
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    57
                        String.class, byte[].class, int.class, int.class);
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    58
                    m.setAccessible(true);
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    59
                    return (Class<?>)m.invoke(parent, name, b, 0, b.length);
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    60
                } catch (NoSuchMethodException
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    61
                        | IllegalAccessException
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    62
                        | InvocationTargetException ex) {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    63
                    throw new IOException(ex);
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    64
                }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    65
            }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    66
        }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    67
        static final PlatformClassLoaderChild INSTANCE = new PlatformClassLoaderChild();
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    68
        static Class<?> loadLoggerAccessor() throws IOException {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    69
            return INSTANCE.definePlatformClass("systempkg.log.SystemLoggerAccessor");
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    70
        }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    71
    }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    72
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    73
    static final Class<?> LOGGER_ACCESSOR_CLASS;
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    74
    static {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    75
        try {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    76
            LOGGER_ACCESSOR_CLASS = PlatformClassLoaderChild.loadLoggerAccessor();
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    77
            ClassLoader platformCL = ClassLoader.getPlatformClassLoader();
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    78
            if (LOGGER_ACCESSOR_CLASS.getClassLoader() != platformCL) {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    79
                throw new ExceptionInInitializerError(
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    80
                    "Could not load accessor class in platform class loader: "
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    81
                     + LOGGER_ACCESSOR_CLASS.getClassLoader());
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    82
            }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    83
        } catch (IOException ex) {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    84
            throw new ExceptionInInitializerError(ex);
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    85
        }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    86
    }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    87
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    88
    // Returns a system logger created on behalf of a class loaded by the
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    89
    // Platform ClassLoader
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    90
    static System.Logger getSystemLogger(String name) {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    91
        try {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    92
            return (System.Logger)LOGGER_ACCESSOR_CLASS.getMethod(
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    93
                    "getSystemLogger", String.class).invoke(null, name);
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    94
        } catch (NoSuchMethodException
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    95
                | IllegalAccessException
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    96
                | InvocationTargetException ex) {
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    97
            throw new RuntimeException("Failed to invoke LoggerAccessor.getJULLogger", ex);
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    98
        }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
    99
    }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
   100
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
   101
    public static void main(String[] args) {
42453
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   102
        System.Logger platformLogger = getSystemLogger("bar"); // for a platform class
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   103
        System.Logger appLogger = System.getLogger("bar"); // for an application class
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   104
        if (appLogger == platformLogger) {
41818
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
   105
            throw new RuntimeException("Same loggers");
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
   106
        }
42453
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   107
        Class<?> platformLoggerType = platformLogger.getClass();
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   108
        System.out.println("platformLogger: " + platformLoggerType);
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42453
diff changeset
   109
        boolean simpleConsoleOnly = !ModuleLayer.boot().findModule("java.logging").isPresent();
42453
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   110
        if (simpleConsoleOnly) {
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   111
            /* Happens if the test is called with custom JDK without java.logging module
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   112
               or in case usage commandline option --limit-modules java.base */
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   113
            if (!platformLoggerType.getSimpleName().equals("SimpleConsoleLogger")) {
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   114
                throw new RuntimeException(platformLoggerType.getSimpleName()
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   115
                      + ": unexpected class for platform logger"
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   116
                      + " (expected a simple console logger class)");
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   117
            }
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   118
        } else {
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   119
            if (!platformLoggerType.getSimpleName().equals("JdkLazyLogger")) {
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   120
                throw new RuntimeException(platformLoggerType.getSimpleName()
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   121
                      + ": unexpected class for platform logger"
41818
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
   122
                      + " (expected a lazy logger for a platform class)");
42453
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   123
            }
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   124
            Class<?> appLoggerType = appLogger.getClass();
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   125
            System.out.println("appLogger: " + appLoggerType);
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   126
            if (appLoggerType.equals(platformLoggerType)) {
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   127
                throw new RuntimeException(appLoggerType
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   128
                      + ": unexpected class for application logger"
41818
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
   129
                      + " (a lazy logger was not expected"
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
   130
                      + " for a non platform class)");
42453
ba352752dea6 8170664: SystemLoggerInPlatformLoader.java failing in case of module limitation
skovalev
parents: 42338
diff changeset
   131
            }
41818
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
   132
        }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
   133
    }
30bacee4827a 8163162: The separation between system loggers and application loggers should take the extension loader in consideration.
dfuchs
parents:
diff changeset
   134
}