// 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;
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>"});
// because of the 20 sec time travel, cached content expired
assertEquals(Cacheability.CACHEABLE,
content.getDependency().getCacheability());
assertEquals(Freshness.REVALIDATE,
content.getDependency().freshness(null));
assertEquals(Period.ZERO, content.getDependency().getTimeToLive());
// let's revalidate the dependency
final DependencyContextMock dependencyContextMock =
new DependencyContextMock("dependencyContextMock", expectations);
final MockFactory mockFactory = MockFactory.getDefaultInstance();
final URLContent[] contents = new URLContent[1];
dependencyContextMock.fuzzy.setProperty(
url, mockFactory.expectsAny()).does(new MethodAction() {
public Object perform(final MethodActionEvent event)
throws Throwable {
contents[0] = (URLContent) event.getArguments()[1];
return null;
}
});
content.getDependency().revalidate(dependencyContextMock);
// there should be a URLContent object stored in the dependency context
assertEquals("New content is returned", "<p>hello2</p>\n",
toString(contents[0].getInputStream()));
// cached content is fresh again
assertEquals(Cacheability.CACHEABLE,
contents[0].getDependency().getCacheability());
assertEquals(Freshness.FRESH,
contents[0].getDependency().freshness(null));
assertTrue(Comparator.GE.compare(Period.inSeconds(10),
contents[0].getDependency().getTimeToLive()));
// all transactions should have been used up by this time
assertFalse(serverMock.hasTransactions());
// manager only returns the content from the dependency context
dependencyContextMock.expects.removeProperty(url).does(
new MethodAction() {
public Object perform(final MethodActionEvent event)
throws Throwable {
final URLContent content = contents[0];
contents[0] = null;
return content;