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