1   /*
2    * ====================================================================
3    *
4    *  Copyright 1999-2004 The Apache Software Foundation
5    *
6    *  Licensed under the Apache License, Version 2.0 (the "License");
7    *  you may not use this file except in compliance with the License.
8    *  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   * ====================================================================
18   *
19   * This software consists of voluntary contributions made by many
20   * individuals on behalf of the Apache Software Foundation.  For more
21   * information on the Apache Software Foundation, please see
22   * <http://www.apache.org/>.
23   *
24   * [Additional notices, if required by prior licensing conditions]
25   *
26   */
27  
28  package org.apache.commons.httpclient;
29  
30  import junit.framework.Test;
31  import junit.framework.TestCase;
32  import junit.framework.TestSuite;
33  
34  import java.util.List;
35  
36  import org.apache.commons.httpclient.util.ParameterParser;
37  
38  /***
39   * Unit tests for {@link ParameterParser}.
40   *
41   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
42   */
43  public class TestParameterParser extends TestCase {
44  
45      // ------------------------------------------------------------ Constructor
46      public TestParameterParser(String testName) {
47          super(testName);
48      }
49  
50      // ------------------------------------------------------------------- Main
51      public static void main(String args[]) {
52          String[] testCaseName = { TestParameterParser.class.getName() };
53          junit.textui.TestRunner.main(testCaseName);
54      }
55  
56      // ------------------------------------------------------- TestCase Methods
57  
58      public static Test suite() {
59          return new TestSuite(TestParameterParser.class);
60      }
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(null, ((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 }