* Tests DatabasePermission.
*/
public void testDatabasePermission() throws IOException {
// test DatabasePermission with null url
try {
new DatabasePermission(null, DatabasePermission.CREATE);
fail("expected NullPointerException");
} catch (NullPointerException ex) {
// expected exception
}
// test DatabasePermission with empty url
try {
new DatabasePermission("", DatabasePermission.CREATE);
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test DatabasePermission with illegal url
try {
new DatabasePermission("no_url", DatabasePermission.CREATE);
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test DatabasePermission with unsupported protocol
try {
new DatabasePermission("unknown:test", DatabasePermission.CREATE);
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// this test's commented out because it's platform-dependent
// (no reliable way to make it pass on Unix)
// test DatabasePermission with non-canonicalizable URL
//try {
// //new DatabasePermission("directory:.*/\\:///../",
// // DatabasePermission.CREATE);
// new DatabasePermission("directory:\n/../../../.*/\\:///../",
// DatabasePermission.CREATE);
// fail("expected IOException");
//} catch (IOException ex) {
// // expected exception
//}
// test DatabasePermission with null actions
try {
new DatabasePermission("directory:dir", null);
fail("expected NullPointerException");
} catch (NullPointerException ex) {
// expected exception
}
// test DatabasePermission with empty actions
try {
new DatabasePermission("directory:dir", "");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test DatabasePermission with illegal action list
try {
new DatabasePermission("directory:dir", "illegal_action");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test DatabasePermission with illegal action list
try {
new DatabasePermission("directory:dir", "illegal,action");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test DatabasePermission with illegal action list
try {
new DatabasePermission("directory:dir", "illegal,create,action");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test DatabasePermission on illegal action list
try {
new DatabasePermission("directory:dir", "illegal;action");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test DatabasePermission with illegal action list
try {
new DatabasePermission("directory:dir", ",");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test DatabasePermission with illegal action list
try {
new DatabasePermission("directory:dir", " ");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test DatabasePermission with illegal action list
try {
new DatabasePermission("directory:dir", "create,");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected exception
}
// test DatabasePermission on relative directory paths
final DatabasePermission[] relDirPathPerms
= new DatabasePermission[relDirPaths.length];
for (int i = 0; i < relDirPaths.length; i++) {
relDirPathPerms[i]
= new DatabasePermission(relDirPaths[i],
DatabasePermission.CREATE);
}
checkNameAndActions(relDirPathPerms, relDirPaths);
checkHashCodeAndEquals(relDirPathPerms, relDirPathPerms);
checkImplies(relDirPathPerms, relDirPathPerms, dirPathImpls);
// test DatabasePermission on relative directory path aliases
final DatabasePermission[] relDirPathAliasPerms
= new DatabasePermission[relDirPathAliases.length];
for (int i = 0; i < relDirPathAliases.length; i++) {
relDirPathAliasPerms[i]
= new DatabasePermission(relDirPathAliases[i],
DatabasePermission.CREATE);
}
checkNameAndActions(relDirPathAliasPerms, relDirPathAliases);
checkHashCodeAndEquals(relDirPathPerms, relDirPathAliasPerms);
checkImplies(relDirPathPerms, relDirPathAliasPerms, dirPathImpls);
checkImplies(relDirPathAliasPerms, relDirPathPerms, dirPathImpls);
// test DatabasePermission on absolute directory paths
final DatabasePermission[] absDirPathPerms
= new DatabasePermission[absDirPaths.length];
for (int i = 0; i < absDirPaths.length; i++) {
absDirPathPerms[i]
= new DatabasePermission(absDirPaths[i],
DatabasePermission.CREATE);
}
checkNameAndActions(absDirPathPerms, absDirPaths);
checkHashCodeAndEquals(absDirPathPerms, absDirPathPerms);
checkImplies(absDirPathPerms, absDirPathPerms, dirPathImpls);
// test DatabasePermission on absolute directory path aliases
final DatabasePermission[] absDirPathAliasPerms
= new DatabasePermission[absDirPathAliases.length];
for (int i = 0; i < absDirPathAliases.length; i++) {
absDirPathAliasPerms[i]
= new DatabasePermission(absDirPathAliases[i],
DatabasePermission.CREATE);
}
checkNameAndActions(absDirPathAliasPerms, absDirPathAliases);
checkHashCodeAndEquals(absDirPathPerms, absDirPathAliasPerms);
checkImplies(absDirPathPerms, absDirPathAliasPerms, dirPathImpls);
checkImplies(absDirPathAliasPerms, absDirPathPerms, dirPathImpls);
// test DatabasePermission for the inclusive path specification
final String inclPermissionUrl = "directory:<<ALL FILES>>";
final DatabasePermission[] inclPerms
= { new DatabasePermission(inclPermissionUrl,
DatabasePermission.CREATE) };
checkNameAndActions(inclPerms,
new String[]{ inclPermissionUrl });
final DatabasePermission[] inclPerms1
= { new DatabasePermission(inclPermissionUrl,
DatabasePermission.CREATE) };
checkHashCodeAndEquals(inclPerms, inclPerms1);
checkImplies(inclPerms, inclPerms1, new boolean[][]{ { true } });
final boolean[][] allTrue = new boolean[1][dirPaths.length];
for (int j = 0; j < dirPaths.length; j++) {
allTrue[0][j] = true;
}
final boolean[][] allFalse = new boolean[dirPaths.length][1];
for (int i = 0; i < dirPaths.length; i++) {
allFalse[i][0] = false;
}
checkImplies(inclPerms, relDirPathPerms, allTrue);
checkImplies(relDirPathPerms, inclPerms, allFalse);
checkImplies(inclPerms, relDirPathAliasPerms, allTrue);
checkImplies(relDirPathAliasPerms, inclPerms, allFalse);
checkImplies(inclPerms, absDirPathPerms, allTrue);
checkImplies(absDirPathPerms, inclPerms, allFalse);
checkImplies(inclPerms, absDirPathAliasPerms, allTrue);
checkImplies(absDirPathAliasPerms, inclPerms, allFalse);
// Actions string is washed (lower-cased, trimmed) and duplicates
// are removed.
DatabasePermission perm =
new DatabasePermission("directory:dir", "create, create");
assertEquals("create", perm.getActions());
perm = new DatabasePermission("directory:dir", " CrEaTe ");
assertEquals("create", perm.getActions());
// DERBY-3476: The DatabasePermission class should be final.
assertTrue(Modifier.isFinal(DatabasePermission.class.getModifiers()));
}