001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.util.test;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.cocoon.util.NetUtils;
025:
026: /**
027: * Test Cases for the NetUtils class.
028: * @see org.apache.cocoon.util.NetUtils
029: *
030: * @author <a href="mailto:berni_huber@a1.net">Bernhard Huber</a>
031: * @author <a href="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a>
032: * @version CVS $Id: NetUtilsTestCase.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class NetUtilsTestCase extends TestCase {
035:
036: /**
037: *Constructor for the IOUtilsTestCase object
038: *
039: * @param name Description of Parameter
040: * @since
041: */
042: public NetUtilsTestCase(String name) {
043: super (name);
044: }
045:
046: /**
047: *Description of the Method
048: *
049: * @param args Description of Parameter
050: * @since
051: */
052: public static void main(String args[]) {
053: junit.textui.TestRunner.run(NetUtilsTestCase.class);
054: }
055:
056: /**
057: * A unit test for <code>NetUtils.getPath()</code>.
058: *
059: * @exception Exception Description of Exception
060: * @since
061: */
062: public void testGetPath() throws Exception {
063: Object[] test_values = { new String[] { "", "" },
064: new String[] { "/", "" },
065: new String[] { "/foo.bar", "" },
066: new String[] { "foo/bar", "foo" },
067: new String[] { "/foo/bar", "/foo" } };
068: for (int i = 0; i < test_values.length; i++) {
069: String tests[] = (String[]) test_values[i];
070: String test = tests[0];
071: String expected = tests[1];
072:
073: String result = NetUtils.getPath(test);
074: String message = "Test " + "'" + test + "'";
075: assertEquals(message, expected, result);
076: }
077: }
078:
079: /**
080: * A unit test for <code>NetUtils.getExtension()</code>
081: *
082: * @exception Exception Description of Exception
083: * @since
084: */
085: public void testGetExtension() throws Exception {
086: Object[] test_values = { new String[] { "/foo.bar", ".bar" },
087: new String[] { "foo.bar#a", ".bar" },
088: new String[] { "foo.bar?b=c", ".bar" },
089: new String[] { "foo.bar#a?b=c", ".bar" },
090: new String[] { "foo.bar", ".bar" },
091: new String[] { "foo/bar", null },
092: new String[] { "/x.html", ".html" },
093: new String[] { "/foo.bar.org/x.y.z.html", ".html" } };
094: for (int i = 0; i < test_values.length; i++) {
095: String tests[] = (String[]) test_values[i];
096: String test = tests[0];
097: String expected = tests[1];
098:
099: String result = NetUtils.getExtension(test);
100: String message = "Test " + "'" + test + "'";
101: assertEquals(message, expected, result);
102: }
103: }
104:
105: /**
106: * A unit test for <code>NetUtils.absolutize()</code>
107: *
108: * @exception Exception Description of Exception
109: * @since
110: */
111: public void testAbsolutize() throws Exception {
112:
113: Object[] test_values = {
114: new String[] { "/base/path", "foo.bar",
115: "/base/path/foo.bar" },
116: new String[] { "/base/path/", "foo.bar",
117: "/base/path/foo.bar" },
118: new String[] { "/base/path", "/foo.bar", "/foo.bar" },
119:
120: new String[] { "/base/path", "", "/base/path" },
121: new String[] { "/base/path", null, "/base/path" },
122:
123: new String[] { "", "foo.bar", "foo.bar" },
124: new String[] { null, "foo.bar", "foo.bar" }, };
125:
126: for (int i = 0; i < test_values.length; i++) {
127: String tests[] = (String[]) test_values[i];
128: String test_path = tests[0];
129: String test_rel_resource = tests[1];
130: String expected = tests[2];
131:
132: String result = NetUtils.absolutize(test_path,
133: test_rel_resource);
134: String message = "Test " + " path " + "'" + test_path + "'"
135: + " relativeResource " + "'" + test_rel_resource;
136: assertEquals(message, expected, result);
137: }
138: }
139:
140: /**
141: * A unit test for <code>NetUtils.testEncodePath()</code>
142: *
143: * @exception Exception Description of Exception
144: * @since
145: */
146: public void testEncodePath() throws Exception {
147:
148: Object[] test_values = {
149: new String[] { "abc def", "abc%20def" },
150: new String[] { "foo/bar?n=v&N=V", "foo/bar%3Fn=v&N=V" } };
151: for (int i = 0; i < test_values.length; i++) {
152: String tests[] = (String[]) test_values[i];
153: String original = tests[0];
154: String expected = tests[1];
155:
156: String result = NetUtils.encodePath(original);
157: String message = "Test " + " original " + "'" + original
158: + "'";
159: assertEquals(message, expected, result);
160: }
161: }
162:
163: /**
164: * A unit test for <code>NetUtils.relativize()</code>
165: *
166: * @exception Exception Description of Exception
167: * @since
168: */
169: public void testRelativize() throws Exception {
170:
171: Object[] test_values = {
172: new String[] { "/xml.apache.org",
173: "/xml.apache.org/foo.bar", "foo.bar" },
174: new String[] { "/xml.apache.org",
175: "/xml.apache.org/foo.bar", "foo.bar" },
176: new String[] { "/xml.apache.org",
177: "/xml.apache.org/foo.bar", "foo.bar" }, };
178: for (int i = 0; i < test_values.length; i++) {
179: String tests[] = (String[]) test_values[i];
180: String test_path = tests[0];
181: String test_abs_resource = tests[1];
182: String expected = tests[2];
183:
184: String result = NetUtils.relativize(test_path,
185: test_abs_resource);
186: String message = "Test " + " path " + "'" + test_path + "'"
187: + " absoluteResource " + "'" + test_abs_resource;
188: assertEquals(message, expected, result);
189: }
190: }
191:
192: /**
193: * A unit test for {@link NetUtils#normalize(String)}
194: */
195: public void testNormalize() throws Exception {
196: Object[] test_values = { new String[] { "", "" },
197: new String[] { "/", "/" },
198: new String[] { "/../", "/../" },
199: new String[] { "/../../", "/../../" },
200: new String[] { "/../../foo", "/../../foo" },
201: new String[] { "/../../foo//./../bar", "/../../bar" },
202: new String[] { "//foo//bar", "//foo/bar" },
203: new String[] { "//foo//./bar", "//foo/bar" },
204: new String[] { "/foo/bar", "/foo/bar" },
205: new String[] { "/foo/bar/", "/foo/bar/" },
206: new String[] { "/foo/../bar", "/bar" },
207: new String[] { "/foo/../bar/", "/bar/" },
208: new String[] { "bar", "bar" },
209: new String[] { "foo/../bar", "bar" },
210: new String[] { "foo/./bar", "foo/bar" },
211: new String[] { "foo/bar1/bar2/bar3/../../..", "foo/" }, };
212: for (int i = 0; i < test_values.length; i++) {
213: String tests[] = (String[]) test_values[i];
214: String test = tests[0];
215: String expected = tests[1];
216: // alternative for JDK 1.4
217: //String expected = new java.net.URI(test).normalize().toString();
218:
219: String result = NetUtils.normalize(test);
220: String message = "Test " + "'" + test + "'";
221: assertEquals(message, expected, result);
222: }
223: }
224:
225: /**
226: * A unit test for <code>NetUtils.deparameterize()</code>
227: *
228: * @exception Exception Description of Exception
229: * @since
230: */
231: public void testDeparameterize() throws Exception {
232: Map parameters = new HashMap();
233:
234: Object[] test_values = {
235: new String[] { "/foo/bar", "/foo/bar" },
236: new String[] { "bar?a=b&c=d", "bar" }, };
237:
238: for (int i = 0; i < test_values.length; i++) {
239: String tests[] = (String[]) test_values[i];
240: String test = tests[0];
241: String expected = tests[1];
242:
243: parameters.clear();
244: String result = NetUtils.deparameterize(test, parameters);
245: if (test.indexOf('?') > -1) {
246: assertTrue(parameters.size() > 0);
247: }
248: String message = "Test " + "'" + test + "'";
249: assertEquals(message, expected, result);
250: }
251: }
252:
253: /**
254: * A unit test for <code>NetUtils.parameterize()</code>
255: *
256: * @exception Exception Description of Exception
257: * @since
258: */
259: public void testParameterize() throws Exception {
260: Map parameters1 = new HashMap();
261:
262: Object[] test_values = { new Object[] { "/foo/bar",
263: parameters1, "/foo/bar" }, };
264:
265: for (int i = 0; i < test_values.length; i++) {
266: Object tests[] = (Object[]) test_values[i];
267: String test = (String) tests[0];
268: Map parameters = (Map) tests[1];
269: String expected = (String) tests[2];
270:
271: String result = NetUtils.parameterize(test, parameters);
272: String message = "Test " + "'" + test + "'";
273: assertEquals(message, expected, result);
274: }
275:
276: Map parameters2 = new HashMap();
277: parameters2.put("a", "b");
278: parameters2.put("c", "d");
279:
280: String test = "bar";
281: String expected1 = "bar?a=b&c=d";
282: String expected2 = "bar?c=d&a=b";
283:
284: String message = "Test " + "'" + test + "'";
285:
286: String result = NetUtils.parameterize(test, parameters2);
287:
288: if (expected1.equals(result)) {
289: assertEquals(message, expected1, result);
290: } else {
291: assertEquals(message, expected2, result);
292: }
293: }
294: }
|