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
This commit is contained in:
Oleg Kalnichevski 2007-04-23 12:54:52 +00:00
parent 4fc15c9a55
commit 62b67ba1ac
4 changed files with 10 additions and 32 deletions

View File

@ -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;
}

View File

@ -256,12 +256,16 @@ public class Cookie {
/**
* Returns true if this cookie has expired.
* @param date Current time
*
* @return <tt>true</tt> 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());
}
/**

View File

@ -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();
}
}

View File

@ -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
}
}
}