root.serialize(out);
out.close();
}
public void testRegistry() throws Exception {
Registry reg = new SimpleURLRegistry();
Properties props = new Properties();
props.put("root", "file:./");
props.put("cachableDuration", "1500");
reg.init(props);
Entry prop = new Entry();
prop.setType(Entry.REMOTE_ENTRY);
prop.setKey(FILE);
// initial load of file from registry
assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
// sleep 1 sec
Thread.sleep(1000);
assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
// sleep another 1 sec, has expired in cache, but content hasnt changed
Thread.sleep(1000);
assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
// the renewed cache should be valid for another 1.5 secs
// change the file now and change next cache duration
writeToFile(TEXT_2);
props.put("cachableDuration", "100");
reg.init(props);
// still cached content should be available and valid
assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
// now sleep ~1 sec, still cache should be valid
Thread.sleep(800);
assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
// sleep another 1 sec.. cache should expire and new content should be loaded
Thread.sleep(1000);
assertEquals(TEXT_2, reg.getResource(prop, new Properties()).toString());
// change content back to original
writeToFile(TEXT_1);
// sleep for .5 sec, now the new content should be loaded as new expiry time
// is .1 sec
Thread.sleep(500);
assertEquals(TEXT_1, reg.getResource(prop, new Properties()).toString());
}