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