KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > test > NetUtilsTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.util.test;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.apache.cocoon.util.NetUtils;
24
25 /**
26  * Test Cases for the NetUtils class.
27  * @see org.apache.cocoon.util.NetUtils
28  *
29  * @author <a HREF="mailto:berni_huber@a1.net">Bernhard Huber</a>
30  * @author <a HREF="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a>
31  * @version CVS $Id: NetUtilsTestCase.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  */

33 public class NetUtilsTestCase extends TestCase
34 {
35
36     /**
37      *Constructor for the IOUtilsTestCase object
38      *
39      * @param name Description of Parameter
40      * @since
41      */

42     public NetUtilsTestCase(String JavaDoc name) {
43         super(name);
44     }
45
46
47     /**
48      *Description of the Method
49      *
50      * @param args Description of Parameter
51      * @since
52      */

53     public static void main(String JavaDoc args[]) {
54         junit.textui.TestRunner.run(NetUtilsTestCase.class);
55     }
56
57
58     /**
59      * A unit test for <code>NetUtils.getPath()</code>.
60      *
61      * @exception Exception Description of Exception
62      * @since
63      */

64     public void testGetPath() throws Exception JavaDoc {
65         Object JavaDoc[] test_values = {
66                 new String JavaDoc[]{"", ""},
67                 new String JavaDoc[]{"/", ""},
68                 new String JavaDoc[]{"/foo.bar", ""},
69                 new String JavaDoc[]{"foo/bar", "foo"},
70                 new String JavaDoc[]{"/foo/bar", "/foo"}
71                 };
72         for (int i = 0; i < test_values.length; i++) {
73             String JavaDoc tests[] = (String JavaDoc[]) test_values[i];
74             String JavaDoc test = tests[0];
75             String JavaDoc expected = tests[1];
76
77             String JavaDoc result = NetUtils.getPath(test);
78             String JavaDoc message = "Test " + "'" + test + "'";
79             assertEquals(message, expected, result);
80         }
81     }
82
83
84     /**
85      * A unit test for <code>NetUtils.getExtension()</code>
86      *
87      * @exception Exception Description of Exception
88      * @since
89      */

90     public void testGetExtension() throws Exception JavaDoc {
91         Object JavaDoc[] test_values = {
92                 new String JavaDoc[]{"/foo.bar", ".bar"},
93                 new String JavaDoc[]{"foo.bar#a", ".bar"},
94                 new String JavaDoc[]{"foo.bar?b=c", ".bar"},
95                 new String JavaDoc[]{"foo.bar#a?b=c", ".bar"},
96                 new String JavaDoc[]{"foo.bar", ".bar"},
97                 new String JavaDoc[]{"foo/bar", null},
98                 new String JavaDoc[]{"/x.html", ".html"},
99                 new String JavaDoc[]{"/foo.bar.org/x.y.z.html", ".html"}
100                 };
101         for (int i = 0; i < test_values.length; i++) {
102             String JavaDoc tests[] = (String JavaDoc[]) test_values[i];
103             String JavaDoc test = tests[0];
104             String JavaDoc expected = tests[1];
105
106             String JavaDoc result = NetUtils.getExtension(test);
107             String JavaDoc message = "Test " + "'" + test + "'";
108             assertEquals(message, expected, result);
109         }
110     }
111
112
113     /**
114      * A unit test for <code>NetUtils.absolutize()</code>
115      *
116      * @exception Exception Description of Exception
117      * @since
118      */

119     public void testAbsolutize() throws Exception JavaDoc {
120
121         Object JavaDoc[] test_values = {
122             new String JavaDoc[]{"/base/path", "foo.bar", "/base/path/foo.bar"},
123             new String JavaDoc[]{"/base/path/", "foo.bar", "/base/path/foo.bar"},
124             new String JavaDoc[]{"/base/path", "/foo.bar", "/foo.bar"},
125             
126             new String JavaDoc[]{"/base/path", "", "/base/path"},
127             new String JavaDoc[]{"/base/path", null, "/base/path"},
128             
129             new String JavaDoc[]{"", "foo.bar", "foo.bar"},
130             new String JavaDoc[]{null, "foo.bar", "foo.bar"},
131         };
132         
133         for (int i = 0; i < test_values.length; i++) {
134             String JavaDoc tests[] = (String JavaDoc[]) test_values[i];
135             String JavaDoc test_path = tests[0];
136             String JavaDoc test_rel_resource = tests[1];
137             String JavaDoc expected = tests[2];
138
139             String JavaDoc result = NetUtils.absolutize(test_path, test_rel_resource);
140             String JavaDoc message = "Test " +
141                     " path " + "'" + test_path + "'" +
142                     " relativeResource " + "'" + test_rel_resource;
143             assertEquals(message, expected, result);
144         }
145     }
146
147
148     /**
149      * A unit test for <code>NetUtils.testEncodePath()</code>
150      *
151      * @exception Exception Description of Exception
152      * @since
153      */

154     public void testEncodePath() throws Exception JavaDoc {
155
156         Object JavaDoc[] test_values = {
157                 new String JavaDoc[]{"abc def", "abc%20def"},
158                 new String JavaDoc[]{"foo/bar?n=v&N=V", "foo/bar%3Fn=v&N=V"}
159                 };
160         for (int i = 0; i < test_values.length; i++) {
161             String JavaDoc tests[] = (String JavaDoc[]) test_values[i];
162             String JavaDoc original = tests[0];
163             String JavaDoc expected = tests[1];
164
165             String JavaDoc result = NetUtils.encodePath(original);
166             String JavaDoc message = "Test " +
167                     " original " + "'" + original + "'";
168             assertEquals(message, expected, result);
169         }
170     }
171
172
173     /**
174      * A unit test for <code>NetUtils.relativize()</code>
175      *
176      * @exception Exception Description of Exception
177      * @since
178      */

179     public void testRelativize() throws Exception JavaDoc {
180
181         Object JavaDoc[] test_values = {
182                 new String JavaDoc[]{"/xml.apache.org", "/xml.apache.org/foo.bar", "foo.bar"},
183                 new String JavaDoc[]{"/xml.apache.org", "/xml.apache.org/foo.bar", "foo.bar"},
184                 new String JavaDoc[]{"/xml.apache.org", "/xml.apache.org/foo.bar", "foo.bar"},
185                 };
186         for (int i = 0; i < test_values.length; i++) {
187             String JavaDoc tests[] = (String JavaDoc[]) test_values[i];
188             String JavaDoc test_path = tests[0];
189             String JavaDoc test_abs_resource = tests[1];
190             String JavaDoc expected = tests[2];
191
192             String JavaDoc result = NetUtils.relativize(test_path, test_abs_resource);
193             String JavaDoc message = "Test " +
194                     " path " + "'" + test_path + "'" +
195                     " absoluteResource " + "'" + test_abs_resource;
196             assertEquals(message, expected, result);
197         }
198     }
199
200
201     /**
202      * A unit test for {@link NetUtils#normalize(String)}
203      */

204     public void testNormalize() throws Exception JavaDoc {
205         Object JavaDoc[] test_values = {
206                 new String JavaDoc[]{"", ""},
207                 new String JavaDoc[]{"/", "/"},
208                 new String JavaDoc[]{"/../", "/../"},
209                 new String JavaDoc[]{"/../../", "/../../"},
210                 new String JavaDoc[]{"/../../foo", "/../../foo"},
211                 new String JavaDoc[]{"/../../foo//./../bar", "/../../bar"},
212                 new String JavaDoc[]{"//foo//bar", "//foo/bar"},
213                 new String JavaDoc[]{"//foo//./bar", "//foo/bar"},
214                 new String JavaDoc[]{"/foo/bar", "/foo/bar"},
215                 new String JavaDoc[]{"/foo/bar/", "/foo/bar/"},
216                 new String JavaDoc[]{"/foo/../bar", "/bar"},
217                 new String JavaDoc[]{"/foo/../bar/", "/bar/"},
218                 new String JavaDoc[]{"bar", "bar"},
219                 new String JavaDoc[]{"foo/../bar", "bar"},
220                 new String JavaDoc[]{"foo/./bar", "foo/bar"},
221                 new String JavaDoc[]{"foo/bar1/bar2/bar3/../../..", "foo/"},
222                 };
223         for (int i = 0; i < test_values.length; i++) {
224             String JavaDoc tests[] = (String JavaDoc[]) test_values[i];
225             String JavaDoc test = tests[0];
226             String JavaDoc expected = tests[1];
227             // alternative for JDK 1.4
228
//String expected = new java.net.URI(test).normalize().toString();
229

230             String JavaDoc result = NetUtils.normalize(test);
231             String JavaDoc message = "Test " + "'" + test + "'";
232             assertEquals(message, expected, result);
233         }
234     }
235
236
237     /**
238      * A unit test for <code>NetUtils.deparameterize()</code>
239      *
240      * @exception Exception Description of Exception
241      * @since
242      */

243     public void testDeparameterize() throws Exception JavaDoc {
244         Map JavaDoc parameters = new HashMap JavaDoc();
245
246         Object JavaDoc[] test_values = {
247             new String JavaDoc[]{"/foo/bar", "/foo/bar"},
248             new String JavaDoc[]{"bar?a=b&c=d", "bar"},
249         };
250
251         for (int i = 0; i < test_values.length; i++) {
252             String JavaDoc tests[] = (String JavaDoc[]) test_values[i];
253             String JavaDoc test = tests[0];
254             String JavaDoc expected = tests[1];
255
256             parameters.clear();
257             String JavaDoc result = NetUtils.deparameterize(test, parameters);
258             if (test.indexOf('?') > -1) {
259                 assertTrue(parameters.size() > 0);
260             }
261             String JavaDoc message = "Test " + "'" + test + "'";
262             assertEquals(message, expected, result);
263         }
264     }
265
266
267     /**
268      * A unit test for <code>NetUtils.parameterize()</code>
269      *
270      * @exception Exception Description of Exception
271      * @since
272      */

273     public void testParameterize() throws Exception JavaDoc {
274         Map JavaDoc parameters1 = new HashMap JavaDoc();
275
276         Object JavaDoc[] test_values = {
277             new Object JavaDoc[]{"/foo/bar", parameters1, "/foo/bar"},
278         };
279
280         for (int i = 0; i < test_values.length; i++) {
281             Object JavaDoc tests[] = (Object JavaDoc[]) test_values[i];
282             String JavaDoc test = (String JavaDoc) tests[0];
283             Map JavaDoc parameters = (Map JavaDoc) tests[1];
284             String JavaDoc expected = (String JavaDoc) tests[2];
285
286             String JavaDoc result = NetUtils.parameterize(test, parameters);
287             String JavaDoc message = "Test " + "'" + test + "'";
288             assertEquals(message, expected, result);
289         }
290
291         Map JavaDoc parameters2 = new HashMap JavaDoc();
292         parameters2.put("a", "b");
293         parameters2.put("c", "d");
294         
295         String JavaDoc test = "bar";
296         String JavaDoc expected1 = "bar?a=b&c=d";
297         String JavaDoc expected2 = "bar?c=d&a=b";
298         
299         String JavaDoc message = "Test " + "'" + test + "'";
300                     
301         String JavaDoc result = NetUtils.parameterize(test, parameters2);
302
303         if (expected1.equals(result)) {
304           assertEquals(message, expected1, result);
305         } else {
306           assertEquals(message, expected2, result);
307         }
308     }
309 }
310
Popular Tags