Java Reference
In-Depth Information
Note We have used specific times for each of these events, providing us with some degree of certainty
that we can duplicate our tests. We cannot be absolutely guaranteed that we can get exactly the same result
every time since minor changes in how long network traffic takes over multiple bookings could have an
effect. But for a small number of bookings on a local area network with low traffic (or on a single machine),
we should be able to predict with confidence what the outcome will be.
public void run() {
int secondsForWatchingDvd = 2;
int secondsForComplaining = 1;
int secondsForBrowsingStore = 2;
try {
for (int i = 0; i < this.numberOfRentals; i++) {
switch (rentDvd(dvdUpc)) {
case RENTAL_SUCCESS:
successfulRentals++;
// watch the DVD
Thread.sleep(secondsForWatchingDvd * 1000);
// then return it so somebody else can rent it
returnDvd(dvdUpc);
break;
case RENTAL_OUT_OF_STOCK:
outOfStock++;
// complain that it is not in stock
Thread.sleep(secondsForComplaining * 1000);
break;
case RENTAL_TIMEOUT:
// just track that we had the problem, and continue
timeouts++;
break;
}
// wander around the DVD store looking at DVDs.
Thread.sleep(secondsForBrowsingStore * 1000);
}
} catch (Exception e) {
// This should never ever go into production code, but for testing
// we are simply catching *every* exception and displaying it
System.err.println("Exception thrown by " + getName());
e.printStackTrace(System.err);
System.err.println();
}
}
The rentDvd method duplicates the business logic required by our application. It reserves
the DVD so that no other client can modify it (assuming, of course, that the other client also
Search WWH ::




Custom Search