Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/openmbean/BadConstraintTest.java
38838 views
/*1* Copyright (c) 2005, 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 620446926* @summary Test that Open MBean attributes and parameters cannot have27* illegal constraints like min greater than max28* @author Eamonn McManus29* @run clean BadConstraintTest30* @run build BadConstraintTest31* @run main BadConstraintTest32*/3334import java.io.*;35import java.lang.reflect.*;36import java.util.*;37import javax.management.*;38import javax.management.openmbean.*;3940public class BadConstraintTest {41private static String failure;4243public static void main(String[] args) throws Exception {44genericTests();45descriptorTests();4647if (failure == null)48System.out.println("Test passed");49else50throw new Exception("TEST FAILED: " + failure);51}5253private static void genericTests() throws Exception {54for (Object[] test : tests) {55if (test.length != 5) {56throw new Exception("Test element has wrong length: " +57toString(test));58}5960OpenType<?> openType = (OpenType<?>) test[0];61Object defaultValue = test[1];62Comparable<?> minValue = (Comparable<?>) test[2];63Comparable<?> maxValue = (Comparable<?>) test[3];64Object[] legalValues = (Object[]) test[4];6566System.out.println("test: openType=" + openType +67"; defaultValue=" + defaultValue +68"; minValue=" + minValue +69"; maxValue=" + maxValue +70"; legalValues=" + toString(legalValues));7172genericTest(openType, defaultValue, minValue, maxValue,73legalValues);74}75}7677private static void descriptorTests() throws Exception {78for (Object[][] test : descriptorTests) {79if (test.length != 2) {80throw new Exception("Test element has wrong length: " +81toString(test));82}8384if (test[0].length != 1) {85throw new Exception("Test element should have one OpenType: " +86toString(test[0]));87}8889OpenType<?> openType = (OpenType<?>) test[0][0];90Descriptor d = descriptor(test[1]);9192System.out.println("test: openType=" + openType +93"; descriptor=" + d);9495descriptorTest(openType, d);96}97}9899/* Tests that apply to both the Descriptor and the non-Descriptor100constructors. We invoke the non-Descriptor constructors by101reflection, then we make the corresponding Descriptor and call102the descriptorTest with it. */103private static void genericTest(OpenType<?> openType,104Object defaultValue,105Comparable<?> minValue,106Comparable<?> maxValue,107Object[] legalValues)108throws Exception {109110if (minValue == null && maxValue == null && legalValues == null) {111if (defaultValue == null)112throw new Exception("What am I testing?");113Class[] params1 = new Class[] {114String.class, String.class, OpenType.class,115boolean.class, boolean.class, boolean.class,116Object.class117};118Constructor<OpenMBeanAttributeInfoSupport> c1 =119OpenMBeanAttributeInfoSupport.class.getConstructor(params1);120Class[] params2 = new Class[] {121String.class, String.class, OpenType.class,122Object.class123};124Constructor<OpenMBeanParameterInfoSupport> c2 =125OpenMBeanParameterInfoSupport.class.getConstructor(params2);126ode(c1, "name", "descr", openType, true, true, false, defaultValue);127ode(c2, "name", "descr", openType, defaultValue);128descriptorTest(openType,129descriptor("defaultValue", defaultValue));130descriptorTest(openType,131descriptor("defaultValue", string(defaultValue)));132}133134if (legalValues == null) {135Class[] params1 = new Class[] {136String.class, String.class, OpenType.class,137boolean.class, boolean.class, boolean.class,138Object.class, Comparable.class, Comparable.class139};140Constructor<OpenMBeanAttributeInfoSupport> c1 =141OpenMBeanAttributeInfoSupport.class.getConstructor(params1);142Class[] params2 = new Class[] {143String.class, String.class, OpenType.class,144Object.class, Comparable.class, Comparable.class145};146Constructor<OpenMBeanParameterInfoSupport> c2 =147OpenMBeanParameterInfoSupport.class.getConstructor(params2);148ode(c1, "name", "descr", openType, true, true, false, defaultValue,149minValue, maxValue);150ode(c2, "name", "descr", openType, defaultValue,151minValue, maxValue);152descriptorTest(openType,153descriptor("defaultValue", defaultValue,154"minValue", minValue,155"maxValue", maxValue));156descriptorTest(openType,157descriptor("defaultValue", string(defaultValue),158"minValue", string(minValue),159"maxValue", string(maxValue)));160}161162if (legalValues != null) {163Class[] params1 = new Class[] {164String.class, String.class, OpenType.class,165boolean.class, boolean.class, boolean.class,166Object.class, Object[].class167};168Constructor<OpenMBeanAttributeInfoSupport> c1 =169OpenMBeanAttributeInfoSupport.class.getConstructor(params1);170Class[] params2 = new Class[] {171String.class, String.class, OpenType.class,172Object.class, Object[].class173};174Constructor<OpenMBeanParameterInfoSupport> c2 =175OpenMBeanParameterInfoSupport.class.getConstructor(params2);176ode(c1, "name", "descr", openType, true, true, false, defaultValue,177legalValues);178ode(c2, "name", "descr", openType, defaultValue,179legalValues);180descriptorTest(openType,181descriptor("defaultValue", defaultValue,182"legalValues", legalValues));183descriptorTest(openType,184descriptor("defaultValue", defaultValue,185"legalValues", arraySet(legalValues)));186Set<String> strings = new HashSet<String>();187for (Object x : legalValues)188strings.add(x.toString());189descriptorTest(openType,190descriptor("defaultValue", defaultValue,191"legalValues", strings));192descriptorTest(openType,193descriptor("defaultValue", defaultValue,194"legalValues",195strings.toArray(new String[0])));196}197}198199private static void descriptorTest(OpenType<?> openType, Descriptor d)200throws Exception {201Class[] params1 = new Class[] {202String.class, String.class, OpenType.class,203boolean.class, boolean.class, boolean.class,204Descriptor.class205};206Constructor<OpenMBeanAttributeInfoSupport> c1 =207OpenMBeanAttributeInfoSupport.class.getConstructor(params1);208Class[] params2 = new Class[] {209String.class, String.class, OpenType.class,210Descriptor.class211};212Constructor<OpenMBeanParameterInfoSupport> c2 =213OpenMBeanParameterInfoSupport.class.getConstructor(params2);214iae(c1, "name", "descr", openType, true, true, false, d);215iae(c2, "name", "descr", openType, d);216}217218/* Check that the given constructor invocation gets an219IllegalArgumentException. */220private static void iae(Constructor<?> con, Object... params) {221checkException(IllegalArgumentException.class, con, params);222}223224/* Check that the given constructor invocation gets an225OpenDataException. */226private static void ode(Constructor<?> con, Object... params) {227checkException(OpenDataException.class, con, params);228}229230private static void checkException(Class<? extends Exception> exc,231Constructor<?> con, Object[] params) {232try {233con.newInstance(params);234fail("Constructor succeeded but should have got " + exc.getName() +235" with params " + Arrays.deepToString(params));236} catch (InvocationTargetException e) {237Throwable cause = e.getCause();238if (exc.isInstance(cause))239return;240StringWriter sw = new StringWriter();241PrintWriter pw = new PrintWriter(sw);242cause.printStackTrace(pw);243pw.close();244fail("Constructor should have got " + exc.getName() +245" with params " + Arrays.deepToString(params) + ": " + sw);246} catch (Exception e) {247throw new IllegalArgumentException("Reflection failed", e);248}249}250251private static void fail(String why) {252System.out.println("FAILED: " + why);253failure = why;254}255256private static Descriptor descriptor(Object... entries) {257if (entries.length % 2 != 0)258throw new RuntimeException("Odd length descriptor entries");259String[] names = new String[entries.length / 2];260Object[] values = new Object[entries.length / 2];261for (int i = 0; i < entries.length; i += 2) {262names[i / 2] = (String) entries[i];263values[i / 2] = entries[i + 1];264}265return new ImmutableDescriptor(names, values);266}267268private static <T> Set<T> arraySet(T[] array) {269return new HashSet<T>(Arrays.asList(array));270}271272private static String toString(Object x) {273if (x == null)274return "null";275else if (x.getClass().isArray()) {276StringBuilder sb = new StringBuilder("[");277int len = Array.getLength(x);278for (int i = 0; i < len; i++) {279if (i > 0)280sb.append(", ");281sb.append(toString(Array.get(x, i)));282}283sb.append("]");284return sb.toString();285} else286return x.toString();287}288289private static String string(Object x) {290if (x == null)291return null;292return toString(x);293}294295private static final OpenType<?>296ostring = SimpleType.STRING,297oint = SimpleType.INTEGER,298obool = SimpleType.BOOLEAN,299olong = SimpleType.LONG,300obyte = SimpleType.BYTE,301ofloat = SimpleType.FLOAT,302odouble = SimpleType.DOUBLE,303ostringarray, ostringarray2;304private static final CompositeType ocomposite;305private static final CompositeData compositeData, compositeData2;306static {307try {308ostringarray = new ArrayType<String[]>(1, ostring);309ostringarray2 = new ArrayType<String[][]>(2, ostring);310ocomposite =311new CompositeType("name", "descr",312new String[] {"s", "i"},313new String[] {"sdesc", "idesc"},314new OpenType[] {ostring, oint});315compositeData =316new CompositeDataSupport(ocomposite,317new String[] {"s", "i"},318new Object[] {"foo", 23});319compositeData2 =320new CompositeDataSupport(ocomposite,321new String[] {"s", "i"},322new Object[] {"bar", -23});323} catch (OpenDataException e) { // damn checked exceptions...324throw new IllegalArgumentException(e.toString(), e);325}326}327328private static final Descriptor329nullD = null,330emptyD = ImmutableDescriptor.EMPTY_DESCRIPTOR;331332/* Each element of this array contains five Objects:333- OpenType;334- defaultValue;335- minValue;336- maxValue;337- legalValues. The objects are used to construct tests cases338where all possible constructors that make sense for that339combination of values are invoked, and all are checked to ensure340that they throw the right exception. */341private static final Object[][] tests = {342343// Values must be of appropriate type344345{oint, "oops", null, null, null},346{oint, Long.MAX_VALUE, null, null, null},347{oint, null, "oops", null, null},348{oint, "oops", 3, null, null},349{oint, 3, "oops", null, null},350{oint, null, null, "oops", null},351{oint, null, 3, "oops", null},352{oint, null, 3, false, null},353{oint, null, null, null, new String[] {"x"}},354{oint, null, null, null, new Object[] {"x"}},355{oint, null, null, null, new Object[] {3, "x"}},356357// If defaultValue is present then it must satisfy the constraints358// defined by legalValues, minValue, or maxValue when any of359// these is also present360361{oint, 3, 4, null, null},362{oint, 3, null, 2, null},363{oint, 3, null, null, new Integer[] {2, 4}},364365// If minValue and maxValue are both present then minValue must366// not be greater than maxValue367368{ostring, null, "z", "a", null},369{oint, null, 3, 2, null},370371// We don't support default values or legal sets for arrays (yet)372373{ostringarray, new String[] {"x"}, null, null, null},374{ostringarray, null, null, null, new String[][]{new String[] {"x"}}},375};376377/* The tests here can only be expressed via Descriptors because an378attempt to invoke one of the non-Descriptor constructors with379the implied parameters would not compile (or would fail at the380reflection stage when reflection is being used).381382Each element of this array contains two subarrays. The first383is an array of OpenTypes that must contain exactly one element.384The second is an array of alternating field names and field385values that will be used to construct a Descriptor that is supposed386to fail when given to an OpenMBean*Info constructor with the given387OpenType. */388private static final Object[][][] descriptorTests = {389390// Values must be of appropriate type391392{{oint},393{"minValue", 25L}},394395{{oint},396{"minValue", new Object()}}, // not even Comparable397{{oint},398{"maxValue", new Object()}}, // not even Comparable399{{oint},400{"defaultValue", 3,401"minValue", new Object()}},402{{oint},403{"defaultValue", 3,404"maxValue", new Object()}},405406{{oint},407{"legalValues", new int[] {3}}}, // should be new Integer[] to work408{{oint},409{"legalValues", 3}},410411// If legalValues is present then neither minValue nor maxValue412// must be present413414{{oint},415{"minValue", 3, "legalValues", new Integer[] {3, 4}}},416{{oint},417{"maxValue", 3, "legalValues", new Integer[] {2, 3}}},418{{oint},419{"defaultValue", 3, "minValue", 3, "legalValues", new Integer[] {3}}},420421// We don't support min or max arrays (what would they mean?)422423{{ostringarray},424{"minValue", new String[] {"x"}}},425{{ostringarray},426{"maxValue", new String[] {"x"}}},427428};429}430431432