1 /*
   2  * Copyright (c) 2005, 2006, 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 6175517
  27  * @summary Test the PropertyNames annotation with MXBeans
  28  * @author Eamonn McManus
  29  * @run clean PropertyNamesTest
  30  * @run build PropertyNamesTest
  31  * @run main PropertyNamesTest
  32  */
  33 
  34 import java.beans.ConstructorProperties;
  35 import java.util.Collections;
  36 import java.util.List;
  37 import javax.management.JMX;
  38 import javax.management.MBeanServer;
  39 import javax.management.MBeanServerFactory;
  40 import javax.management.ObjectName;
  41 import javax.management.openmbean.CompositeData;
  42 import javax.management.openmbean.CompositeDataSupport;
  43 import javax.management.openmbean.CompositeType;
  44 import javax.management.openmbean.OpenType;
  45 import javax.management.openmbean.SimpleType;
  46 
  47 public class PropertyNamesTest {
  48     public static void main(String[] args) throws Exception {
  49         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
  50         ObjectName pointName = new ObjectName("a:type=Point");
  51         PointMXBean pointmx = new PointImpl();
  52         mbs.registerMBean(pointmx, pointName);
  53         Point point = new Point(1, 2);
  54         PointMXBean pointproxy =
  55             JMX.newMXBeanProxy(mbs, pointName, PointMXBean.class);
  56         Point point1 = pointproxy.identity(point);
  57         if (point1.getX() != point.getX() || point1.getY() != point.getY())
  58             throw new Exception("Point doesn't match");
  59         System.out.println("Point test passed");
  60 
  61         ObjectName evolveName = new ObjectName("a:type=Evolve");
  62         EvolveMXBean evolvemx = new EvolveImpl();
  63         mbs.registerMBean(evolvemx, evolveName);
  64         Evolve evolve =
  65             new Evolve(59, "tralala", Collections.singletonList("tiddly"));
  66         EvolveMXBean evolveProxy =
  67             JMX.newMXBeanProxy(mbs, evolveName, EvolveMXBean.class);
  68         Evolve evolve1 = evolveProxy.identity(evolve);
  69         if (evolve1.getOldInt() != evolve.getOldInt()
  70                 || !evolve1.getNewString().equals(evolve.getNewString())
  71                 || !evolve1.getNewerList().equals(evolve.getNewerList()))
  72             throw new Exception("Evolve doesn't match");
  73         System.out.println("Evolve test passed");
  74 
  75         ObjectName evolvedName = new ObjectName("a:type=Evolved");
  76         EvolveMXBean evolvedmx = new EvolveImpl();
  77         mbs.registerMBean(evolvedmx, evolvedName);
  78         CompositeType evolvedType =
  79             new CompositeType("Evolved", "descr", new String[] {"oldInt"},
  80                               new String[] {"oldInt descr"},
  81                               new OpenType[] {SimpleType.INTEGER});
  82         CompositeData evolvedData =
  83             new CompositeDataSupport(evolvedType, new String[] {"oldInt"},
  84                                      new Object[] {5});
  85         CompositeData evolved1 = (CompositeData)
  86             mbs.invoke(evolvedName, "identity", new Object[] {evolvedData},
  87                        new String[] {CompositeData.class.getName()});
  88         if ((Integer) evolved1.get("oldInt") != 5
  89                 || !evolved1.get("newString").equals("defaultString")
  90                 || ((String[]) evolved1.get("newerList")).length != 0)
  91             throw new Exception("Evolved doesn't match: " + evolved1);
  92         System.out.println("Evolved test passed");
  93     }
  94 
  95     public static class Point {
  96         @ConstructorProperties({"x", "y"})
  97         public Point(int x, int y) {
  98             this.x = x;
  99             this.y = y;
 100         }
 101 
 102         public int getY() {
 103             return y;
 104         }
 105 
 106         public int getX() {
 107             return x;
 108         }
 109 
 110         private final int x, y;
 111     }
 112 
 113     public static interface PointMXBean {
 114         Point identity(Point x);
 115     }
 116 
 117     public static class PointImpl implements PointMXBean {
 118         public Point identity(Point x) {
 119             return x;
 120         }
 121     }
 122 
 123     public static class Evolve {
 124         @ConstructorProperties({"oldInt"})
 125         public Evolve(int oldInt) {
 126             this(oldInt, "defaultString");
 127         }
 128 
 129         @ConstructorProperties({"oldInt", "newString"})
 130         public Evolve(int oldInt, String newString) {
 131             this(oldInt, newString, Collections.<String>emptyList());
 132         }
 133 
 134         @ConstructorProperties({"oldInt", "newString", "newerList"})
 135         public Evolve(int oldInt, String newString, List<String> newerList) {
 136             this.oldInt = oldInt;
 137             this.newString = newString;
 138             this.newerList = newerList;
 139         }
 140 
 141         public int getOldInt() {
 142             return oldInt;
 143         }
 144 
 145         public String getNewString() {
 146             return newString;
 147         }
 148 
 149         public List<String> getNewerList() {
 150             return newerList;
 151         }
 152 
 153         private final int oldInt;
 154         private final String newString;
 155         private final List<String> newerList;
 156     }
 157 
 158     public static interface EvolveMXBean {
 159         Evolve identity(Evolve x);
 160     }
 161 
 162     public static class EvolveImpl implements EvolveMXBean {
 163         public Evolve identity(Evolve x) {
 164             return x;
 165         }
 166     }
 167 }