1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.luni.tests.java.net;
18
19import java.net.CookiePolicy;
20import java.net.HttpCookie;
21import java.net.URI;
22import java.net.URISyntaxException;
23
24import junit.framework.TestCase;
25
26public class CookiePolicyTest extends TestCase {
27
28    /**
29     * @tests java.net.CookiePolicy#shouldAccept(java.net.URI,
30     *        java.net.HttpCookie).
31     *
32     * @since 1.6
33     */
34    public void test_ShouldAccept_LURI_LHttpCookie() throws URISyntaxException {
35        HttpCookie cookie = new HttpCookie("Harmony_6", "ongoing");
36        URI uri = new URI("");
37        try {
38            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, cookie);
39            fail("Should throw NullPointerException");
40        } catch (NullPointerException e) {
41            // expected
42        }
43
44        try {
45            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, null);
46            fail("Should throw NullPointerException");
47        } catch (NullPointerException e) {
48            // expected
49        }
50
51        try {
52            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, null);
53            fail("Should throw NullPointerException");
54        } catch (NullPointerException e) {
55            // expected
56        }
57
58        // Policy: ACCEPT_ALL, always returns true
59        boolean accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, cookie);
60        assertTrue(accept);
61
62        accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, null);
63        assertTrue(accept);
64
65        accept = CookiePolicy.ACCEPT_ALL.shouldAccept(uri, null);
66        assertTrue(accept);
67
68        // Policy: ACCEPT_NONE, always returns false
69        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, cookie);
70        assertFalse(accept);
71
72        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, null);
73        assertFalse(accept);
74
75        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(uri, null);
76        assertFalse(accept);
77
78        // Policy: ACCEPT_ORIGINAL_SERVER
79        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, cookie);
80        assertFalse(accept);
81
82        cookie.setDomain(".b.c");
83        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
84                "schema://a.b.c"), cookie);
85        assertTrue(accept);
86
87        cookie.setDomain(".b.c");
88        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
89                "s://a.b.c.d"), cookie);
90        assertFalse(accept);
91
92        cookie.setDomain("b.c");
93        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
94                "s://a.b.c.d"), cookie);
95        assertFalse(accept);
96
97        cookie.setDomain("a.b.c.d");
98        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
99                "s://a.b.c.d"), cookie);
100        assertTrue(accept);
101
102        cookie.setDomain(".");
103        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
104                "s://a.b.c.d"), cookie);
105        assertFalse(accept);
106
107        cookie.setDomain("");
108        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
109                "s://a.b.c.d"), cookie);
110        assertFalse(accept);
111    }
112
113}
114