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.hc.client5.http.validator;
28  
29  import org.junit.jupiter.api.Assertions;
30  import org.junit.jupiter.api.Test;
31  
32  public class TestETag {
33  
34      @Test
35      public void testHashCodeEquals() {
36          final ETag tag1 = new ETag("this");
37          final ETag tag2 = new ETag("this");
38          final ETag tag3 = new ETag("this", ValidatorType.WEAK);
39          final ETag tag4 = new ETag("that", ValidatorType.WEAK);
40  
41          Assertions.assertEquals(tag1.hashCode(), tag2.hashCode());
42          Assertions.assertNotEquals(tag2.hashCode(), tag3.hashCode());
43          Assertions.assertNotEquals(tag3.hashCode(), tag4.hashCode());
44  
45          Assertions.assertEquals(tag1, tag2);
46          Assertions.assertNotEquals(tag2, tag3);
47          Assertions.assertNotEquals(tag3, tag4);
48      }
49  
50      @Test
51      public void testToString() {
52          Assertions.assertEquals("\"blah\"", new ETag("blah").toString());
53          Assertions.assertEquals("W/\"blah\"", new ETag("blah", ValidatorType.WEAK).toString());
54          Assertions.assertEquals("\"\"", new ETag("").toString());
55      }
56  
57      @Test
58      public void testParse() {
59          Assertions.assertEquals(new ETag("blah", ValidatorType.WEAK), ETag.parse("  W/\"blah\"  "));
60          Assertions.assertEquals(new ETag(" huh?"), ETag.parse("  \" huh?\"   "));
61          Assertions.assertEquals(new ETag(" ", ValidatorType.WEAK), ETag.parse("W/\" \""));
62          Assertions.assertEquals(new ETag(""), ETag.parse("\"\""));
63          Assertions.assertNull(ETag.parse("wrong"));
64          Assertions.assertNull(ETag.parse("w/\"wrong\""));
65          Assertions.assertNull(ETag.parse("W /\"wrong\""));
66          Assertions.assertNull(ETag.parse("W/ \"wrong\""));
67          Assertions.assertNull(ETag.parse("W/wrong"));
68          Assertions.assertNull(ETag.parse("\"cut"));
69          Assertions.assertNull(ETag.parse("    \""));
70          Assertions.assertNull(ETag.parse("W/\""));
71      }
72  
73      @Test
74      public void testComparison() {
75          Assertions.assertFalse(ETag.strongCompare(new ETag("1", ValidatorType.WEAK), new ETag("1", ValidatorType.WEAK)));
76          Assertions.assertTrue(ETag.weakCompare(new ETag("1", ValidatorType.WEAK), new ETag("1", ValidatorType.WEAK)));
77          Assertions.assertFalse(ETag.strongCompare(new ETag("1", ValidatorType.WEAK), new ETag("2", ValidatorType.WEAK)));
78          Assertions.assertFalse(ETag.weakCompare(new ETag("1", ValidatorType.WEAK), new ETag("2", ValidatorType.WEAK)));
79          Assertions.assertFalse(ETag.strongCompare(new ETag("1", ValidatorType.WEAK), new ETag("1")));
80          Assertions.assertTrue(ETag.weakCompare(new ETag("1", ValidatorType.WEAK), new ETag("1")));
81          Assertions.assertTrue(ETag.strongCompare(new ETag("1"), new ETag("1")));
82          Assertions.assertTrue(ETag.weakCompare(new ETag("1"), new ETag("1")));
83  
84          Assertions.assertFalse(ETag.weakCompare(new ETag("1", ValidatorType.WEAK), null));
85          Assertions.assertFalse(ETag.weakCompare(null, new ETag("1", ValidatorType.WEAK)));
86          Assertions.assertFalse(ETag.weakCompare(null, null));
87  
88          Assertions.assertFalse(ETag.strongCompare(new ETag("1"), null));
89          Assertions.assertFalse(ETag.strongCompare(null, new ETag("1")));
90          Assertions.assertFalse(ETag.strongCompare(null, null));
91      }
92  
93  }