jdk/test/java/rmi/server/RMIClassLoader/loadProxyClasses/LoadProxyClasses.java
author smarks
Wed, 12 Dec 2012 09:53:01 -0800
changeset 14778 5947768d173d
parent 5506 202f599c92aa
child 16872 f5a07dbe9b1a
permissions -rw-r--r--
8004748: clean up @build tags in RMI tests Reviewed-by: alanb, darcy, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary functional test for RMIClassLoader.loadProxyClass; test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * ensures that the default RMI class loader provider implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * RMIClassLoader.loadProxyClass correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Laird Dornin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @library ../../../testlibrary
14778
5947768d173d 8004748: clean up @build tags in RMI tests
smarks
parents: 5506
diff changeset
    33
 * @build TestLibrary FnnClass FnnUnmarshal NonpublicInterface
5947768d173d 8004748: clean up @build tags in RMI tests
smarks
parents: 5506
diff changeset
    34
 *     NonpublicInterface1 PublicInterface PublicInterface1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @run main/othervm/policy=security.policy LoadProxyClasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.rmi.server.RMIClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.InvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.MarshalledObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.zip.Checksum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *  Invokes RMIClassLoader.loadProxyClass() to load a proxy class with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *  multiple interfaces using using RMI class unmarshalling.  Test is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  composed of cases which each unmarshal a proxy class in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *  different environment.  All of the cases create needed class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *  loaders, load appropriate interfaces, create a proxy class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *  implements those interfaces, create a marshalled object from that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *  proxy class, and finally call .get() on that object.  Get of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *  object should pass in some cases and fail in others.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *  1. Nonpublic interface loaded from the parent of the First
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *  Non-Null class Loader on the execution stack (FNNL).  Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *  interface loaded from grandparent of FNNL parent. Proxy class must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *  be defined in non-null FNNL parent. Should succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *  2. Nonpublic interface (java.util.zip.ZipConstants) and public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *  interface (java.util.zip.CheckSum) loaded from bootclasspath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *  proxy class defined in null/boot class loader.  Should succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *  3. Public interface classes loaded in FNNL are also available in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *  RMI loader parent.  FNNL is grandparent of RMI loader. Proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *  must be defined in RMI class loader. Should succeed. public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *  interface must be defined in FNNL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *  4. Non-public interfaces have multiple class loaders. Should fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *  with a LinkageError.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *  5. Interface classes loaded from RMI class loader. Proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *  defined in RMI class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *  6. Not all interfaces classes can be loaded from a single class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *  loader; should fail with ClassNotFoundException.  All interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *  classes will exist (but not all interfaces will be available from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *  one class loader).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *  7. prove that proxy loader has correct annotation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *  8. REMIND: may want to add a case where the FNNL is null (This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *  would be for class unmarshalling in the implemntation of a remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *  method invocation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
public class LoadProxyClasses {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static URL publicUrl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static boolean boomerangSemantics = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            System.err.println("\nFunctional test to verify that RMI " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                               "loads proxy classes correctly\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            /* install proxy interfaces */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            publicUrl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                TestLibrary.installClassInCodebase("PublicInterface",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                                   "public");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            URL publicUrl1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                TestLibrary.installClassInCodebase("PublicInterface1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                                   "public1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            URL nonpublicUrl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                TestLibrary.installClassInCodebase("NonpublicInterface",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                                   "nonpublic", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            URL nonpublicUrl1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                TestLibrary.installClassInCodebase("NonpublicInterface1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                                   "nonpublic1", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            URL bothNonpublicUrl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                TestLibrary.installClassInCodebase("NonpublicInterface",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                                   "bothNonpublic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            TestLibrary.installClassInCodebase("NonpublicInterface1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                               "bothNonpublic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            URL fnnUrl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                TestLibrary.installClassInCodebase("FnnClass", "fnn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            TestLibrary.suggestSecurityManager(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            /* Case 1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            ClassLoader grandParentPublic =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                new URLClassLoader(new URL[] {publicUrl});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            ClassLoader parentNonpublic =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                new URLClassLoader(new URL[] {nonpublicUrl},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                   grandParentPublic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            URLClassLoader fnnLoader1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                new URLClassLoader(new URL[] {fnnUrl}, parentNonpublic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            Class nonpublicInterface =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                fnnLoader1.loadClass("NonpublicInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            Class publicInterface =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                fnnLoader1.loadClass("PublicInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            Proxy proxy1 = (Proxy) Proxy.newProxyInstance(parentNonpublic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                new Class[] {nonpublicInterface, publicInterface},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                new TestInvocationHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            unmarshalProxyClass(proxy1, fnnLoader1, parentNonpublic, 1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            /* Case 2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            Class zipConstantsClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                Class.forName("java.util.zip.ZipConstants");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            URLClassLoader fnnLoader2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                new URLClassLoader(new URL[] {fnnUrl});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            Proxy proxy2 = (Proxy) Proxy.newProxyInstance(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                new Class[] {zipConstantsClass, Checksum.class},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                new TestInvocationHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            unmarshalProxyClass(proxy2, fnnLoader2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                (ClassLoader) null, 2, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            /* Case 3 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            Thread currentThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            ClassLoader fnnLoader3 = new URLClassLoader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                new URL[] {publicUrl, fnnUrl});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            ClassLoader newCtxLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                new URLClassLoader(new URL[] {publicUrl}, fnnLoader3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            Class publicInterface3 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                fnnLoader3.loadClass("PublicInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            ClassLoader currentCtxLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                currentThread.getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            currentThread.setContextClassLoader(newCtxLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            Proxy proxy3 = (Proxy) Proxy.newProxyInstance(newCtxLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                new Class[] {publicInterface3},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                new TestInvocationHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            unmarshalProxyClass(proxy3, fnnLoader3, fnnLoader3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                3, new Case3Checker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            currentThread.setContextClassLoader(currentCtxLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            /* Case 4 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            ClassLoader bothNonpublicLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                new URLClassLoader(new URL[] {bothNonpublicUrl});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            Class nonpublicInterface4a =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                bothNonpublicLoader.loadClass("NonpublicInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            Class nonpublicInterface4b =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                bothNonpublicLoader.loadClass("NonpublicInterface1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            Proxy proxy4 = (Proxy) Proxy.newProxyInstance(bothNonpublicLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                new Class[] {nonpublicInterface4a, nonpublicInterface4b},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                new TestInvocationHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            ClassLoader nonpublicLoaderA =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                new URLClassLoader(new URL[] {nonpublicUrl});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            ClassLoader nonpublicLoaderB =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                new URLClassLoader(new URL[] {nonpublicUrl1}, nonpublicLoaderA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            currentCtxLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                currentThread.getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            currentThread.setContextClassLoader(nonpublicLoaderB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            IllegalAccessError illegal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                unmarshalProxyClass(proxy4, fnnLoader2, nonpublicLoaderB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                    4, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            } catch (IllegalAccessError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                illegal = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (illegal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                TestLibrary.bomb("case4: IllegalAccessError not thrown " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                 "when multiple nonpublic interfaces have \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                 "different class loaders");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                System.err.println("\ncase4: IllegalAccessError correctly " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                   "thrown \n when trying to load proxy " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                   "with multiple nonpublic interfaces in \n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                   "  different class loaders");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            currentThread.setContextClassLoader(currentCtxLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            /* Case 5*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            ClassLoader publicLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                new URLClassLoader(new URL[] {publicUrl});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            Class publicInterface5 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                publicLoader.loadClass("PublicInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            Proxy proxy5 = (Proxy) Proxy.newProxyInstance(publicLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                new Class[] {publicInterface5},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                new TestInvocationHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            currentCtxLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                currentThread.getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            currentThread.setContextClassLoader(publicLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            unmarshalProxyClass(proxy5, fnnLoader2, publicLoader, 5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                new Case5Checker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            currentThread.setContextClassLoader(currentCtxLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            /* Case 6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            ClassLoader fnnLoader6 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                new URLClassLoader(new URL[] {fnnUrl, publicUrl});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            ClassLoader publicLoader6 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                new URLClassLoader(new URL[] {publicUrl1}, fnnLoader6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            Class publicInterface6a =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                publicLoader6.loadClass("PublicInterface1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            Class publicInterface6b =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                fnnLoader6.loadClass("PublicInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            Proxy proxy6 = (Proxy) Proxy.newProxyInstance(publicLoader6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                new Class[] {publicInterface6a, publicInterface6b},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                new TestInvocationHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            ClassNotFoundException cnfe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                unmarshalProxyClass(proxy6, fnnLoader6, publicLoader6, 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                    null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                cnfe = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (cnfe == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                TestLibrary.bomb("ClassNotFoundException not thrown " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                 "when not all proxy interfaces could " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                 " be found in a single class loader ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                System.err.println("Case6: ClassNotFoundException " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                   "correctly thrown when not all proxy" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                   " interfaces could be found in a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                   "single class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                cnfe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            System.err.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (e instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                throw (RuntimeException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            TestLibrary.bomb(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private interface LoadChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        void checkLoad(Proxy proxy, ClassLoader expectedLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private static Proxy unmarshalProxyClass(Proxy proxy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                             ClassLoader fnnLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                             ClassLoader expectedLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                             int n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                             LoadChecker checker)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        throws ClassNotFoundException, IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
               InstantiationException, IllegalAccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        FnnUnmarshal fnnUnmarshal = (FnnUnmarshal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                fnnLoader.loadClass("FnnClass").newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        Proxy unmarshalled = (Proxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            fnnUnmarshal.unmarshal(new MarshalledObject(proxy));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        ClassLoader unmarshalledLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            unmarshalled.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (checker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            checker.checkLoad(unmarshalled, expectedLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (unmarshalledLoader != expectedLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                TestLibrary.bomb("case" + n + ": proxy class not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                 "placed into incorrect loader: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                 unmarshalledLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                System.err.println("\ncase" + n + ": proxy class correctly" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                   " placed into expected loader: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                   expectedLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private static class Case3Checker implements LoadChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        public void checkLoad(Proxy proxy, ClassLoader expectedLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            ClassLoader ifaceLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                proxy.getClass().getInterfaces()[0].getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            ClassLoader proxyLoader = proxy.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            boolean proxyOk = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (boomerangSemantics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                ClassLoader ctxLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (proxyLoader == ctxLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    proxyOk = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            } else if (proxyLoader.getClass().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                       getName().indexOf("sun.rmi") >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                proxyOk = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (proxyOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                System.err.println("\ncase3: proxy loaded in" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                   " correct loader: " + proxyLoader +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                   Arrays.asList(((URLClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                                 proxyLoader).getURLs()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                TestLibrary.bomb("case3: proxy class loaded in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                 "incorrect loader: " + proxyLoader +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                   Arrays.asList(((URLClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                                  proxyLoader).getURLs()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (ifaceLoader == expectedLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                System.err.println("case3: proxy interface loaded in" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                   " correct loader: " + ifaceLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                TestLibrary.bomb("public proxy interface loaded in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                 "incorrect loader: " + ifaceLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private static class Case5Checker implements LoadChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        public void checkLoad(Proxy proxy, ClassLoader expectedLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            ClassLoader proxyLoader = proxy.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            String proxyAnnotation =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                RMIClassLoader.getClassAnnotation(proxy.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if ((proxyAnnotation == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                !proxyAnnotation.equals(publicUrl.toString()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                TestLibrary.bomb("proxy class had incorrect annotation: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                 proxyAnnotation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                System.err.println("proxy class had correct annotation: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                   proxyAnnotation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            boolean proxyOk = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (boomerangSemantics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                ClassLoader ctxLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                if (proxyLoader == ctxLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    proxyOk = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            } else if (proxyLoader.getClass().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                       getName().indexOf("sun.rmi") >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                proxyOk = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (proxyOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                System.err.println("\ncase5: proxy loaded from" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                   " correct loader: " + proxyLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                TestLibrary.bomb("case5: proxy interface loaded from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                                 "incorrect loader: " + proxyLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    private static class TestInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        implements InvocationHandler, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        public Object invoke(Object proxy, Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throws Throwable {return null;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
}