TestAnonymousLogger.java revision 12745:f068a4ffddd2
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.Arrays;
24import java.util.Collections;
25import java.util.Enumeration;
26import java.util.LinkedHashSet;
27import java.util.ResourceBundle;
28import java.util.Set;
29import java.util.logging.Filter;
30import java.util.logging.Handler;
31import java.util.logging.Level;
32import java.util.logging.LogRecord;
33import java.util.logging.Logger;
34import java.util.logging.SimpleFormatter;
35
36/**
37 * @test
38 * @bug 8026863
39 * @summary checks that anonymous logger setters work as expected when a
40 *          security manager is set, and checks that getters return expected
41 *          values.
42 * @run main/othervm TestAnonymousLogger
43 */
44public class TestAnonymousLogger {
45
46    public static final class TestHandler extends Handler {
47        @Override
48        public void publish(LogRecord record) {
49            System.out.println(new SimpleFormatter().format(record));
50        }
51        @Override
52        public void flush() {
53            System.out.flush();
54        }
55        @Override
56        public void close() {
57            flush();
58        }
59    }
60
61    public static final class TestFilter implements Filter {
62        @Override
63        public boolean isLoggable(LogRecord record) {
64            return true;
65        }
66    }
67
68    public static final class TestBundle extends ResourceBundle {
69        Set<String> keys = Collections.synchronizedSet(new LinkedHashSet<>());
70        @Override
71        protected Object handleGetObject(String key) {
72            keys.add(key);
73            return "[LOCALIZED] "+key;
74        }
75        @Override
76        public Enumeration<String> getKeys() {
77            return Collections.enumeration(keys);
78        }
79    }
80
81    public static void main(String[] args) {
82        System.setSecurityManager(new SecurityManager());
83        Logger anonymous = Logger.getAnonymousLogger();
84
85        final TestHandler handler = new TestHandler();
86        final TestFilter filter = new TestFilter();
87        final ResourceBundle bundle = ResourceBundle.getBundle(TestBundle.class.getName());
88        anonymous.setLevel(Level.FINEST);
89        anonymous.addHandler(handler);
90        anonymous.setFilter(filter);
91        anonymous.setUseParentHandlers(true);
92        anonymous.setResourceBundle(bundle);
93
94        if (anonymous.getLevel() != Level.FINEST) {
95            throw new RuntimeException("Unexpected level: " + anonymous.getLevel());
96        } else {
97            System.out.println("Got expected level: " + anonymous.getLevel());
98        }
99        if (!Arrays.asList(anonymous.getHandlers()).contains(handler)) {
100            throw new RuntimeException("Expected handler not found in: "
101                    + Arrays.asList(anonymous.getHandlers()));
102        } else {
103            System.out.println("Got expected handler in: " + Arrays.asList(anonymous.getHandlers()));
104        }
105        if (anonymous.getFilter() != filter) {
106            throw new RuntimeException("Unexpected filter: " + anonymous.getFilter());
107        } else {
108            System.out.println("Got expected filter: " + anonymous.getFilter());
109        }
110        if (!anonymous.getUseParentHandlers()) {
111            throw new RuntimeException("Unexpected flag: " + anonymous.getUseParentHandlers());
112        } else {
113            System.out.println("Got expected flag: " + anonymous.getUseParentHandlers());
114        }
115        if (anonymous.getResourceBundle() != bundle) {
116            throw new RuntimeException("Unexpected bundle: " + anonymous.getResourceBundle());
117        } else {
118            System.out.println("Got expected bundle: " + anonymous.getResourceBundle());
119        }
120        try {
121            anonymous.setParent(Logger.getLogger("foo.bar"));
122            throw new RuntimeException("Expected SecurityException not raised!");
123        } catch (SecurityException x) {
124            System.out.println("Got expected exception: " + x);
125        }
126        if (anonymous.getParent() != Logger.getLogger("")) {
127            throw new RuntimeException("Unexpected parent: " + anonymous.getParent());
128        } else {
129            System.out.println("Got expected parent: " + anonymous.getParent());
130        }
131    }
132
133}
134