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