Search in sources :

Example 6 with Duration

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;
}
Also used : Duration(org.apache.wicket.util.time.Duration)

Example 7 with Duration

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;
}
Also used : Duration(org.apache.wicket.util.time.Duration)

Example 8 with Duration

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));
}
Also used : Time(org.apache.wicket.util.time.Time) Duration(org.apache.wicket.util.time.Duration) Test(org.junit.Test)

Example 9 with Duration

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();
    }
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Duration(org.apache.wicket.util.time.Duration) AbstractResource(org.apache.wicket.request.resource.AbstractResource) BaseWicketTester(org.apache.wicket.util.tester.BaseWicketTester) Test(org.junit.Test)

Example 10 with Duration

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);
}
Also used : MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Label(org.apache.wicket.markup.html.basic.Label) Duration(org.apache.wicket.util.time.Duration) Test(org.junit.Test)

Aggregations

Duration (org.apache.wicket.util.time.Duration)17 Test (org.junit.Test)11 MockPageWithLinkAndComponent (org.apache.wicket.MockPageWithLinkAndComponent)4 Time (org.apache.wicket.util.time.Time)4 Label (org.apache.wicket.markup.html.basic.Label)3 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 AbstractResource (org.apache.wicket.request.resource.AbstractResource)2 BaseWicketTester (org.apache.wicket.util.tester.BaseWicketTester)2 JSONArray (com.github.openjson.JSONArray)1 JSONException (com.github.openjson.JSONException)1 JSONObject (com.github.openjson.JSONObject)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Page (org.apache.wicket.Page)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 Method (org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method)1 IAjaxCallListener (org.apache.wicket.ajax.attributes.IAjaxCallListener)1 ThrottlingSettings (org.apache.wicket.ajax.attributes.ThrottlingSettings)1 JSONFunction (org.apache.wicket.ajax.json.JSONFunction)1