/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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 check constraints
* @author Eamonn McManus
* @run clean ConstraintTest
* @run build ConstraintTest
* @run main ConstraintTest
*/
import javax.management.*;
public class ConstraintTest {
throw new Exception("Test element has wrong length: " +
}
throw new Exception("Test constraints should have size 4: " +
}
"; minValue=" + minValue +
"; maxValue=" + maxValue +
"; legalValues=" +
throw new Exception("OpenType list should have size 1: " +
}
}
else
}
Comparable<?> minValue,
throws Exception {
/* This hack is needed to avoid grief from the parameter checking
in the OpenMBean*InfoSupport constructors. Since they are defined
to check that the defaultValue etc are of the same type as the
OpenType<T>, there is no way to pass a defaultValue etc when
the type is OpenType<?>. So either you have to write plain
OpenType, and get unchecked warnings for every constructor
invocation, or you do this, and get the unchecked warnings just
here. */
}
Comparable<T> minValue,
throws Exception {
legalValues == null) {
true, true, false),
true, true, false, nullD),
true, true, false, emptyD),
nullD),
emptyD),
}
true, true, false, d),
openType, true, true,
false, defaultValue),
d),
}
if (legalValues == null) {
"minValue", minValue,
"maxValue", maxValue);
true, true, false, d),
true, true, false,
d),
}
// Legal values in descriptor can be either an array or a set
"legalValues", legalValues);
if (legalValues == null)
else {
}
true, true, false, d1),
true, true, false, d2),
true, true, false,
d1),
d2),
}
}
/* Test one of the objects. Note that OpenMBeanAttributeInfo
extends OpenMBeanParameterInfo, so OpenMBeanAttributeInfoSupport
is-an OpenMBeanParameterInfo. */
// Check that the constraints can be specified as strings
// rather than objects
continue;
Collection<?> legals;
if (values[i] instanceof Collection<?>)
else
}
if (info instanceof OpenMBeanAttributeInfo) {
info.getOpenType(),
ai.isReadable(),
ai.isWritable(),
d);
} else {
info.getOpenType(),
d);
}
}
}
info);
}
}
info);
}
}
/* If you specify e.g. minValue in a descriptor, then we arrange
for getMinValue() to return the same value, and if you specify
a minValue as a constructor parameter then we arrange for the
descriptor to have a minValue entry. Check that these values
do in fact match. */
d.getFieldValue("defaultValue"));
d.getFieldValue("minValue"));
d.getFieldValue("maxValue"));
d.getFieldValue("legalValues"));
}
if (getterValue == null) {
if (descriptorValue != null) {
fail("Getter returned null but descriptor has entry for " +
}
} else if (descriptorValue == null) {
fail("Getter returned value but descriptor has no entry for " +
" but descriptor entry is " + descriptorValue);
}
}
if (getterValues == null) {
if (descriptorValues != null) {
fail("Getter returned null but descriptor has entry for " +
}
} else if (descriptorValues == null) {
fail("Getter returned value but descriptor has no entry for " +
} else {
if (descriptorValues instanceof Set<?>)
else
boolean same = true;
same = false;
else {
for (Object x : getterValues) {
if (!descriptorValueSet.contains(x)
same = false;
break;
}
}
}
if (!same) {
" but descriptor entry is " + descriptorValueSet);
}
}
}
}
throw new RuntimeException("Odd length descriptor entries");
}
}
}
private static final OpenType<?>
static {
try {
} catch (OpenDataException e) { // damn checked exceptions...
throw new IllegalArgumentException(e.toString(), e);
}
}
private static final Descriptor
/* The elements of this array are grouped as follows. Each
element contains four Object[]s. The first one is a set of
four values: default value, min value, max value, legal values
(an Object[]), some of which can be null. These will be used
to derive the OpenMBean*Info values to be tested. The second
is an array with one element that is the OpenType that will be
given to the constructors of the OpenMBean*Infos. The third
element is a set of values that should be valid according to
the constraints in the OpenMBean*Info. The fourth is a set of
values that should be invalid according to those
constraints. */
// Test cases when there are no constraints
// Validity checking is limited to type of object
{oint},
{obool},
{true, false},
{ostring},
{"", "yes!"},
{obyte},
{ostringarray},
{"goodbye", "cruel", "world"}}},
{ocomposite},
// Test cases where there is a default value, so null is allowed
{oint},
{obool},
{null, true, false},
{ostring},
{obyte},
{ocomposite},
{oint},
{oint},
{0, 25, 50},
{oint},
{oint},
{ostring},
{"A", "~", "", -1}},
// Test cases where there is a set of legal values
{oint},
{ocomposite},
{olong},
{}, // constraint is impossible to satisfy!
};
}