harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r546302 [1/2] - in /harmony/enhanced/classlib/branches/java6/modules/luni/src: main/java/java/net/ test/java/org/apache/harmony/luni/tests/java/net/
Date Mon, 11 Jun 2007 22:07:07 GMT
Author: tellison
Date: Mon Jun 11 15:07:06 2007
New Revision: 546302

URL: http://svn.apache.org/viewvc?view=rev&rev=546302
Log:
Apply patches for the following:
HARMONY-4097 ([classlib][luni][java6] New class java.net.HttpCookieTest for java6)
HARMONY-4098 ([classlib][luni][java6] New Interface java.net.CookiePolicy for java6)
HARMONY-4101 ([classlib][luni][java6] New Implementation of Interface java.net.CookieStore for java6)
HARMONY-4102 ([classlib][luni][java6] New Class java.net.CookieManager for java6)
HARMONY-4103 ([classlib][luni][java6] New unit test for java.net.CookieStore for java6)


Added:
    harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieManager.java   (with props)
    harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookiePolicy.java   (with props)
    harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieStoreImpl.java   (with props)
    harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/HttpCookie.java   (with props)
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieManagerTest.java   (with props)
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookiePolicyTest.java   (with props)
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieStoreTest.java   (with props)
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/HttpCookieTest.java   (with props)

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieManager.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieManager.java?view=auto&rev=546302
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieManager.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieManager.java Mon Jun 11 15:07:06 2007
@@ -0,0 +1,227 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.net;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Collections;
+
+import org.apache.harmony.luni.util.Msg;
+
+/**
+ * This class provides a concrete implementation of CookieHandler. It separates
+ * the storage of cookies from the policy which decides to accept or deny
+ * cookies. The constructor can have two arguments: a CookieStore and a
+ * CookiePolicy. The former is in charge of cookie storage and the latter makes
+ * decision on acceptance/rejection.
+ * 
+ * CookieHandler is in the center of cookie management. User can make use of
+ * CookieHandler.setDefault to set a CookieManager as the default one used.
+ * 
+ * CookieManager.put uses CookiePolicy.shouldAccept to decide whether to put
+ * some cookies into a cookie store. Three built-in CookiePolicy is defined:
+ * ACCEPT_ALL, ACCEPT_NONE and ACCEPT_ORIGINAL_SERVER. Users can also customize
+ * the policy by implementing CookiePolicy. Any accepted HTTP cookie is stored
+ * in CookieStore and users can also have their own implementation. Up to now,
+ * Only add(URI, HttpCookie) and get(URI) are used by CookieManager. Other
+ * methods in this class may probably be used in a more complicated
+ * implementation.
+ * 
+ * There are many ways to customize user's own HTTP cookie management:
+ * 
+ * First, call CookieHandler.setDefault to set a new CookieHandler
+ * implementation. Second, call CookieHandler.getDefault to use CookieManager.
+ * The CookiePolicy and CookieStore used are customized. Third, use the
+ * customized CookiePolicy and the CookieStore.
+ * 
+ * This implementation conforms to RFC 2965, section 3.3.
+ * 
+ * @since 1.6
+ */
+public class CookieManager extends CookieHandler {
+    private CookieStore store;
+
+    private CookiePolicy policy;
+
+    private static final String VERSION_ZERO_HEADER = "Set-cookie"; //$NON-NLS-1$
+
+    private static final String VERSION_ONE_HEADER = "Set-cookie2"; //$NON-NLS-1$
+
+    /**
+     * Constructs a new cookie manager.
+     * 
+     * The invocation of this constructor is the same as the invocation of
+     * CookieManager(null, null).
+     * 
+     */
+    public CookieManager() {
+        this(null, null);
+    }
+
+    /**
+     * Constructs a new cookie manager using a specified cookie store and a
+     * cookie policy.
+     * 
+     * @param store
+     *            a CookieStore to be used by cookie manager. The manager will
+     *            use a default one if the arg is null.
+     * @param cookiePolicy
+     *            a CookiePolicy to be used by cookie manager
+     *            ACCEPT_ORIGINAL_SERVER will be used if the arg is null.
+     */
+    public CookieManager(CookieStore s, CookiePolicy cookiePolicy) {
+        store = s == null ? new CookieStoreImpl() : s;
+        policy = cookiePolicy == null ? CookiePolicy.ACCEPT_ORIGINAL_SERVER
+                : cookiePolicy;
+    }
+
+    /**
+     * Searchs and gets all cookies in the cache by the specified uri in the
+     * request header.
+     * 
+     * @param uri
+     *            the specified uri to search for
+     * @param requestHeaders
+     *            a list of request headers
+     * @return a map that record all such cookies, the map is unchangeable
+     * @throws IOException
+     *             if some error of I/O operation occurs
+     */
+    @Override
+    public Map<String, List<String>> get(URI uri,
+            Map<String, List<String>> requestHeaders) throws IOException {
+        if (uri == null || requestHeaders == null) {
+            throw new IllegalArgumentException(Msg.getString("KB004")); //$NON-NLS-1$
+        }
+        List<HttpCookie> cookies = store.get(uri);
+        for (int i = 0; i < cookies.size(); i++) {
+            HttpCookie cookie = cookies.get(i);
+            String uriPath = uri.getPath();
+            String cookiePath = cookie.getPath();
+            // if the uri's path does not path-match cookie's path, remove
+            // cookies from the list
+            if (cookiePath == null || uriPath.length() == 0
+                    || !uriPath.startsWith(cookiePath)) {
+                cookies.remove(i);
+            }
+        }
+        // TODO parse cookies into Map and so far requesterHeaders are not used
+        return getCookieMap(cookies, requestHeaders);
+    }
+
+    private static Map<String, List<String>> getCookieMap(
+            List<HttpCookie> cookies, Map<String, List<String>> requestHeaders) {
+        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
+        ArrayList<String> cookieStr = new ArrayList<String>();
+        // If all cookies are version 1, add a "$Version="1"" header
+        boolean versionOne = true;
+        for (HttpCookie cookie : cookies) {
+            if (cookie.getVersion() == 0) {
+                versionOne = false;
+                break;
+            }
+        }
+        if (versionOne && !cookies.isEmpty()) {
+            cookieStr.add("$Version=\"1\""); //$NON-NLS-1$
+        }
+        // add every cookie's string representation into map
+        for (HttpCookie cookie : cookies) {
+            cookieStr.add(cookie.toString());
+        }
+        // TODO So far only "Cookie" head detected
+        map.put("Cookie", cookieStr); //$NON-NLS-1$
+        return Collections.unmodifiableMap(map);
+    }
+
+    /**
+     * Sets cookies according to uri and responseHeaders
+     * 
+     * @param uri
+     *            the specified uri
+     * @param responseHeaders
+     *            a list of request headers
+     * @throws IOException
+     *             if some error of I/O operation occurs
+     */
+    @Override
+    public void put(URI uri, Map<String, List<String>> responseHeaders)
+            throws IOException {
+        if (uri == null || responseHeaders == null) {
+            throw new IllegalArgumentException(Msg.getString("KA019")); //$NON-NLS-1$
+        }
+        // parse and construct cookies according to the map
+        List<HttpCookie> cookies = parseCookie(responseHeaders);
+        for (HttpCookie cookie : cookies) {
+            // if the cookie conforms to the policy and matches the uri's path,
+            // add it into the store
+            if (policy.shouldAccept(uri, cookie)) {
+                store.add(uri, cookie);
+            }
+        }
+    }
+
+    private static List<HttpCookie> parseCookie(
+            Map<String, List<String>> responseHeaders) {
+        List<HttpCookie> cookies = new ArrayList<HttpCookie>();
+        for (Map.Entry<String, List<String>> entry : responseHeaders.entrySet()) {
+            String key = entry.getKey();
+            // Only "Set-cookie" and "Set-cookie2" pair will be parsed
+            if (key != null
+                    && (key.equalsIgnoreCase(VERSION_ZERO_HEADER) || key
+                            .equalsIgnoreCase(VERSION_ONE_HEADER))) {
+                // parse list elements one by one
+                for (String cookieStr : entry.getValue()) {
+                    try {
+                        for (HttpCookie cookie : HttpCookie.parse(cookieStr)) {
+                            cookies.add(cookie);
+                        }
+                    } catch (IllegalArgumentException e) {
+                        // this string is invalid, jump to the next one.
+                    }
+                }
+            }
+        }
+        return cookies;
+    }
+
+    /**
+     * Sets the cookie policy of this cookie manager.
+     * 
+     * ACCEPT_ORIGINAL_SERVER is the default policy for CookieManager.
+     * 
+     * @param cookiePolicy
+     *            the cookie policy. if null, the original policy will not be
+     *            changed.
+     */
+    public void setCookiePolicy(CookiePolicy cookiePolicy) {
+        if (cookiePolicy != null) {
+            policy = cookiePolicy;
+        }
+    }
+
+    /**
+     * Gets current cookie store.
+     * 
+     * @return the cookie store currently used by cookie manager.
+     */
+    public CookieStore getCookieStore() {
+        return store;
+    }
+}

Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookiePolicy.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookiePolicy.java?view=auto&rev=546302
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookiePolicy.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookiePolicy.java Mon Jun 11 15:07:06 2007
@@ -0,0 +1,68 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.net;
+
+/**
+ * CookiePolicy has three pre-defined policy. They are ACCEPT_ALL, ACCEPT_NONE
+ * and ACCEPT_ORIGINAL_SERVER respectively. They are used to decide which
+ * cookies should be accepted and which should not be.
+ * 
+ * See RFC 2965 sec. 3.3 & 7 for more detail.
+ * 
+ * @since 1.6
+ */
+public interface CookiePolicy {
+
+    /**
+     * A pre-defined policy, accepts all cookies.
+     */
+    static final CookiePolicy ACCEPT_ALL = new CookiePolicy() {
+        public boolean shouldAccept(URI uri, HttpCookie cookie) {
+            return true;
+        }
+    };
+
+    /**
+     * A pre-defined policy, accepts no cookies at all.
+     */
+    static final CookiePolicy ACCEPT_NONE = new CookiePolicy() {
+        public boolean shouldAccept(URI uri, HttpCookie cookie) {
+            return false;
+        }
+    };
+
+    /**
+     * A pre-defined policy, only accepts cookies from original server.
+     */
+    static final CookiePolicy ACCEPT_ORIGINAL_SERVER = new CookiePolicy() {
+        public boolean shouldAccept(URI uri, HttpCookie cookie) {
+            return HttpCookie.domainMatches(cookie.getDomain(), uri.getHost());
+        }
+    };
+
+    /**
+     * This method is used to determine whether or not the specified cookie
+     * should be accepted.
+     * 
+     * @param uri
+     *            the URI to used to determine acceptability
+     * @param cookie
+     *            the HttpCookie to be determined
+     * @return true if this cookie should be accepted; false otherwise
+     */
+    boolean shouldAccept(URI uri, HttpCookie cookie);
+}

Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookiePolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieStoreImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieStoreImpl.java?view=auto&rev=546302
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieStoreImpl.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieStoreImpl.java Mon Jun 11 15:07:06 2007
@@ -0,0 +1,131 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.net;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.harmony.luni.util.Msg;
+
+/**
+ * Provides an in-memory implementation of CookieStore as the default value of
+ * HttpCookie.getCookieStore().
+ */
+class CookieStoreImpl implements CookieStore {
+    private Map<URI, ArrayList<HttpCookie>> storeMap = new HashMap<URI, ArrayList<HttpCookie>>();
+
+    public void add(URI uri, HttpCookie cookie) {
+        if (uri == null || cookie == null) {
+            throw new NullPointerException();
+        }
+        ArrayList<HttpCookie> cookies;
+        if (storeMap.containsKey(uri)) {
+            cookies = storeMap.get(uri);
+            cookies.remove(cookie);
+            cookies.add(cookie);
+        } else {
+            cookies = new ArrayList<HttpCookie>();
+            cookies.add(cookie);
+            storeMap.put(uri, cookies);
+        }
+    }
+
+    public List<HttpCookie> get(URI uri) {
+        if (uri == null) {
+            throw new NullPointerException(Msg.getString("KA019")); //$NON-NLS-1$
+        }
+        // get cookies associated with given URI. If none, returns an empty list
+        List<HttpCookie> cookies = storeMap.get(uri);
+        if (cookies == null) {
+            cookies = new ArrayList<HttpCookie>();
+        } else {
+            // eliminate expired cookies
+            for (HttpCookie cookie : cookies) {
+                if (cookie.hasExpired()) {
+                    cookies.remove(cookie);
+                }
+            }
+        }
+
+        // get cookies whose domain matches the given URI
+        Set<URI> uris = storeMap.keySet();
+        for (URI u : uris) {
+            // exclude the given URI
+            if (!u.equals(uri)) {
+                List<HttpCookie> listCookie = storeMap.get(u);
+                for (HttpCookie cookie : listCookie) {
+                    if (HttpCookie.domainMatches(cookie.getDomain(), uri
+                            .getHost())) {
+                        if (cookie.hasExpired()) {
+                            listCookie.remove(cookie);
+                        } else if (!(cookie.hasExpired() || cookies
+                                .contains(cookie))) {
+                            cookies.add(cookie);
+                        }
+                    }
+                }
+            }
+        }
+
+        return cookies;
+    }
+
+    public List<HttpCookie> getCookies() {
+        List<HttpCookie> cookies = new ArrayList<HttpCookie>();
+        Collection<ArrayList<HttpCookie>> values = storeMap.values();
+        for (ArrayList<HttpCookie> list : values) {
+            for (HttpCookie cookie : list) {
+                if (cookie.hasExpired()) {
+                    list.remove(cookie); // eliminate expired cookies
+                } else if (!cookies.contains(cookie)) {
+                    cookies.add(cookie);
+                }
+            }
+        }
+        return Collections.unmodifiableList(cookies);
+    }
+
+    public List<URI> getURIs() {
+        return new ArrayList<URI>(storeMap.keySet());
+    }
+
+    public boolean remove(URI uri, HttpCookie cookie) {
+        if (cookie == null) {
+            throw new NullPointerException(Msg.getString("KA020")); //$NON-NLS-1$
+        }
+        boolean success = false;
+        Collection<ArrayList<HttpCookie>> values = storeMap.values();
+        for (ArrayList<HttpCookie> list : values) {
+            if (list.remove(cookie)) {
+                success = true;
+            }
+        }
+        return success;
+    }
+
+    public boolean removeAll() {
+        if (!storeMap.isEmpty()) {
+            storeMap.clear();
+        }
+        return true;
+    }
+}

Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/CookieStoreImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/HttpCookie.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/HttpCookie.java?view=auto&rev=546302
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/HttpCookie.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/HttpCookie.java Mon Jun 11 15:07:06 2007
@@ -0,0 +1,821 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.net;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.harmony.luni.util.Msg;
+
+/**
+ * This class represents a http cookie, which indicates the status information
+ * between the client agent side and the server side. According to RFC, there
+ * are 3 http cookie specifications. This class is compatible with all the three
+ * forms. HttpCookie class can accept all these 3 forms of syntax.
+ * 
+ * @since 1.6
+ */
+public final class HttpCookie implements Cloneable {
+
+    private abstract static class Setter {
+        boolean set;
+
+        Setter() {
+            set = false;
+        }
+
+        boolean isSet() {
+            return set;
+        }
+
+        void set(boolean isSet) {
+            set = isSet;
+        }
+
+        abstract void setValue(String value, HttpCookie cookie);
+
+        void validate(String value, HttpCookie cookie) {
+            if (cookie.getVersion() == 1 && value != null
+                    && value.contains(COMMA_STR)) {
+                throw new IllegalArgumentException();
+            }
+        }
+    }
+
+    private static final String DOT_STR = "."; //$NON-NLS-1$
+
+    private static final String LOCAL_STR = ".local"; //$NON-NLS-1$
+
+    private static final String QUOTE_STR = "\""; //$NON-NLS-1$
+
+    private static final String COMMA_STR = ","; //$NON-NLS-1$
+
+    private static Pattern HEAD_PATTERN = Pattern.compile("Set-Cookie2?:", //$NON-NLS-1$
+            Pattern.CASE_INSENSITIVE);
+
+    private static Pattern NAME_PATTERN = Pattern
+            .compile(
+                    "([^$=,\u0085\u2028\u2029][^,\n\t\r\r\n\u0085\u2028\u2029]*?)=([^;]*)(;)?", //$NON-NLS-1$
+                    Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
+
+    private static Pattern ATTR_PATTERN0 = Pattern
+            .compile("([^;=]*)(?:=([^;]*))?"); //$NON-NLS-1$
+
+    private static Pattern ATTR_PATTERN1 = Pattern
+            .compile("(,?[^;=]*)(?:=([^;,]*))?((?=.))?"); //$NON-NLS-1$
+
+    private static HashMap<String, Setter> attributeSet = new HashMap<String, Setter>();
+
+    /**
+     * A utility method used to check whether the host name is in a domain or
+     * not.
+     * 
+     * @param domain
+     *            the domain to be checked against
+     * @param host
+     *            the host to be checked
+     * @return true if the host is in the domain, false otherwise
+     */
+    public static boolean domainMatches(String domain, String host) {
+        if (domain == null || host == null) {
+            return false;
+        }
+        String newDomain = domain.toLowerCase();
+        String newHost = host.toLowerCase();
+        return isValidDomain(newDomain) && effDomainMatches(newDomain, newHost)
+                && isValidHost(newDomain, newHost);
+    }
+
+    private static boolean effDomainMatches(String domain, String host) {
+        // calculate effective host name
+        String effHost = host.indexOf(DOT_STR) != -1 ? host
+                : (host + LOCAL_STR);
+
+        // Rule 2: domain and host are string-compare equal, or A = NB, B = .B'
+        // and N is a non-empty name string
+        boolean inDomain = domain.equals(effHost);
+        inDomain = inDomain
+                || (effHost.endsWith(domain)
+                        && effHost.length() > domain.length() && domain
+                        .startsWith(DOT_STR));
+        return inDomain;
+    }
+
+    private static boolean isCommaDelim(HttpCookie cookie) {
+        String value = cookie.getValue();
+        if (value.startsWith(QUOTE_STR) && value.endsWith(QUOTE_STR)) {
+            cookie.setValue(value.substring(1, value.length() - 1));
+            return false;
+        }
+        if (cookie.getVersion() == 1 && value.contains(COMMA_STR)) {
+            cookie.setValue(value.substring(0, value.indexOf(COMMA_STR)));
+            return true;
+        }
+        return false;
+    }
+
+    private static boolean isValidDomain(String domain) {
+        // Rule 1: The value for Domain contains embedded dots, or is .local
+        if (domain.length() <= 2) {
+            return false;
+        }
+        return domain.substring(1, domain.length() - 1).indexOf(DOT_STR) != -1
+                || domain.equals(LOCAL_STR);
+    }
+
+    private static boolean isValidHost(String domain, String host) {
+        // Rule 3: host does not end with domain, or the remainder does not
+        // contain "."
+        boolean matches = !host.endsWith(domain);
+        if (!matches) {
+            String hostSub = host.substring(0, host.length() - domain.length());
+            matches = hostSub.indexOf(DOT_STR) == -1;
+        }
+        return matches;
+    }
+
+    /**
+     * Constructs a cookie from a string. The string should comply with
+     * set-cookie or set-cookie2 header format as specified in RFC 2965. Since
+     * set-cookies2 syntax allows more than one cookie definitions in one
+     * header, the returned object is a list.
+     * 
+     * @param header
+     *            a set-cookie or set-cookie2 header.
+     * @return a list of constructed cookies
+     * @throws IllegalArgumentException
+     *             if the string does not comply with cookie specification, or
+     *             the cookie name contains illegal characters, or reserved
+     *             tokens of cookie specification appears
+     * @throws NullPointerException
+     *             if header is null
+     */
+    public static List<HttpCookie> parse(String header) {
+        Matcher matcher = HEAD_PATTERN.matcher(header);
+        // Parse cookie name & value
+        List<HttpCookie> list = null;
+        HttpCookie cookie = null;
+        String headerString = header;
+        int version = 0;
+        // process set-cookie | set-cookie2 head
+        if (matcher.find()) {
+            String cookieHead = matcher.group();
+            if ("set-cookie2:".equalsIgnoreCase(cookieHead)) { //$NON-NLS-1$
+                version = 1;
+            }
+            headerString = header.substring(cookieHead.length());
+        }
+
+        // parse cookie name/value pair
+        matcher = NAME_PATTERN.matcher(headerString);
+        if (matcher.lookingAt()) {
+            list = new ArrayList<HttpCookie>();
+            cookie = new HttpCookie(matcher.group(1), matcher.group(2));
+            cookie.setVersion(version);
+
+            /*
+             * Comma is a delimiter in cookie spec 1.1. If find comma in version
+             * 1 cookie header, part of matched string need to be spitted out.
+             */
+            String nameGroup = matcher.group();
+            if (isCommaDelim(cookie)) {
+                headerString = headerString.substring(nameGroup
+                        .indexOf(COMMA_STR));
+            } else {
+                headerString = headerString.substring(nameGroup.length());
+            }
+            list.add(cookie);
+        } else {
+            throw new IllegalArgumentException();
+        }
+
+        // parse cookie headerString
+        while (!(headerString.length() == 0)) {
+            matcher = cookie.getVersion() == 1 ? ATTR_PATTERN1
+                    .matcher(headerString) : ATTR_PATTERN0
+                    .matcher(headerString);
+
+            if (matcher.lookingAt()) {
+                String attrName = matcher.group(1).trim();
+
+                // handle special situation like: <..>;;<..>
+                if (attrName.length() == 0) {
+                    headerString = headerString.substring(1);
+                    continue;
+                }
+
+                // If port is the attribute, then comma will not be used as a
+                // delimiter
+                if (attrName.equalsIgnoreCase("port") //$NON-NLS-1$
+                        || attrName.equalsIgnoreCase("expires")) { //$NON-NLS-1$
+                    int start = matcher.regionStart();
+                    matcher = ATTR_PATTERN0.matcher(headerString);
+                    matcher.region(start, headerString.length());
+                    matcher.lookingAt();
+                } else if (cookie.getVersion() == 1
+                        && attrName.startsWith(COMMA_STR)) {
+                    // If the last encountered token is comma, and the parsed
+                    // attribute is not port, then this attribute/value pair
+                    // ends.
+                    headerString = headerString.substring(1);
+                    matcher = NAME_PATTERN.matcher(headerString);
+                    if (matcher.lookingAt()) {
+                        cookie = new HttpCookie(matcher.group(1), matcher
+                                .group(2));
+                        list.add(cookie);
+                        headerString = headerString.substring(matcher.group()
+                                .length());
+                        continue;
+                    }
+                }
+
+                Setter setter = attributeSet.get(attrName.toLowerCase());
+                if (null == setter) {
+                    throw new IllegalArgumentException();
+                }
+                if (!setter.isSet()) {
+                    String attrValue = matcher.group(2);
+                    setter.validate(attrValue, cookie);
+                    setter.setValue(matcher.group(2), cookie);
+                }
+                headerString = headerString.substring(matcher.end());
+            }
+        }
+
+        return list;
+    }
+
+    private String comment;
+
+    private String commentURL;
+
+    private boolean discard;
+
+    private String domain;
+
+    private long maxAge = -1l;
+
+    private String name;
+
+    private String path;
+
+    private String portList;
+
+    private boolean secure;
+
+    private String value;
+
+    private int version = 1;
+
+    {
+        attributeSet.put("comment", new Setter() { //$NON-NLS-1$
+                    @Override
+                    void setValue(String value, HttpCookie cookie) {
+                        cookie.setComment(value);
+                        if (cookie.getComment() != null) {
+                            set(true);
+                        }
+                    }
+                });
+        attributeSet.put("commenturl", new Setter() { //$NON-NLS-1$
+                    @Override
+                    void setValue(String value, HttpCookie cookie) {
+                        cookie.setCommentURL(value);
+                        if (cookie.getCommentURL() != null) {
+                            set(true);
+                        }
+                    }
+                });
+        attributeSet.put("discard", new Setter() { //$NON-NLS-1$
+                    @Override
+                    void setValue(String value, HttpCookie cookie) {
+                        cookie.setDiscard(true);
+                        set(true);
+                    }
+                });
+        attributeSet.put("domain", new Setter() { //$NON-NLS-1$
+                    @Override
+                    void setValue(String value, HttpCookie cookie) {
+                        cookie.setDomain(value);
+                        if (cookie.getDomain() != null) {
+                            set(true);
+                        }
+                    }
+                });
+        attributeSet.put("max-age", new Setter() { //$NON-NLS-1$
+                    @Override
+                    void setValue(String value, HttpCookie cookie) {
+                        try {
+                            cookie.setMaxAge(Long.parseLong(value));
+                        } catch (NumberFormatException e) {
+                            throw new IllegalArgumentException(Msg.getString(
+                                    "KB001", "max-age")); //$NON-NLS-1$//$NON-NLS-2$
+                        }
+                        set(true);
+
+                        if (!attributeSet.get("version").isSet()) { //$NON-NLS-1$
+                            cookie.setVersion(1);
+                        }
+                    }
+                });
+
+        attributeSet.put("path", new Setter() { //$NON-NLS-1$
+                    @Override
+                    void setValue(String value, HttpCookie cookie) {
+                        cookie.setPath(value);
+                        if (cookie.getPath() != null) {
+                            set(true);
+                        }
+                    }
+                });
+        attributeSet.put("port", new Setter() { //$NON-NLS-1$
+                    @Override
+                    void setValue(String value, HttpCookie cookie) {
+                        cookie.setPortlist(value);
+                        if (cookie.getPortlist() != null) {
+                            set(true);
+                        }
+                    }
+
+                    @Override
+                    void validate(String v, HttpCookie cookie) {
+                        return;
+                    }
+                });
+        attributeSet.put("secure", new Setter() { //$NON-NLS-1$
+                    @Override
+                    void setValue(String value, HttpCookie cookie) {
+                        cookie.setSecure(true);
+                        set(true);
+                    }
+                });
+        attributeSet.put("version", new Setter() { //$NON-NLS-1$
+                    @Override
+                    void setValue(String value, HttpCookie cookie) {
+                        try {
+                            int v = Integer.parseInt(value);
+                            if (v > cookie.getVersion()) {
+                                cookie.setVersion(v);
+                            }
+                        } catch (NumberFormatException e) {
+                            throw new IllegalArgumentException(Msg.getString(
+                                    "KB001", "version"));//$NON-NLS-1$//$NON-NLS-2$
+                        }
+                        if (cookie.getVersion() != 0) {
+                            set(true);
+                        }
+                    }
+                });
+
+        attributeSet.put("expires", new Setter() { //$NON-NLS-1$
+                    @Override
+                    void setValue(String value, HttpCookie cookie) {
+                        cookie.setVersion(0);
+                        attributeSet.get("version").set(true); //$NON-NLS-1$
+                        if (!attributeSet.get("max-age").isSet()) { //$NON-NLS-1$
+                            attributeSet.get("max-age").set(true); //$NON-NLS-1$
+                            if (!"en".equalsIgnoreCase(Locale.getDefault() //$NON-NLS-1$
+                                    .getLanguage())) {
+                                cookie.setMaxAge(0);
+                                return;
+                            }
+                            try {
+                                cookie.setMaxAge((Date.parse(value) - System
+                                        .currentTimeMillis()) / 1000);
+                            } catch (IllegalArgumentException e) {
+                                cookie.setMaxAge(0);
+                            }
+                        }
+                    }
+
+                    @Override
+                    void validate(String v, HttpCookie cookie) {
+                        return;
+                    }
+                });
+    }
+
+    /**
+     * Initializes a cookie with the specified name and value.
+     * 
+     * The name attribute can just contain ASCII characters, which is immutable
+     * after creation. Commas, white space and semicolons are not allowed. The $
+     * character is also not allowed to be the beginning of the name.
+     * 
+     * The value attribute depends on what the server side is interested. The
+     * setValue method can be used to change it.
+     * 
+     * RFC 2965 is the default cookie specification of this class. If one wants
+     * to change the version of the cookie, the setVersion method is available.
+     * 
+     * @param name -
+     *            the specific name of the cookie
+     * @param value -
+     *            the specific value of the cookie
+     * 
+     * @throws IllegalArgumentException -
+     *             if the name contains not-allowed or reserved characters
+     * 
+     * @throws NullPointerException
+     *             if the value of name is null
+     */
+    public HttpCookie(String n, String v) {
+        String ntrim = n.trim(); // erase leading and trailing whitespaces
+        if (!isValidName(ntrim)) {
+            throw new IllegalArgumentException(Msg.getString("KB002")); //$NON-NLS-1$
+        }
+
+        name = ntrim;
+        value = v;
+    }
+
+    private void attrToString(StringBuilder builder, String attrName,
+            String attrValue) {
+        if (attrValue != null && builder != null) {
+            builder.append(";"); //$NON-NLS-1$ 
+            builder.append("$");//$NON-NLS-1$ 
+            builder.append(attrName);
+            builder.append("=\""); //$NON-NLS-1$			
+            builder.append(attrValue);
+            builder.append(QUOTE_STR);
+        }
+    }
+
+    /**
+     * Answers a copy of this object.
+     * 
+     * @return a copy of this cookie
+     */
+    @Override
+    public Object clone() {
+        try {
+            HttpCookie obj = (HttpCookie) super.clone();
+            return obj;
+        } catch (CloneNotSupportedException e) {
+            return null;
+        }
+    }
+
+    /**
+     * Answers whether two cookies are equal. Two cookies are equal if they have
+     * the same domain and name in a case-insensitive mode and path in a
+     * case-sensitive mode.
+     * 
+     * @param obj
+     *            the object to be compared.
+     * @return true if two cookies equals, false otherwise
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (obj instanceof HttpCookie) {
+            HttpCookie anotherCookie = (HttpCookie) obj;
+            if (name.equalsIgnoreCase(anotherCookie.getName())) {
+                String anotherDomain = anotherCookie.getDomain();
+                boolean equals = domain == null ? anotherDomain == null
+                        : domain.equalsIgnoreCase(anotherDomain);
+                if (equals) {
+                    String anotherPath = anotherCookie.getPath();
+                    return path == null ? anotherPath == null : path
+                            .equals(anotherPath);
+                }
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Answers the value of comment attribute(specified in RFC 2965) of this
+     * cookie.
+     * 
+     * @return the value of comment attribute
+     */
+    public String getComment() {
+        return comment;
+    }
+
+    /**
+     * Answers the value of commentURL attribute(specified in RFC 2965) of this
+     * cookie.
+     * 
+     * @return the value of commentURL attribute
+     */
+    public String getCommentURL() {
+        return commentURL;
+    }
+
+    /**
+     * Answers the value of discard attribute(specified in RFC 2965) of this
+     * cookie.
+     * 
+     * @return discard value of this cookie
+     */
+    public boolean getDiscard() {
+        return discard;
+    }
+
+    /**
+     * Answers the domain name for this cookie in the format specified in RFC
+     * 2965
+     * 
+     * @return the domain value of this cookie
+     */
+    public String getDomain() {
+        return domain;
+    }
+
+    /**
+     * Returns the Max-Age value as specified in RFC 2965 of this cookie.
+     * 
+     * @return the Max-Age value
+     */
+    public long getMaxAge() {
+        return maxAge;
+    }
+
+    /**
+     * Answers the name for this cookie.
+     * 
+     * @return the name for this cookie
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * Answers the path part of a request URL to which this cookie is returned.
+     * This cookie is visible to all subpaths.
+     * 
+     * @return the path used to return the cookie
+     */
+    public String getPath() {
+        return path;
+    }
+
+    /**
+     * Answers the value of port attribute(specified in RFC 2965) of this
+     * cookie.
+     * 
+     * @return port list of this cookie
+     */
+    public String getPortlist() {
+        return portList;
+    }
+
+    /**
+     * Answers true if the browser only sends cookies over a secure protocol.
+     * False if can send cookies through any protocols.
+     * 
+     * @return true if sends cookies only through secure protocol, false
+     *         otherwise
+     */
+    public boolean getSecure() {
+        return secure;
+    }
+
+    /**
+     * Answers the value of this cookie.
+     * 
+     * @return the value of this cookie
+     */
+    public String getValue() {
+        return value;
+    }
+
+    /**
+     * Get the version of this cookie
+     * 
+     * @return 0 indicates the original Netscape cookie specification, while 1
+     *         indicates RFC 2965/2109 specification.
+     */
+    public int getVersion() {
+        return version;
+    }
+
+    /**
+     * Answers whether the cookie has expired.
+     * 
+     * @return true is the cookie has expired, false otherwise
+     */
+    public boolean hasExpired() {
+        // -1 indicates the cookie will persist until browser shutdown
+        // so the cookie is not expired.
+        if (maxAge == -1l) {
+            return false;
+        }
+
+        boolean expired = false;
+        if (maxAge <= 0l) {
+            expired = true;
+        }
+        return expired;
+    }
+
+    /**
+     * Answers hash code of this http cookie. The result is calculated as below:
+     * 
+     * getName().toLowerCase().hashCode() + getDomain().toLowerCase().hashCode() +
+     * getPath().hashCode()
+     * 
+     * @return the hash code of this cookie
+     */
+    @Override
+    public int hashCode() {
+        int hashCode = name.toLowerCase().hashCode();
+        hashCode += domain == null ? 0 : domain.toLowerCase().hashCode();
+        hashCode += path == null ? 0 : path.hashCode();
+        return hashCode;
+    }
+
+    private boolean isValidName(String n) {
+        // name cannot be empty or begin with '$' or equals the reserved
+        // attributes (case-insensitive)
+        boolean isValid = !(n.length() == 0 || n.startsWith("$") || attributeSet.containsKey(n.toLowerCase())); //$NON-NLS-1$
+        if (isValid) {
+            for (int i = 0; i < n.length(); i++) {
+                char nameChar = n.charAt(i);
+                // name must be ASCII characters and cannot contain ';', ',' and
+                // whitespace
+                if (nameChar < 0
+                        || nameChar >= 127
+                        || nameChar == ';'
+                        || nameChar == ','
+                        || (Character.isWhitespace(nameChar) && nameChar != ' ')) {
+                    isValid = false;
+                    break;
+                }
+            }
+        }
+        return isValid;
+    }
+
+    /**
+     * Set the value of comment attribute(specified in RFC 2965) of this cookie.
+     * 
+     * @param purpose
+     *            the comment value to be set
+     */
+    public void setComment(String purpose) {
+        comment = purpose;
+    }
+
+    /**
+     * Set the value of commentURL attribute(specified in RFC 2965) of this
+     * cookie.
+     * 
+     * @param purpose
+     *            the value of commentURL attribute to be set
+     */
+    public void setCommentURL(String purpose) {
+        commentURL = purpose;
+    }
+
+    /**
+     * Set the value of discard attribute(specified in RFC 2965) of this cookie.
+     * 
+     * @param discard
+     *            the value for discard attribute
+     */
+    public void setDiscard(boolean dis) {
+        discard = dis;
+    }
+
+    /**
+     * Set the domain value for this cookie. Browsers send the cookie to the
+     * domain specified by this value. The form of the domain is specified in
+     * RFC 2965.
+     * 
+     * @param pattern
+     *            the domain pattern
+     */
+    public void setDomain(String pattern) {
+        domain = pattern == null ? null : pattern.toLowerCase();
+    }
+
+    /**
+     * Sets the Max-Age value as specified in RFC 2965 of this cookie to expire.
+     * 
+     * @param expiry
+     *            the value used to set the Max-Age value of this cookie
+     */
+    public void setMaxAge(long expiry) {
+        maxAge = expiry;
+    }
+
+    /**
+     * Set the path to which this cookie is returned. This cookie is visible to
+     * all the pages under the path and all subpaths.
+     * 
+     * @param path
+     *            the path to which this cookie is returned
+     */
+    public void setPath(String p) {
+        path = p;
+    }
+
+    /**
+     * Set the value of port attribute(specified in RFC 2965) of this cookie.
+     * 
+     * @param ports
+     *            the value for port attribute
+     */
+    public void setPortlist(String ports) {
+        portList = ports;
+    }
+
+    /*
+     * Handle 2 special cases: 1. value is wrapped by a quotation 2. value
+     * contains comma
+     */
+
+    /**
+     * Tells the browser whether the cookies should be sent to server through
+     * secure protocols.
+     * 
+     * @param flag
+     *            tells browser to send cookie to server only through secure
+     *            protocol if flag is true
+     */
+    public void setSecure(boolean flag) {
+        secure = flag;
+    }
+
+    /**
+     * Sets the value for this cookie after it has been instantiated. String
+     * newValue can be in BASE64 form. If the version of the cookie is 0,
+     * special value as: white space, brackets, parentheses, equals signs,
+     * commas, double quotes, slashes, question marks, at signs, colons, and
+     * semicolons are not recommended. Empty values may lead to different
+     * behavior on different browsers.
+     * 
+     * @param newValue
+     *            the value for this cookie
+     */
+    public void setValue(String newValue) {
+        // FIXME: According to spec, version 0 cookie value does not allow many
+        // symbols. But RI does not implement it. Follow RI temporarily.
+        value = newValue;
+    }
+
+    /**
+     * Sets the version of the cookie. 0 indicates the original Netscape cookie
+     * specification, while 1 indicates RFC 2965/2109 specification.
+     * 
+     * @param v
+     *            0 or 1 as stated above
+     * @throws IllegalArgumentException
+     *             if v is neither 0 nor 1
+     */
+    public void setVersion(int v) {
+        if (v != 0 && v != 1) {
+            throw new IllegalArgumentException(Msg.getString("KB003")); //$NON-NLS-1$
+        }
+        version = v;
+    }
+
+    /**
+     * Returns a string to represent the cookie. The format of string follows
+     * the cookie specification. The leading token "Cookie" is not included
+     * 
+     * @return the string format of the cookie object
+     */
+    @Override
+    public String toString() {
+        StringBuilder cookieStr = new StringBuilder();
+        cookieStr.append(name);
+        cookieStr.append("="); //$NON-NLS-1$
+        if (version == 0) {
+            cookieStr.append(value);
+        } else if (version == 1) {
+            cookieStr.append(QUOTE_STR);
+            cookieStr.append(value);
+            cookieStr.append(QUOTE_STR);
+
+            attrToString(cookieStr, "Path", path); //$NON-NLS-1$
+            attrToString(cookieStr, "Domain", domain); //$NON-NLS-1$
+            attrToString(cookieStr, "Port", portList);//$NON-NLS-1$		
+        }
+        return cookieStr.toString();
+    }
+}

Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/net/HttpCookie.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieManagerTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieManagerTest.java?view=auto&rev=546302
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieManagerTest.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieManagerTest.java Mon Jun 11 15:07:06 2007
@@ -0,0 +1,306 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.luni.tests.java.net;
+
+import java.io.IOException;
+import java.net.CookieManager;
+import java.net.CookiePolicy;
+import java.net.CookieStore;
+import java.net.HttpCookie;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+import junit.framework.TestCase;
+
+public class CookieManagerTest extends TestCase {
+
+    private static void checkValidParams4Get(URI uri,
+            Map<String, List<String>> map) throws IOException {
+        CookieManager manager = new CookieManager();
+        try {
+            manager.get(uri, map);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+    }
+
+    private static void checkValidParams4Put(URI uri,
+            Map<String, List<String>> map) throws IOException {
+        CookieManager manager = new CookieManager();
+        try {
+            manager.put(uri, map);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+    }
+
+    /**
+     * @tests {@link java.net.CookieManager#get(java.net.URI, java.util.Map)} &
+     * @tests {@link java.net.CookieManager#put(java.net.URI, java.util.Map)}
+     *        IllegalArgumentException
+     * @since 1.6
+     */
+    public void test_Put_Get_LURI_LMap_exception() throws IOException,
+            URISyntaxException {
+        // get
+        checkValidParams4Get(new URI(""), null);
+        checkValidParams4Get(new URI("http://www.test.com"), null);
+        checkValidParams4Get(null, null);
+        checkValidParams4Get(null, new HashMap<String, List<String>>());
+
+        // put
+        checkValidParams4Put(new URI(""), null);
+        checkValidParams4Put(new URI("http://www.test.com"), null);
+        checkValidParams4Put(null, null);
+        checkValidParams4Put(null, new HashMap<String, List<String>>());
+    }
+
+    private static Map<String, List<String>> addCookie(String[][] cookies) {
+        Map<String, List<String>> responseHeaders = new TreeMap<String, List<String>>();
+        for (int i = 0; i < cookies.length; i++) {
+            List<String> fields = new ArrayList<String>();
+            for (int j = 1; j < cookies[i].length; j += 2) {
+                fields.add(cookies[i][j]);
+            }
+            responseHeaders.put(cookies[i][0], fields);
+        }
+        return responseHeaders;
+    }
+
+    private static CookieManager store(String[][] cookies,
+            Map<String, List<String>> responseHeaders, CookiePolicy policy)
+            throws IOException, URISyntaxException {
+        CookieManager manager = new CookieManager(null, policy);
+        // Put all cookies into manager
+        for (int i = 0; i < cookies.length; i++) {
+            for (int j = 2; j < cookies[i].length; j += 2) {
+                URI uri = new URI(cookies[i][j]);
+                manager.put(uri, responseHeaders);
+            }
+        }
+        return manager;
+    }
+
+    /**
+     * @tests {@link java.net.CookieManager#get(java.net.URI, java.util.Map)} &
+     * @tests {@link java.net.CookieManager#put(java.net.URI, java.util.Map)}
+     * 
+     * @since 1.6
+     */
+    public void test_Put_Get_LURI_LMap() throws IOException, URISyntaxException {
+        // cookie-key | (content, URI)...
+        String[][] cookies = {
+                { "Set-cookie", 
+                    "Set-cookie:PREF=test;path=/;domain=.b.c;", "http://a.b.c/", 
+                    "Set-cookie:PREF1=test2;path=/;domain=.beg.com;", "http://a.b.c/"},
+
+                { "Set-cookie2", 
+                    "Set-cookie2:NAME1=VALUE1;path=/te;domain=.b.c;", "http://a.b.c/test"},
+
+                { "Set-cookie", 
+                    "Set-cookie2:NAME=VALUE;path=/;domain=.beg.com;", "http://a.beg.com/test",  
+                    "Set-cookie2:NAME1=VALUE1;path=/;domain=.beg.com;",    "http://a.beg.com/test"},
+
+                { "Set-cookie2", 
+                    "Set-cookie3:NAME=VALUE;path=/;domain=.test.org;", "http://a.test.org/test"},
+
+                { null, 
+                    "Set-cookie3:NAME=VALUE;path=/te;domain=.test.org;", "http://a.test.org/test"},
+                
+                { "Set-cookie2",
+                    "lala", "http://a.test.org/test"}
+
+        };
+
+        // requires path of cookie is the prefix of uri
+        // domain of cookie must match that of uri
+        Map<String, List<String>> responseHeaders = addCookie(new String[][] {
+                cookies[0], cookies[1] });
+        CookieManager manager = store(
+                new String[][] { cookies[0], cookies[1] }, responseHeaders,
+                null);
+
+        HashMap<String, List<String>> dummyMap = new HashMap<String, List<String>>();
+        Map<String, List<String>> map = manager.get(new URI("http://a.b.c/"),
+                dummyMap);
+
+        assertEquals(1, map.size());
+        List<String> list = map.get("Cookie");
+        assertEquals(1, list.size());
+
+        // requires path of cookie is the prefix of uri
+        map = manager.get(new URI("http://a.b.c/te"), dummyMap);
+        list = map.get("Cookie");
+        assertEquals(2, list.size());
+
+        // If all cookies are of version 1, then $version=1 will be added
+        // ,no matter the value cookie-key
+        responseHeaders = addCookie(new String[][] { cookies[2] });
+        manager = store(new String[][] { cookies[2] }, responseHeaders, null);
+        map = manager.get(new URI("http://a.beg.com/test"), dummyMap);
+        list = map.get("Cookie");
+        assertEquals("$Version=\"1\"", list.get(0));
+        assertEquals(3, list.size());
+
+        // cookie-key will not have effect on determining cookie version
+        responseHeaders = addCookie(new String[][] { cookies[3] });
+        manager = store(new String[][] { cookies[3] }, responseHeaders, null);
+        map = manager.get(new URI("http://a.test.org/"), responseHeaders);
+        list = map.get("Cookie");
+        assertEquals(1, list.size());
+        assertEquals("Set-cookie3:NAME=VALUE", list.get(0));
+
+        // When key is null, no cookie can be stored/retrieved, even if policy =
+        // ACCEPT_ALL
+        responseHeaders = addCookie(new String[][] { cookies[4] });
+        manager = store(new String[][] { cookies[4] }, responseHeaders,
+                CookiePolicy.ACCEPT_ALL);
+        map = manager.get(new URI("http://a.test.org/"), responseHeaders);
+        list = map.get("Cookie");
+        assertEquals(0, list.size());
+
+        // All cookies will be rejected if policy == ACCEPT_NONE
+        responseHeaders = addCookie(new String[][] { cookies[3] });
+        manager = store(new String[][] { cookies[3] }, responseHeaders,
+                CookiePolicy.ACCEPT_NONE);
+        map = manager.get(new URI("http://a.test.org/"), responseHeaders);
+        list = map.get("Cookie");
+        assertEquals(0, list.size());
+        
+        responseHeaders = addCookie(new String[][] { cookies[5] });
+        manager = store(new String[][] { cookies[5] }, responseHeaders,
+                CookiePolicy.ACCEPT_ALL);
+        list = map.get("Cookie");
+        assertEquals(0, list.size());
+
+        try {
+            map.put(null, null);
+            fail("Should throw UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+
+    }
+
+    /**
+     * @tests {@link java.net.CookieManager#CookieManager()}
+     * 
+     * @since 1.6
+     */
+    public void test_CookieManager() {
+        CookieManager cookieManager = new CookieManager();
+        assertNotNull(cookieManager);
+        assertNotNull(cookieManager.getCookieStore());
+    }
+
+    /**
+     * @tests {@link java.net.CookieManager#CookieManager(java.net.CookieStore, java.net.CookiePolicy)}
+     * 
+     * @since 1.6
+     */
+    public void testCookieManager_LCookieStore_LCookiePolicy() {
+        class DummyStore implements CookieStore {
+            public String getName() {
+                return "A dummy store";
+            }
+
+            public void add(URI uri, HttpCookie cookie) {
+                // expected
+            }
+
+            public List<HttpCookie> get(URI uri) {
+                return null;
+            }
+
+            public List<HttpCookie> getCookies() {
+                return null;
+            }
+
+            public List<URI> getURIs() {
+                return null;
+            }
+
+            public boolean remove(URI uri, HttpCookie cookie) {
+                return false;
+            }
+
+            public boolean removeAll() {
+                return false;
+            }
+        }
+        CookieStore store = new DummyStore();
+        CookieManager cookieManager = new CookieManager(store,
+                CookiePolicy.ACCEPT_ALL);
+        assertEquals("A dummy store", ((DummyStore) cookieManager
+                .getCookieStore()).getName());
+        assertSame(store, cookieManager.getCookieStore());
+    }
+
+    /**
+     * @tests {@link java.net.CookieManager#setCookiePolicy(java.net.CookiePolicy)}
+     * 
+     * @since 1.6
+     */
+    public void test_SetCookiePolicy_LCookiePolicy() throws URISyntaxException,
+            IOException {
+
+        // Policy = ACCEPT_NONE
+        CookieManager manager = new CookieManager();
+        manager.setCookiePolicy(CookiePolicy.ACCEPT_NONE);
+        Map<String, List<String>> responseHeaders = new TreeMap<String, List<String>>();        
+        URI uri = new URI("http://a.b.c");
+        manager.put(uri, responseHeaders);
+        Map<String, List<String>> map = manager.get(uri,
+                new HashMap<String, List<String>>());
+
+        assertEquals(1, map.size());
+        assertTrue(map.get("Cookie").isEmpty());
+        Object key = map.keySet().toArray()[0];
+        assertNotNull(key);
+        assertEquals("Cookie", key);
+
+        // Policy = ACCEPT_ALL
+        manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
+        responseHeaders = new TreeMap<String, List<String>>();    
+        ArrayList<String> list = new ArrayList<String>();
+        list.add("test=null");
+        responseHeaders.put("Set-cookie", list);
+        manager.put(new URI("http://b.c.d"), responseHeaders);
+        map = manager.get(uri, new HashMap<String, List<String>>());
+        assertEquals(1, map.size());        
+    }
+
+    /**
+     * @tests {@link java.net.CookieManager#getCookieStore()}
+     * 
+     * @since 1.6
+     */
+    public void test_GetCookieStore() {
+        CookieManager cookieManager = new CookieManager();
+        CookieStore store = cookieManager.getCookieStore();
+        assertNotNull(store);
+    }
+
+}

Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieManagerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookiePolicyTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookiePolicyTest.java?view=auto&rev=546302
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookiePolicyTest.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookiePolicyTest.java Mon Jun 11 15:07:06 2007
@@ -0,0 +1,113 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.luni.tests.java.net;
+
+import java.net.CookiePolicy;
+import java.net.HttpCookie;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import junit.framework.TestCase;
+
+public class CookiePolicyTest extends TestCase {
+
+    /**
+     * @tests java.net.CookiePolicy#shouldAccept(java.net.URI,
+     *        java.net.HttpCookie).
+     * 
+     * @since 1.6
+     */
+    public void test_ShouldAccept_LURI_LHttpCookie() throws URISyntaxException {
+        HttpCookie cookie = new HttpCookie("Harmony_6", "ongoing");
+        URI uri = new URI("");
+        try {
+            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, cookie);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        // Policy: ACCEPT_ALL, always returns true
+        boolean accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, cookie);
+        assertTrue(accept);
+
+        accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, null);
+        assertTrue(accept);
+
+        accept = CookiePolicy.ACCEPT_ALL.shouldAccept(uri, null);
+        assertTrue(accept);
+
+        // Policy: ACCEPT_NONE, always returns false
+        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, cookie);
+        assertFalse(accept);
+
+        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, null);
+        assertFalse(accept);
+
+        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(uri, null);
+        assertFalse(accept);
+
+        // Policy: ACCEPT_ORIGINAL_SERVER
+        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, cookie);
+        assertFalse(accept);
+
+        cookie.setDomain(".b.c");
+        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
+                "schema://a.b.c"), cookie);
+        assertTrue(accept);
+
+        cookie.setDomain(".b.c");
+        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
+                "s://a.b.c.d"), cookie);
+        assertFalse(accept);
+
+        cookie.setDomain("b.c");
+        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
+                "s://a.b.c.d"), cookie);
+        assertFalse(accept);
+
+        cookie.setDomain("a.b.c.d");
+        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
+                "s://a.b.c.d"), cookie);
+        assertTrue(accept);
+
+        cookie.setDomain(".");
+        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
+                "s://a.b.c.d"), cookie);
+        assertFalse(accept);
+
+        cookie.setDomain("");
+        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
+                "s://a.b.c.d"), cookie);
+        assertFalse(accept);
+    }
+
+}

Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookiePolicyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieStoreTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieStoreTest.java?view=auto&rev=546302
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieStoreTest.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieStoreTest.java Mon Jun 11 15:07:06 2007
@@ -0,0 +1,410 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.luni.tests.java.net;
+
+import java.net.CookieManager;
+import java.net.CookieStore;
+import java.net.HttpCookie;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.RandomAccess;
+
+import junit.framework.TestCase;
+
+public class CookieStoreTest extends TestCase {
+
+    private CookieManager cookieManager;
+
+    private CookieStore cookieStore;
+
+    /**
+     * @tests java.net.CookieStore#add(URI, HttpCookie)
+     * 
+     * @since 1.6
+     */
+    public void test_add_LURI_LHttpCookie() throws URISyntaxException {
+        URI uri = new URI("http://harmony.test.unit.org");
+        HttpCookie cookie = new HttpCookie("name1", "value1");
+        cookie.setDiscard(true);
+        try {
+            cookieStore.add(null, cookie);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            cookieStore.add(uri, null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            cookieStore.add(null, null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+        cookieStore.add(uri, cookie);
+        List<HttpCookie> list = cookieStore.get(uri);
+        assertEquals(1, list.size());
+        assertTrue(list.contains(cookie));
+
+        HttpCookie cookie2 = new HttpCookie("  NaME1   ", "  TESTVALUE1   ");
+        cookieStore.add(uri, cookie2);
+        list = cookieStore.get(uri);
+        assertEquals(1, list.size());
+        assertEquals("  TESTVALUE1   ", list.get(0).getValue());
+        assertTrue(list.contains(cookie2));        
+        
+        // domain and path attributes works
+        HttpCookie anotherCookie = new HttpCookie("name1", "value1");
+        anotherCookie.setDomain("domain");
+        anotherCookie.setPath("Path"); 
+        cookieStore.add(uri, anotherCookie);
+        list = cookieStore.get(uri);
+        assertEquals(2, list.size());
+        assertNull(list.get(0).getDomain());
+        assertEquals("domain", list.get(1).getDomain());
+        assertEquals("Path", list.get(1).getPath());
+
+        URI uri2 = new URI("http://.test.unit.org");
+        HttpCookie cookie3 = new HttpCookie("NaME2", "VALUE2");
+        cookieStore.add(uri2, cookie3);
+        list = cookieStore.get(uri2);
+        assertEquals(1, list.size());
+        assertEquals("VALUE2", list.get(0).getValue());
+        list = cookieStore.getCookies();
+        assertEquals(3, list.size());
+        
+        // expired cookie won't be selected.
+        HttpCookie cookie4 = new HttpCookie("cookie4", "value4");
+        cookie4.setMaxAge(-2);
+        assertTrue(cookie4.hasExpired());
+        cookieStore.add(uri2, cookie4);
+        list = cookieStore.getCookies();
+        assertEquals(3, list.size());
+        assertFalse(cookieStore.remove(uri2, cookie4));
+
+        cookie4.setMaxAge(3000);
+        cookie4.setDomain("domain");
+        cookie4.setPath("path");
+        cookieStore.add(uri2, cookie4);
+        list = cookieStore.get(uri2);
+        assertEquals(2, list.size());
+        
+        cookieStore.add(uri, cookie4);
+        list = cookieStore.get(uri);
+        assertEquals(3, list.size());
+        list = cookieStore.get(uri2);
+        assertEquals(2, list.size());
+        list = cookieStore.getCookies();
+        assertEquals(4, list.size());
+                
+        URI baduri = new URI("bad_url");
+        HttpCookie cookie6 = new HttpCookie("cookie5", "value5");
+        cookieStore.add(baduri, cookie6);
+        list = cookieStore.get(baduri);
+        assertTrue(list.contains(cookie6));
+    }
+
+    /**
+     * @tests java.net.CookieStore#get(URI)
+     * 
+     * @since 1.6
+     */
+    public void test_get_LURI() throws URISyntaxException {
+        try {
+            cookieStore.get(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        URI uri1 = new URI("http://get.uri1.test.org");
+        List<HttpCookie> list = cookieStore.get(uri1);
+        assertTrue(list.isEmpty());
+        assertTrue(list instanceof ArrayList);
+
+        HttpCookie cookie1 = new HttpCookie("cookie_name1", "cookie_value1");
+        HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
+        cookieStore.add(uri1, cookie1);
+        cookieStore.add(uri1, cookie2);
+        URI uri2 = new URI("http://get.uri2.test.org");
+        HttpCookie cookie3 = new HttpCookie("cookie_name3", "cookie_value3");
+        cookieStore.add(uri2, cookie3);
+        list = cookieStore.get(uri1);
+        assertEquals(2, list.size());
+        list = cookieStore.get(uri2);
+        assertEquals(1, list.size());        
+
+        // domain-match cookies also be selected.
+        HttpCookie cookie4 = new HttpCookie("cookie_name4", "cookie_value4");
+        cookie4.setDomain(".uri1.test.org");
+        cookieStore.add(uri2, cookie4);
+        list = cookieStore.get(uri1);
+        assertEquals(3, list.size());
+        
+        cookieStore.add(uri1, cookie4);
+        list = cookieStore.get(uri1);
+        assertEquals(3, list.size());
+        list = cookieStore.get(uri2);
+        assertEquals(2, list.size());
+
+        // expired cookie won't be selected.
+        HttpCookie cookie5 = new HttpCookie("cookie_name5", "cookie_value5");
+        cookie5.setMaxAge(-333);
+        assertTrue(cookie5.hasExpired());
+        cookieStore.add(uri1, cookie5);
+        list = cookieStore.get(uri1);
+        assertEquals(3, list.size());
+        assertFalse(cookieStore.remove(uri1, cookie5));
+        list = cookieStore.getCookies();
+        assertEquals(4, list.size());
+        
+        cookie4.setMaxAge(-123);
+        list = cookieStore.get(uri1);
+        assertEquals(2, list.size());
+        list = cookieStore.getCookies();
+        assertEquals(3, list.size());
+        // expired cookies are also deleted even if it domain-matches the URI
+        HttpCookie cookie6 = new HttpCookie("cookie_name6", "cookie_value6");
+        cookie6.setMaxAge(-2);
+        cookie6.setDomain(".uri1.test.org");
+        cookieStore.add(uri2, cookie6);
+        list = cookieStore.get(uri1);
+        assertEquals(2, list.size());
+        assertFalse(cookieStore.remove(null, cookie6));
+
+        URI uri3 = new URI("http://get.uri3.test.org");
+        assertTrue(cookieStore.get(uri3).isEmpty());
+        URI baduri = new URI("invalid_uri");
+        assertTrue(cookieStore.get(baduri).isEmpty());
+    }
+
+    /**
+     * @tests java.net.CookieStore#getCookies()
+     * 
+     * @since 1.6
+     */
+    public void test_getCookies() throws URISyntaxException {
+        List<HttpCookie> list = cookieStore.getCookies();
+        assertTrue(list.isEmpty());
+        assertTrue(list instanceof RandomAccess);
+
+        HttpCookie cookie1 = new HttpCookie("cookie_name", "cookie_value");
+        URI uri1 = new URI("http://getcookies1.test.org");
+        cookieStore.add(uri1, cookie1);
+        list = cookieStore.getCookies();
+        assertTrue(list.contains(cookie1));
+
+        HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
+        URI uri2 = new URI("http://getcookies2.test.org");
+        cookieStore.add(uri2, cookie2);
+        list = cookieStore.getCookies();
+        assertEquals(2, list.size());
+        assertTrue(list.contains(cookie1));
+        assertTrue(list.contains(cookie2));
+
+        // duplicated cookie won't be selected.
+        cookieStore.add(uri2, cookie1);
+        list = cookieStore.getCookies();
+        assertEquals(2, list.size());
+        // expired cookie won't be selected.
+        HttpCookie cookie3 = new HttpCookie("cookie_name3", "cookie_value3");
+        cookie3.setMaxAge(-1357);
+        cookieStore.add(uri1, cookie3);
+        list = cookieStore.getCookies();
+        assertEquals(2, list.size());
+
+        try {
+            list.add(new HttpCookie("readOnlyName", "readOnlyValue"));
+            fail("should throw UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+
+        try {
+            list.remove(new HttpCookie("readOnlyName", "readOnlyValue"));
+            fail("should throw UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.net.CookieStore#getURIs()
+     * 
+     * @since 1.6
+     */
+    public void test_getURIs() throws URISyntaxException {
+        List<URI> list = cookieStore.getURIs();
+        assertTrue(list.isEmpty());
+        assertTrue(list instanceof ArrayList);
+
+        URI uri1 = new URI("http://geturis1.test.com");
+        HttpCookie cookie1 = new HttpCookie("cookie_name1", "cookie_value1");
+        cookieStore.add(uri1, cookie1);
+        list = cookieStore.getURIs();
+        assertEquals("geturis1.test.com", list.get(0).getHost());
+
+        HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
+        cookieStore.add(uri1, cookie2);
+        list = cookieStore.getURIs();
+        assertEquals(1, list.size());
+
+        URI uri2 = new URI("http://geturis2.test.com");
+        cookieStore.add(uri2, cookie2);
+        list = cookieStore.getURIs();
+        assertEquals(2, list.size());
+        assertTrue(list.contains(uri1));
+        assertTrue(list.contains(uri2));
+    }
+
+    /**
+     * @tests java.net.CookieStore#remove(URI, HttpCookie)
+     * 
+     * @since 1.6
+     */
+    public void test_remove_LURI_LHttpCookie() throws URISyntaxException {
+        URI uri1 = new URI("http://remove1.test.com");
+        HttpCookie cookie1 = new HttpCookie("cookie_name1", "cookie_value1");
+        try {
+            cookieStore.remove(uri1, null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        assertFalse(cookieStore.remove(uri1, cookie1));
+        assertFalse(cookieStore.remove(null, cookie1));
+
+        cookieStore.add(uri1, cookie1);
+        URI uri2 = new URI("http://remove2.test.com");
+        HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
+        cookieStore.add(uri2, cookie2);
+        assertTrue(cookieStore.remove(uri1, cookie1));
+        assertFalse(cookieStore.remove(uri1, cookie1));
+        assertEquals(2, cookieStore.getURIs().size());
+        assertEquals(1, cookieStore.getCookies().size());
+        assertTrue(cookieStore.remove(uri2, cookie2));
+        assertFalse(cookieStore.remove(uri2, cookie2));
+        assertEquals(2, cookieStore.getURIs().size());
+        assertEquals(0, cookieStore.getCookies().size());
+        
+        assertTrue(cookieStore.removeAll());
+        cookieStore.add(uri1, cookie1);
+        cookieStore.add(uri2, cookie2);
+        HttpCookie cookie3 = new HttpCookie("cookie_name3", "cookie_value3");
+        assertFalse(cookieStore.remove(null, cookie3));
+        assertTrue(cookieStore.remove(null, cookie1));
+        assertFalse(cookieStore.remove(null, cookie1));
+        assertEquals(2, cookieStore.getURIs().size());
+        assertEquals(1, cookieStore.getCookies().size());
+        assertTrue(cookieStore.remove(null, cookie2));
+        assertFalse(cookieStore.remove(null, cookie2));
+        assertEquals(2, cookieStore.getURIs().size());
+        assertEquals(0, cookieStore.getCookies().size());
+
+        cookieStore.removeAll();
+        // expired cookies can also be deleted.
+        cookie2.setMaxAge(-34857);
+        cookieStore.add(uri2, cookie2);
+        assertTrue(cookieStore.remove(uri2, cookie2));
+        assertFalse(cookieStore.remove(uri2, cookie2));
+        assertEquals(0, cookieStore.getCookies().size());
+
+        cookie2.setMaxAge(34857);
+        cookieStore.add(uri1, cookie1);
+        cookieStore.add(uri2, cookie1);
+        cookieStore.add(uri2, cookie2);
+        assertTrue(cookieStore.remove(uri1, cookie1));
+        assertFalse(cookieStore.remove(uri1, cookie1));
+        assertFalse(cookieStore.get(uri2).contains(cookie1));
+        assertTrue(cookieStore.get(uri2).contains(cookie2));
+        assertEquals(0, cookieStore.get(uri1).size());
+        cookieStore.remove(uri2, cookie2);
+
+        cookieStore.removeAll();
+        cookieStore.add(uri2, cookie2);
+        cookieStore.add(uri1, cookie1);
+        assertEquals(2, cookieStore.getCookies().size());
+        assertTrue(cookieStore.remove(uri2, cookie1));
+        assertFalse(cookieStore.remove(uri2, cookie1));
+        assertEquals(2, cookieStore.getURIs().size());
+        assertEquals(1, cookieStore.getCookies().size());
+        assertTrue(cookieStore.getCookies().contains(cookie2));
+        
+        cookieStore.removeAll();
+        URI uri3 = new URI("http://remove3.test.com");
+        URI uri4 = new URI("http://test.com");
+        HttpCookie cookie4 = new HttpCookie("cookie_name4", "cookie_value4");
+        cookie4.setDomain(".test.com");
+        cookie2.setMaxAge(-34857);
+        cookie3.setMaxAge(-22);
+        cookie4.setMaxAge(-45);
+        cookieStore.add(uri1, cookie1);
+        cookieStore.add(uri2, cookie2);
+        cookieStore.add(uri3, cookie3);
+        cookieStore.add(uri4, cookie4);
+        assertEquals(0, cookieStore.get(uri2).size());
+        assertFalse(cookieStore.remove(uri2, cookie2));
+        assertTrue(cookieStore.remove(uri3, cookie3));
+        assertFalse(cookieStore.remove(uri4, cookie4));
+    }
+
+    /**
+     * @tests java.net.CookieStore#test_removeAll()
+     * 
+     * @since 1.6
+     */
+    public void test_removeAll() throws URISyntaxException {
+        // Spec says returns true if this store changed as a result of the call.
+        // But RI always return true.
+        assertTrue(cookieStore.removeAll());
+
+        URI uri1 = new URI("http://removeAll1.test.com");
+        HttpCookie cookie1 = new HttpCookie("cookie_name1", "cookie_value1");
+        cookieStore.add(uri1, cookie1);
+        URI uri2 = new URI("http://removeAll2.test.com");
+        HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
+        cookieStore.add(uri2, cookie2);
+
+        assertTrue(cookieStore.removeAll());
+        assertTrue(cookieStore.getURIs().isEmpty());
+        assertTrue(cookieStore.getCookies().isEmpty());
+
+        assertTrue(cookieStore.removeAll());
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        cookieManager = new CookieManager();
+        cookieStore = cookieManager.getCookieStore();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        cookieManager = null;
+        cookieStore = null;
+        super.tearDown();
+    }
+
+}

Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookieStoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message