"<p>hello</p>"});
// create a system clock mock
final long[] timeShift = new long[1];
timeShift[0] = 0;
final SystemClockMock clock = new SystemClockMock("clock", expectations);
clock.expects.getCurrentTime().does(new MethodAction() {
public Object perform(MethodActionEvent event) throws Throwable {
return Time.inMilliSeconds(
System.currentTimeMillis() + timeShift[0]);
}
}).any();
// create a manager using the system clock mock
final URLContentManager manager = createURLContentManager(
clock, Period.INDEFINITELY, true, 1000);
final URL url = serverMock.getURL("/blah.txt?foo");
URLContent content = manager.getURLContent(url, null, null);
assertEquals("Content was received", "<p>hello</p>\n",
toString(content.getInputStream()));
assertEquals(Cacheability.CACHEABLE,
content.getDependency().getCacheability());
assertEquals(Freshness.FRESH,
content.getDependency().freshness(null));
assertTrue(Comparator.GE.compare(Period.inSeconds(10),
content.getDependency().getTimeToLive()));
assertTrue(Comparator.LT.compare(Period.inSeconds(5),
content.getDependency().getTimeToLive()));
// get the same resource when the cached entry become stale
timeShift[0] = 20000; // jump into the future
serverMock.addTransaction(new String[]{
"GET /blah.txt?foo HTTP/1.1",
"If-None-Match: \"aa\"",
UA_STRING,
"Host: " + serverMock.getServerAddress()},
new String[]{
"HTTP/1.0 200 OK",
"Date: " +
RFC1123.format(new Date(clock.getCurrentTime().inMillis())),
"Content-Type: text/plain",
"Cache-Control: max-age=10",
"ETag: \"aa2\"",
"",
"<p>hello2</p>"});