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 package org.apache.commons.httpclient;
30
31 import org.apache.commons.httpclient.util.ParameterFormatter;
32
33 import junit.framework.Test;
34 import junit.framework.TestCase;
35 import junit.framework.TestSuite;
36
37 /***
38 * Unit tests for {@link ParameterFormatter}.
39 *
40 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
41 */
42 public class TestParameterFormatter extends TestCase {
43
44
45 public TestParameterFormatter(String testName) {
46 super(testName);
47 }
48
49
50 public static void main(String args[]) {
51 String[] testCaseName = { TestParameterFormatter.class.getName() };
52 junit.textui.TestRunner.main(testCaseName);
53 }
54
55
56
57 public static Test suite() {
58 return new TestSuite(TestParameterFormatter.class);
59 }
60
61 public void testBasicValueFormatting() throws Exception {
62 ParameterFormatter formatter = new ParameterFormatter();
63
64 NameValuePair param1 = new NameValuePair("param", "regular_stuff");
65 NameValuePair param2 = new NameValuePair("param", "this//that");
66 NameValuePair param3 = new NameValuePair("param", "this,that");
67 NameValuePair param4 = new NameValuePair("param", "quote marks (\") must be escaped");
68 NameValuePair param5 = new NameValuePair("param", "back slash (//) must be escaped too");
69 NameValuePair param6 = new NameValuePair("param", "values with\tblanks must always be quoted");
70
71 formatter.setAlwaysUseQuotes(false);
72 assertEquals("param=regular_stuff", formatter.format(param1));
73 assertEquals("param=\"this////that\"", formatter.format(param2));
74 assertEquals("param=\"this,that\"", formatter.format(param3));
75 assertEquals("param=\"quote marks (//\") must be escaped\"", formatter.format(param4));
76 assertEquals("param=\"back slash (////) must be escaped too\"", formatter.format(param5));
77 assertEquals("param=\"values with\tblanks must always be quoted\"", formatter.format(param6));
78
79 formatter.setAlwaysUseQuotes(true);
80 assertEquals("param=\"regular_stuff\"", formatter.format(param1));
81 assertEquals("param=\"this////that\"", formatter.format(param2));
82 assertEquals("param=\"this,that\"", formatter.format(param3));
83 assertEquals("param=\"quote marks (//\") must be escaped\"", formatter.format(param4));
84 assertEquals("param=\"back slash (////) must be escaped too\"", formatter.format(param5));
85 assertEquals("param=\"values with\tblanks must always be quoted\"", formatter.format(param6));
86 }
87
88 }