1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient;
31
32 import junit.framework.Test;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35
36 import java.util.List;
37
38 import org.apache.commons.httpclient.util.ParameterParser;
39
40 /***
41 * Unit tests for {@link ParameterParser}.
42 *
43 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
44 */
45 public class TestParameterParser extends TestCase {
46
47
48 public TestParameterParser(String testName) {
49 super(testName);
50 }
51
52
53 public static void main(String args[]) {
54 String[] testCaseName = { TestParameterParser.class.getName() };
55 junit.textui.TestRunner.main(testCaseName);
56 }
57
58
59
60 public static Test suite() {
61 return new TestSuite(TestParameterParser.class);
62 }
63
64 public void testParsing() {
65 String s =
66 "test; test1 = stuff ; test2 = \"stuff; stuff\"; test3=\"stuff";
67 ParameterParser parser = new ParameterParser();
68 List params = parser.parse(s, ';');
69 assertEquals("test", ((NameValuePair)params.get(0)).getName());
70 assertEquals(null, ((NameValuePair)params.get(0)).getValue());
71 assertEquals("test1", ((NameValuePair)params.get(1)).getName());
72 assertEquals("stuff", ((NameValuePair)params.get(1)).getValue());
73 assertEquals("test2", ((NameValuePair)params.get(2)).getName());
74 assertEquals("stuff; stuff", ((NameValuePair)params.get(2)).getValue());
75 assertEquals("test3", ((NameValuePair)params.get(3)).getName());
76 assertEquals("\"stuff", ((NameValuePair)params.get(3)).getValue());
77
78 s = " test , test1=stuff , , test2=, test3, ";
79 params = parser.parse(s, ',');
80 assertEquals("test", ((NameValuePair)params.get(0)).getName());
81 assertEquals(null, ((NameValuePair)params.get(0)).getValue());
82 assertEquals("test1", ((NameValuePair)params.get(1)).getName());
83 assertEquals("stuff", ((NameValuePair)params.get(1)).getValue());
84 assertEquals("test2", ((NameValuePair)params.get(2)).getName());
85 assertEquals("", ((NameValuePair)params.get(2)).getValue());
86 assertEquals("test3", ((NameValuePair)params.get(3)).getName());
87 assertEquals(null, ((NameValuePair)params.get(3)).getValue());
88
89 s = " test";
90 params = parser.parse(s, ';');
91 assertEquals("test", ((NameValuePair)params.get(0)).getName());
92 assertEquals(null, ((NameValuePair)params.get(0)).getValue());
93
94 s = " ";
95 params = parser.parse(s, ';');
96 assertEquals(0, params.size());
97
98 s = " = stuff ";
99 params = parser.parse(s, ';');
100 assertEquals(1, params.size());
101 assertEquals("", ((NameValuePair)params.get(0)).getName());
102 assertEquals("stuff", ((NameValuePair)params.get(0)).getValue());
103 }
104
105 public void testParsingEscapedChars() {
106 String s = "param = \"stuff//\"; more stuff\"";
107 ParameterParser parser = new ParameterParser();
108 List params = parser.parse(s, ';');
109 assertEquals(1, params.size());
110 assertEquals("param",
111 ((NameValuePair)params.get(0)).getName());
112 assertEquals("stuff//\"; more stuff",
113 ((NameValuePair)params.get(0)).getValue());
114
115 s = "param = \"stuff////\"; anotherparam";
116 params = parser.parse(s, ';');
117 assertEquals(2, params.size());
118 assertEquals("param",
119 ((NameValuePair)params.get(0)).getName());
120 assertEquals("stuff////",
121 ((NameValuePair)params.get(0)).getValue());
122 assertEquals("anotherparam",
123 ((NameValuePair)params.get(1)).getName());
124 assertNull(
125 ((NameValuePair)params.get(1)).getValue());
126 }
127
128 public void testParsingBlankParams() {
129 String s = "test; test1 = ; test2 = \"\"";
130 ParameterParser parser = new ParameterParser();
131 List params = parser.parse(s, ';');
132 assertEquals("test", ((NameValuePair)params.get(0)).getName());
133 assertEquals(null, ((NameValuePair)params.get(0)).getValue());
134 assertEquals("test1", ((NameValuePair)params.get(1)).getName());
135 assertEquals("", ((NameValuePair)params.get(1)).getValue());
136 assertEquals("test2", ((NameValuePair)params.get(2)).getName());
137 assertEquals("", ((NameValuePair)params.get(2)).getValue());
138 }
139 }