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