jdk/test/java/lang/reflect/Proxy/ClassRestrictions.java
author uta
Mon, 17 Dec 2012 14:34:37 +0400
changeset 14897 99faf9db81d8
parent 7668 d4a77089c587
child 23010 6dadb192ad81
permissions -rw-r--r--
8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem Summary: the tests were refactored to drop AWT dependence where it was possible. Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5808
diff changeset
     2
 * Copyright (c) 1999, 2010, 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
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
    25
 * @bug 4227192 8004928
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary This is a test of the restrictions on the parameters that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * be passed to the Proxy.getProxyClass method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @build ClassRestrictions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main ClassRestrictions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
    34
import java.lang.reflect.Modifier;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class ClassRestrictions {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public interface Bar {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        int foo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public interface Baz {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        long foo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    interface Bashful {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        void foo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
    52
    public static final String nonPublicIntrfaceName = "java.util.zip.ZipConstants";
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
    53
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            "\nTest of restrictions on parameters to Proxy.getProxyClass\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        try {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    60
            ClassLoader loader = ClassRestrictions.class.getClassLoader();
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    61
            Class<?>[] interfaces;
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    62
            Class<?> proxyClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
             * All of the Class objects in the interfaces array must represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
             * interfaces, not classes or primitive types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            try {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    69
                interfaces = new Class<?>[] { Object.class };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                proxyClass = Proxy.getProxyClass(loader, interfaces);
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
    71
                throw new Error(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    "proxy class created with java.lang.Object as interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                // assume exception is for intended failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            try {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    79
                interfaces = new Class<?>[] { Integer.TYPE };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                proxyClass = Proxy.getProxyClass(loader, interfaces);
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
    81
                throw new Error(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    "proxy class created with int.class as interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                // assume exception is for intended failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             * No two elements in the interfaces array may refer to identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             * Class objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            try {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    94
                interfaces = new Class<?>[] { Bar.class, Bar.class };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                proxyClass = Proxy.getProxyClass(loader, interfaces);
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
    96
                throw new Error(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    "proxy class created with repeated interfaces");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                // assume exception is for intended failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
             * All of the interfaces types must be visible by name though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
             * specified class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            ClassLoader altLoader = new URLClassLoader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                ((URLClassLoader) loader).getURLs(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Class altBarClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            altBarClass = Class.forName(Bar.class.getName(), false, altLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            try {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   113
                interfaces = new Class<?>[] { altBarClass };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                proxyClass = Proxy.getProxyClass(loader, interfaces);
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
   115
                throw new Error(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    "proxy class created with interface " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    "not visible to class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                // assume exception is for intended failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
             * All non-public interfaces must be in the same package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
             */
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   127
            Class<?> nonPublic1 = Bashful.class;
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
   128
            Class<?> nonPublic2 = Class.forName(nonPublicIntrfaceName);
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
   129
            if (Modifier.isPublic(nonPublic2.getModifiers())) {
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
   130
                throw new Error(
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
   131
                    "Interface " + nonPublicIntrfaceName +
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
   132
                    " is public and need to be changed!");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            try {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   135
                interfaces = new Class<?>[] { nonPublic1, nonPublic2 };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                proxyClass = Proxy.getProxyClass(loader, interfaces);
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
   137
                throw new Error(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    "proxy class created with two non-public interfaces " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    "in different packages");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                // assume exception is for intended failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
             * No two interfaces may each have a method with the same name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
             * parameter signature but different return type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            try {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   151
                interfaces = new Class<?>[] { Bar.class, Baz.class };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                proxyClass = Proxy.getProxyClass(loader, interfaces);
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
   153
                throw new Error(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    "proxy class created with conflicting methods");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                // assume exception is for intended failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
             * All components of this test have passed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            System.err.println("\nTEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
   166
        } catch (Throwable e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            System.err.println("\nTEST FAILED:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            e.printStackTrace();
14897
99faf9db81d8 8004928: TEST_BUG: Reduce dependence of CoreLib tests from the AWT subsystem
uta
parents: 7668
diff changeset
   169
            throw new Error("TEST FAILED: ", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}