Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/openmbean/TabularDataOrderTest.java
38839 views
/*1* Copyright (c) 2008, 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 633466326* @summary Test that TabularDataSupport preserves the order elements were added27* @author Eamonn McManus28*/2930import java.io.ByteArrayInputStream;31import java.io.ByteArrayOutputStream;32import java.io.ObjectInputStream;33import java.io.ObjectOutputStream;34import java.lang.reflect.Field;35import java.lang.reflect.Modifier;36import java.util.ArrayList;37import java.util.HashMap;38import java.util.LinkedHashMap;39import java.util.List;40import java.util.Map;41import javax.management.JMX;42import javax.management.MBeanServer;43import javax.management.MBeanServerFactory;44import javax.management.ObjectName;45import javax.management.openmbean.CompositeData;46import javax.management.openmbean.CompositeDataSupport;47import javax.management.openmbean.CompositeType;48import javax.management.openmbean.OpenDataException;49import javax.management.openmbean.OpenType;50import javax.management.openmbean.SimpleType;51import javax.management.openmbean.TabularData;52import javax.management.openmbean.TabularDataSupport;53import javax.management.openmbean.TabularType;5455public class TabularDataOrderTest {56private static String failure;5758private static final String COMPAT_PROP_NAME = "jmx.tabular.data.hash.map";5960private static final String[] intNames = {61"unus", "duo", "tres", "quatuor", "quinque", "sex", "septem",62"octo", "novem", "decim",63};64private static final Map<String, Integer> stringToValue =65new LinkedHashMap<String, Integer>();66static {67for (int i = 0; i < intNames.length; i++)68stringToValue.put(intNames[i], i + 1);69}7071public static interface TestMXBean {72public Map<String, Integer> getMap();73}7475public static class TestImpl implements TestMXBean {76public Map<String, Integer> getMap() {77return stringToValue;78}79}8081private static final CompositeType ct;82private static final TabularType tt;83static {84try {85ct = new CompositeType(86"a.b.c", "name and int",87new String[] {"name", "int"},88new String[] {"name of integer", "value of integer"},89new OpenType<?>[] {SimpleType.STRING, SimpleType.INTEGER});90tt = new TabularType(91"d.e.f", "name and int indexed by name", ct,92new String[] {"name"});93} catch (OpenDataException e) {94throw new AssertionError(e);95}96}9798private static TabularData makeTable() throws OpenDataException {99TabularData td = new TabularDataSupport(tt);100for (Map.Entry<String, Integer> entry : stringToValue.entrySet()) {101CompositeData cd = new CompositeDataSupport(102ct,103new String[] {"name", "int"},104new Object[] {entry.getKey(), entry.getValue()});105td.put(cd);106}107return td;108}109110public static void main(String[] args) throws Exception {111System.out.println("Testing standard behaviour");112TabularData td = makeTable();113System.out.println(td);114115// Test that a default TabularData has the order keys were added in116int last = 0;117boolean ordered = true;118for (Object x : td.values()) {119CompositeData cd = (CompositeData) x;120String name = (String) cd.get("name");121int value = (Integer) cd.get("int");122System.out.println(name + " = " + value);123if (last + 1 != value)124ordered = false;125last = value;126}127if (!ordered)128fail("Order not preserved");129130// Now test the undocumented property that causes HashMap to be used131// instead of LinkedHashMap, in case serializing to a 1.3 client.132// We serialize and deserialize in case the implementation handles133// this at serialization time. Then we look at object fields; that's134// not guaranteed to work but at worst it will fail spuriously and135// we'll have to update the test.136System.out.println("Testing compatible behaviour");137System.setProperty(COMPAT_PROP_NAME, "true");138td = makeTable();139System.out.println(td);140ByteArrayOutputStream bout = new ByteArrayOutputStream();141ObjectOutputStream oout = new ObjectOutputStream(bout);142oout.writeObject(td);143oout.close();144byte[] bytes = bout.toByteArray();145ByteArrayInputStream bin = new ByteArrayInputStream(bytes);146ObjectInputStream oin = new ObjectInputStream(bin);147td = (TabularData) oin.readObject();148boolean found = false;149for (Field f : td.getClass().getDeclaredFields()) {150if (Modifier.isStatic(f.getModifiers()))151continue;152f.setAccessible(true);153Object x = f.get(td);154if (x != null && x.getClass() == HashMap.class) {155found = true;156System.out.println(157x.getClass().getName() + " TabularDataSupport." +158f.getName() + " = " + x);159break;160}161}162if (!found) {163fail("TabularDataSupport does not contain HashMap though " +164COMPAT_PROP_NAME + "=true");165}166System.clearProperty(COMPAT_PROP_NAME);167168System.out.println("Testing MXBean behaviour");169MBeanServer mbs = MBeanServerFactory.newMBeanServer();170ObjectName name = new ObjectName("a:b=c");171mbs.registerMBean(new TestImpl(), name);172TestMXBean proxy = JMX.newMXBeanProxy(mbs, name, TestMXBean.class);173Map<String, Integer> map = proxy.getMap();174List<String> origNames = new ArrayList<String>(stringToValue.keySet());175List<String> proxyNames = new ArrayList<String>(map.keySet());176if (!origNames.equals(proxyNames))177fail("Order mangled after passage through MXBean: " + proxyNames);178179if (failure == null)180System.out.println("TEST PASSED");181else182throw new Exception("TEST FAILED: " + failure);183}184185private static void fail(String why) {186System.out.println("FAILED: " + why);187failure = why;188}189}190191192