1   /*
2    * $HeadURL: http://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestParameterParser.java $
3    * $Revision: 345737 $
4    * $Date: 2005-11-20 07:30:47 -0500 (Sun, 20 Nov 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      public void testParsing() {
64          String s = 
65            "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
66          ParameterParser  parser = new ParameterParser();
67          List params = parser.parse(s, ';');
68          assertEquals("test", ((NameValuePair)params.get(0)).getName());
69          assertEquals(null, ((NameValuePair)params.get(0)).getValue());
70          assertEquals("test1", ((NameValuePair)params.get(1)).getName());
71          assertEquals("stuff", ((NameValuePair)params.get(1)).getValue());
72          assertEquals("test2", ((NameValuePair)params.get(2)).getName());
73          assertEquals("stuff; stuff", ((NameValuePair)params.get(2)).getValue());
74          assertEquals("test3", ((NameValuePair)params.get(3)).getName());
75          assertEquals("\"stuff", ((NameValuePair)params.get(3)).getValue());
76  
77          s = "  test  , test1=stuff   ,  , test2=, test3, ";
78          params = parser.parse(s, ',');
79          assertEquals("test", ((NameValuePair)params.get(0)).getName());
80          assertEquals(null, ((NameValuePair)params.get(0)).getValue());
81          assertEquals("test1", ((NameValuePair)params.get(1)).getName());
82          assertEquals("stuff", ((NameValuePair)params.get(1)).getValue());
83          assertEquals("test2", ((NameValuePair)params.get(2)).getName());
84          assertEquals("", ((NameValuePair)params.get(2)).getValue());
85          assertEquals("test3", ((NameValuePair)params.get(3)).getName());
86          assertEquals(null, ((NameValuePair)params.get(3)).getValue());
87  
88          s = "  test";
89          params = parser.parse(s, ';');
90          assertEquals("test", ((NameValuePair)params.get(0)).getName());
91          assertEquals(null, ((NameValuePair)params.get(0)).getValue());
92  
93          s = "  ";
94          params = parser.parse(s, ';');
95          assertEquals(0, params.size());
96  
97          s = " = stuff ";
98          params = parser.parse(s, ';');
99          assertEquals(1, params.size());
100         assertEquals("", ((NameValuePair)params.get(0)).getName());
101         assertEquals("stuff", ((NameValuePair)params.get(0)).getValue());
102     }
103     
104     public void testParsingEscapedChars() {
105         String s = "param = \"stuff//\"; more stuff\"";
106         ParameterParser parser = new ParameterParser();
107         List params = parser.parse(s, ';');
108         assertEquals(1, params.size());
109         assertEquals("param", 
110                 ((NameValuePair)params.get(0)).getName());
111         assertEquals("stuff//\"; more stuff", 
112                 ((NameValuePair)params.get(0)).getValue());
113 
114         s = "param = \"stuff////\"; anotherparam";
115         params = parser.parse(s, ';');
116         assertEquals(2, params.size());
117         assertEquals("param", 
118                 ((NameValuePair)params.get(0)).getName());
119         assertEquals("stuff////", 
120                 ((NameValuePair)params.get(0)).getValue());
121         assertEquals("anotherparam", 
122                 ((NameValuePair)params.get(1)).getName());
123         assertNull(
124                 ((NameValuePair)params.get(1)).getValue());
125     }
126     
127     public void testParsingBlankParams() {
128         String s =  "test; test1 =  ; test2 = \"\"";
129         ParameterParser  parser = new ParameterParser();
130         List params = parser.parse(s, ';');
131         assertEquals("test", ((NameValuePair)params.get(0)).getName());
132         assertEquals(null, ((NameValuePair)params.get(0)).getValue());
133         assertEquals("test1", ((NameValuePair)params.get(1)).getName());
134         assertEquals("", ((NameValuePair)params.get(1)).getValue());
135         assertEquals("test2", ((NameValuePair)params.get(2)).getName());
136         assertEquals("", ((NameValuePair)params.get(2)).getValue());
137     }
138 }