TwiceIndirectlyLoadABundle.java revision 12745:f068a4ffddd2
1/*
2 * Copyright (c) 2013, 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
24import java.lang.reflect.InvocationTargetException;
25import java.lang.reflect.Method;
26import java.net.URL;
27import java.net.URLClassLoader;
28import java.nio.file.Paths;
29
30/**
31 * This class constructs a scenario where a bundle is accessible on the call
32 * stack two levels up from the call to getLogger(), but not on the immediate
33 * caller.  This tests that getLogger() isn't doing a stack crawl more than one
34 * level up to find a bundle.
35 *
36 * @author Jim Gish
37 */
38public class TwiceIndirectlyLoadABundle {
39
40    private static final String rbName = "StackSearchableResource";
41
42    public boolean loadAndTest() throws Throwable {
43        // Find out where we are running from so we can setup the URLClassLoader URLs
44        // test.src and test.classes will be set if running in jtreg, but probably
45        // not otherwise
46        String testDir = System.getProperty("test.src", System.getProperty("user.dir"));
47        String testClassesDir = System.getProperty("test.classes",
48                                                   System.getProperty("user.dir"));
49        URL[] urls = new URL[2];
50
51        // Allow for both jtreg and standalone cases here
52        // Unlike the 1-level test where we can get the bundle from the caller's
53        // class loader, for this one we don't want to expose the resource directory
54        // to the next class.  That way we're invoking the LoadItUp2Invoker class
55        // from this class that does have access to the resources (two levels
56        // up the call stack), but the Invoker itself won't have access to resource
57        urls[0] = Paths.get(testDir,"resources").toUri().toURL();
58        urls[1] = Paths.get(testClassesDir).toUri().toURL();
59
60        // Make sure we can find it via the URLClassLoader
61        URLClassLoader yetAnotherResourceCL = new URLClassLoader(urls, null);
62        Class<?> loadItUp2InvokerClazz = Class.forName("LoadItUp2Invoker", true,
63                                                       yetAnotherResourceCL);
64        ClassLoader actual = loadItUp2InvokerClazz.getClassLoader();
65        if (actual != yetAnotherResourceCL) {
66            throw new Exception("LoadItUp2Invoker was loaded by an unexpected CL: "
67                                 + actual);
68        }
69        Object loadItUp2Invoker = loadItUp2InvokerClazz.newInstance();
70
71        Method setupMethod = loadItUp2InvokerClazz.getMethod("setup",
72                urls.getClass(), String.class);
73        try {
74            // For the next class loader we create, we want to leave off
75            // the resources.  That way loadItUp2Invoker will have access to
76            // them, but the next class won't.
77            URL[] noResourceUrl = new URL[1];
78            noResourceUrl[0] = urls[1];  // from above -- just the test classes
79            setupMethod.invoke(loadItUp2Invoker, noResourceUrl, rbName);
80        } catch (InvocationTargetException ex) {
81            throw ex.getTargetException();
82        }
83
84        Method testMethod = loadItUp2InvokerClazz.getMethod("test");
85        try {
86            return (Boolean) testMethod.invoke(loadItUp2Invoker);
87        } catch (InvocationTargetException ex) {
88            throw ex.getTargetException();
89        }
90    }
91}
92