001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.util;
016:
017: import java.io.ByteArrayInputStream;
018: import java.io.UnsupportedEncodingException;
019:
020: import junit.framework.TestSuite;
021:
022: /* ------------------------------------------------------------ */
023: /** Util meta Tests.
024: * @author Greg Wilkins (gregw)
025: */
026: public class URLEncodedTest extends junit.framework.TestCase {
027: public URLEncodedTest(String name) {
028: super (name);
029: }
030:
031: public static junit.framework.Test suite() {
032: TestSuite suite = new TestSuite(URLEncodedTest.class);
033: return suite;
034: }
035:
036: /* ------------------------------------------------------------ */
037: /** main.
038: */
039: public static void main(String[] args) {
040: junit.textui.TestRunner.run(suite());
041: }
042:
043: /* -------------------------------------------------------------- */
044: public void testUrlEncoded() throws UnsupportedEncodingException {
045:
046: UrlEncoded url_encoded = new UrlEncoded();
047: assertEquals("Empty", 0, url_encoded.size());
048:
049: url_encoded.clear();
050: url_encoded.decode("Name1=Value1");
051: assertEquals("simple param size", 1, url_encoded.size());
052: assertEquals("simple encode", "Name1=Value1", url_encoded
053: .encode());
054: assertEquals("simple get", "Value1", url_encoded
055: .getString("Name1"));
056:
057: url_encoded.clear();
058: url_encoded.decode("Name2=");
059: assertEquals("dangling param size", 1, url_encoded.size());
060: assertEquals("dangling encode", "Name2", url_encoded.encode());
061: assertEquals("dangling get", "", url_encoded.getString("Name2"));
062:
063: url_encoded.clear();
064: url_encoded.decode("Name3");
065: assertEquals("noValue param size", 1, url_encoded.size());
066: assertEquals("noValue encode", "Name3", url_encoded.encode());
067: assertEquals("noValue get", "", url_encoded.getString("Name3"));
068:
069: url_encoded.clear();
070: url_encoded.decode("Name4=Value+4%21");
071: assertEquals("encoded param size", 1, url_encoded.size());
072: assertEquals("encoded encode", "Name4=Value+4%21", url_encoded
073: .encode());
074: assertEquals("encoded get", "Value 4!", url_encoded
075: .getString("Name4"));
076:
077: url_encoded.clear();
078: url_encoded.decode("Name4=Value%2B4%21");
079: assertEquals("encoded param size", 1, url_encoded.size());
080: assertEquals("encoded encode", "Name4=Value%2B4%21",
081: url_encoded.encode());
082: assertEquals("encoded get", "Value+4!", url_encoded
083: .getString("Name4"));
084:
085: url_encoded.clear();
086: url_encoded.decode("Name4=Value+4%21%20%214");
087: assertEquals("encoded param size", 1, url_encoded.size());
088: assertEquals("encoded encode", "Name4=Value+4%21+%214",
089: url_encoded.encode());
090: assertEquals("encoded get", "Value 4! !4", url_encoded
091: .getString("Name4"));
092:
093: url_encoded.clear();
094: url_encoded.decode("Name5=aaa&Name6=bbb");
095: assertEquals("multi param size", 2, url_encoded.size());
096: assertTrue("multi encode " + url_encoded.encode(), url_encoded
097: .encode().equals("Name5=aaa&Name6=bbb")
098: || url_encoded.encode().equals("Name6=bbb&Name5=aaa"));
099: assertEquals("multi get", "aaa", url_encoded.getString("Name5"));
100: assertEquals("multi get", "bbb", url_encoded.getString("Name6"));
101:
102: url_encoded.clear();
103: url_encoded.decode("Name7=aaa&Name7=b%2Cb&Name7=ccc");
104: assertEquals("multi encode", "Name7=aaa&Name7=b%2Cb&Name7=ccc",
105: url_encoded.encode());
106: assertEquals("list get all", url_encoded.getString("Name7"),
107: "aaa,b,b,ccc");
108: assertEquals("list get", "aaa", url_encoded.getValues("Name7")
109: .get(0));
110: assertEquals("list get", url_encoded.getValues("Name7").get(1),
111: "b,b");
112: assertEquals("list get", "ccc", url_encoded.getValues("Name7")
113: .get(2));
114:
115: url_encoded.clear();
116: url_encoded.decode("Name8=xx%2C++yy++%2Czz");
117: assertEquals("encoded param size", 1, url_encoded.size());
118: assertEquals("encoded encode", "Name8=xx%2C++yy++%2Czz",
119: url_encoded.encode());
120: assertEquals("encoded get", url_encoded.getString("Name8"),
121: "xx, yy ,zz");
122:
123: url_encoded.clear();
124: url_encoded.decode("Name9=%83e%83X%83g", "SJIS"); // "Test" in Japanese Katakana
125: assertEquals("encoded param size", 1, url_encoded.size());
126: assertEquals("encoded get", "\u30c6\u30b9\u30c8", url_encoded
127: .getString("Name9"));
128:
129: }
130:
131: /* -------------------------------------------------------------- */
132: public void testUrlEncodedStream() throws Exception {
133: ByteArrayInputStream in = new ByteArrayInputStream(
134: "name0=value+%30&name1=&name2&name3=value+3".getBytes());
135: MultiMap m = new MultiMap();
136: UrlEncoded.decodeTo(in, m, null, -1);
137: System.err.println(m);
138: assertEquals("stream length", 4, m.size());
139: assertEquals("stream name0", "value 0", m.getString("name0"));
140: assertEquals("stream name1", "", m.getString("name1"));
141: assertEquals("stream name2", "", m.getString("name2"));
142: assertEquals("stream name3", "value 3", m.getString("name3"));
143:
144: }
145: }
|