You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2014/07/21 14:31:14 UTC
git commit: WICKET-5648 CookieUtils - add #loadValues(),
make #getCookie() public, properly initialize from the defaults
Repository: wicket
Updated Branches:
refs/heads/wicket-6.x e50f29810 -> b5e29819b
WICKET-5648 CookieUtils - add #loadValues(), make #getCookie() public, properly initialize from the defaults
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/b5e29819
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/b5e29819
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/b5e29819
Branch: refs/heads/wicket-6.x
Commit: b5e29819b1aa75ef8648c6ebaf0b5ebf72bdb2da
Parents: e50f298
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Mon Jul 21 15:28:26 2014 +0300
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Mon Jul 21 15:28:26 2014 +0300
----------------------------------------------------------------------
.../wicket/util/cookies/CookieDefaults.java | 6 +-
.../apache/wicket/util/cookies/CookieUtils.java | 44 ++++++--
.../wicket/util/cookies/CookieUtilsTest.java | 107 ++++++++++++++++++-
3 files changed, 137 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/b5e29819/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieDefaults.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieDefaults.java b/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieDefaults.java
index a438e1a..1ccc883 100644
--- a/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieDefaults.java
+++ b/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieDefaults.java
@@ -19,12 +19,10 @@ package org.apache.wicket.util.cookies;
import org.apache.wicket.util.io.IClusterable;
/**
- * This class provides default values that are used by the CookieValuePersister class when it
- * creates cookies.
+ * This class provides default values that are used by {@link org.apache.wicket.util.cookies.CookieUtils}
+ * class when it creates cookies.
*
* @author Juergen Donnerstag
- *
- * @TODO rename to CookieDefaultValues in 1.5
*/
public class CookieDefaults implements IClusterable
{
http://git-wip-us.apache.org/repos/asf/wicket/blob/b5e29819/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieUtils.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieUtils.java b/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieUtils.java
index ad4b72f..af2d26e 100644
--- a/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieUtils.java
+++ b/wicket-core/src/main/java/org/apache/wicket/util/cookies/CookieUtils.java
@@ -53,6 +53,7 @@ public class CookieUtils
* Construct.
*
* @param settings
+ * the default settings for the saved cookies
*/
public CookieUtils(final CookieDefaults settings)
{
@@ -71,6 +72,7 @@ public class CookieUtils
* Remove the cookie identified by the key
*
* @param key
+ * The cookie name
*/
public final void remove(final String key)
{
@@ -105,8 +107,9 @@ public class CookieUtils
/**
* Retrieve the cookie value by means of its key.
- *
+ *
* @param key
+ * The cookie name
* @return The cookie value associated with the key
*/
public final String load(final String key)
@@ -120,6 +123,25 @@ public class CookieUtils
}
/**
+ * Retrieve an array of the cookie value split by {@link org.apache.wicket.markup.html.form.FormComponent#VALUE_SEPARATOR}.
+ *
+ * @param key
+ * The cookie name
+ * @return The cookie value split by {@link org.apache.wicket.markup.html.form.FormComponent#VALUE_SEPARATOR}.
+ */
+ public final String[] loadValues(final String key)
+ {
+ final Cookie cookie = getCookie(key);
+ if (cookie != null)
+ {
+ String value = cookie.getValue();
+ String[] values = splitValue(value);
+ return values;
+ }
+ return null;
+ }
+
+ /**
* Retrieve the cookie value associated with the formComponent and load the model object with
* the cookie value.
*
@@ -145,14 +167,14 @@ public class CookieUtils
*/
protected String[] splitValue(final String value)
{
- return value.split(FormComponent.VALUE_SEPARATOR);
+ return Strings.split(value, FormComponent.VALUE_SEPARATOR.charAt(0));
}
/**
* Join all fragments into one Cookie value
*
* @param values
- * @return The cookie's value splitted into its constituent parts
+ * @return The cookie's value split into its constituent parts
*/
protected String joinValues(final String... values)
{
@@ -163,7 +185,9 @@ public class CookieUtils
* Create a Cookie with key and value and save it in the browser with the next response
*
* @param key
+ * The cookie name
* @param values
+ * The cookie values
*/
public final void save(String key, final String... values)
{
@@ -178,9 +202,6 @@ public class CookieUtils
{
cookie.setValue(value);
}
- cookie.setSecure(false);
- cookie.setMaxAge(settings.getMaxAge());
-
save(cookie);
}
@@ -210,8 +231,8 @@ public class CookieUtils
// cookie names cannot contain ':',
// we replace ':' with '.' but first we have to encode '.' as '..'
- key = key.replace(".", "..");
- key = key.replace(":", ".");
+ key = Strings.replaceAll(key, ".", "..").toString();
+ key = key.replace(':', '.');
return key;
}
@@ -226,12 +247,12 @@ public class CookieUtils
{
if (cookie != null)
{
+ save(cookie);
+
// Delete the cookie by setting its maximum age to zero
cookie.setMaxAge(0);
cookie.setValue(null);
- save(cookie);
-
if (log.isDebugEnabled())
{
log.debug("Removed Cookie: " + cookie.getName());
@@ -247,7 +268,7 @@ public class CookieUtils
*
* @return Any cookies for this request
*/
- private Cookie getCookie(final String name)
+ public Cookie getCookie(final String name)
{
String key = getSaveKey(name);
@@ -332,6 +353,7 @@ public class CookieUtils
cookie.setPath(path);
cookie.setVersion(settings.getVersion());
cookie.setSecure(settings.getSecure());
+ cookie.setMaxAge(settings.getMaxAge());
}
/**
http://git-wip-us.apache.org/repos/asf/wicket/blob/b5e29819/wicket-core/src/test/java/org/apache/wicket/util/cookies/CookieUtilsTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/util/cookies/CookieUtilsTest.java b/wicket-core/src/test/java/org/apache/wicket/util/cookies/CookieUtilsTest.java
index f00eacf..2068cf4 100644
--- a/wicket-core/src/test/java/org/apache/wicket/util/cookies/CookieUtilsTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/util/cookies/CookieUtilsTest.java
@@ -16,6 +16,13 @@
*/
package org.apache.wicket.util.cookies;
+import static org.hamcrest.Matchers.arrayContaining;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.emptyArray;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.equalToIgnoringCase;
+import static org.hamcrest.Matchers.is;
+
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -25,8 +32,11 @@ import javax.servlet.http.Cookie;
import org.apache.wicket.Page;
import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.util.cookies.CookieValuePersisterTestPage.TestForm;
+import org.apache.wicket.util.string.Strings;
+import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
@@ -36,11 +46,8 @@ import org.junit.Test;
*/
public class CookieUtilsTest extends WicketTestCase
{
- /**
- * @throws Exception
- */
@Before
- public void before() throws Exception
+ public void before()
{
tester.startPage(CookieValuePersisterTestPage.class);
}
@@ -54,7 +61,7 @@ public class CookieUtilsTest extends WicketTestCase
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 setUp().
+ // up (getRelativePath() etc.). See #before().
final Page page = tester.getLastRenderedPage();
// Get the form and form component created
@@ -126,6 +133,96 @@ public class CookieUtilsTest extends WicketTestCase
assertEquals(0, (getResponseCookies().get(0)).getMaxAge());
}
+ @Test
+ public void splitValuesNullString()
+ {
+ CookieUtils utils = new CookieUtils();
+ String[] values = utils.splitValue(null);
+ assertArrayEquals(new String[0], values);
+ }
+
+ @Test
+ public void splitValuesEmptyString()
+ {
+ CookieUtils utils = new CookieUtils();
+ String[] values = utils.splitValue("");
+ assertThat(values, is(emptyArray()));
+ }
+
+ @Test
+ public void splitValuesSingleValue()
+ {
+ CookieUtils utils = new CookieUtils();
+ String value1 = "value one";
+ String[] values = utils.splitValue(value1);
+ assertThat(values, is(arrayContaining(value1)));
+ }
+
+ @Test
+ public void splitValuesManyValues()
+ {
+ CookieUtils utils = new CookieUtils();
+ String value1 = "value one";
+ String value2 = "value two";
+ String value = value1 + FormComponent.VALUE_SEPARATOR + value2;
+ String[] values = utils.splitValue(value);
+ assertThat(values, is(arrayContaining(value1, value2)));
+ }
+
+ @Test
+ public void joinValues()
+ {
+ CookieUtils utils = new CookieUtils();
+ String value1 = "value one";
+ String value2 = "value two";
+ String joined = utils.joinValues(value1, value2);
+ assertThat(joined, is(equalTo(value1 + FormComponent.VALUE_SEPARATOR + value2)));
+ }
+
+ @Test
+ public void saveLoadValue()
+ {
+ CookieUtils utils = new CookieUtils();
+ String value1 = "value one";
+ String key = "key";
+ utils.save(key, value1);
+ before(); // execute a request cycle, so the response cookie is send with the next request
+ String result = utils.load(key);
+ assertThat(result, is(equalTo(value1)));
+ }
+
+ @Test
+ public void saveLoadValues()
+ {
+ CookieUtils utils = new CookieUtils();
+ String value1 = "value one";
+ String value2 = "value two";
+ String value3 = "value three";
+ String key = "key";
+ utils.save(key, value1, value2, value3);
+ before(); // execute a request cycle, so the response cookie is send with the next request
+ String[] result = utils.loadValues(key);
+ assertThat(result, is(Matchers.arrayContaining(value1, value2, value3)));
+ }
+
+ @Test
+ public void defaults()
+ {
+ CookieDefaults defaults = new CookieDefaults();
+ defaults.setComment("A comment");
+ defaults.setDomain("A domain");
+ defaults.setMaxAge(123);
+ defaults.setSecure(true);
+ defaults.setVersion(456);
+ CookieUtils utils = new CookieUtils(defaults);
+ String value1 = "value one";
+ String key = "key";
+ utils.save(key, value1);
+ before(); // execute a request cycle, so the response cookie is send with the next request
+ Cookie result = utils.getCookie(key);
+ assertThat(result.getComment(), is(equalTo(defaults.getComment())));
+ }
+
private void copyCookieFromResponseToRequest()
{
tester.getRequest().addCookie(getResponseCookies().iterator().next());