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.message;
29  
30  import org.apache.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 testHashCode() {
59          final NameValuePair param1 = new BasicNameValuePair("name1", "value1");
60          final NameValuePair param2 = new BasicNameValuePair("name2", "value2");
61          final NameValuePair param3 = new BasicNameValuePair("name1", "value1");
62          Assert.assertTrue(param1.hashCode() != param2.hashCode());
63          Assert.assertTrue(param1.hashCode() == param3.hashCode());
64      }
65  
66      @Test
67      public void testEquals() {
68          final NameValuePair param1 = new BasicNameValuePair("name1", "value1");
69          final NameValuePair param2 = new BasicNameValuePair("name2", "value2");
70          final NameValuePair param3 = new BasicNameValuePair("name1", "value1");
71          Assert.assertFalse(param1.equals(param2));
72          Assert.assertFalse(param1.equals(null));
73          Assert.assertFalse(param1.equals("name1 = value1"));
74          Assert.assertTrue(param1.equals(param1));
75          Assert.assertTrue(param2.equals(param2));
76          Assert.assertTrue(param1.equals(param3));
77      }
78  
79      @Test
80      public void testToString() {
81          final NameValuePair param1 = new BasicNameValuePair("name1", "value1");
82          Assert.assertEquals("name1=value1", param1.toString());
83          final NameValuePair param2 = new BasicNameValuePair("name1", null);
84          Assert.assertEquals("name1", param2.toString());
85      }
86  
87      @Test
88      public void testCloning() throws Exception {
89          final BasicNameValuePair orig = new BasicNameValuePair("name1", "value1");
90          final BasicNameValuePair clone = (BasicNameValuePair) orig.clone();
91          Assert.assertEquals(orig, clone);
92      }
93  
94  }