LogManagerInModuleTest.java revision 16492:c8db4f1737c4
1/*
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.nio.file.Paths;
25import java.util.logging.Handler;
26import java.util.logging.LogManager;
27import java.util.logging.Logger;
28
29/**
30 * @test
31 * @bug 8172886
32 * @summary Verifies that a custom LogManager or custom Handler can be
33 *          instantiated by the logging system if they are in a package
34 *          that is exported to java.logging by a module.
35 * @build test.logmanager/test.logmanager.TestLogManager
36 *        test.handlers/test.handlers.TestHandler
37 *        test.config/test.config.LogConfig
38 *        LogManagerInModuleTest
39 * @run main/othervm --add-modules test.logmanager,test.handlers
40 *          -Djava.util.logging.manager=test.logmanager.TestLogManager
41 *          LogManagerInModuleTest
42 * @run main/othervm --add-modules test.logmanager,test.handlers,test.config
43 *          -Djava.util.logging.manager=test.logmanager.TestLogManager
44 *          -Djava.util.logging.config.class=test.config.LogConfig
45 *          LogManagerInModuleTest
46 *
47 * @author danielfuchs
48 */
49public class LogManagerInModuleTest {
50
51    public static void main(String[] args) throws Exception {
52        if (System.getProperty("java.util.logging.config.class", null) == null) {
53            System.setProperty("java.util.logging.config.file",
54                Paths.get(System.getProperty("test.src", "src"),
55                          "logging.properties").toString());
56        }
57        // sanity check
58        if (LogManagerInModuleTest.class.getModule().isNamed()) {
59            throw new RuntimeException("Unexpected named module for "
60                  + LogManagerInModuleTest.class + ": "
61                  + LogManagerInModuleTest.class.getModule().getName());
62        }
63
64        // now check that the LogManager was correctly instantiated.
65        LogManager manager = LogManager.getLogManager();
66        System.out.println("LogManager: " + manager);
67        Class<?> logManagerClass = manager.getClass();
68        if (!"test.logmanager".equals(logManagerClass.getModule().getName())) {
69            throw new RuntimeException("Bad module for log manager: "
70                    + logManagerClass.getModule() + "; class is: "
71                    + logManagerClass.getName());
72        }
73
74        Logger logger = Logger.getLogger("com.xyz.foo");
75        Handler[] handlers = logger.getHandlers();
76        if (handlers.length != 1) {
77            throw new RuntimeException("Expected 1 handler, found " + handlers.length);
78        }
79        Class<?> handlerClass = handlers[0].getClass();
80        if (!"test.handlers".equals(handlerClass.getModule().getName())) {
81            throw new RuntimeException("Bad module for handler: "
82                    + handlerClass.getModule() + "; class is: "
83                    + handlerClass.getName());
84        }
85    }
86
87}
88