use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class StoredResponsesMap method get.
@Override
public BufferedWebResponse get(Object key) {
BufferedWebResponse result = null;
Value value;
synchronized (this) {
value = (Value) super.get(key);
}
if (value != null) {
Duration elapsedTime = Time.now().subtract(value.creationTime);
if (lifetime.greaterThan(elapsedTime)) {
result = value.response;
} else {
// expired, remove it
remove(key);
}
}
return result;
}
use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class StoredResponsesMap method removeEldestEntry.
@Override
protected synchronized boolean removeEldestEntry(java.util.Map.Entry<String, Object> eldest) {
boolean removed = super.removeEldestEntry(eldest);
if (removed == false) {
Value value = (Value) eldest.getValue();
if (value != null) {
Duration elapsedTime = Time.now().subtract(value.creationTime);
if (lifetime.lessThanOrEqual(elapsedTime)) {
removedValue = value.response;
removed = true;
}
}
}
return removed;
}
use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class ValueMapTest method getAs.
/**
* test other getAs methods
*/
@Test
public void getAs() {
ValueMap vm = new ValueMap();
Boolean booleanValue = true;
Integer integerValue = 42;
Long longValue = integerValue * 1L;
Double doubleValue = integerValue * 1.0D;
Time timeValue = Time.now();
Duration durationValue = Duration.hours(1);
boolean defBoolean = !booleanValue;
int defInteger = 10101;
long defLong = defInteger * 1L;
double defDouble = defInteger * 1.0D;
Time defTime = Time.now();
Duration defDuration = Duration.hours(42);
vm.put("num", integerValue.toString());
vm.put("num.bad", "xxx");
vm.put("time", timeValue.toString());
vm.put("time.bad", "xxx");
vm.put("duration", durationValue.toString());
vm.put("duration.bad", "xxx");
vm.put("boolean", booleanValue.toString());
vm.put("boolean.bad", "xxx");
// boolean
assertEquals(booleanValue, vm.getAsBoolean("boolean"));
assertNull(vm.getAsBoolean("boolean.bad"));
assertEquals(defBoolean, vm.getAsBoolean("boolean.bad", defBoolean));
assertNull(vm.getAsBoolean("boolean.missing"));
assertEquals(defBoolean, vm.getAsBoolean("boolean.missing", defBoolean));
assertEquals(!defBoolean, vm.getAsBoolean("boolean.missing", !defBoolean));
// integer
assertEquals(integerValue, vm.getAsInteger("num"));
assertNull(vm.getAsInteger("num.bad"));
assertEquals(defInteger, vm.getAsInteger("num.bad", defInteger));
assertNull(vm.getAsInteger("num.missing"));
assertEquals(defInteger, vm.getAsInteger("num.missing", defInteger));
// long
assertEquals(longValue, vm.getAsLong("num"));
assertNull(vm.getAsLong("num.bad"));
assertEquals(defLong, vm.getAsLong("num.bad", defLong));
assertNull(vm.getAsLong("num.missing"));
assertEquals(defLong, vm.getAsLong("num.missing", defLong));
// double
assertEquals(doubleValue, vm.getAsDouble("num"));
assertNull(vm.getAsDouble("num.bad"));
assertEquals(defDouble, vm.getAsDouble("num.bad", defDouble), 0.001);
assertNull(vm.getAsDouble("num.missing"));
assertEquals(defDouble, vm.getAsDouble("num.missing", defDouble), 0.001);
// time
// use toSTring since
assertEquals(timeValue.toString(), vm.getAsTime("time").toString());
// equals seems
// broken
assertNull(vm.getAsTime("time.bad"));
assertEquals(defTime, vm.getAsTime("time.bad", defTime));
assertNull(vm.getAsTime("time.missing"));
assertEquals(defTime, vm.getAsTime("time.missing", defTime));
// duration
assertEquals(durationValue, vm.getAsDuration("duration"));
assertNull(vm.getAsDuration("duration.bad"));
assertEquals(defDuration, vm.getAsDuration("duration.bad", defDuration));
assertNull(vm.getAsDuration("duration.missing"));
assertEquals(defDuration, vm.getAsDuration("duration.missing", defDuration));
}
use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class FilenameWithVersionResourceCachingStrategyTest method testDecorateResponse.
@Test
public void testDecorateResponse() throws Exception {
Duration defaultDuration = Duration.minutes(60);
// setup RequestCycle
BaseWicketTester tester = new BaseWicketTester();
RequestCycle requestCycle = ThreadContext.getRequestCycle();
Application.get().getResourceSettings().setDefaultCacheDuration(defaultDuration);
try {
// version match
requestCycle.setMetaData(IResourceCachingStrategy.URL_VERSION, TEST_RESOURCE_VERSION);
AbstractResource.ResourceResponse response = new AbstractResource.ResourceResponse();
strategy.decorateResponse(response, new TestResource());
assertEquals(WebResponse.MAX_CACHE_DURATION, response.getCacheDuration());
assertEquals(WebResponse.CacheScope.PUBLIC, response.getCacheScope());
// version mismatch
requestCycle.setMetaData(IResourceCachingStrategy.URL_VERSION, "foo");
response = new AbstractResource.ResourceResponse();
strategy.decorateResponse(response, new TestResource());
assertEquals(defaultDuration, response.getCacheDuration());
assertEquals(WebResponse.CacheScope.PRIVATE, response.getCacheScope());
} finally {
tester.destroy();
}
}
use of org.apache.wicket.util.time.Duration in project wicket by apache.
the class AjaxTimerBehaviorTest method pageRenderSetsTimeout.
/**
* tests timer behavior in a WebPage.
*/
@Test
public void pageRenderSetsTimeout() {
Duration dur = Duration.seconds(20);
final AjaxSelfUpdatingTimerBehavior timer = new AjaxSelfUpdatingTimerBehavior(dur);
final MockPageWithLinkAndComponent page = new MockPageWithLinkAndComponent();
Label label = new Label(MockPageWithLinkAndComponent.COMPONENT_ID, "Hello");
page.add(label);
page.add(new Link<Void>(MockPageWithLinkAndComponent.LINK_ID) {
private static final long serialVersionUID = 1L;
@Override
public void onClick() {
// do nothing, link is just used to simulate a roundtrip
}
});
label.setOutputMarkupId(true);
label.add(timer);
tester.startPage(page);
assertMatches("Wicket.Timer.set", 1);
tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
assertMatches("Wicket.Timer.set", 1);
tester.executeBehavior(timer);
assertMatches("Wicket.Timer.set", 1);
}
Aggregations