test/java/util/logging/Logger/getGlobal/TestGetGlobalConcurrent.java

Print this page




   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 


 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 }


   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.Level;
  26 import 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/timeout=10 TestGetGlobalConcurrent
  34  * @run main/othervm/timeout=10/policy=policy -Djava.security.manager TestGetGlobalConcurrent
  35  * @run main/othervm/timeout=10 -Djava.util.logging.manager=testgetglobal.LogManagerImpl1 TestGetGlobalConcurrent
  36  * @run main/othervm/timeout=10/policy=policy -Djava.security.manager -Djava.util.logging.manager=testgetglobal.LogManagerImpl1 TestGetGlobalConcurrent
  37  * @run main/othervm/timeout=10 -Djava.util.logging.manager=testgetglobal.LogManagerImpl2 TestGetGlobalConcurrent
  38  * @run main/othervm/timeout=10/policy=policy -Djava.security.manager -Djava.util.logging.manager=testgetglobal.LogManagerImpl2 TestGetGlobalConcurrent
  39  * @run main/othervm/timeout=10 -Djava.util.logging.manager=testgetglobal.LogManagerImpl3 TestGetGlobalConcurrent
  40  * @run main/othervm/timeout=10/policy=policy -Djava.security.manager -Djava.util.logging.manager=testgetglobal.LogManagerImpl3 TestGetGlobalConcurrent
  41  * @run main/othervm/timeout=10 -Djava.util.logging.manager=testgetglobal.BadLogManagerImpl TestGetGlobalConcurrent
  42  * @run main/othervm/timeout=10/policy=policy -Djava.security.manager -Djava.util.logging.manager=testgetglobal.BadLogManagerImpl TestGetGlobalConcurrent
  43  * @run main/othervm/timeout=10 -Djava.util.logging.manager=testgetglobal.DummyLogManagerImpl TestGetGlobalConcurrent
  44  * @run main/othervm/timeout=10/policy=policy -Djava.security.manager -Djava.util.logging.manager=testgetglobal.DummyLogManagerImpl TestGetGlobalConcurrent
  45  * @author danielfuchs
  46  */
  47 public class TestGetGlobalConcurrent {
  48 
  49     final static 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 


 131                  run.run();
 132               } catch (Exception | Error x) {
 133                  fail(x);
 134               }
 135           }
 136     }
 137 
 138     final static class Run1 implements Runnable {
 139         public void run() { test1(); }
 140     }
 141     final static class Run2 implements Runnable {
 142         public void run() { test2(); }
 143     }
 144     final static class Run3 implements Runnable {
 145         public void run() { test3(); }
 146     }
 147     final static 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 }