1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestParameterParser.java $
3    * $Revision: 178662 $
4    * $Date: 2005-05-26 14:21:14 -0400 (Thu, 26 May 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   */
28  
29  package org.apache.commons.httpclient;
30  
31  import junit.framework.Test;
32  import junit.framework.TestCase;
33  import junit.framework.TestSuite;
34  
35  import java.util.List;
36  
37  import org.apache.commons.httpclient.util.ParameterParser;
38  
39  /***
40   * Unit tests for {@link ParameterParser}.
41   *
42   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
43   */
44  public class TestParameterParser extends TestCase {
45  
46      // ------------------------------------------------------------ Constructor
47      public TestParameterParser(String testName) {
48          super(testName);
49      }
50  
51      // ------------------------------------------------------------------- Main
52      public static void main(String args[]) {
53          String[] testCaseName = { TestParameterParser.class.getName() };
54          junit.textui.TestRunner.main(testCaseName);
55      }
56  
57      // ------------------------------------------------------- TestCase Methods
58  
59      public static Test suite() {
60          return new TestSuite(TestParameterParser.class);
61      }
62  
63  
64      public void testParsing() {
65          String s = 
66            "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
67          ParameterParser  parser = new ParameterParser();
68          List params = parser.parse(s, ';');
69          assertEquals("test", ((NameValuePair)params.get(0)).getName());
70          assertEquals(null, ((NameValuePair)params.get(0)).getValue());
71          assertEquals("test1", ((NameValuePair)params.get(1)).getName());
72          assertEquals("stuff", ((NameValuePair)params.get(1)).getValue());
73          assertEquals("test2", ((NameValuePair)params.get(2)).getName());
74          assertEquals("stuff; stuff", ((NameValuePair)params.get(2)).getValue());
75          assertEquals("test3", ((NameValuePair)params.get(3)).getName());
76          assertEquals("\"stuff", ((NameValuePair)params.get(3)).getValue());
77  
78          s = "  test  , test1=stuff   ,  , test2=, test3, ";
79          params = parser.parse(s, ',');
80          assertEquals("test", ((NameValuePair)params.get(0)).getName());
81          assertEquals(null, ((NameValuePair)params.get(0)).getValue());
82          assertEquals("test1", ((NameValuePair)params.get(1)).getName());
83          assertEquals("stuff", ((NameValuePair)params.get(1)).getValue());
84          assertEquals("test2", ((NameValuePair)params.get(2)).getName());
85          assertEquals(null, ((NameValuePair)params.get(2)).getValue());
86          assertEquals("test3", ((NameValuePair)params.get(3)).getName());
87          assertEquals(null, ((NameValuePair)params.get(3)).getValue());
88  
89          s = "  test";
90          params = parser.parse(s, ';');
91          assertEquals("test", ((NameValuePair)params.get(0)).getName());
92          assertEquals(null, ((NameValuePair)params.get(0)).getValue());
93  
94          s = "  ";
95          params = parser.parse(s, ';');
96          assertEquals(0, params.size());
97  
98          s = " = stuff ";
99          params = parser.parse(s, ';');
100         assertEquals(0, params.size());
101     }
102     
103     public void testParsingEscapedChars() {
104         String s = "param = \"stuff//\"; more stuff\"";
105         ParameterParser parser = new ParameterParser();
106         List params = parser.parse(s, ';');
107         assertEquals(1, params.size());
108         assertEquals("param", 
109                 ((NameValuePair)params.get(0)).getName());
110         assertEquals("stuff//\"; more stuff", 
111                 ((NameValuePair)params.get(0)).getValue());
112 
113         s = "param = \"stuff////\"; anotherparam";
114         params = parser.parse(s, ';');
115         assertEquals(2, params.size());
116         assertEquals("param", 
117                 ((NameValuePair)params.get(0)).getName());
118         assertEquals("stuff////", 
119                 ((NameValuePair)params.get(0)).getValue());
120         assertEquals("anotherparam", 
121                 ((NameValuePair)params.get(1)).getName());
122         assertNull(
123                 ((NameValuePair)params.get(1)).getValue());
124     }    
125 }