jdk/test/javax/management/mxbean/PropertyNamesTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 5506 202f599c92aa
child 33315 d2a313368ccb
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6175517
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test the PropertyNames annotation with MXBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    29
 * @modules java.desktop
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    30
 *          java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean PropertyNamesTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build PropertyNamesTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main PropertyNamesTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.JMX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.openmbean.CompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.openmbean.CompositeDataSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.openmbean.CompositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.openmbean.OpenType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.openmbean.SimpleType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class PropertyNamesTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ObjectName pointName = new ObjectName("a:type=Point");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        PointMXBean pointmx = new PointImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        mbs.registerMBean(pointmx, pointName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Point point = new Point(1, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        PointMXBean pointproxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            JMX.newMXBeanProxy(mbs, pointName, PointMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Point point1 = pointproxy.identity(point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (point1.getX() != point.getX() || point1.getY() != point.getY())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new Exception("Point doesn't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.out.println("Point test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        ObjectName evolveName = new ObjectName("a:type=Evolve");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        EvolveMXBean evolvemx = new EvolveImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        mbs.registerMBean(evolvemx, evolveName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        Evolve evolve =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            new Evolve(59, "tralala", Collections.singletonList("tiddly"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        EvolveMXBean evolveProxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            JMX.newMXBeanProxy(mbs, evolveName, EvolveMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Evolve evolve1 = evolveProxy.identity(evolve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (evolve1.getOldInt() != evolve.getOldInt()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                || !evolve1.getNewString().equals(evolve.getNewString())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                || !evolve1.getNewerList().equals(evolve.getNewerList()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new Exception("Evolve doesn't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        System.out.println("Evolve test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        ObjectName evolvedName = new ObjectName("a:type=Evolved");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        EvolveMXBean evolvedmx = new EvolveImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        mbs.registerMBean(evolvedmx, evolvedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        CompositeType evolvedType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            new CompositeType("Evolved", "descr", new String[] {"oldInt"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                              new String[] {"oldInt descr"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                              new OpenType[] {SimpleType.INTEGER});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        CompositeData evolvedData =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            new CompositeDataSupport(evolvedType, new String[] {"oldInt"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                     new Object[] {5});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        CompositeData evolved1 = (CompositeData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            mbs.invoke(evolvedName, "identity", new Object[] {evolvedData},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                       new String[] {CompositeData.class.getName()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if ((Integer) evolved1.get("oldInt") != 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                || !evolved1.get("newString").equals("defaultString")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                || ((String[]) evolved1.get("newerList")).length != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new Exception("Evolved doesn't match: " + evolved1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.out.println("Evolved test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static class Point {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        @ConstructorProperties({"x", "y"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        public Point(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            this.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            this.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        public int getY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        public int getX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        private final int x, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static interface PointMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Point identity(Point x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public static class PointImpl implements PointMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public Point identity(Point x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static class Evolve {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        @ConstructorProperties({"oldInt"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public Evolve(int oldInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            this(oldInt, "defaultString");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        @ConstructorProperties({"oldInt", "newString"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        public Evolve(int oldInt, String newString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            this(oldInt, newString, Collections.<String>emptyList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        @ConstructorProperties({"oldInt", "newString", "newerList"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public Evolve(int oldInt, String newString, List<String> newerList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            this.oldInt = oldInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            this.newString = newString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            this.newerList = newerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        public int getOldInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return oldInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        public String getNewString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return newString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        public List<String> getNewerList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return newerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        private final int oldInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        private final String newString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        private final List<String> newerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public static interface EvolveMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        Evolve identity(Evolve x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public static class EvolveImpl implements EvolveMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        public Evolve identity(Evolve x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}