1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestParameterParser.java $
3    * $Revision$
4    * $Date$
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   */
29  
30  package org.apache.commons.httpclient;
31  
32  import junit.framework.Test;
33  import junit.framework.TestCase;
34  import junit.framework.TestSuite;
35  
36  import java.util.List;
37  
38  import org.apache.commons.httpclient.util.ParameterParser;
39  
40  /***
41   * Unit tests for {@link ParameterParser}.
42   *
43   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
44   */
45  public class TestParameterParser extends TestCase {
46  
47      // ------------------------------------------------------------ Constructor
48      public TestParameterParser(String testName) {
49          super(testName);
50      }
51  
52      // ------------------------------------------------------------------- Main
53      public static void main(String args[]) {
54          String[] testCaseName = { TestParameterParser.class.getName() };
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      // ------------------------------------------------------- TestCase Methods
59  
60      public static Test suite() {
61          return new TestSuite(TestParameterParser.class);
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("", ((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(1, params.size());
101         assertEquals("", ((NameValuePair)params.get(0)).getName());
102         assertEquals("stuff", ((NameValuePair)params.get(0)).getValue());
103     }
104     
105     public void testParsingEscapedChars() {
106         String s = "param = \"stuff//\"; more stuff\"";
107         ParameterParser parser = new ParameterParser();
108         List params = parser.parse(s, ';');
109         assertEquals(1, params.size());
110         assertEquals("param", 
111                 ((NameValuePair)params.get(0)).getName());
112         assertEquals("stuff//\"; more stuff", 
113                 ((NameValuePair)params.get(0)).getValue());
114 
115         s = "param = \"stuff////\"; anotherparam";
116         params = parser.parse(s, ';');
117         assertEquals(2, params.size());
118         assertEquals("param", 
119                 ((NameValuePair)params.get(0)).getName());
120         assertEquals("stuff////", 
121                 ((NameValuePair)params.get(0)).getValue());
122         assertEquals("anotherparam", 
123                 ((NameValuePair)params.get(1)).getName());
124         assertNull(
125                 ((NameValuePair)params.get(1)).getValue());
126     }
127     
128     public void testParsingBlankParams() {
129         String s =  "test; test1 =  ; test2 = \"\"";
130         ParameterParser  parser = new ParameterParser();
131         List params = parser.parse(s, ';');
132         assertEquals("test", ((NameValuePair)params.get(0)).getName());
133         assertEquals(null, ((NameValuePair)params.get(0)).getValue());
134         assertEquals("test1", ((NameValuePair)params.get(1)).getName());
135         assertEquals("", ((NameValuePair)params.get(1)).getValue());
136         assertEquals("test2", ((NameValuePair)params.get(2)).getName());
137         assertEquals("", ((NameValuePair)params.get(2)).getValue());
138     }
139 }