int readSecs = 30;
int writeSecs = 45;
// Create a READ lock on Group testKeys[3], owned by testIds[0]:
print("Creating new READ lock");
IEntityLock lock = service.newLock(GROUP_CLASS, testKeys[3], IEntityLockService.READ_LOCK, testIds[0], readSecs);
msg = "Testing if new lock is valid";
valid = service.isValid(lock);
print(msg);
assertTrue(msg, valid);
// Convert the READ lock to a WRITE lock:
print("Converting READ lock to WRITE");
service.convert(lock, IEntityLockService.WRITE_LOCK, writeSecs);
msg = "Testing if converted lock is still valid";
valid = service.isValid(lock);
print(msg);
assertTrue(msg, valid);
// Convert the WRITE lock back to a READ lock:
print("Converting WRITE lock to READ");
service.convert(lock, IEntityLockService.READ_LOCK, readSecs);
msg = "Testing if converted lock is still valid";
valid = service.isValid(lock);
print(msg);
assertTrue(msg, valid);
// Now try to create a WRITE lock on the same entity for a different owner.
IEntityLock duplicateLock = null;
msg = "Attempting to create a duplicate lock; should be null";
print(msg);
try { duplicateLock = service.newLock(GROUP_CLASS, testKeys[3], IEntityLockService.WRITE_LOCK, testIds[1]); }
catch (LockingException le) {print("Caught exception: " + le.getMessage()); }