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