/*
* 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 6273765
* @summary Test various aspects of the Descriptor interface
* @author Eamonn McManus
* @run clean DescriptorTest
* @run build DescriptorTest
* @run main DescriptorTest
*/
import javax.management.*;
public class DescriptorTest {
// Warning: many tests here know the contents of these variables
// so if you change them you must change the tests
"a", "C", "aa", "int", "nul",
};
};
"a=b", "C=D", "aa=bb", "int=(5)", "nul=",
};
genericTests(ImmutableDescriptor.class);
if (failureMessage != null)
else
}
" ---");
}
/*
Testing has three parts. We take the input parameter, of type P,
and give it to the "prepare" method. That returns us a test
parameter, of type T. We give that to the "test" method. That
in turn returns us a check value, of type C. We give this to the
"check" method. If the "check" method returns null, the test passes.
If the "check" method returns a string, that string explains the
test failure. If any of the methods throws an exception, the
test fails.
*/
private static abstract class Case<P, T, C> {
}
void run(P p) {
try {
T t = prepare(p);
C c = test(t);
}
} catch (Exception e) {
failureMessage = e.toString();
}
}
}
/*
Test case where the preparation step consists of constructing an
instance of the given Descriptor subclass containing test values,
then giving that to the "test" method.
*/
private static abstract class ProtoCase<C>
super(name);
}
throws Exception {
}
}
/*
Test case where the "test" method must return a value of type C
which we will compare against the testValue parameter given to
the test constructor.
*/
super(name);
}
final boolean equal =
array ?
if (equal)
return null;
}
private final C testValue;
}
/*
Test case where the dontChange method does some operation on the
test Descriptor that is not supposed to change the contents of
the Descriptor. This should work for both mutable and immutable
Descriptors, since immutable Descriptors are supposed to do
nothing (rather than throw an exception) for mutation operations
that would not in fact change the contents.
*/
super(name);
}
dontChange(d);
return d;
}
return "descriptor value changed: " + testFieldNames[i] +
}
return null;
}
}
/*
Test case where the change(d) method attempts to make some
change to the Descriptor d. The behaviour depends on whether
the Descriptor is mutable or not. If the Descriptor is
immutable, then the change attempt must throw a
RuntimeOperationsException wrapping an
UnsupportedOperationException. If the Descriptor is mutable,
then the change attempt must succeed, and the Descriptor must
then look like the fieldsAndValues parameter to the constructor.
This is simply an alternating set of field names and corresponding
values. So for example if it is
"a", "b", "x", 5
that represents a Descriptor with fields "a" and "x" whose
corresponding values are "x" and Integer.valueOf(5).
*/
super(name);
throw new AssertionError("test wrong: odd fieldsAndValues");
this.fieldsAndValues = fieldsAndValues;
void provoke(Descriptor d) {
ChangedCase.this.change(d);
}
};
}
if (immutable(d))
return immutableTest.test(d);
else {
change(d);
return d;
}
}
if (c instanceof Exception)
else if (!(c instanceof Descriptor)) {
return "test returned strange value: " +
c.getClass() + ": " + c;
} else {
Descriptor d = (Descriptor) c;
}
return "wrong field names after change: found " +
}
return "wrong value after change: for fields " +
}
return null;
}
}
}
/*
Test case where an operation provoke(d) on the test Descriptor d
is supposed to provoke an exception. The exception must be a
RuntimeOperationsException wrapping another exception whose type
is determined by the exceptionClass() method.
*/
super(name);
}
try {
provoke(d);
return null;
} catch (Exception e) {
return e;
}
}
if (e == null)
return "did not throw exception: " + expected();
if (!(e instanceof RuntimeOperationsException)) {
e.printStackTrace(pw);
}
return null;
}
}
}
super(name);
}
return IllegalArgumentException.class;
}
}
private static abstract class UnsupportedExceptionCase
extends ExceptionCase {
super(name);
}
return UnsupportedOperationException.class;
}
}
/*
List of test cases. We will run through these once for
ImmutableDescriptor and once for DescriptorSupport.
Expect a compiler [unchecked] warning for this initialization.
Writing
new Case<Class<? extends Descriptor>, ?, ?>[] = {...}
would cause a compiler error since you can't have arrays of
parameterized types unless all the parameters are just "?".
This hack with varargs gives us a compiler warning instead.
Writing just:
new Case<?, ?, ?>[] = {...}
would compile here, but not where we call test.run, since you
cannot pass an object to the run(P) method if P is "?".
*/
// TEST VALUES RETURNED BY GETTERS
"getFieldValues on empty Descriptor") {
throws Exception {
c.getConstructor(String[].class);
}
}
return null;
return "value should be array with null elements: " +
Arrays.deepToString(v);
}
},
return set(d.getFieldNames());
}
},
}
},
return d.getFieldValue("a");
}
},
"D") {
return d.getFieldValue("c");
}
},
"bb") {
return d.getFieldValue("AA");
}
},
"bb") {
return d.getFieldValue("aA");
}
},
set(testFieldValues)) {
}
},
}
},
"lower case",
}
},
"upper case",
}
},
}
},
return d.getFieldValues("");
}
},
new Object[0]) {
return d.getFieldValues();
}
},
// TEST OPERATIONS THAT DON'T CHANGE THE DESCRIPTOR
// Even for immutable descriptors, these are allowed
new UnchangedCase("removeField with nonexistent field") {
void dontChange(Descriptor d) {
d.removeField("noddy");
}
},
new UnchangedCase("removeField with null field") {
void dontChange(Descriptor d) {
d.removeField(null);
}
},
new UnchangedCase("removeField with empty field") {
void dontChange(Descriptor d) {
d.removeField("");
}
},
new UnchangedCase("setField leaving string unchanged") {
void dontChange(Descriptor d) {
}
},
new UnchangedCase("setField leaving int unchanged") {
void dontChange(Descriptor d) {
}
},
// We do not test whether you can do a setField/s with an
// unchanged value but the case of the name different.
// From the spec, that should probably be illegal, but
// it's such a corner case that we leave it alone.
new UnchangedCase("setFields with empty arrays") {
void dontChange(Descriptor d) {
}
},
new UnchangedCase("setFields with unchanged values") {
void dontChange(Descriptor d) {
}
},
// TEST OPERATIONS THAT DO CHANGE THE DESCRIPTOR
// For immutable descriptors, these should provoke an exception
new ChangedCase("removeField with exact case",
void change(Descriptor d) {
d.removeField("aa");
}
},
new ChangedCase("removeField with upper case for lower",
void change(Descriptor d) {
d.removeField("AA");
}
},
new ChangedCase("removeField with lower case for upper",
void change(Descriptor d) {
d.removeField("c");
}
},
new ChangedCase("setField keeping lower case",
"a", "x", "C", "D", "aa", "bb", "int", 5,
"nul", null) {
void change(Descriptor d) {
}
},
// spec says we should conserve the original case of the field name:
new ChangedCase("setField changing lower case to upper",
"a", "x", "C", "D", "aa", "bb", "int", 5,
"nul", null) {
void change(Descriptor d) {
}
},
new ChangedCase("setField changing upper case to lower",
"a", "b", "C", "x", "aa", "bb", "int", 5,
"nul", null) {
void change(Descriptor d) {
}
},
new ChangedCase("setField adding new field",
"a", "b", "C", "D", "aa", "bb", "int", 5, "xX", "yY",
"nul", null) {
void change(Descriptor d) {
}
},
new ChangedCase("setField changing type of field",
"a", true, "C", "D", "aa", "bb", "int", 5,
"nul", null) {
void change(Descriptor d) {
d.setField("a", true);
}
},
new ChangedCase("setField changing non-null to null",
"nul", null) {
void change(Descriptor d) {
}
},
new ChangedCase("setField changing null to non-null",
"a", "b", "C", "D", "aa", "bb", "int", 5,
"nul", 3.14) {
void change(Descriptor d) {
}
},
// TEST EXCEPTION BEHAVIOUR COMMON BETWEEN MUTABLE AND IMMUTABLE
new IllegalExceptionCase("getFieldValue with null name") {
void provoke(Descriptor d) {
d.getFieldValue(null);
}
},
new IllegalExceptionCase("getFieldValue with empty name") {
void provoke(Descriptor d) {
d.getFieldValue("");
}
},
new IllegalExceptionCase("setField with null name") {
void provoke(Descriptor d) {
}
},
new IllegalExceptionCase("setField with empty name") {
void provoke(Descriptor d) {
}
},
new IllegalExceptionCase("setFields with null fieldNames") {
void provoke(Descriptor d) {
}
},
new IllegalExceptionCase("setFields with null fieldValues") {
void provoke(Descriptor d) {
}
},
new IllegalExceptionCase("setFields with null fieldNames and " +
"fieldValues") {
void provoke(Descriptor d) {
}
},
new IllegalExceptionCase("setFields with more fieldNames than " +
"fieldValues") {
void provoke(Descriptor d) {
}
},
new IllegalExceptionCase("setFields with more fieldValues than " +
"fieldNames") {
void provoke(Descriptor d) {
}
},
new IllegalExceptionCase("setFields with null element of fieldNames") {
void provoke(Descriptor d) {
}
}
);
return array;
}
if (x instanceof Object[])
else
}
}
}
return (d instanceof ImmutableDescriptor);
// good enough for our purposes
}
}