< prev index next >

test/jdk/java/util/logging/LogManager/TestLoggerNames.java

Print this page




  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.ArrayList;
  24 import java.util.Collections;
  25 import java.util.Enumeration;
  26 import java.util.Iterator;
  27 import java.util.List;
  28 import java.util.concurrent.CopyOnWriteArrayList;
  29 import java.util.concurrent.Phaser;
  30 import java.util.concurrent.Semaphore;
  31 import java.util.logging.Handler;
  32 import java.util.logging.LogManager;
  33 import java.util.logging.Logger;

  34 
  35 
  36 /**
  37  * @test
  38  * @bug 7113878
  39  * @summary This is not a test that will check that 7113878 is fixed, but
  40  *          rather a test that will invoke the modified code & try to verify
  41  *          that fixing 7113878 has not introduced some big regression.
  42  *          This test should pass, whether 7113878 is there or not.
  43  * @run main/othervm TestLoggerNames
  44  * @author danielfuchs
  45  */
  46 public class TestLoggerNames {
  47 
  48     static final class TestLogger extends java.util.logging.Logger {
  49 
  50         final Semaphore sem = new Semaphore(0);
  51         final Semaphore wait = new Semaphore(0);
  52 
  53         public TestLogger(String name, String resourceBundleName) {
  54             super(name, resourceBundleName);
  55         }
  56 
  57         @Override
  58         public Handler[] getHandlers() {


 116         List<String> loggerNames = Collections.list(names);
 117         if (!loggerNames.contains("")) {
 118             throw new RuntimeException("\"\"" +
 119                     " not found in " + loggerNames);
 120         }
 121         if (!loggerNames.contains("global")) {
 122             throw new RuntimeException("global" +
 123                     " not found in " + loggerNames);
 124         }
 125         for (Logger l : loggers) {
 126             if (!loggerNames.contains(l.getName())) {
 127                 throw new RuntimeException(l.getName() +
 128                         " not found in " + loggerNames);
 129             }
 130         }
 131         System.out.println("Got all expected logger names");
 132     }
 133 
 134 
 135     public static void main(String[] args) throws InterruptedException {
 136         LogManager.getLogManager().addLogger(new TestLogger("com.foo.bar.zzz", null));

 137         try {
 138             Logger.getLogger(null);
 139             throw new RuntimeException("Logger.getLogger(null) didn't throw expected NPE");
 140         } catch (NullPointerException x) {
 141             System.out.println("Got expected NullPointerException for Logger.getLogger(null)");
 142         }
 143         List<Logger> loggers = new CopyOnWriteArrayList<>();
 144         loggers.add(Logger.getLogger("one.two.addMeAChild"));
 145         loggers.add(Logger.getLogger("aaa.bbb.replaceMe"));
 146         loggers.add(Logger.getLogger("bbb.aaa.addMeAChild"));
 147         TestLogger test = (TestLogger)Logger.getLogger("com.foo.bar.zzz");

 148         loggers.add(Logger.getLogger("ddd.aaa.addMeAChild"));
 149 
 150         checkLoggerNames(loggers);
 151 
 152         Thread loggerNamesThread = new Thread(() -> {
 153             try {
 154                 while (!stop) {
 155                     // Make a defensive copy of the list of loggers that we pass
 156                     // to checkLoggerNames - in order to make sure that
 157                     // we won't see new loggers that might appear after
 158                     // checkLoggerNames has called LogManager.getLoggerNames().
 159                     // ('loggers' is a live list and the main thread adds and
 160                     //  and removes loggers from it concurrently to this thread).
 161                     checkLoggerNames(new ArrayList<>(loggers));
 162                     Thread.sleep(10);
 163                     if (!stop) {
 164                         phaser.arriveAndAwaitAdvance();
 165                     }
 166                 }
 167             } catch (Throwable t) {




  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.ArrayList;
  24 import java.util.Collections;
  25 import java.util.Enumeration;
  26 import java.util.Iterator;
  27 import java.util.List;
  28 import java.util.concurrent.CopyOnWriteArrayList;
  29 import java.util.concurrent.Phaser;
  30 import java.util.concurrent.Semaphore;
  31 import java.util.logging.Handler;
  32 import java.util.logging.LogManager;
  33 import java.util.logging.Logger;
  34 import java.lang.ref.Reference;
  35 
  36 
  37 /**
  38  * @test
  39  * @bug 7113878 8222027
  40  * @summary This is not a test that will check that 7113878 is fixed, but
  41  *          rather a test that will invoke the modified code & try to verify
  42  *          that fixing 7113878 has not introduced some big regression.
  43  *          This test should pass, whether 7113878 is there or not.
  44  * @run main/othervm TestLoggerNames
  45  * @author danielfuchs
  46  */
  47 public class TestLoggerNames {
  48 
  49     static final class TestLogger extends java.util.logging.Logger {
  50 
  51         final Semaphore sem = new Semaphore(0);
  52         final Semaphore wait = new Semaphore(0);
  53 
  54         public TestLogger(String name, String resourceBundleName) {
  55             super(name, resourceBundleName);
  56         }
  57 
  58         @Override
  59         public Handler[] getHandlers() {


 117         List<String> loggerNames = Collections.list(names);
 118         if (!loggerNames.contains("")) {
 119             throw new RuntimeException("\"\"" +
 120                     " not found in " + loggerNames);
 121         }
 122         if (!loggerNames.contains("global")) {
 123             throw new RuntimeException("global" +
 124                     " not found in " + loggerNames);
 125         }
 126         for (Logger l : loggers) {
 127             if (!loggerNames.contains(l.getName())) {
 128                 throw new RuntimeException(l.getName() +
 129                         " not found in " + loggerNames);
 130             }
 131         }
 132         System.out.println("Got all expected logger names");
 133     }
 134 
 135 
 136     public static void main(String[] args) throws InterruptedException {
 137         TestLogger tl = new TestLogger("com.foo.bar.zzz", null);
 138         LogManager.getLogManager().addLogger(tl);
 139         try {
 140             Logger.getLogger(null);
 141             throw new RuntimeException("Logger.getLogger(null) didn't throw expected NPE");
 142         } catch (NullPointerException x) {
 143             System.out.println("Got expected NullPointerException for Logger.getLogger(null)");
 144         }
 145         List<Logger> loggers = new CopyOnWriteArrayList<>();
 146         loggers.add(Logger.getLogger("one.two.addMeAChild"));
 147         loggers.add(Logger.getLogger("aaa.bbb.replaceMe"));
 148         loggers.add(Logger.getLogger("bbb.aaa.addMeAChild"));
 149         TestLogger test = (TestLogger)Logger.getLogger("com.foo.bar.zzz");
 150         Reference.reachabilityFence(tl);
 151         loggers.add(Logger.getLogger("ddd.aaa.addMeAChild"));
 152 
 153         checkLoggerNames(loggers);
 154 
 155         Thread loggerNamesThread = new Thread(() -> {
 156             try {
 157                 while (!stop) {
 158                     // Make a defensive copy of the list of loggers that we pass
 159                     // to checkLoggerNames - in order to make sure that
 160                     // we won't see new loggers that might appear after
 161                     // checkLoggerNames has called LogManager.getLoggerNames().
 162                     // ('loggers' is a live list and the main thread adds and
 163                     //  and removes loggers from it concurrently to this thread).
 164                     checkLoggerNames(new ArrayList<>(loggers));
 165                     Thread.sleep(10);
 166                     if (!stop) {
 167                         phaser.arriveAndAwaitAdvance();
 168                     }
 169                 }
 170             } catch (Throwable t) {


< prev index next >