01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: // ------------------------------------------------------------ Constructor
32: public ParameterParserTest(String testName) {
33: super (testName);
34: }
35:
36: // ------------------------------------------------------------------- Main
37: public static void main(String args[]) {
38: String[] testCaseName = { ParameterParserTest.class.getName() };
39: junit.textui.TestRunner.main(testCaseName);
40: }
41:
42: // ------------------------------------------------------- TestCase Methods
43:
44: public static Test suite() {
45: return new TestSuite(ParameterParserTest.class);
46: }
47:
48: public void testParsing() {
49: String s = "test; test1 = stuff ; test2 = \"stuff; stuff\"; test3=\"stuff";
50: ParameterParser parser = new ParameterParser();
51: Map params = parser.parse(s, ';');
52: assertEquals(null, params.get("test"));
53: assertEquals("stuff", params.get("test1"));
54: assertEquals("stuff; stuff", params.get("test2"));
55: assertEquals("\"stuff", params.get("test3"));
56:
57: s = " test , test1=stuff , , test2=, test3, ";
58: params = parser.parse(s, ',');
59: assertEquals(null, params.get("test"));
60: assertEquals("stuff", params.get("test1"));
61: assertEquals(null, params.get("test2"));
62: assertEquals(null, params.get("test3"));
63:
64: s = " test";
65: params = parser.parse(s, ';');
66: assertEquals(null, params.get("test"));
67:
68: s = " ";
69: params = parser.parse(s, ';');
70: assertEquals(0, params.size());
71:
72: s = " = stuff ";
73: params = parser.parse(s, ';');
74: assertEquals(0, params.size());
75: }
76:
77: public void testContentTypeParsing() {
78: String s = "text/plain; Charset=UTF-8";
79: ParameterParser parser = new ParameterParser();
80: parser.setLowerCaseNames(true);
81: Map params = parser.parse(s, ';');
82: assertEquals("UTF-8", params.get("charset"));
83: }
84:
85: public void testParsingEscapedChars() {
86: String s = "param = \"stuff\\\"; more stuff\"";
87: ParameterParser parser = new ParameterParser();
88: Map params = parser.parse(s, ';');
89: assertEquals(1, params.size());
90: assertEquals("stuff\\\"; more stuff", params.get("param"));
91:
92: s = "param = \"stuff\\\\\"; anotherparam";
93: params = parser.parse(s, ';');
94: assertEquals(2, params.size());
95: assertEquals("stuff\\\\", params.get("param"));
96: assertNull(params.get("anotherparam"));
97: }
98: }
|