Path: blob/master/test/jdk/javax/management/mxbean/PropertyNamesTest.java
41149 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 617551726* @summary Test the PropertyNames annotation with MXBeans27* @author Eamonn McManus28*29* @run clean PropertyNamesTest30* @run build PropertyNamesTest31* @run main PropertyNamesTest32*/3334import javax.management.ConstructorParameters;35import java.util.Collections;36import java.util.List;37import javax.management.JMX;38import javax.management.MBeanServer;39import javax.management.MBeanServerFactory;40import javax.management.ObjectName;41import javax.management.openmbean.CompositeData;42import javax.management.openmbean.CompositeDataSupport;43import javax.management.openmbean.CompositeType;44import javax.management.openmbean.OpenType;45import javax.management.openmbean.SimpleType;4647public class PropertyNamesTest {48public static void main(String[] args) throws Exception {49MBeanServer mbs = MBeanServerFactory.newMBeanServer();50ObjectName pointName = new ObjectName("a:type=Point");51PointMXBean pointmx = new PointImpl();52mbs.registerMBean(pointmx, pointName);53Point point = new Point(1, 2);54PointMXBean pointproxy =55JMX.newMXBeanProxy(mbs, pointName, PointMXBean.class);56Point point1 = pointproxy.identity(point);57if (point1.getX() != point.getX() || point1.getY() != point.getY())58throw new Exception("Point doesn't match");59System.out.println("Point test passed");6061ObjectName evolveName = new ObjectName("a:type=Evolve");62EvolveMXBean evolvemx = new EvolveImpl();63mbs.registerMBean(evolvemx, evolveName);64Evolve evolve =65new Evolve(59, "tralala", Collections.singletonList("tiddly"));66EvolveMXBean evolveProxy =67JMX.newMXBeanProxy(mbs, evolveName, EvolveMXBean.class);68Evolve evolve1 = evolveProxy.identity(evolve);69if (evolve1.getOldInt() != evolve.getOldInt()70|| !evolve1.getNewString().equals(evolve.getNewString())71|| !evolve1.getNewerList().equals(evolve.getNewerList()))72throw new Exception("Evolve doesn't match");73System.out.println("Evolve test passed");7475ObjectName evolvedName = new ObjectName("a:type=Evolved");76EvolveMXBean evolvedmx = new EvolveImpl();77mbs.registerMBean(evolvedmx, evolvedName);78CompositeType evolvedType =79new CompositeType("Evolved", "descr", new String[] {"oldInt"},80new String[] {"oldInt descr"},81new OpenType[] {SimpleType.INTEGER});82CompositeData evolvedData =83new CompositeDataSupport(evolvedType, new String[] {"oldInt"},84new Object[] {5});85CompositeData evolved1 = (CompositeData)86mbs.invoke(evolvedName, "identity", new Object[] {evolvedData},87new String[] {CompositeData.class.getName()});88if ((Integer) evolved1.get("oldInt") != 589|| !evolved1.get("newString").equals("defaultString")90|| ((String[]) evolved1.get("newerList")).length != 0)91throw new Exception("Evolved doesn't match: " + evolved1);92System.out.println("Evolved test passed");93}9495public static class Point {96@ConstructorParameters({"x", "y"})97public Point(int x, int y) {98this.x = x;99this.y = y;100}101102public int getY() {103return y;104}105106public int getX() {107return x;108}109110private final int x, y;111}112113public static interface PointMXBean {114Point identity(Point x);115}116117public static class PointImpl implements PointMXBean {118public Point identity(Point x) {119return x;120}121}122123public static class Evolve {124@ConstructorParameters({"oldInt"})125public Evolve(int oldInt) {126this(oldInt, "defaultString");127}128129@ConstructorParameters({"oldInt", "newString"})130public Evolve(int oldInt, String newString) {131this(oldInt, newString, Collections.<String>emptyList());132}133134@ConstructorParameters({"oldInt", "newString", "newerList"})135public Evolve(int oldInt, String newString, List<String> newerList) {136this.oldInt = oldInt;137this.newString = newString;138this.newerList = newerList;139}140141public int getOldInt() {142return oldInt;143}144145public String getNewString() {146return newString;147}148149public List<String> getNewerList() {150return newerList;151}152153private final int oldInt;154private final String newString;155private final List<String> newerList;156}157158public static interface EvolveMXBean {159Evolve identity(Evolve x);160}161162public static class EvolveImpl implements EvolveMXBean {163public Evolve identity(Evolve x) {164return x;165}166}167}168169170