Added Cookie copy constructor and test.
git-svn-id: https://svn.apache.org/repos/asf/mina/asyncweb/trunk@633705 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/common/src/main/java/org/apache/asyncweb/common/DefaultCookie.java b/common/src/main/java/org/apache/asyncweb/common/DefaultCookie.java
index 6bb45b2..3b4dc8c 100644
--- a/common/src/main/java/org/apache/asyncweb/common/DefaultCookie.java
+++ b/common/src/main/java/org/apache/asyncweb/common/DefaultCookie.java
@@ -69,15 +69,27 @@
* @param value the default value of the new cookie
*/
public DefaultCookie(String name, String value) {
- if (name == null) {
- throw new IllegalArgumentException("name can NOT be null");
- }
- if (value == null) {
- throw new IllegalArgumentException("name can NOT be null");
- }
- this.name = name;
+ this(name);
this.value = value;
}
+
+ /**
+ * Creates a new cookie based on the values of the specified <tt>Cookie</tt>.
+ *
+ * @param cookie the <tt>Cookie</tt> object from which the new
+ * instance of <tt>DefaultCookie</tt> will copy its values
+ */
+ public DefaultCookie(Cookie cookie) {
+ this(cookie.getName());
+ this.comment = cookie.getComment();
+ this.createdDate = cookie.getCreatedDate();
+ this.domain = cookie.getDomain();
+ this.maxAge = cookie.getMaxAge();
+ this.path = cookie.getPath();
+ this.secure = cookie.isSecure();
+ this.value = cookie.getValue();
+ this.version = cookie.getVersion();
+ }
public String getComment() {
return comment;
diff --git a/common/src/test/java/org/apache/asyncweb/common/DefaultCookieTest.java b/common/src/test/java/org/apache/asyncweb/common/DefaultCookieTest.java
new file mode 100644
index 0000000..1bb3a2e
--- /dev/null
+++ b/common/src/test/java/org/apache/asyncweb/common/DefaultCookieTest.java
@@ -0,0 +1,72 @@
+package org.apache.asyncweb.common;
+
+import junit.framework.TestCase;
+
+public class DefaultCookieTest extends TestCase {
+
+ public void testCopyConstructor() throws Exception {
+ // Cookie values
+ final String name = "cookieName";
+ final String value = "The cookie value goes here";
+ final int version = -489243132;
+ final String domain = "mina.apache.org";
+ final String path = "/cookie/path";
+ final boolean secure = true;
+ final int maxAge = 324987;
+ final String comment = "This is the cookie comment";
+ final long createdDate = 437874235475L;
+
+ // Create an instance of Cookie
+ Cookie cookie = new Cookie() {
+ public int getVersion() {
+ return version;
+ }
+ public String getName() {
+ return name;
+ }
+ public String getValue() {
+ return value;
+ }
+ public String getDomain() {
+ return domain;
+ }
+ public String getPath() {
+ return path;
+ }
+ public boolean isSecure() {
+ return secure;
+ }
+ public int getMaxAge() {
+ return maxAge;
+ }
+ public String getComment() {
+ return comment;
+ }
+ public long getCreatedDate() {
+ return createdDate;
+ }
+ public long getExpirationDate() {
+ // This should be a calculated field so we'll just return 0
+ return 0;
+ }
+ public int compareTo(Cookie o) {
+ return -1;
+ }
+ };
+
+ // Invoke copy constructor
+ Cookie copy = new DefaultCookie(cookie);
+
+ // Ensure all fields were copied successfully
+ assertEquals(name, copy.getName());
+ assertEquals(value, copy.getValue());
+ assertEquals(version, copy.getVersion());
+ assertEquals(domain, copy.getDomain());
+ assertEquals(path, copy.getPath());
+ assertEquals(secure, copy.isSecure());
+ assertEquals(maxAge, copy.getMaxAge());
+ assertEquals(comment, copy.getComment());
+ assertEquals(createdDate, copy.getCreatedDate());
+ }
+
+}