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