View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client;
28  
29  import org.apache.http.cookie.CookieIdentityComparator;
30  import org.apache.http.impl.cookie.BasicClientCookie;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  /**
35   * Simple tests for {@link CookieIdentityComparator}.
36   */
37  public class TestCookieIdentityComparator {
38  
39      @Test
40      public void testCookieIdentityComparasionByName() {
41          final CookieIdentityComparator comparator = new CookieIdentityComparator();
42          final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
43          final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
44          Assert.assertTrue(comparator.compare(c1, c2) == 0);
45  
46          final BasicClientCookie c3 = new BasicClientCookie("name1", "value");
47          final BasicClientCookie c4 = new BasicClientCookie("name2", "value");
48          Assert.assertFalse(comparator.compare(c3, c4) == 0);
49      }
50  
51      @Test
52      public void testCookieIdentityComparasionByNameAndDomain() {
53          final CookieIdentityComparator comparator = new CookieIdentityComparator();
54          final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
55          c1.setDomain("www.domain.com");
56          final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
57          c2.setDomain("www.domain.com");
58          Assert.assertTrue(comparator.compare(c1, c2) == 0);
59  
60          final BasicClientCookie c3 = new BasicClientCookie("name", "value1");
61          c3.setDomain("www.domain.com");
62          final BasicClientCookie c4 = new BasicClientCookie("name", "value2");
63          c4.setDomain("domain.com");
64          Assert.assertFalse(comparator.compare(c3, c4) == 0);
65      }
66  
67      @Test
68      public void testCookieIdentityComparasionByNameAndNullDomain() {
69          final CookieIdentityComparator comparator = new CookieIdentityComparator();
70          final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
71          c1.setDomain(null);
72          final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
73          c2.setDomain(null);
74          Assert.assertTrue(comparator.compare(c1, c2) == 0);
75  
76          final BasicClientCookie c3 = new BasicClientCookie("name", "value1");
77          c3.setDomain("www.domain.com");
78          final BasicClientCookie c4 = new BasicClientCookie("name", "value2");
79          c4.setDomain(null);
80          Assert.assertFalse(comparator.compare(c3, c4) == 0);
81      }
82  
83      @Test
84      public void testCookieIdentityComparasionByNameAndLocalHost() {
85          final CookieIdentityComparator comparator = new CookieIdentityComparator();
86          final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
87          c1.setDomain("localhost");
88          final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
89          c2.setDomain("localhost");
90          Assert.assertTrue(comparator.compare(c1, c2) == 0);
91  
92          final BasicClientCookie c3 = new BasicClientCookie("name", "value1");
93          c3.setDomain("localhost.local");
94          final BasicClientCookie c4 = new BasicClientCookie("name", "value2");
95          c4.setDomain("localhost");
96          Assert.assertTrue(comparator.compare(c3, c4) == 0);
97      }
98  
99      @Test
100     public void testCookieIdentityComparasionByNameDomainAndPath() {
101         final CookieIdentityComparator comparator = new CookieIdentityComparator();
102         final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
103         c1.setDomain("www.domain.com");
104         c1.setPath("/whatever");
105         final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
106         c2.setDomain("www.domain.com");
107         c2.setPath("/whatever");
108         Assert.assertTrue(comparator.compare(c1, c2) == 0);
109 
110         final BasicClientCookie c3 = new BasicClientCookie("name", "value1");
111         c3.setDomain("www.domain.com");
112         c3.setPath("/whatever");
113         final BasicClientCookie c4 = new BasicClientCookie("name", "value2");
114         c4.setDomain("domain.com");
115         c4.setPath("/whatever-not");
116         Assert.assertFalse(comparator.compare(c3, c4) == 0);
117     }
118 
119     @Test
120     public void testCookieIdentityComparasionByNameDomainAndNullPath() {
121         final CookieIdentityComparator comparator = new CookieIdentityComparator();
122         final BasicClientCookie c1 = new BasicClientCookie("name", "value1");
123         c1.setDomain("www.domain.com");
124         c1.setPath("/");
125         final BasicClientCookie c2 = new BasicClientCookie("name", "value2");
126         c2.setDomain("www.domain.com");
127         c2.setPath(null);
128         Assert.assertTrue(comparator.compare(c1, c2) == 0);
129 
130         final BasicClientCookie c3 = new BasicClientCookie("name", "value1");
131         c3.setDomain("www.domain.com");
132         c3.setPath("/whatever");
133         final BasicClientCookie c4 = new BasicClientCookie("name", "value2");
134         c4.setDomain("domain.com");
135         c4.setPath(null);
136         Assert.assertFalse(comparator.compare(c3, c4) == 0);
137     }
138 
139 }