1/*
2 * Copyright (c) 1999, 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
24/* @test
25 * @bug 4227192 4487672
26 * @summary This is a basic functional test of the dynamic proxy API (part 1).
27 * @author Peter Jones
28 *
29 * @build Basic1
30 * @run main Basic1
31 */
32
33import java.lang.reflect.*;
34import java.security.*;
35import java.util.*;
36
37public class Basic1 {
38
39    public static void main(String[] args) {
40
41        System.err.println(
42            "\nBasic functional test of dynamic proxy API, part 1\n");
43
44        try {
45            Class<?>[] interfaces =
46                new Class<?>[] { Runnable.class, Observer.class };
47
48            ClassLoader loader = ClassLoader.getSystemClassLoader();
49
50            /*
51             * Generate a proxy class.
52             */
53            Class<?> proxyClass = Proxy.getProxyClass(loader, interfaces);
54            System.err.println("+ generated proxy class: " + proxyClass);
55
56            /*
57             * Verify that it is public, final, and not abstract.
58             */
59            int flags = proxyClass.getModifiers();
60            System.err.println(
61                "+ proxy class's modifiers: " + Modifier.toString(flags));
62            if (!Modifier.isPublic(flags)) {
63                throw new RuntimeException("proxy class in not public");
64            }
65            if (!Modifier.isFinal(flags)) {
66                throw new RuntimeException("proxy class in not final");
67            }
68            if (Modifier.isAbstract(flags)) {
69                throw new RuntimeException("proxy class in abstract");
70            }
71
72            /*
73             * Verify that it is assignable to the proxy interfaces.
74             */
75            for (Class<?> intf : interfaces) {
76                if (!intf.isAssignableFrom(proxyClass)) {
77                    throw new RuntimeException(
78                        "proxy class not assignable to proxy interface " +
79                        intf.getName());
80                }
81            }
82
83            /*
84             * Verify that it has the given permutation of interfaces.
85             */
86            List<Class<?>> l1 = Arrays.asList(interfaces);
87            List<Class<?>> l2 = Arrays.asList(proxyClass.getInterfaces());
88            System.err.println("+ proxy class's interfaces: " + l2);
89            if (!l1.equals(l2)) {
90                throw new RuntimeException(
91                    "proxy class interfaces are " + l2 +
92                    " (expected " + l1 + ")");
93            }
94
95            /*
96             * Verify that system agress that it is a proxy class.
97             */
98            if (Proxy.isProxyClass(Object.class)) {
99                throw new RuntimeException(
100                    "Proxy.isProxyClass returned true for java.lang.Object");
101            }
102            if (!Proxy.isProxyClass(proxyClass)) {
103                throw new RuntimeException(
104                    "Proxy.isProxyClass returned false for proxy class");
105            }
106
107            /*
108             * Verify that its protection domain is the bootstrap domain.
109             */
110            ProtectionDomain pd = proxyClass.getProtectionDomain();
111            System.err.println("+ proxy class's protection domain: " + pd);
112            if (!pd.implies(new AllPermission())) {
113                throw new RuntimeException(
114                    "proxy class does not have AllPermission");
115            }
116
117            /*
118             * Verify that it has a constructor that takes an
119             * InvocationHandler instance.
120             */
121            Constructor<?> cons = proxyClass.getConstructor(InvocationHandler.class);
122
123            /*
124             * Test constructor with null InvocationHandler
125             */
126            try {
127                cons.newInstance(new Object[] { null });
128                throw new RuntimeException("Expected NullPointerException thrown");
129            } catch (InvocationTargetException e) {
130                Throwable t = e.getTargetException();
131                if (!(t instanceof NullPointerException)) {
132                    throw t;
133                }
134            }
135
136            /*
137             * Construct a proxy instance.
138             */
139            Handler handler = new Handler();
140            Object proxy = cons.newInstance(handler);
141            handler.currentProxy = proxy;
142
143            /*
144             * Invoke a method on a proxy instance.
145             */
146            Method m = Runnable.class.getMethod("run");
147            ((Runnable) proxy).run();
148            if (!handler.lastMethod.equals(m)) {
149                throw new RuntimeException(
150                    "proxy method invocation failure (lastMethod = " +
151                        handler.lastMethod + ")");
152            }
153
154            System.err.println("\nTEST PASSED");
155
156        } catch (Throwable e) {
157            System.err.println("\nTEST FAILED:");
158            e.printStackTrace();
159            throw new RuntimeException("TEST FAILED: " + e.toString());
160        }
161    }
162
163    public static class Handler implements InvocationHandler {
164
165        Object currentProxy;
166        Method lastMethod;
167
168        public Object invoke(Object proxy, Method method, Object[] args)
169            throws Throwable
170        {
171            if (proxy != currentProxy) {
172                throw new RuntimeException(
173                    "wrong proxy instance passed to invoke method");
174            }
175            lastMethod = method;
176            return null;
177        }
178    }
179}
180