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  
28  package org.apache.http.cookie;
29  
30  import java.util.Comparator;
31  
32  import org.apache.http.impl.cookie.BasicClientCookie;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  /**
37   * Test cases for {@link CookiePathComparator}.
38   */
39  public class TestCookiePathComparator {
40  
41      @Test
42      public void testUnequality1() {
43          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
44          cookie1.setPath("/a/b/");
45          final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
46          cookie2.setPath("/a/");
47          final Comparator<Cookie> comparator = new CookiePathComparator();
48          Assert.assertTrue(comparator.compare(cookie1, cookie2) < 0);
49          Assert.assertTrue(comparator.compare(cookie2, cookie1) > 0);
50      }
51  
52      @Test
53      public void testUnequality2() {
54          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
55          cookie1.setPath("/a/b");
56          final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
57          cookie2.setPath("/a");
58          final Comparator<Cookie> comparator = new CookiePathComparator();
59          Assert.assertTrue(comparator.compare(cookie1, cookie2) < 0);
60          Assert.assertTrue(comparator.compare(cookie2, cookie1) > 0);
61      }
62  
63      @Test
64      public void testEquality1() {
65          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
66          cookie1.setPath("/a");
67          final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
68          cookie2.setPath("/a");
69          final Comparator<Cookie> comparator = new CookiePathComparator();
70          Assert.assertTrue(comparator.compare(cookie1, cookie2) == 0);
71          Assert.assertTrue(comparator.compare(cookie2, cookie1) == 0);
72      }
73  
74      @Test
75      public void testEquality2() {
76          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
77          cookie1.setPath("/a/");
78          final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
79          cookie2.setPath("/a");
80          final Comparator<Cookie> comparator = new CookiePathComparator();
81          Assert.assertTrue(comparator.compare(cookie1, cookie2) == 0);
82          Assert.assertTrue(comparator.compare(cookie2, cookie1) == 0);
83      }
84  
85      @Test
86      public void testEquality3() {
87          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
88          cookie1.setPath(null);
89          final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
90          cookie2.setPath("/");
91          final Comparator<Cookie> comparator = new CookiePathComparator();
92          Assert.assertTrue(comparator.compare(cookie1, cookie2) == 0);
93          Assert.assertTrue(comparator.compare(cookie2, cookie1) == 0);
94      }
95  
96      @Test
97      public void testEquality4() {
98          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
99          cookie1.setPath("/this");
100         final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
101         cookie2.setPath("/that");
102         final Comparator<Cookie> comparator = new CookiePathComparator();
103         Assert.assertTrue(comparator.compare(cookie1, cookie2) == 0);
104         Assert.assertTrue(comparator.compare(cookie2, cookie1) == 0);
105     }
106 
107 }
108