1   /*
2    * $HeadURL: http://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestParameterFormatter.java $
3    * $Revision: 178662 $
4    * $Date: 2005-05-26 14:21:14 -0400 (Thu, 26 May 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
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      // ------------------------------------------------------------ Constructor
45      public TestParameterFormatter(String testName) {
46          super(testName);
47      }
48  
49      // ------------------------------------------------------------------- Main
50      public static void main(String args[]) {
51          String[] testCaseName = { TestParameterFormatter.class.getName() };
52          junit.textui.TestRunner.main(testCaseName);
53      }
54  
55      // ------------------------------------------------------- TestCase Methods
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  }