// Announce where we are in the test
logger.log(Level.FINE, "ExpirationListenerTest: In run() method.");
// get the service for test
LeaseRenewalService lrs = getLRS();
// create a lease renewal set
LeaseRenewalSet set = lrs.createLeaseRenewalSet(renewSetDur);
set = prepareSet(set);
// create a handback object
MarshalledObject handback = new MarshalledObject(new Integer(99));
// register listener to receive events
EventRegistration evReg =
set.setExpirationWarningListener(rrl, minWarning, handback);
evReg = prepareRegistration(evReg);
if (rstUtil.isValidExpWarnEventReg(evReg, set) == false) {
String message = "Event Registration is invalid.";
message += rstUtil.getFailureReason();
throw new TestException(message);
}
// allow the set's lease to expire
rstUtil.waitForRemoteEvents(rrl, 1, renewSetDur);
/* Assert that the notification was received at approximately
the lease expiration time - minWarning */
Long[] arrivalTimes = rrl.getArrivalTimes();
// must at lease have received one event
if (arrivalTimes.length == 0) {
String message = "ExpirationWarning event never received.";
throw new TestException(message);
}
// must NOT have received more than one event
if (arrivalTimes.length > 1) {
String message = "Too many events received.";
throw new TestException(message);
}
/* TESTING ASSERTION #1
was the event received around the right time? */
String prop = "com.sun.jini.test.spec.renewalservice." +
"slop_percent_tolerance";
int tolerance =
getConfig().getIntConfigVal(prop, SLOP_PERCENT_TOLERANCE);
logger.log(Level.FINE, "Allowing a slop tolerance of (+/-)" +
tolerance + "% of expected warning time.");
long leaseExpiration = prepareLease(set.getRenewalSetLease()).getExpiration();
long idealArrival = leaseExpiration - minWarning;
long arrivalSlop = minWarning * tolerance / 100;
long minAllowed = idealArrival - arrivalSlop;
long maxAllowed = idealArrival + arrivalSlop;
long actualArrivalTime = arrivalTimes[0].longValue();
if (actualArrivalTime > minAllowed &&
actualArrivalTime < maxAllowed) {
// we say event was received around the right time
logger.log(Level.FINE, "Assertion #1 passes!");
} else {
/* There was a lag. This could be network problem or an
overloaded cpu but we will just have to assume that the
specification was not met. */
String message = "Assertion #1 failed ...\n";
message += "Event was not received +/- " + arrivalSlop;
message += " milliseconds.";
throw new TestException(message);
}
/* TESTING ASSERTION #2
the handback object is the one we expect. */
RemoteEvent[] events = rrl.getEvents();
MarshalledObject mObj = events[0].getRegistrationObject();
if (handback.equals(mObj) == false) {
String message = "Assertion #2 failed ...\n";
message += "Handback object does not match original.";
throw new TestException(message);
}
logger.log(Level.FINE, "Assertion #2 passes!");
/* TESTING ASSERTION #3
a null listener results in a NullPointException. */
set = lrs.createLeaseRenewalSet(renewSetDur);
set = prepareSet(set);
logger.log(Level.FINE, "Created Set with lease duration of " +
renewSetDur + " milliseconds");
try {
evReg = set.setExpirationWarningListener(null, minWarning,
handback);
evReg = prepareRegistration(evReg);
if (rstUtil.isValidExpWarnEventReg(evReg, set) == false) {
String message = "Assertion #3 failed ...\n";
message += "Event Registration is invalid.";
throw new TestException(message);
}
String message = "Assertion #3 failed ...\n";
message += "Registration of null listener was allowed.";
throw new TestException(message);
} catch (NullPointerException ex) {
// success, continue with the rest of the test
logger.log(Level.FINE, "Assertion #3 passes!");
}
/* TESTING ASSERTION #4
a negative value for minWarning results in an
IllegalArgumentException */
try {
evReg = set.setExpirationWarningListener(rrl, -10, handback);
evReg = prepareRegistration(evReg);
if (rstUtil.isValidExpWarnEventReg(evReg, set) == false) {
String message = "Assertion #4 failed ...\n";
message += "Event Registration is invalid.";
throw new TestException(message);
}
String message = "Assertion #4 failed ...\n";
message += "A negative minWarning value was allowed.";
throw new TestException(message);
} catch (IllegalArgumentException ex) {
// success, continue with the rest of the test
}
// edge case (try 0 to ensure it is allowed)
try {
evReg = set.setExpirationWarningListener(rrl, 0, handback);
evReg = prepareRegistration(evReg);
if (rstUtil.isValidExpWarnEventReg(evReg, set) == false) {
String message = "Assertion #4 failed ...\n";
message += "Event Registration is invalid.";
throw new TestException(message);
}
// success, continue with the rest of the test
} catch (IllegalArgumentException ex) {
String message = "Assertion #4 failed ...\n";
message += "A minWarning value of 0 was not allowed.";
logger.log(Level.SEVERE, message, ex);
throw new TestException(message, ex);
}
logger.log(Level.FINE, "Assertion #4 passes!");
/* TESTING ASSERTION #5
a minWarning value greater than the duration of the set's
lease causes an immediate delivery of the
ExpirationWarningEvent */
// forget about all previous events
rrl.clear();
events = rrl.getEvents();
if (events.length > 0) {
String message = "Assertion #5 failed ...\n";
message += "An expected error. RemoteListener did";
message += " not reset properly.";
throw new TestException(message);
}
// create a renewal set
set = lrs.createLeaseRenewalSet(renewSetDur);
set = prepareSet(set);
logger.log(Level.FINE, "Created Set with lease duration of " +
renewSetDur + " milliseconds");
// register with minWarning after lease expiration