* Tests SystemPermission.
*/
public void testSystemPermission() {
// test SystemPermission with null name argument
try {
new SystemPermission(null, null);
fail("expected NullPointerException");
} catch (NullPointerException ex) {
// expected exception
}
// test SystemPermission with empty name argument
try {
new SystemPermission("", null);
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test SystemPermission with illegal name argument
try {
new SystemPermission("illegal_name", null);
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
String[] validNames = {
SystemPermission.ENGINE,
SystemPermission.JMX,
SystemPermission.SERVER
};
// In order of the canonical actions expected
String[] validActions = {
SystemPermission.CONTROL,
SystemPermission.MONITOR,
SystemPermission.SHUTDOWN,
};
// Check all valid combinations (which is all) with
// a single action
Permission[] all = new Permission[
validNames.length * validActions.length];
int c = 0;
for (int tn = 0; tn < validNames.length; tn++)
{
for (int a = 0; a < validActions.length; a++) {
Permission p = new SystemPermission(
validNames[tn], validActions[a]);
assertEquals(validNames[tn], p.getName());
assertEquals(validActions[a], p.getActions());
// test SystemPermission.equals()
assertFalse(p.equals(null));
assertFalse(p.equals(new Object()));
this.assertEquivalentPermissions(p, p);
all[c++] = p;
}
}
// All the permissions are different.
checkDistinctPermissions(all);
// Check two actions
for (int n = 0; n < validNames.length; n++)
{
for (int a = 0; a < validActions.length; a++)
{
Permission base = new SystemPermission(
validNames[n], validActions[a]);
// Two actions
for (int oa = 0; oa < validActions.length; oa++)
{
Permission p = new SystemPermission(
validNames[n],
validActions[a] + "," + validActions[oa]);
if (oa == a)
{
// Same action added twice
assertEquivalentPermissions(base, p);
// Canonical form should collapse into a single action
assertEquals(validActions[a], p.getActions());
}
else
{
// Implies logic, the one with one permission
// is implied by the other but not vice-versa.
assertTrue(p.implies(base));
assertFalse(base.implies(p));
// Names in canonical form
int f;
int s;
if (oa < a)
{
f = oa;
s = a;
}
else
{
f = a;
s = oa;
}
if (oa < a)
assertEquals(validActions[f] + "," + validActions[s],
p.getActions());
}
}
}
}
}