TestGetGlobalConcurrent.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.List;
25import java.util.logging.Level;
26import java.util.logging.Logger;
27
28/**
29 * @test
30 * @bug 7184195 8021003
31 * @summary Test that the global logger can log with no configuration when accessed from multiple threads.
32 * @build TestGetGlobalConcurrent testgetglobal.HandlerImpl testgetglobal.LogManagerImpl1 testgetglobal.LogManagerImpl2 testgetglobal.LogManagerImpl3 testgetglobal.BadLogManagerImpl testgetglobal.DummyLogManagerImpl
33 * @run main/othervm TestGetGlobalConcurrent
34 * @run main/othervm/policy=policy -Djava.security.manager TestGetGlobalConcurrent
35 * @run main/othervm -Djava.util.logging.manager=testgetglobal.LogManagerImpl1 TestGetGlobalConcurrent
36 * @run main/othervm/policy=policy -Djava.security.manager -Djava.util.logging.manager=testgetglobal.LogManagerImpl1 TestGetGlobalConcurrent
37 * @run main/othervm -Djava.util.logging.manager=testgetglobal.LogManagerImpl2 TestGetGlobalConcurrent
38 * @run main/othervm/policy=policy -Djava.security.manager -Djava.util.logging.manager=testgetglobal.LogManagerImpl2 TestGetGlobalConcurrent
39 * @run main/othervm -Djava.util.logging.manager=testgetglobal.LogManagerImpl3 TestGetGlobalConcurrent
40 * @run main/othervm/policy=policy -Djava.security.manager -Djava.util.logging.manager=testgetglobal.LogManagerImpl3 TestGetGlobalConcurrent
41 * @run main/othervm -Djava.util.logging.manager=testgetglobal.BadLogManagerImpl TestGetGlobalConcurrent
42 * @run main/othervm/policy=policy -Djava.security.manager -Djava.util.logging.manager=testgetglobal.BadLogManagerImpl TestGetGlobalConcurrent
43 * @run main/othervm -Djava.util.logging.manager=testgetglobal.DummyLogManagerImpl TestGetGlobalConcurrent
44 * @run main/othervm/policy=policy -Djava.security.manager -Djava.util.logging.manager=testgetglobal.DummyLogManagerImpl TestGetGlobalConcurrent
45 * @author danielfuchs
46 */
47public class TestGetGlobalConcurrent {
48
49    static final String[] messages = {
50        "1. This message should not appear on the console.",
51        "2. This message should appear on the console.",
52        "3. This message should now appear on the console too.",
53        "4. This message should appear on the console.",
54        "5. This message should now appear on the console too.",
55        "6. This message should appear on the console.",
56        "7. This message should now appear on the console too.",
57        "8. This message should appear on the console.",
58        "9. This message should now appear on the console too."
59    };
60
61    static {
62        System.setProperty("java.util.logging.config.file",
63            System.getProperty("test.src", ".") + java.io.File.separator + "logging.properties");
64    }
65
66    public static void test1() {
67        final int nb = 1;
68        final int i = 2*nb + 1;
69        Logger.getGlobal().info(messages[i]); // calling getGlobal() will
70             // initialize the LogManager - and thus this message should appear.
71        Logger.global.info(messages[i+1]); // Now that the LogManager is
72             // initialized, this message should appear too.
73        final List<String> expected = Arrays.asList(Arrays.copyOfRange(messages, i, i+2));
74        if (!testgetglobal.HandlerImpl.received.containsAll(expected)) {
75            fail(new Error("Unexpected message list: "+testgetglobal.HandlerImpl.received+" vs "+ expected));
76        }
77    }
78    public static void test2() {
79        final int nb = 2;
80        final int i = 2*nb + 1;
81        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(messages[i]); // calling getGlobal() will
82             // initialize the LogManager - and thus this message should appear.
83        Logger.global.info(messages[i+1]); // Now that the LogManager is
84             // initialized, this message should appear too.
85        final List<String> expected = Arrays.asList(Arrays.copyOfRange(messages, i, i+2));
86        if (!testgetglobal.HandlerImpl.received.containsAll(expected)) {
87            fail(new Error("Unexpected message list: "+testgetglobal.HandlerImpl.received+" vs "+ expected));
88        }
89    }
90    public static void test3() {
91        final int nb = 3;
92        final int i = 2*nb + 1;
93        java.util.logging.LogManager.getLogManager();
94        Logger.getGlobal().info(messages[i]); // calling getGlobal() will
95             // initialize the LogManager - and thus this message should appear.
96        Logger.global.info(messages[i+1]); // Now that the LogManager is
97             // initialized, this message should appear too.
98        final List<String> expected = Arrays.asList(Arrays.copyOfRange(messages, i, i+2));
99        if (!testgetglobal.HandlerImpl.received.containsAll(expected)) {
100            fail(new Error("Unexpected message list: "+testgetglobal.HandlerImpl.received+" vs "+ expected));
101        }
102    }
103    public static void test4() {
104        log = new MyLogger("foo.bar");
105        java.util.logging.LogManager.getLogManager().addLogger(log);
106    }
107
108
109    private static volatile Throwable failed = null;
110    private static volatile Logger log = null;
111
112    public static class MyLogger extends Logger {
113        public MyLogger(String name) {
114            super(name, null);
115        }
116    }
117
118    public static void fail(Throwable failure) {
119        failure.printStackTrace();
120        if (failed == null) failed = failure;
121    }
122
123    public static class WaitAndRun implements Runnable {
124          private final Runnable run;
125          public WaitAndRun(Runnable run) {
126              this.run = run;
127          }
128          public void run() {
129              try {
130                 Thread.sleep(10);
131                 run.run();
132              } catch (Exception | Error x) {
133                 fail(x);
134              }
135          }
136    }
137
138    static final class Run1 implements Runnable {
139        public void run() { test1(); }
140    }
141    static final class Run2 implements Runnable {
142        public void run() { test2(); }
143    }
144    static final class Run3 implements Runnable {
145        public void run() { test3(); }
146    }
147    static final class Run4 implements Runnable {
148        public void run() { test4(); }
149    }
150
151    static String description = "Unknown";
152
153    public static void main(String... args) throws Exception {
154
155        final String manager = System.getProperty("java.util.logging.manager", null);
156
157        description = "TestGetGlobalConcurrent"
158            + (System.getSecurityManager() == null ? " " :
159               " -Djava.security.manager ")
160            + (manager == null ? "" : "-Djava.util.logging.manager=" + manager);
161
162        final Thread t1 = new Thread(new WaitAndRun(new Run1()), "test1");
163        final Thread t2 = new Thread(new WaitAndRun(new Run2()), "test2");
164        final Thread t3 = new Thread(new WaitAndRun(new Run3()), "test3");
165        final Thread t4 = new Thread(new WaitAndRun(new Run4()), "test4");
166
167        t1.setDaemon(true); t2.setDaemon(true); t3.setDaemon(true); t4.setDaemon(true);
168        t1.start(); t2.start(); t3.start(); t4.start();
169
170        Thread.sleep(10);
171
172        Logger.getGlobal().info(messages[1]); // calling getGlobal() will
173             // initialize the LogManager - and thus this message should appear.
174        Logger.global.info(messages[2]); // Now that the LogManager is
175             // initialized, this message should appear too.
176
177        final List<String> expected = Arrays.asList(Arrays.copyOfRange(messages, 1, 3));
178        if (!testgetglobal.HandlerImpl.received.containsAll(expected)) {
179            fail(new Error("Unexpected message list: "+testgetglobal.HandlerImpl.received+" vs "+ expected));
180        }
181
182        t1.join(); t2.join(); t3.join(); t4.join();
183
184        if (failed != null) {
185             throw new Error("Test failed: "+description, failed);
186        }
187
188        System.out.println("Test passed");
189    }
190}
191