1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.fileupload;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import java.util.Map;
23  
24  /**
25   * Unit tests for {@link ParameterParser}.
26   *
27   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
28   */
29  public class ParameterParserTest extends TestCase
30  {
31  
32      // ------------------------------------------------------------ Constructor
33      public ParameterParserTest(String testName)
34      {
35          super(testName);
36      }
37  
38      // ------------------------------------------------------------------- Main
39      public static void main(String args[])
40      {
41          String[] testCaseName = { ParameterParserTest.class.getName()};
42          junit.textui.TestRunner.main(testCaseName);
43      }
44  
45      // ------------------------------------------------------- TestCase Methods
46  
47      public static Test suite()
48      {
49          return new TestSuite(ParameterParserTest.class);
50      }
51  
52      public void testParsing()
53      {
54          String s =
55              "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
56          ParameterParser parser = new ParameterParser();
57          Map params = parser.parse(s, ';');
58          assertEquals(null, params.get("test"));
59          assertEquals("stuff", params.get("test1"));
60          assertEquals("stuff; stuff", params.get("test2"));
61          assertEquals("\"stuff", params.get("test3"));
62  
63          s = "  test  , test1=stuff   ,  , test2=, test3, ";
64          params = parser.parse(s, ',');
65          assertEquals(null, params.get("test"));
66          assertEquals("stuff", params.get("test1"));
67          assertEquals(null, params.get("test2"));
68          assertEquals(null, params.get("test3"));
69  
70          s = "  test";
71          params = parser.parse(s, ';');
72          assertEquals(null, params.get("test"));
73  
74          s = "  ";
75          params = parser.parse(s, ';');
76          assertEquals(0, params.size());
77  
78          s = " = stuff ";
79          params = parser.parse(s, ';');
80          assertEquals(0, params.size());
81      }
82  
83      public void testContentTypeParsing()
84      {
85          String s = "text/plain; Charset=UTF-8";
86          ParameterParser parser = new ParameterParser();
87          parser.setLowerCaseNames(true);
88          Map params = parser.parse(s, ';');
89          assertEquals("UTF-8", params.get("charset"));
90      }
91  
92      public void testParsingEscapedChars()
93      {
94          String s = "param = \"stuff\\\"; more stuff\"";
95          ParameterParser parser = new ParameterParser();
96          Map params = parser.parse(s, ';');
97          assertEquals(1, params.size());
98          assertEquals("stuff\\\"; more stuff", params.get("param"));
99  
100         s = "param = \"stuff\\\\\"; anotherparam";
101         params = parser.parse(s, ';');
102         assertEquals(2, params.size());
103         assertEquals("stuff\\\\", params.get("param"));
104         assertNull(params.get("anotherparam"));
105     }
106 }