1 16 package org.apache.cocoon.util.test; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import junit.framework.TestCase; 22 23 import org.apache.cocoon.util.NetUtils; 24 25 33 public class NetUtilsTestCase extends TestCase 34 { 35 36 42 public NetUtilsTestCase(String name) { 43 super(name); 44 } 45 46 47 53 public static void main(String args[]) { 54 junit.textui.TestRunner.run(NetUtilsTestCase.class); 55 } 56 57 58 64 public void testGetPath() throws Exception { 65 Object [] test_values = { 66 new String []{"", ""}, 67 new String []{"/", ""}, 68 new String []{"/foo.bar", ""}, 69 new String []{"foo/bar", "foo"}, 70 new String []{"/foo/bar", "/foo"} 71 }; 72 for (int i = 0; i < test_values.length; i++) { 73 String tests[] = (String []) test_values[i]; 74 String test = tests[0]; 75 String expected = tests[1]; 76 77 String result = NetUtils.getPath(test); 78 String message = "Test " + "'" + test + "'"; 79 assertEquals(message, expected, result); 80 } 81 } 82 83 84 90 public void testGetExtension() throws Exception { 91 Object [] test_values = { 92 new String []{"/foo.bar", ".bar"}, 93 new String []{"foo.bar#a", ".bar"}, 94 new String []{"foo.bar?b=c", ".bar"}, 95 new String []{"foo.bar#a?b=c", ".bar"}, 96 new String []{"foo.bar", ".bar"}, 97 new String []{"foo/bar", null}, 98 new String []{"/x.html", ".html"}, 99 new String []{"/foo.bar.org/x.y.z.html", ".html"} 100 }; 101 for (int i = 0; i < test_values.length; i++) { 102 String tests[] = (String []) test_values[i]; 103 String test = tests[0]; 104 String expected = tests[1]; 105 106 String result = NetUtils.getExtension(test); 107 String message = "Test " + "'" + test + "'"; 108 assertEquals(message, expected, result); 109 } 110 } 111 112 113 119 public void testAbsolutize() throws Exception { 120 121 Object [] test_values = { 122 new String []{"/base/path", "foo.bar", "/base/path/foo.bar"}, 123 new String []{"/base/path/", "foo.bar", "/base/path/foo.bar"}, 124 new String []{"/base/path", "/foo.bar", "/foo.bar"}, 125 126 new String []{"/base/path", "", "/base/path"}, 127 new String []{"/base/path", null, "/base/path"}, 128 129 new String []{"", "foo.bar", "foo.bar"}, 130 new String []{null, "foo.bar", "foo.bar"}, 131 }; 132 133 for (int i = 0; i < test_values.length; i++) { 134 String tests[] = (String []) test_values[i]; 135 String test_path = tests[0]; 136 String test_rel_resource = tests[1]; 137 String expected = tests[2]; 138 139 String result = NetUtils.absolutize(test_path, test_rel_resource); 140 String message = "Test " + 141 " path " + "'" + test_path + "'" + 142 " relativeResource " + "'" + test_rel_resource; 143 assertEquals(message, expected, result); 144 } 145 } 146 147 148 154 public void testEncodePath() throws Exception { 155 156 Object [] test_values = { 157 new String []{"abc def", "abc%20def"}, 158 new String []{"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 tests[] = (String []) test_values[i]; 162 String original = tests[0]; 163 String expected = tests[1]; 164 165 String result = NetUtils.encodePath(original); 166 String message = "Test " + 167 " original " + "'" + original + "'"; 168 assertEquals(message, expected, result); 169 } 170 } 171 172 173 179 public void testRelativize() throws Exception { 180 181 Object [] test_values = { 182 new String []{"/xml.apache.org", "/xml.apache.org/foo.bar", "foo.bar"}, 183 new String []{"/xml.apache.org", "/xml.apache.org/foo.bar", "foo.bar"}, 184 new String []{"/xml.apache.org", "/xml.apache.org/foo.bar", "foo.bar"}, 185 }; 186 for (int i = 0; i < test_values.length; i++) { 187 String tests[] = (String []) test_values[i]; 188 String test_path = tests[0]; 189 String test_abs_resource = tests[1]; 190 String expected = tests[2]; 191 192 String result = NetUtils.relativize(test_path, test_abs_resource); 193 String message = "Test " + 194 " path " + "'" + test_path + "'" + 195 " absoluteResource " + "'" + test_abs_resource; 196 assertEquals(message, expected, result); 197 } 198 } 199 200 201 204 public void testNormalize() throws Exception { 205 Object [] test_values = { 206 new String []{"", ""}, 207 new String []{"/", "/"}, 208 new String []{"/../", "/../"}, 209 new String []{"/../../", "/../../"}, 210 new String []{"/../../foo", "/../../foo"}, 211 new String []{"/../../foo//./../bar", "/../../bar"}, 212 new String []{"//foo//bar", "//foo/bar"}, 213 new String []{"//foo//./bar", "//foo/bar"}, 214 new String []{"/foo/bar", "/foo/bar"}, 215 new String []{"/foo/bar/", "/foo/bar/"}, 216 new String []{"/foo/../bar", "/bar"}, 217 new String []{"/foo/../bar/", "/bar/"}, 218 new String []{"bar", "bar"}, 219 new String []{"foo/../bar", "bar"}, 220 new String []{"foo/./bar", "foo/bar"}, 221 new String []{"foo/bar1/bar2/bar3/../../..", "foo/"}, 222 }; 223 for (int i = 0; i < test_values.length; i++) { 224 String tests[] = (String []) test_values[i]; 225 String test = tests[0]; 226 String expected = tests[1]; 227 230 String result = NetUtils.normalize(test); 231 String message = "Test " + "'" + test + "'"; 232 assertEquals(message, expected, result); 233 } 234 } 235 236 237 243 public void testDeparameterize() throws Exception { 244 Map parameters = new HashMap (); 245 246 Object [] test_values = { 247 new String []{"/foo/bar", "/foo/bar"}, 248 new String []{"bar?a=b&c=d", "bar"}, 249 }; 250 251 for (int i = 0; i < test_values.length; i++) { 252 String tests[] = (String []) test_values[i]; 253 String test = tests[0]; 254 String expected = tests[1]; 255 256 parameters.clear(); 257 String result = NetUtils.deparameterize(test, parameters); 258 if (test.indexOf('?') > -1) { 259 assertTrue(parameters.size() > 0); 260 } 261 String message = "Test " + "'" + test + "'"; 262 assertEquals(message, expected, result); 263 } 264 } 265 266 267 273 public void testParameterize() throws Exception { 274 Map parameters1 = new HashMap (); 275 276 Object [] test_values = { 277 new Object []{"/foo/bar", parameters1, "/foo/bar"}, 278 }; 279 280 for (int i = 0; i < test_values.length; i++) { 281 Object tests[] = (Object []) test_values[i]; 282 String test = (String ) tests[0]; 283 Map parameters = (Map ) tests[1]; 284 String expected = (String ) tests[2]; 285 286 String result = NetUtils.parameterize(test, parameters); 287 String message = "Test " + "'" + test + "'"; 288 assertEquals(message, expected, result); 289 } 290 291 Map parameters2 = new HashMap (); 292 parameters2.put("a", "b"); 293 parameters2.put("c", "d"); 294 295 String test = "bar"; 296 String expected1 = "bar?a=b&c=d"; 297 String expected2 = "bar?c=d&a=b"; 298 299 String message = "Test " + "'" + test + "'"; 300 301 String 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 |