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