001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/test/org/apache/commons/httpclient/TestParameterParser.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 junit.framework.Test;
033: import junit.framework.TestCase;
034: import junit.framework.TestSuite;
035:
036: import java.util.List;
037:
038: import org.apache.commons.httpclient.util.ParameterParser;
039:
040: /**
041: * Unit tests for {@link ParameterParser}.
042: *
043: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
044: */
045: public class TestParameterParser extends TestCase {
046:
047: // ------------------------------------------------------------ Constructor
048: public TestParameterParser(String testName) {
049: super (testName);
050: }
051:
052: // ------------------------------------------------------------------- Main
053: public static void main(String args[]) {
054: String[] testCaseName = { TestParameterParser.class.getName() };
055: junit.textui.TestRunner.main(testCaseName);
056: }
057:
058: // ------------------------------------------------------- TestCase Methods
059:
060: public static Test suite() {
061: return new TestSuite(TestParameterParser.class);
062: }
063:
064: public void testParsing() {
065: String s = "test; test1 = stuff ; test2 = \"stuff; stuff\"; test3=\"stuff";
066: ParameterParser parser = new ParameterParser();
067: List params = parser.parse(s, ';');
068: assertEquals("test", ((NameValuePair) params.get(0)).getName());
069: assertEquals(null, ((NameValuePair) params.get(0)).getValue());
070: assertEquals("test1", ((NameValuePair) params.get(1)).getName());
071: assertEquals("stuff", ((NameValuePair) params.get(1))
072: .getValue());
073: assertEquals("test2", ((NameValuePair) params.get(2)).getName());
074: assertEquals("stuff; stuff", ((NameValuePair) params.get(2))
075: .getValue());
076: assertEquals("test3", ((NameValuePair) params.get(3)).getName());
077: assertEquals("\"stuff", ((NameValuePair) params.get(3))
078: .getValue());
079:
080: s = " test , test1=stuff , , test2=, test3, ";
081: params = parser.parse(s, ',');
082: assertEquals("test", ((NameValuePair) params.get(0)).getName());
083: assertEquals(null, ((NameValuePair) params.get(0)).getValue());
084: assertEquals("test1", ((NameValuePair) params.get(1)).getName());
085: assertEquals("stuff", ((NameValuePair) params.get(1))
086: .getValue());
087: assertEquals("test2", ((NameValuePair) params.get(2)).getName());
088: assertEquals("", ((NameValuePair) params.get(2)).getValue());
089: assertEquals("test3", ((NameValuePair) params.get(3)).getName());
090: assertEquals(null, ((NameValuePair) params.get(3)).getValue());
091:
092: s = " test";
093: params = parser.parse(s, ';');
094: assertEquals("test", ((NameValuePair) params.get(0)).getName());
095: assertEquals(null, ((NameValuePair) params.get(0)).getValue());
096:
097: s = " ";
098: params = parser.parse(s, ';');
099: assertEquals(0, params.size());
100:
101: s = " = stuff ";
102: params = parser.parse(s, ';');
103: assertEquals(1, params.size());
104: assertEquals("", ((NameValuePair) params.get(0)).getName());
105: assertEquals("stuff", ((NameValuePair) params.get(0))
106: .getValue());
107: }
108:
109: public void testParsingEscapedChars() {
110: String s = "param = \"stuff\\\"; more stuff\"";
111: ParameterParser parser = new ParameterParser();
112: List params = parser.parse(s, ';');
113: assertEquals(1, params.size());
114: assertEquals("param", ((NameValuePair) params.get(0)).getName());
115: assertEquals("stuff\\\"; more stuff", ((NameValuePair) params
116: .get(0)).getValue());
117:
118: s = "param = \"stuff\\\\\"; anotherparam";
119: params = parser.parse(s, ';');
120: assertEquals(2, params.size());
121: assertEquals("param", ((NameValuePair) params.get(0)).getName());
122: assertEquals("stuff\\\\", ((NameValuePair) params.get(0))
123: .getValue());
124: assertEquals("anotherparam", ((NameValuePair) params.get(1))
125: .getName());
126: assertNull(((NameValuePair) params.get(1)).getValue());
127: }
128:
129: public void testParsingBlankParams() {
130: String s = "test; test1 = ; test2 = \"\"";
131: ParameterParser parser = new ParameterParser();
132: List params = parser.parse(s, ';');
133: assertEquals("test", ((NameValuePair) params.get(0)).getName());
134: assertEquals(null, ((NameValuePair) params.get(0)).getValue());
135: assertEquals("test1", ((NameValuePair) params.get(1)).getName());
136: assertEquals("", ((NameValuePair) params.get(1)).getValue());
137: assertEquals("test2", ((NameValuePair) params.get(2)).getName());
138: assertEquals("", ((NameValuePair) params.get(2)).getValue());
139: }
140: }
|