From 62b67ba1acf32e621d7806be5a11c370eff05849 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 23 Apr 2007 12:54:52 +0000 Subject: [PATCH] Changed Cookie#isExpired() to take current system time as an argument instead of obtaining it through System#currentTimeMillis() Suggested by Roland Weber git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@531455 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/http/client/HttpState.java | 6 ++++-- src/java/org/apache/http/cookie/Cookie.java | 8 ++++++-- .../http/impl/cookie/BasicExpiresHandler.java | 8 -------- .../cookie/TestBasicCookieAttribHandlers.java | 20 ------------------- 4 files changed, 10 insertions(+), 32 deletions(-) diff --git a/src/java/org/apache/http/client/HttpState.java b/src/java/org/apache/http/client/HttpState.java index 490345689..dcd5274cb 100644 --- a/src/java/org/apache/http/client/HttpState.java +++ b/src/java/org/apache/http/client/HttpState.java @@ -31,6 +31,7 @@ package org.apache.http.client; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.Iterator; @@ -109,7 +110,7 @@ public class HttpState { break; } } - if (!cookie.isExpired()) { + if (!cookie.isExpired(new Date())) { cookies.add(cookie); } } @@ -155,9 +156,10 @@ public class HttpState { */ public synchronized boolean purgeExpiredCookies() { boolean removed = false; + Date now = new Date(); Iterator it = cookies.iterator(); while (it.hasNext()) { - if (((Cookie) (it.next())).isExpired()) { + if (((Cookie) (it.next())).isExpired(now)) { it.remove(); removed = true; } diff --git a/src/java/org/apache/http/cookie/Cookie.java b/src/java/org/apache/http/cookie/Cookie.java index e701dc6a9..cbdc7e289 100644 --- a/src/java/org/apache/http/cookie/Cookie.java +++ b/src/java/org/apache/http/cookie/Cookie.java @@ -256,12 +256,16 @@ public class Cookie { /** * Returns true if this cookie has expired. + * @param date Current time * * @return true if the cookie has expired. */ - public boolean isExpired() { + public boolean isExpired(final Date date) { + if (date == null) { + throw new IllegalArgumentException("Date may not be null"); + } return (cookieExpiryDate != null - && cookieExpiryDate.getTime() <= System.currentTimeMillis()); + && cookieExpiryDate.getTime() <= date.getTime()); } /** diff --git a/src/java/org/apache/http/impl/cookie/BasicExpiresHandler.java b/src/java/org/apache/http/impl/cookie/BasicExpiresHandler.java index 1a86e6f0e..79d600d8e 100644 --- a/src/java/org/apache/http/impl/cookie/BasicExpiresHandler.java +++ b/src/java/org/apache/http/impl/cookie/BasicExpiresHandler.java @@ -31,7 +31,6 @@ package org.apache.http.impl.cookie; import org.apache.http.cookie.Cookie; -import org.apache.http.cookie.CookieOrigin; import org.apache.http.cookie.MalformedCookieException; import org.apache.http.util.DateParseException; import org.apache.http.util.DateUtils; @@ -64,11 +63,4 @@ public class BasicExpiresHandler extends AbstractCookieAttributeHandler { } } - public boolean match(final Cookie cookie, final CookieOrigin origin) { - if (cookie == null) { - throw new IllegalArgumentException("Cookie may not be null"); - } - return !cookie.isExpired(); - } - } diff --git a/src/test/org/apache/http/impl/cookie/TestBasicCookieAttribHandlers.java b/src/test/org/apache/http/impl/cookie/TestBasicCookieAttribHandlers.java index eeffb5ab5..b541bc018 100644 --- a/src/test/org/apache/http/impl/cookie/TestBasicCookieAttribHandlers.java +++ b/src/test/org/apache/http/impl/cookie/TestBasicCookieAttribHandlers.java @@ -447,20 +447,6 @@ public class TestBasicCookieAttribHandlers extends TestCase { } } - public void testBasicExpiresMatch() throws Exception { - Cookie cookie = new Cookie("name", "value"); - CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123}); - CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false); - - Date past = new Date(System.currentTimeMillis() - 20000000); - Date future = new Date(System.currentTimeMillis() + 20000000); - - cookie.setExpiryDate(past); - assertFalse(h.match(cookie, origin)); - cookie.setExpiryDate(future); - assertTrue(h.match(cookie, origin)); - } - public void testBasicExpiresInvalidInput() throws Exception { try { new BasicExpiresHandler(null); @@ -475,12 +461,6 @@ public class TestBasicCookieAttribHandlers extends TestCase { } catch (IllegalArgumentException ex) { // expected } - try { - h.match(null, null); - fail("IllegalArgumentException must have been thrown"); - } catch (IllegalArgumentException ex) { - // expected - } } }