jdk/test/java/util/logging/LogManagerInstanceTest.java
author dfuchs
Tue, 02 Jul 2013 11:30:31 +0200
changeset 18593 40715ebb6681
parent 18269 349b1570d2cb
permissions -rw-r--r--
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger Summary: This patch makes sure that LoggerContext instances created for applets have a root and global logger. Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18269
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
     1
/*
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
     4
 *
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
     8
 *
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    13
 * accompanied this code).
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    14
 *
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    18
 *
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    21
 * questions.
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    22
 */
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    23
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    24
import java.util.logging.*;
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    25
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    26
/*
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    27
 * @test
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    28
 * @bug 8010727
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    29
 * @summary  LogManager.addLogger should succeed to add a logger named ""
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    30
 *           if LogManager.getLogger("") returns null.
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    31
 *
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    32
 * @run main LogManagerInstanceTest
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    33
 */
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    34
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    35
public class LogManagerInstanceTest {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    36
    public static void main(String[] argv) {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    37
        LogManager mgr = LogManager.getLogManager();
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    38
        if (getRootLogger(mgr) == null) {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    39
            throw new RuntimeException("Root logger not exist");
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    40
        }
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    41
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    42
        SecondLogManager mgr2 = new SecondLogManager();
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    43
        Logger root = getRootLogger(mgr2);
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    44
        if (mgr2.base != root) {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    45
            throw new RuntimeException(mgr2.base + " is not the root logger");
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    46
        }
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    47
    }
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    48
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    49
    private static Logger getRootLogger(LogManager mgr) {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    50
        Logger l = mgr.getLogger("");
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    51
        if (l != null && !l.getName().isEmpty()) {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    52
            throw new RuntimeException(l.getName() + " is not an invalid root logger");
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    53
        }
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    54
        return l;
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    55
    }
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    56
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    57
    static class SecondLogManager extends LogManager {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    58
        final Logger base;
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    59
        private SecondLogManager() {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    60
            Logger root = getLogger("");
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    61
            if (root == null) {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    62
                root = new BaseLogger("", null);
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    63
                if (!super.addLogger(root))
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    64
                    throw new RuntimeException("Fail to addLogger " + root);
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    65
            } else {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18269
diff changeset
    66
                throw new RuntimeException("Root logger already exists");
18269
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    67
            }
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    68
            this.base = root;
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    69
        }
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    70
    }
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    71
    static class BaseLogger extends Logger {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    72
        BaseLogger(String name, String rbname) {
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    73
            super(name, rbname);
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    74
        }
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    75
    }
349b1570d2cb 8010727: WLS fails to add a logger with "" in its own LogManager subclass instance
mchung
parents:
diff changeset
    76
}