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