1 /*
2  * Copyright (c) 2013, 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 
24 import java.util.logging.*;
25 
26 /*
27  * @test
28  * @bug 8010727
29  * @summary  LogManager.addLogger should succeed to add a logger named ""
30  *           if LogManager.getLogger("") returns null.
31  *
32  * @run main LogManagerInstanceTest
33  */
34 
35 public class LogManagerInstanceTest {
main(String[] argv)36     public static void main(String[] argv) {
37         LogManager mgr = LogManager.getLogManager();
38         if (getRootLogger(mgr) == null) {
39             throw new RuntimeException("Root logger not exist");
40         }
41 
42         SecondLogManager mgr2 = new SecondLogManager();
43         Logger root = getRootLogger(mgr2);
44         if (mgr2.base != root) {
45             throw new RuntimeException(mgr2.base + " is not the root logger");
46         }
47     }
48 
getRootLogger(LogManager mgr)49     private static Logger getRootLogger(LogManager mgr) {
50         Logger l = mgr.getLogger("");
51         if (l != null && !l.getName().isEmpty()) {
52             throw new RuntimeException(l.getName() + " is not an invalid root logger");
53         }
54         return l;
55     }
56 
57     static class SecondLogManager extends LogManager {
58         final Logger base;
SecondLogManager()59         private SecondLogManager() {
60             Logger root = getLogger("");
61             if (root == null) {
62                 root = new BaseLogger("", null);
63                 if (!super.addLogger(root))
64                     throw new RuntimeException("Fail to addLogger " + root);
65             } else {
66                 throw new RuntimeException("Root logger already exists");
67             }
68             this.base = root;
69         }
70     }
71     static class BaseLogger extends Logger {
BaseLogger(String name, String rbname)72         BaseLogger(String name, String rbname) {
73             super(name, rbname);
74         }
75     }
76 }
77