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