1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestHeaderElement.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 18:29:41 +0000 (Sat, 22 Dec 2012) $
5    * ====================================================================
6    *
7    *  Licensed to the Apache Software Foundation (ASF) under one or more
8    *  contributor license agreements.  See the NOTICE file distributed with
9    *  this work for additional information regarding copyright ownership.
10   *  The ASF licenses this file to You under the Apache License, Version 2.0
11   *  (the "License"); you may not use this file except in compliance with
12   *  the License.  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient;
33  
34  import junit.framework.*;
35  
36  /***
37   * Simple tests for {@link HeaderElement}.
38   *
39   * @author Rodney Waldhoff
40   * @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
41   * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
42   * @author <a href="mailto:oleg@ural.ru">oleg Kalnichevski</a>
43   * @version $Id: TestHeaderElement.java 608014 2008-01-02 05:48:53Z rolandw $
44   */
45  public class TestHeaderElement extends TestNVP {
46  
47      // ------------------------------------------------------------ Constructor
48      public TestHeaderElement(String testName) {
49          super(testName);
50      }
51  
52      // ------------------------------------------------------------------- Main
53      public static void main(String args[]) {
54          String[] testCaseName = { TestHeaderElement.class.getName() };
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      // ------------------------------------------------------- TestCase Methods
59  
60      public static Test suite() {
61          return new TestSuite(TestHeaderElement.class);
62      }
63  
64      // ------------------------------------------------------ Protected Methods
65  
66      protected NameValuePair makePair() {
67          return new HeaderElement();
68      }
69  
70      protected NameValuePair makePair(String name, String value) {
71          return new HeaderElement(name,value);
72      }
73  
74  
75      // ----------------------------------------------------------- Test Methods
76  
77      public void testOldMain() throws Exception {
78          // this is derived from the old main method in HeaderElement
79          String headerValue = "name1 = value1; name2; name3=\"value3\" , name4=value4; " +
80              "name5=value5, name6= ; name7 = value7; name8 = \" value8\"";
81          HeaderElement[] elements = HeaderElement.parseElements(headerValue);
82          // there are 3 elements
83          assertEquals(3,elements.length);
84          // 1st element
85          assertEquals("name1",elements[0].getName());
86          assertEquals("value1",elements[0].getValue());
87          // 1st element has 2 getParameters()
88          assertEquals(2,elements[0].getParameters().length);
89          assertEquals("name2",elements[0].getParameters()[0].getName());
90          assertEquals(null, elements[0].getParameters()[0].getValue());
91          assertEquals("name3",elements[0].getParameters()[1].getName());
92          assertEquals("value3",elements[0].getParameters()[1].getValue());
93          // 2nd element
94          assertEquals("name4",elements[1].getName());
95          assertEquals("value4",elements[1].getValue());
96          // 2nd element has 1 parameter
97          assertEquals(1,elements[1].getParameters().length);
98          assertEquals("name5",elements[1].getParameters()[0].getName());
99          assertEquals("value5",elements[1].getParameters()[0].getValue());
100         // 3rd element
101         assertEquals("name6",elements[2].getName());
102         assertEquals("",elements[2].getValue());
103         // 3rd element has 2 getParameters()
104         assertEquals(2,elements[2].getParameters().length);
105         assertEquals("name7",elements[2].getParameters()[0].getName());
106         assertEquals("value7",elements[2].getParameters()[0].getValue());
107         assertEquals("name8",elements[2].getParameters()[1].getName());
108         assertEquals(" value8",elements[2].getParameters()[1].getValue());
109     }
110 
111     public void testFringeCase1() throws Exception {
112         String headerValue = "name1 = value1,";
113         HeaderElement[] elements = HeaderElement.parseElements(headerValue);
114         assertEquals("Number of elements", 1, elements.length);
115     }
116 
117 
118     public void testFringeCase2() throws Exception {
119         String headerValue = "name1 = value1, ";
120         HeaderElement[] elements = HeaderElement.parseElements(headerValue);
121         assertEquals("Number of elements", 1, elements.length);
122     }
123 
124 
125     public void testFringeCase3() throws Exception {
126         String headerValue = ",, ,, ,";
127         HeaderElement[] elements = HeaderElement.parseElements(headerValue);
128         assertEquals("Number of elements", 0, elements.length);
129     }
130 }