1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestParameterParser.java $
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   */
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(0, params.size());
100     }
101     
102     public void testParsingEscapedChars() {
103         String s = "param = \"stuff//\"; more stuff\"";
104         ParameterParser parser = new ParameterParser();
105         List params = parser.parse(s, ';');
106         assertEquals(1, params.size());
107         assertEquals("param", 
108                 ((NameValuePair)params.get(0)).getName());
109         assertEquals("stuff//\"; more stuff", 
110                 ((NameValuePair)params.get(0)).getValue());
111 
112         s = "param = \"stuff////\"; anotherparam";
113         params = parser.parse(s, ';');
114         assertEquals(2, params.size());
115         assertEquals("param", 
116                 ((NameValuePair)params.get(0)).getName());
117         assertEquals("stuff////", 
118                 ((NameValuePair)params.get(0)).getValue());
119         assertEquals("anotherparam", 
120                 ((NameValuePair)params.get(1)).getName());
121         assertNull(
122                 ((NameValuePair)params.get(1)).getValue());
123     }
124     
125     public void testParsingBlankParams() {
126         String s =  "test; test1 =  ; test2 = \"\"";
127         ParameterParser  parser = new ParameterParser();
128         List params = parser.parse(s, ';');
129         assertEquals("test", ((NameValuePair)params.get(0)).getName());
130         assertEquals(null, ((NameValuePair)params.get(0)).getValue());
131         assertEquals("test1", ((NameValuePair)params.get(1)).getName());
132         assertEquals("", ((NameValuePair)params.get(1)).getValue());
133         assertEquals("test2", ((NameValuePair)params.get(2)).getName());
134         assertEquals("", ((NameValuePair)params.get(2)).getValue());
135     }
136 }