Search in sources :

Example 1 with TestForm

use of org.apache.wicket.util.cookies.CookieValuePersisterTestPage.TestForm in project wicket by apache.

the class CookieUtilsTest method test1.

/**
 * @throws Exception
 */
@SuppressWarnings({ "unchecked" })
@Test
public void test1() throws Exception {
    // How does the test work: Make sure you have a page, form and form component properly set
    // up (getRelativePath() etc.). See #before().
    final Page page = tester.getLastRenderedPage();
    // Get the form and form component created
    final TestForm form = (TestForm) page.get("form");
    final TextField<String> textField = (TextField<String>) form.get("input");
    // Right after init, the requests and responses cookie lists must be empty
    assertEquals(0, getRequestCookies().size());
    assertEquals(0, getResponseCookies().size());
    // Create a persister for the test
    final CookieUtils persister = new CookieUtils();
    // See comment in CookieUtils on how removing a Cookies works. As no cookies in the request,
    // no "delete" cookie will be added to the response.
    persister.remove(textField);
    assertEquals(0, getRequestCookies().size());
    assertEquals(0, getResponseCookies().size());
    // Save the input field's value (add it to the response's cookie list)
    persister.save(textField);
    assertTrue(getRequestCookies().isEmpty());
    assertEquals(1, getResponseCookies().size());
    assertEquals("test", (getResponseCookies().get(0)).getValue());
    assertEquals("form.input", (getResponseCookies().get(0)).getName());
    assertEquals(tester.getRequest().getContextPath() + tester.getRequest().getServletPath(), (getResponseCookies().get(0)).getPath());
    // To remove a cookie means to add a cookie with maxAge=0. Provided a cookie with the same
    // name has been provided in the request. Thus, no changes in our test case
    persister.remove(textField);
    assertEquals(0, getRequestCookies().size());
    assertEquals(1, getResponseCookies().size());
    assertEquals("test", (getResponseCookies().get(0)).getValue());
    assertEquals("form.input", (getResponseCookies().get(0)).getName());
    assertEquals(tester.getRequest().getContextPath() + tester.getRequest().getServletPath(), (getResponseCookies().get(0)).getPath());
    // Try to load it. Because there is no Cookie matching the textfield's name the model's
    // value remains unchanged
    persister.load(textField);
    assertEquals("test", textField.getDefaultModelObjectAsString());
    // Simulate loading a textfield. Initialize textfield with a new (default) value, copy the
    // cookie from response to request (simulating a browser), than load the textfield from
    // cookie and voala the textfield's value should change.
    // save means: add it to the response
    // load means: take it from request
    assertEquals("test", textField.getDefaultModelObjectAsString());
    textField.setDefaultModelObject("new text");
    assertEquals("new text", textField.getDefaultModelObjectAsString());
    copyCookieFromResponseToRequest();
    assertEquals(1, getRequestCookies().size());
    assertEquals(1, getResponseCookies().size());
    persister.load(textField);
    assertEquals("test", textField.getDefaultModelObjectAsString());
    assertEquals(1, getRequestCookies().size());
    assertEquals(1, getResponseCookies().size());
    // remove all cookies from mock response. Because I'll find the cookie to be removed in the
    // request, the persister will create a "delete" cookie to remove the cookie on the client
    // and add it to the response. The already existing Cookie from the previous test gets
    // removed from response since it is the same.
    persister.remove(textField);
    assertEquals(1, getRequestCookies().size());
    assertEquals(1, getResponseCookies().size());
    assertEquals("form.input", (getResponseCookies().get(0)).getName());
    assertEquals(0, (getResponseCookies().get(0)).getMaxAge());
}
Also used : TextField(org.apache.wicket.markup.html.form.TextField) Page(org.apache.wicket.Page) TestForm(org.apache.wicket.util.cookies.CookieValuePersisterTestPage.TestForm) Test(org.junit.Test)

Aggregations

Page (org.apache.wicket.Page)1 TextField (org.apache.wicket.markup.html.form.TextField)1 TestForm (org.apache.wicket.util.cookies.CookieValuePersisterTestPage.TestForm)1 Test (org.junit.Test)1