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.hc.client5.http.cookie;
29  
30  import java.time.Instant;
31  import java.util.Comparator;
32  
33  import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Test cases for {@link org.apache.hc.client5.http.cookie.CookiePriorityComparator}.
40   */
41  public class TestCookiePriorityComparator {
42  
43      private Comparator<Cookie> comparator;
44  
45      @BeforeEach
46      public void setup() {
47          comparator = CookiePriorityComparator.INSTANCE;
48      }
49  
50      @Test
51      public void testUnequality() {
52          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
53          cookie1.setPath("/a/b/");
54          final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
55          cookie2.setPath("/a/");
56          Assertions.assertTrue(comparator.compare(cookie1, cookie2) < 0);
57          Assertions.assertTrue(comparator.compare(cookie2, cookie1) > 0);
58      }
59  
60      @Test
61      public void testEquality() {
62          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
63          cookie1.setPath("/a");
64          final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
65          cookie2.setPath("/a");
66          Assertions.assertEquals(0, comparator.compare(cookie1, cookie2));
67          Assertions.assertEquals(0, comparator.compare(cookie2, cookie1));
68      }
69  
70      @Test
71      public void testUnequalityTrailingSlash() {
72          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
73          cookie1.setPath("/a/");
74          final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
75          cookie2.setPath("/a");
76          Assertions.assertTrue(comparator.compare(cookie1, cookie2) < 0);
77          Assertions.assertTrue(comparator.compare(cookie2, cookie1) > 0);
78      }
79  
80      @Test
81      public void testEqualityNullPath() {
82          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
83          cookie1.setPath(null);
84          final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
85          cookie2.setPath("/");
86          Assertions.assertEquals(0, comparator.compare(cookie1, cookie2));
87          Assertions.assertEquals(0, comparator.compare(cookie2, cookie1));
88      }
89  
90      @Test
91      public void testEqualitySameLength() {
92          final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
93          cookie1.setPath("/this");
94          final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
95          cookie2.setPath("/that");
96          Assertions.assertEquals(0, comparator.compare(cookie1, cookie2));
97          Assertions.assertEquals(0, comparator.compare(cookie2, cookie1));
98      }
99  
100     @Test
101     public void testUnequalityCreationDate() {
102         final BasicClientCookie cookie1 = new BasicClientCookie("name1", "value");
103         cookie1.setPath("/blah");
104         cookie1.setCreationDate(Instant.now().minusMillis(200000));
105         final BasicClientCookie cookie2 = new BasicClientCookie("name1", "value");
106         cookie2.setPath("/blah");
107         cookie2.setCreationDate(Instant.now());
108         Assertions.assertTrue(comparator.compare(cookie1, cookie2) < 0);
109         Assertions.assertTrue(comparator.compare(cookie2, cookie1) > 0);
110     }
111 
112 }
113