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