1/*
2 * Copyright (c) 2004, 2015, 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/*
25 * @test
26 * @bug 6177524
27 * @summary Test how to execute the 3 Object methods by a Proxy.
28 * @author Shanliang JIANG
29 *
30 * @run clean ProxyObjectMethodsTest
31 * @run build ProxyObjectMethodsTest
32 * @run main ProxyObjectMethodsTest
33 */
34
35import java.lang.management.ManagementFactory;
36import java.lang.reflect.*;
37import java.util.*;
38
39import javax.management.*;
40import javax.management.remote.*;
41
42public class ProxyObjectMethodsTest {
43
44    public static void main(String[] args) throws Exception {
45        System.out.println("<<< Test how to execute the 3 Object methods by a Proxy.");
46
47        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
48        final ObjectName name = new ObjectName(":class=Simple");
49
50        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
51        final JMXConnectorServer server =
52            JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
53        server.start();
54        url = server.getAddress();
55
56        final JMXConnector client = JMXConnectorFactory.connect(url);
57
58        System.out.println("<<< Test the methods at local side.");
59
60        final Simple simple = new Simple();
61        mbs.registerMBean(simple, name);
62
63        SimpleMBean simple0 =
64            MBeanServerInvocationHandler.newProxyInstance(client.getMBeanServerConnection(),
65                                                          name,
66                                                          SimpleMBean.class,
67                                                          false);
68
69        SimpleMBean simple1 =
70            MBeanServerInvocationHandler.newProxyInstance(client.getMBeanServerConnection(),
71                                                          name,
72                                                          SimpleMBean.class,
73                                                          false);
74
75        Simplest simple3 =
76            MBeanServerInvocationHandler.newProxyInstance(client.getMBeanServerConnection(),
77                                                          name,
78                                                          Simplest.class,
79                                                          false);
80
81        if (!simple0.equals(simple1) ||
82            simple0.equals(simple) ||
83            simple0.equals(simple3)) {
84            throw new RuntimeException("The method equals does not work correctly.");
85        }
86
87        if (simple0.hashCode() != simple1.hashCode() ||
88            simple.hashCode() == simple0.hashCode()) {
89            throw new RuntimeException("The method hashCode does not work correctly.");
90        }
91
92        if (!simple0.toString().equals(simple1.toString()) ||
93            simple.toString().equals(simple0.toString())) {
94            throw new RuntimeException("The method toString does not work correctly.");
95        }
96
97        /* Sorry about this.  This is the equals(String) method,
98           which returns String, not boolean.  */
99        if (!simple0.equals("foo").equals("foo"))
100            throw new RuntimeException("The method equals(String) was not forwarded.");
101
102        ArrayList al = new ArrayList();
103        al.add(simple0);
104
105        if (!al.contains(simple0) || !al.contains(simple1)) {
106            throw new RuntimeException("Cannot find correctly a proxy in an ArrayList.");
107        }
108
109        System.out.println("<<< Test whether the methods are done at server side.");
110
111        final ObjectName name1 = new ObjectName(":class=Test");
112        mbs.registerMBean(new Test(), name1);
113
114        TestMBean test0 = MBeanServerInvocationHandler.newProxyInstance(mbs,
115                                                                name1,
116                                                                TestMBean.class,
117                                                                false);
118
119        if(test0.equals(test0)) {
120            throw new RuntimeException("The method equals is not done remotely as expected.");
121        }
122
123        if (!test0.toString().equals("Test-toString")) {
124            throw new RuntimeException("The method toString is not done remotely as expected.");
125        }
126
127        if (test0.hashCode() != 123) {
128            throw new RuntimeException("The method hashCode is not done remotely as expected.");
129        }
130
131        System.out.println("<<< Test on using a null connection or a null name.");
132        SimpleMBean simple2;
133        try {
134            simple2 = MBeanServerInvocationHandler.newProxyInstance(null,
135                                                                name,
136                                                                SimpleMBean.class,
137                                                                false);
138            throw new RuntimeException(
139                  "Null connection does not cause an IllegalArgumentException.");
140        } catch (IllegalArgumentException ie) {
141            // as expected
142        }
143
144        try {
145            simple2 = MBeanServerInvocationHandler.newProxyInstance(mbs,
146                                                                null,
147                                                                SimpleMBean.class,
148                                                                false);
149            throw new RuntimeException(
150                  "Null object name does not cause an IllegalArgumentException.");
151        } catch (IllegalArgumentException ie) {
152            // as expected
153        }
154    }
155
156    public static interface Simplest {
157
158    }
159
160    public static interface SimpleMBean extends Simplest {
161        public String equals(String x);
162    }
163
164    private static class Simple implements SimpleMBean {
165        public String equals(String x) {
166            return x;
167        }
168    }
169
170    public static interface TestMBean {
171        public boolean equals(Object o);
172
173        public String toString();
174
175        public int hashCode();
176    }
177
178    private static class Test implements TestMBean {
179        public boolean equals(Object o) {
180            // what can do here?
181
182            return false;
183        }
184
185        public String toString() {
186            return "Test-toString";
187        }
188
189        public int hashCode() {
190            return 123;
191        }
192    }
193}
194