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.core5.http.message;
29  
30  import org.apache.hc.core5.http.NameValuePair;
31  import org.junit.jupiter.api.Assertions;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Unit tests for {@link NameValuePair}.
36   *
37   */
38  public class TestNameValuePair {
39  
40      @Test
41      public void testConstructor() {
42          final NameValuePair param = new BasicNameValuePair("name", "value");
43          Assertions.assertEquals("name", param.getName());
44          Assertions.assertEquals("value", param.getValue());
45      }
46  
47      @Test
48      public void testInvalidName() {
49          Assertions.assertThrows(NullPointerException.class, () -> new BasicNameValuePair(null, null));
50      }
51  
52      @Test
53      public void testToString() {
54          final NameValuePair param1 = new BasicNameValuePair("name1", "value1");
55          Assertions.assertEquals("name1=value1", param1.toString());
56          final NameValuePair param2 = new BasicNameValuePair("name1", null);
57          Assertions.assertEquals("name1", param2.toString());
58      }
59  
60      @Test
61      public void testNullNotEqual() throws Exception {
62          final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
63  
64          Assertions.assertNotEquals(null, NameValuePair);
65      }
66  
67      @Test
68      public void testObjectNotEqual() throws Exception {
69          final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
70  
71          Assertions.assertNotEquals(NameValuePair, new Object());
72      }
73  
74      @Test
75      public void testNameEquals() throws Exception {
76          final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
77          final NameValuePair NameValuePair2 = new BasicNameValuePair("NAME", "value");
78  
79          Assertions.assertEquals(NameValuePair, NameValuePair2);
80          Assertions.assertEquals(NameValuePair.hashCode(), NameValuePair2.hashCode());
81  
82          final NameValuePair NameValuePair3 = new BasicNameValuePair("NAME", "value");
83          final NameValuePair NameValuePair4 = new BasicNameValuePair("name", "value");
84  
85          Assertions.assertEquals(NameValuePair3, NameValuePair4);
86          Assertions.assertEquals(NameValuePair3.hashCode(), NameValuePair4.hashCode());
87      }
88  
89      @Test
90      public void testValueEquals() throws Exception {
91          final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
92          final NameValuePair NameValuePair2 = new BasicNameValuePair("name", "value");
93  
94          Assertions.assertEquals(NameValuePair, NameValuePair2);
95          Assertions.assertEquals(NameValuePair.hashCode(), NameValuePair2.hashCode());
96      }
97  
98      @Test
99      public void testNameNotEqual() throws Exception {
100         final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
101         final NameValuePair NameValuePair2 = new BasicNameValuePair("name2", "value");
102 
103         Assertions.assertNotEquals(NameValuePair, NameValuePair2);
104     }
105 
106     @Test
107     public void testValueNotEqual() throws Exception {
108         final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
109         final NameValuePair NameValuePair2 = new BasicNameValuePair("name", "value2");
110 
111         Assertions.assertNotEquals(NameValuePair, NameValuePair2);
112 
113         final NameValuePair NameValuePair3 = new BasicNameValuePair("name", "value");
114         final NameValuePair NameValuePair4 = new BasicNameValuePair("name", "VALUE");
115 
116         Assertions.assertNotEquals(NameValuePair3, NameValuePair4);
117 
118         final NameValuePair NameValuePair5 = new BasicNameValuePair("name", "VALUE");
119         final NameValuePair NameValuePair6 = new BasicNameValuePair("name", "value");
120 
121         Assertions.assertNotEquals(NameValuePair5, NameValuePair6);
122     }
123 
124     @Test
125     public void testNullValuesEquals() throws Exception {
126         final NameValuePair NameValuePair = new BasicNameValuePair("name", null);
127         final NameValuePair NameValuePair2 = new BasicNameValuePair("name", null);
128 
129         Assertions.assertEquals(NameValuePair, NameValuePair2);
130         Assertions.assertEquals(NameValuePair.hashCode(), NameValuePair2.hashCode());
131     }
132 
133     @Test
134     public void testNullValueNotEqual() throws Exception {
135         final NameValuePair NameValuePair = new BasicNameValuePair("name", null);
136         final NameValuePair NameValuePair2 = new BasicNameValuePair("name", "value");
137 
138         Assertions.assertNotEquals(NameValuePair, NameValuePair2);
139     }
140 
141     @Test
142     public void testNullValue2NotEqual() throws Exception {
143         final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
144         final NameValuePair NameValuePair2 = new BasicNameValuePair("name", null);
145 
146         Assertions.assertNotEquals(NameValuePair, NameValuePair2);
147     }
148 
149     @Test
150     public void testEquals() throws Exception {
151         final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
152         final NameValuePair NameValuePair2 = new BasicNameValuePair("name", "value");
153 
154         Assertions.assertEquals(NameValuePair, NameValuePair);
155         Assertions.assertEquals(NameValuePair.hashCode(), NameValuePair.hashCode());
156         Assertions.assertEquals(NameValuePair2, NameValuePair2);
157         Assertions.assertEquals(NameValuePair2.hashCode(), NameValuePair2.hashCode());
158         Assertions.assertEquals(NameValuePair, NameValuePair2);
159         Assertions.assertEquals(NameValuePair.hashCode(), NameValuePair2.hashCode());
160     }
161 
162     @Test
163     public void testHashCode() throws Exception {
164         final NameValuePair NameValuePair = new BasicNameValuePair("name", null);
165         final NameValuePair NameValuePair2 = new BasicNameValuePair("name2", null);
166 
167         Assertions.assertNotEquals(NameValuePair.hashCode(), NameValuePair2.hashCode());
168 
169         final NameValuePair NameValuePair3 = new BasicNameValuePair("name", "value");
170         final NameValuePair NameValuePair4 = new BasicNameValuePair("name", "value2");
171 
172         Assertions.assertNotEquals(NameValuePair3.hashCode(), NameValuePair4.hashCode());
173 
174         final NameValuePair NameValuePair5 = new BasicNameValuePair("name", "value");
175         final NameValuePair NameValuePair6 = new BasicNameValuePair("name", null);
176 
177         Assertions.assertNotEquals(NameValuePair5.hashCode(), NameValuePair6.hashCode());
178     }
179 
180 }