1 /*
   2  * Copyright (c) 2013, 2014, 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.io.PrintStream;
  24 import java.security.Permission;
  25 import java.security.Policy;
  26 import java.security.ProtectionDomain;
  27 import java.util.logging.LogManager;
  28 import java.util.logging.Logger;
  29 import sun.misc.JavaAWTAccess;
  30 import sun.misc.SharedSecrets;
  31 
  32 /*
  33  * @test
  34  * @bug 8025512
  35  *
  36  * @summary NPE with logging while launching webstart
  37  *
  38  * @modules java.base/sun.misc
  39  * @build TestGetLoggerNPE
  40  * @run main/othervm TestGetLoggerNPE getLogger
  41  * @run main/othervm TestGetLoggerNPE getLogManager
  42  */
  43 public class TestGetLoggerNPE {
  44     static volatile Throwable thrown = null;
  45     public static void main(String[] args) throws Exception {
  46         final String testCase = args.length == 0 ? "getLogger" : args[0];
  47         final JavaAWTAccessStub access = new JavaAWTAccessStub();
  48         SharedSecrets.setJavaAWTAccess(access);
  49         final ThreadGroup tg = new ThreadGroup("TestGroup");
  50         Thread t = new Thread(tg, "test") {
  51             public void run() {
  52                 try {
  53                     access.setContext(Context.ONE);
  54                     final PrintStream out = System.out;
  55                     System.setOut(null);
  56                     try {
  57                         if ("getLogger".equals(testCase)) {
  58                            Logger.getLogger("sun.plugin");
  59                         } else {
  60                            LogManager.getLogManager();
  61                         }
  62                     } finally {
  63                         System.setOut(out);
  64                     }
  65 
  66                     System.out.println(Logger.global);
  67                 } catch (Throwable x) {
  68                     x.printStackTrace();
  69                     thrown = x;
  70                 }
  71             }
  72         };
  73         Policy.setPolicy(new Policy() {
  74              public boolean implies(ProtectionDomain domain,
  75                                     Permission permission) {
  76                  return true; // all permissions
  77              }
  78         });
  79         System.setSecurityManager(new SecurityManager());
  80         t.start();
  81         t.join();
  82         if (thrown == null) {
  83             System.out.println("PASSED: " + testCase);
  84         } else {
  85             System.err.println("FAILED: " + testCase);
  86             throw new Error("Test failed: " + testCase + " - " + thrown, thrown);
  87         }
  88 
  89     }
  90 
  91     static enum Context { ONE, TWO };
  92 
  93     static final class JavaAWTAccessStub implements JavaAWTAccess {
  94         private static final InheritableThreadLocal<Context> context = new InheritableThreadLocal<>();
  95 
  96 
  97         public void setContext(Context context) {
  98             JavaAWTAccessStub.context.set(context);
  99         }
 100 
 101         @Override
 102         public Object getAppletContext() {
 103             return context.get();
 104         }
 105 
 106      }
 107 
 108 }