|
Java example source code file (BadConstraintTest.java)
The BadConstraintTest.java Java example source code
/*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6204469
* @summary Test that Open MBean attributes and parameters cannot have
* illegal constraints like min greater than max
* @author Eamonn McManus
* @run clean BadConstraintTest
* @run build BadConstraintTest
* @run main BadConstraintTest
*/
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.management.*;
import javax.management.openmbean.*;
public class BadConstraintTest {
private static String failure;
public static void main(String[] args) throws Exception {
genericTests();
descriptorTests();
if (failure == null)
System.out.println("Test passed");
else
throw new Exception("TEST FAILED: " + failure);
}
private static void genericTests() throws Exception {
for (Object[] test : tests) {
if (test.length != 5) {
throw new Exception("Test element has wrong length: " +
toString(test));
}
OpenType<?> openType = (OpenType>) test[0];
Object defaultValue = test[1];
Comparable<?> minValue = (Comparable>) test[2];
Comparable<?> maxValue = (Comparable>) test[3];
Object[] legalValues = (Object[]) test[4];
System.out.println("test: openType=" + openType +
"; defaultValue=" + defaultValue +
"; minValue=" + minValue +
"; maxValue=" + maxValue +
"; legalValues=" + toString(legalValues));
genericTest(openType, defaultValue, minValue, maxValue,
legalValues);
}
}
private static void descriptorTests() throws Exception {
for (Object[][] test : descriptorTests) {
if (test.length != 2) {
throw new Exception("Test element has wrong length: " +
toString(test));
}
if (test[0].length != 1) {
throw new Exception("Test element should have one OpenType: " +
toString(test[0]));
}
OpenType<?> openType = (OpenType>) test[0][0];
Descriptor d = descriptor(test[1]);
System.out.println("test: openType=" + openType +
"; descriptor=" + d);
descriptorTest(openType, d);
}
}
/* Tests that apply to both the Descriptor and the non-Descriptor
constructors. We invoke the non-Descriptor constructors by
reflection, then we make the corresponding Descriptor and call
the descriptorTest with it. */
private static void genericTest(OpenType<?> openType,
Object defaultValue,
Comparable<?> minValue,
Comparable<?> maxValue,
Object[] legalValues)
throws Exception {
if (minValue == null && maxValue == null && legalValues == null) {
if (defaultValue == null)
throw new Exception("What am I testing?");
Class[] params1 = new Class[] {
String.class, String.class, OpenType.class,
boolean.class, boolean.class, boolean.class,
Object.class
};
Constructor<OpenMBeanAttributeInfoSupport> c1 =
OpenMBeanAttributeInfoSupport.class.getConstructor(params1);
Class[] params2 = new Class[] {
String.class, String.class, OpenType.class,
Object.class
};
Constructor<OpenMBeanParameterInfoSupport> c2 =
OpenMBeanParameterInfoSupport.class.getConstructor(params2);
ode(c1, "name", "descr", openType, true, true, false, defaultValue);
ode(c2, "name", "descr", openType, defaultValue);
descriptorTest(openType,
descriptor("defaultValue", defaultValue));
descriptorTest(openType,
descriptor("defaultValue", string(defaultValue)));
}
if (legalValues == null) {
Class[] params1 = new Class[] {
String.class, String.class, OpenType.class,
boolean.class, boolean.class, boolean.class,
Object.class, Comparable.class, Comparable.class
};
Constructor<OpenMBeanAttributeInfoSupport> c1 =
OpenMBeanAttributeInfoSupport.class.getConstructor(params1);
Class[] params2 = new Class[] {
String.class, String.class, OpenType.class,
Object.class, Comparable.class, Comparable.class
};
Constructor<OpenMBeanParameterInfoSupport> c2 =
OpenMBeanParameterInfoSupport.class.getConstructor(params2);
ode(c1, "name", "descr", openType, true, true, false, defaultValue,
minValue, maxValue);
ode(c2, "name", "descr", openType, defaultValue,
minValue, maxValue);
descriptorTest(openType,
descriptor("defaultValue", defaultValue,
"minValue", minValue,
"maxValue", maxValue));
descriptorTest(openType,
descriptor("defaultValue", string(defaultValue),
"minValue", string(minValue),
"maxValue", string(maxValue)));
}
if (legalValues != null) {
Class[] params1 = new Class[] {
String.class, String.class, OpenType.class,
boolean.class, boolean.class, boolean.class,
Object.class, Object[].class
};
Constructor<OpenMBeanAttributeInfoSupport> c1 =
OpenMBeanAttributeInfoSupport.class.getConstructor(params1);
Class[] params2 = new Class[] {
String.class, String.class, OpenType.class,
Object.class, Object[].class
};
Constructor<OpenMBeanParameterInfoSupport> c2 =
OpenMBeanParameterInfoSupport.class.getConstructor(params2);
ode(c1, "name", "descr", openType, true, true, false, defaultValue,
legalValues);
ode(c2, "name", "descr", openType, defaultValue,
legalValues);
descriptorTest(openType,
descriptor("defaultValue", defaultValue,
"legalValues", legalValues));
descriptorTest(openType,
descriptor("defaultValue", defaultValue,
"legalValues", arraySet(legalValues)));
Set<String> strings = new HashSet
Other Java examples (source code examples)Here is a short list of links related to this Java BadConstraintTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.