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 */
23import java.util.ArrayList;
24import java.util.List;
25import java.util.logging.Logger;
26
27/**
28 * @test
29 * @bug 8005899
30 * @build TestLogger testlogger.MyResource
31 * @run main/othervm TestLogger
32 * @run main/othervm -Dsecurity=on TestLogger
33 **/
34public class TestLogger {
35
36    public static final String RESOURCE_BUNDLE = "testlogger.MyResource";
37    public static final String ORG_LOGGER = "org";
38    public static final String FOO_LOGGER = ORG_LOGGER + ".foo.Foo";
39    public static final String BAR_LOGGER = ORG_LOGGER + ".bar.Bar";
40    public static final String GEE_LOGGER = ORG_LOGGER + ".gee.Gee";
41    public static final String GEE_GEE_LOGGER = GEE_LOGGER+".Gee";
42
43    public static void main(String[] args) {
44        final String security = System.getProperty("security", "off");
45        System.out.println("Security is " + security);
46        if ("on".equals(security)) {
47           System.setSecurityManager(new SecurityManager());
48        }
49
50        newLogger(FOO_LOGGER, RESOURCE_BUNDLE);
51        newLogger(FOO_LOGGER);
52        newLogger(BAR_LOGGER);
53        newLogger(BAR_LOGGER, RESOURCE_BUNDLE);
54        newLogger(GEE_LOGGER, null);
55        newLogger(GEE_LOGGER, RESOURCE_BUNDLE);
56        newLogger(ORG_LOGGER);
57        newLogger(GEE_GEE_LOGGER);
58
59        for (String log : new String[] { FOO_LOGGER, BAR_LOGGER, GEE_LOGGER }) {
60            if (!RESOURCE_BUNDLE.equals(Logger.getLogger(log).getResourceBundleName())) {
61                throw new RuntimeException("Shouldn't allow to reset the resource bundle for " + log);
62            }
63            try {
64                Logger logger = Logger.getLogger(log, null);
65                if (!RESOURCE_BUNDLE.equals(logger.getResourceBundleName())) {
66                    throw new RuntimeException("Shouldn't allow to reset the resource bundle for " + log);
67                }
68                throw new RuntimeException("Expected IllegalArgumentException not thrown for " + log);
69            } catch (IllegalArgumentException e) {
70                System.out.println("Got expected exception for " + log +": " + e);
71            }
72        }
73        for (String log : new String[] { ORG_LOGGER, GEE_GEE_LOGGER }) {
74            if (Logger.getLogger(log).getResourceBundleName() != null) {
75                throw new RuntimeException("Resource bundle is not null for log: "
76                           + Logger.getLogger(log).getResourceBundleName());
77            }
78            try {
79                Logger logger = Logger.getLogger(log, null);
80                if (logger.getResourceBundleName() != null) {
81                    throw new RuntimeException("Resource bundle is not null for log: "
82                               + logger.getResourceBundleName());
83                }
84                System.out.println("Success calling Logger.getLogger(\""+log+"\", null)");
85            } catch (IllegalArgumentException e) {
86                throw new RuntimeException("Unexpected exception for " + log +": " + e, e);
87            }
88        }
89    }
90
91    private static List<Logger> strongRefs = new ArrayList<>();
92    private static void newLogger(String name) {
93        strongRefs.add(Logger.getLogger(name));
94    }
95    private static void newLogger(String name, String resourceBundleName) {
96        strongRefs.add(Logger.getLogger(name, resourceBundleName));
97    }
98}
99