Source code: com/tripi/asp/test/ParseQueryStringTest.java
1 /**
2 * ArrowHead ASP Server
3 * This is a source file for the ArrowHead ASP Server - an 100% Java
4 * VBScript interpreter and ASP server.
5 *
6 * For more information, see http://www.tripi.com/arrowhead
7 *
8 * Copyright (C) 2002 Terence Haddock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 package com.tripi.asp.test;
26
27 import java.util.Map;
28
29 import junit.framework.Assert;
30 import junit.framework.TestCase;
31
32 import org.apache.log4j.BasicConfigurator;
33
34 import com.tripi.asp.util.ParseQueryString;
35
36 /**
37 * This class test the ParseQueryString class
38 *
39 * @author Terence Haddock
40 */
41 public class ParseQueryStringTest extends TestCase
42 {
43 /**
44 * Constructor.
45 * @param name Test name
46 */
47 public ParseQueryStringTest(String name)
48 {
49 super(name);
50 BasicConfigurator.configure();
51 }
52
53 public void testEmptyString()
54 {
55 Map map = ParseQueryString.parse("");
56 Assert.assertEquals(0, map.size());
57 }
58
59 public void testEscape1()
60 {
61 Map map = ParseQueryString.parse("a=the+value");
62 Assert.assertEquals(1, map.size());
63 String aValue[] = (String[])map.get("a");
64 Assert.assertEquals(1, aValue.length);
65 Assert.assertEquals("the value", aValue[0]);
66 }
67
68 public void testEscape2()
69 {
70 Map map = ParseQueryString.parse("a=the%26value");
71 Assert.assertEquals(1, map.size());
72 String aValue[] = (String[])map.get("a");
73 Assert.assertEquals(1, aValue.length);
74 Assert.assertEquals("the&value", aValue[0]);
75 }
76
77 public void testEscape3()
78 {
79 Map map = ParseQueryString.parse("a=the%2d%2Dvalue");
80 Assert.assertEquals(1, map.size());
81 String aValue[] = (String[])map.get("a");
82 Assert.assertEquals(1, aValue.length);
83 Assert.assertEquals("the--value", aValue[0]);
84 }
85
86 public void testSimpleStringNoValue1()
87 {
88 Map map = ParseQueryString.parse("a");
89 Assert.assertEquals(1, map.size());
90 String aValue[] = (String[])map.get("a");
91 Assert.assertEquals(1, aValue.length);
92 Assert.assertEquals("", aValue[0]);
93 }
94
95 public void testSimpleStringValue1()
96 {
97 Map map = ParseQueryString.parse("a=1");
98 Assert.assertEquals(1, map.size());
99 String aValue[] = (String[])map.get("a");
100 Assert.assertEquals(1, aValue.length);
101 Assert.assertEquals("1", aValue[0]);
102 }
103
104 public void testSimpleStringValue2()
105 {
106 Map map = ParseQueryString.parse("a=value=5=4");
107 Assert.assertEquals(1, map.size());
108 String aValue[] = (String[])map.get("a");
109 Assert.assertEquals(1, aValue.length);
110 Assert.assertEquals("value=5=4", aValue[0]);
111 }
112
113 public void testMultipleValues1()
114 {
115 Map map = ParseQueryString.parse("a=1&b=2");
116 Assert.assertEquals(2, map.size());
117 String aValue[] = (String[])map.get("a");
118 Assert.assertEquals(1, aValue.length);
119 Assert.assertEquals("1", aValue[0]);
120 String bValue[] = (String[])map.get("b");
121 Assert.assertEquals(1, bValue.length);
122 Assert.assertEquals("2", bValue[0]);
123 }
124
125 public void testMultipleValues2()
126 {
127 Map map = ParseQueryString.parse("a=1&b=2&c=3");
128 Assert.assertEquals(3, map.size());
129 String aValue[] = (String[])map.get("a");
130 Assert.assertEquals(1, aValue.length);
131 Assert.assertEquals("1", aValue[0]);
132 String bValue[] = (String[])map.get("b");
133 Assert.assertEquals(1, bValue.length);
134 Assert.assertEquals("2", bValue[0]);
135 String cValue[] = (String[])map.get("c");
136 Assert.assertEquals(1, cValue.length);
137 Assert.assertEquals("3", cValue[0]);
138 }
139
140 public void testMultipleValues3()
141 {
142 Map map = ParseQueryString.parse("a=1&b=first&b=second&c=3&b=third");
143 Assert.assertEquals(3, map.size());
144 Object bObject = map.get("b");
145 Assert.assertSame(String[].class, bObject.getClass());
146 String[] bArray = (String[])bObject;
147 Assert.assertEquals(3, bArray.length);
148 Assert.assertEquals("first", bArray[0]);
149 Assert.assertEquals("second", bArray[1]);
150 Assert.assertEquals("third", bArray[2]);
151 }
152 }
153