1 /*
   2  * Copyright (c) 2009, 2010, 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 
  24 import java.io.File;
  25 import java.util.TreeSet;
  26 import java.util.Set;
  27 import java.net.URLStreamHandlerFactory;
  28 import sun.misc.BootClassLoaderHook;
  29 import sun.misc.URLClassPath;
  30 
  31 
  32 /* @test
  33  * @bug     6888802
  34  * @summary Sanity test of BootClassLoaderHook interface
  35  *
  36  * @build TestHook
  37  * @run main TestHook
  38  */
  39 
  40 public class TestHook extends BootClassLoaderHook {
  41 
  42     private static final TestHook hook = new TestHook();
  43     private static Set<String> names = new TreeSet<String>();
  44     private static final String LOGRECORD_CLASS =
  45         "java.util.logging.LogRecord";
  46     private static final String NONEXIST_RESOURCE =
  47         "non.exist.resource";
  48     private static final String LIBHELLO = "hello";
  49 
  50     public static void main(String[] args) throws Exception {
  51         BootClassLoaderHook.setHook(hook);
  52         if (BootClassLoaderHook.getHook() == null) {
  53            throw new RuntimeException("Null boot classloader hook ");
  54         }
  55 
  56         testHook();
  57 
  58         if (!names.contains(LOGRECORD_CLASS)) {
  59            throw new RuntimeException("loadBootstrapClass for " + LOGRECORD_CLASS + " not called");
  60         }
  61 
  62         if (!names.contains(NONEXIST_RESOURCE)) {
  63            throw new RuntimeException("getBootstrapResource for " + NONEXIST_RESOURCE + " not called");
  64         }
  65         if (!names.contains(LIBHELLO)) {
  66            throw new RuntimeException("loadLibrary for " + LIBHELLO + " not called");
  67         }
  68 
  69         Set<String> copy = new TreeSet<String>();
  70         copy.addAll(names);
  71         for (String s : copy) {
  72             System.out.println("  Loaded " + s);
  73         }
  74     }
  75 
  76     private static void testHook() throws Exception {
  77         Class.forName(LOGRECORD_CLASS);
  78         ClassLoader.getSystemResource(NONEXIST_RESOURCE);
  79         try {
  80           System.loadLibrary(LIBHELLO);
  81         } catch (UnsatisfiedLinkError e) {
  82         }
  83     }
  84 
  85     public String loadBootstrapClass(String className) {
  86         names.add(className);
  87         return null;
  88     }
  89 
  90     public String getBootstrapResource(String resourceName) {
  91         names.add(resourceName);
  92         return null;
  93     }
  94 
  95     public boolean loadLibrary(String libname) {
  96         names.add(libname);
  97         return false;
  98     }
  99 
 100     public URLClassPath getBootstrapClassPath(URLClassPath bcp,
 101             URLStreamHandlerFactory factory) {
 102         return bcp;
 103     }
 104 
 105     public boolean isCurrentThreadPrefetching() {
 106         return false;
 107     }
 108 
 109     public boolean prefetchFile(String name) {
 110         return false;
 111     }
 112 }