001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/test/org/apache/commons/httpclient/TestParameterFormatter.java $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: */
029:
030: package org.apache.commons.httpclient;
031:
032: import org.apache.commons.httpclient.util.ParameterFormatter;
033:
034: import junit.framework.Test;
035: import junit.framework.TestCase;
036: import junit.framework.TestSuite;
037:
038: /**
039: * Unit tests for {@link ParameterFormatter}.
040: *
041: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
042: */
043: public class TestParameterFormatter extends TestCase {
044:
045: // ------------------------------------------------------------ Constructor
046: public TestParameterFormatter(String testName) {
047: super (testName);
048: }
049:
050: // ------------------------------------------------------------------- Main
051: public static void main(String args[]) {
052: String[] testCaseName = { TestParameterFormatter.class
053: .getName() };
054: junit.textui.TestRunner.main(testCaseName);
055: }
056:
057: // ------------------------------------------------------- TestCase Methods
058:
059: public static Test suite() {
060: return new TestSuite(TestParameterFormatter.class);
061: }
062:
063: public void testBasicValueFormatting() throws Exception {
064: ParameterFormatter formatter = new ParameterFormatter();
065:
066: NameValuePair param1 = new NameValuePair("param",
067: "regular_stuff");
068: NameValuePair param2 = new NameValuePair("param", "this\\that");
069: NameValuePair param3 = new NameValuePair("param", "this,that");
070: NameValuePair param4 = new NameValuePair("param",
071: "quote marks (\") must be escaped");
072: NameValuePair param5 = new NameValuePair("param",
073: "back slash (\\) must be escaped too");
074: NameValuePair param6 = new NameValuePair("param",
075: "values with\tblanks must always be quoted");
076:
077: formatter.setAlwaysUseQuotes(false);
078: assertEquals("param=regular_stuff", formatter.format(param1));
079: assertEquals("param=\"this\\\\that\"", formatter.format(param2));
080: assertEquals("param=\"this,that\"", formatter.format(param3));
081: assertEquals("param=\"quote marks (\\\") must be escaped\"",
082: formatter.format(param4));
083: assertEquals("param=\"back slash (\\\\) must be escaped too\"",
084: formatter.format(param5));
085: assertEquals(
086: "param=\"values with\tblanks must always be quoted\"",
087: formatter.format(param6));
088:
089: formatter.setAlwaysUseQuotes(true);
090: assertEquals("param=\"regular_stuff\"", formatter
091: .format(param1));
092: assertEquals("param=\"this\\\\that\"", formatter.format(param2));
093: assertEquals("param=\"this,that\"", formatter.format(param3));
094: assertEquals("param=\"quote marks (\\\") must be escaped\"",
095: formatter.format(param4));
096: assertEquals("param=\"back slash (\\\\) must be escaped too\"",
097: formatter.format(param5));
098: assertEquals(
099: "param=\"values with\tblanks must always be quoted\"",
100: formatter.format(param6));
101: }
102:
103: }
|