1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package ifc.util; 25 26 import com.sun.star.util.URL; 27 import com.sun.star.util.XURLTransformer; 28 import lib.MultiMethodTest; 29 30 /** 31 * Testing <code>com.sun.star.util.XURLTransformer</code> 32 * interface methods : 33 * <ul> 34 * <li><code> assemble() </code></li> 35 * <li><code> parseStrict() </code></li> 36 * <li><code> parseSmart() </code></li> 37 * <li><code> getPresentation() </code></li> 38 * </ul> <p> 39 * Test is <b> NOT </b> multithread compilant. <p> 40 * @see com.sun.star.util.XURLTransformer 41 */ 42 public class _XURLTransformer extends MultiMethodTest { 43 44 public XURLTransformer oObj = null; 45 46 URL url; 47 48 final static String user = "user"; 49 final static String invalidUserPrefix = "1"; 50 final static String password = "password"; 51 final static String server = "server"; 52 final static String invalidServerPrefix = "1"; 53 final static String port = "8080"; 54 final static String path = "/pub/path"; 55 final static String name = "file.txt"; 56 final static String arguments = "a=b"; 57 final static String mark = "mark"; 58 59 final static String expectedCompleteHTTP = "http://" 60 + server + ":" + port + path 61 + "/" + name + "?" + arguments + "#" + mark; 62 final static String expectedCompleteFTP = "ftp://" 63 + user + ":" + password + "@" + server + ":" + port + path 64 + "/" + name; 65 66 /** 67 * First the complete URL (all URL fields are filled) is 68 * passed and assembled. Then incomplete URL (only 69 * <code>Server</code> field is set) is passed. <p> 70 * Has <b> OK </b> status if in the first case <code>true</code> 71 * retruned and <code>Complete</code> field is set and in the 72 * second case <code>false</code> is returned. <p> 73 */ _assemble()74 public void _assemble(){ 75 URL[] url = new URL[1]; 76 url[0] = new URL(); 77 78 url[0].Protocol = "http://"; 79 url[0].Server = server; 80 url[0].Port = new Integer(port).shortValue(); 81 url[0].Path = path; 82 url[0].Name = name; 83 url[0].Arguments = arguments; 84 url[0].Mark = mark; 85 url[0].Main = "http://" + server + ":" + 86 port + path + "/" + name; 87 88 boolean res = true; 89 90 log.print("assemble http-URL: "); 91 boolean complete = oObj.assemble(url); 92 log.println(complete); 93 res &= complete; 94 95 if (!expectedCompleteHTTP.equals(url[0].Complete)) { 96 log.println("assemble works wrong"); 97 log.println("complete field : " + url[0].Complete); 98 log.println("expected : " + expectedCompleteHTTP); 99 res = false; 100 } 101 102 url[0] = new URL(); 103 url[0].Protocol = "ftp://"; 104 url[0].User = user; 105 url[0].Password = password; 106 url[0].Server = server; 107 url[0].Port = new Integer(port).shortValue(); 108 url[0].Path = path; 109 url[0].Name = name; 110 url[0].Main = "ftp://" + user + ":" + password + "@" + server + ":" + 111 port + path + "/" + name; 112 113 log.print("assemble ftp-URL: "); 114 complete = oObj.assemble(url); 115 log.println(complete); 116 res &= complete; 117 118 if (!expectedCompleteFTP.equals(url[0].Complete)) { 119 log.println("assemble works wrong"); 120 log.println("complete field : " + url[0].Complete); 121 log.println("expected : " + expectedCompleteFTP); 122 res = false; 123 } 124 125 URL[] incompleteUrl = new URL[1]; 126 incompleteUrl[0] = new URL(); 127 incompleteUrl[0].Server = server; 128 129 log.print("assemble incomplete URL: "); 130 complete = oObj.assemble(incompleteUrl); 131 log.println(complete); 132 res &= !complete; 133 134 // should be incomplete 135 tRes.tested("assemble()", res); 136 } 137 138 /** 139 * First the complete URL (<code>Complete</code> field is set 140 * to proper URL) is passed and parsed. Then incomplete URL (only 141 * <code>Server</code> field is set) is passed. <p> 142 * Has <b> OK </b> status if in the first case <code>true</code> 143 * retruned and all URL fields are set to proper values and in the 144 * second case <code>false</code> is returned. <p> 145 */ _parseStrict()146 public void _parseStrict() { 147 URL[] url = new URL[1]; 148 149 url[0] = new URL(); 150 url[0].Complete = expectedCompleteHTTP; 151 152 boolean res = true; 153 log.print("parseStrict(" + expectedCompleteHTTP + "): "); 154 boolean complete = oObj.parseStrict(url); 155 log.println(complete); 156 res &= complete; 157 158 if (!url[0].Protocol.equals("http://")) { 159 log.println("parseStrict works wrong"); 160 log.println("protocol field : " + url[0].Protocol); 161 log.println("expected : http://"); 162 res = false; 163 } 164 165 if (!url[0].Server.equals(server)) { 166 log.println("parseStrict works wrong"); 167 log.println("server field : " + url[0].Server); 168 log.println("expected : " + server); 169 res = false; 170 } 171 172 if (url[0].Port != new Integer(port).shortValue()) { 173 log.println("parseStrict works wrong"); 174 log.println("port field : " + url[0].Port); 175 log.println("expected : " + port); 176 res = false; 177 } 178 179 if ((!url[0].Path.equals(path)) && (!url[0].Path.equals(path + "/"))) { 180 log.println("parseStrict works wrong"); 181 log.println("path field : " + url[0].Path); 182 log.println("expected : " + path); 183 res = false; 184 } 185 186 if (!url[0].Name.equals(name)) { 187 log.println("parseStrict works wrong"); 188 log.println("name field : " + url[0].Name); 189 log.println("expected : " + name); 190 res = false; 191 } 192 193 if (!url[0].Arguments.equals(arguments)) { 194 log.println("parseStrict works wrong"); 195 log.println("arguments field : " + url[0].Arguments); 196 log.println("expected : " + arguments); 197 res = false; 198 } 199 200 if (!url[0].Mark.equals(mark)) { 201 log.println("parseStrict works wrong"); 202 log.println("mark field : " + url[0].Mark); 203 log.println("expected : " + mark); 204 res = false; 205 } 206 207 url[0] = new URL(); 208 url[0].Complete = expectedCompleteFTP; 209 210 log.print("parseStrict(" + expectedCompleteFTP + "): "); 211 complete = oObj.parseStrict(url); 212 log.println(complete); 213 res &= complete; 214 215 if (!url[0].Protocol.equals("ftp://")) { 216 log.println("parseStrict works wrong"); 217 log.println("protocol field : " + url[0].Protocol); 218 log.println("expected : ftp://"); 219 res = false; 220 } 221 222 if (!url[0].User.equals(user)) { 223 log.println("parseStrict works wrong"); 224 log.println("user field : " + url[0].User); 225 log.println("expected : " + user); 226 res = false; 227 } 228 229 if (!url[0].Password.equals(password)) { 230 log.println("parseStrict works wrong"); 231 log.println("password field : " + url[0].Password); 232 log.println("expected : " + password); 233 res = false; 234 } 235 236 if (!url[0].Server.equals(server)) { 237 log.println("parseStrict works wrong"); 238 log.println("server field : " + url[0].Server); 239 log.println("expected : " + server); 240 res = false; 241 } 242 243 if (url[0].Port != new Integer(port).shortValue()) { 244 log.println("parseStrict works wrong"); 245 log.println("port field : " + url[0].Port); 246 log.println("expected : " + port); 247 res = false; 248 } 249 250 if ((!url[0].Path.equals(path)) && (!url[0].Path.equals(path + "/"))) { 251 log.println("parseStrict works wrong"); 252 log.println("path field : " + url[0].Path); 253 log.println("expected : " + path); 254 res = false; 255 } 256 257 if (!url[0].Name.equals(name)) { 258 log.println("parseStrict works wrong"); 259 log.println("name field : " + url[0].Name); 260 log.println("expected : " + name); 261 res = false; 262 } 263 264 URL[] incompleteUrl = new URL[1]; 265 incompleteUrl[0] = new URL(); 266 incompleteUrl[0].Complete = server; 267 268 log.print("parseStrict(" + server + "): "); 269 complete = oObj.parseStrict(incompleteUrl); 270 log.println(complete); 271 // should be incomplete 272 res &= !complete; 273 274 tRes.tested("parseStrict()", res); 275 } 276 277 /** 278 * Tries to parse WWW server name. <p> 279 * Has <b> OK </b> status if the method return <code>true</code> 280 * value and <code>Protocol, Server, Port</code> URL fields are 281 * set properly. 282 */ _parseSmart()283 public void _parseSmart() { 284 URL[] url = new URL[1]; 285 286 String httpURL = invalidServerPrefix + server + ":" + port + path + "/" + name + "?" + 287 arguments + "#" + mark; 288 289 url[0] = new URL(); 290 url[0].Complete = httpURL; 291 292 boolean res = true; 293 log.print("parseSmart('" + httpURL + "', 'http://'): "); 294 boolean complete = oObj.parseSmart(url, "http://"); 295 log.println(complete); 296 res &= complete; 297 298 if (!url[0].Protocol.equals("http://")) { 299 log.println("parseSmart works wrong"); 300 log.println("protocol field : " + url[0].Protocol); 301 log.println("expected : http://"); 302 res = false; 303 } 304 305 if (!url[0].Server.equals(invalidServerPrefix+server)) { 306 log.println("parseSmart works wrong"); 307 log.println("server field : " + url[0].Server); 308 log.println("expected : " + server); 309 res = false; 310 } 311 312 if (url[0].Port != new Integer(port).shortValue()) { 313 log.println("parseSmart works wrong"); 314 log.println("port field : " + url[0].Port); 315 log.println("expected : " + port); 316 res = false; 317 } 318 319 if ((!url[0].Path.equals(path)) && (!url[0].Path.equals(path + "/"))) { 320 log.println("parseSmart works wrong"); 321 log.println("path field : " + url[0].Path); 322 log.println("expected : " + path); 323 res = false; 324 } 325 326 if (!url[0].Name.equals(name)) { 327 log.println("parseSmart works wrong"); 328 log.println("name field : " + url[0].Name); 329 log.println("expected : " + name); 330 res = false; 331 } 332 333 if (!url[0].Arguments.equals(arguments)) { 334 log.println("parseSmart works wrong"); 335 log.println("arguments field : " + url[0].Arguments); 336 log.println("expected : " + arguments); 337 res = false; 338 } 339 340 if (!url[0].Mark.equals(mark)) { 341 log.println("parseSmart works wrong"); 342 log.println("mark field : " + url[0].Mark); 343 log.println("expected : " + mark); 344 res = false; 345 } 346 347 String ftpURL = invalidUserPrefix +user + ":" + password + "@" + server + ":" + 348 port + path + "/" + name; 349 350 url[0] = new URL(); 351 url[0].Complete = ftpURL; 352 log.print("parseSmart('" + ftpURL + "', 'ftp://'): "); 353 complete = oObj.parseSmart(url, "ftp://"); 354 log.println(complete); 355 res &= complete; 356 357 if (!url[0].Protocol.equals("ftp://")) { 358 log.println("parseSmart works wrong"); 359 log.println("protocol field : " + url[0].Protocol); 360 log.println("expected : ftp://"); 361 res = false; 362 } 363 364 if (!url[0].User.equals(invalidUserPrefix+user)) { 365 log.println("parseSmart works wrong"); 366 log.println("user field : " + url[0].User); 367 log.println("expected : " + user); 368 res = false; 369 } 370 371 if (!url[0].Password.equals(password)) { 372 log.println("parseSmart works wrong"); 373 log.println("password field : " + url[0].Password); 374 log.println("expected : " + password); 375 res = false; 376 } 377 378 if (!url[0].Server.equals(server)) { 379 log.println("parseSmart works wrong"); 380 log.println("server field : " + url[0].Server); 381 log.println("expected : " + server); 382 res = false; 383 } 384 385 if (url[0].Port != new Integer(port).shortValue()) { 386 log.println("parseSmart works wrong"); 387 log.println("port field : " + url[0].Port); 388 log.println("expected : " + port); 389 res = false; 390 } 391 392 if ((!url[0].Path.equals(path)) && (!url[0].Path.equals(path + "/"))) { 393 log.println("parseSmart works wrong"); 394 log.println("path field : " + url[0].Path); 395 log.println("expected : " + path); 396 res = false; 397 } 398 399 if (!url[0].Name.equals(name)) { 400 log.println("parseSmart works wrong"); 401 log.println("name field : " + url[0].Name); 402 log.println("expected : " + name); 403 res = false; 404 } 405 406 tRes.tested("parseSmart()", res); 407 } 408 409 /** 410 * Gets the presentation of a URL. <p> 411 * Has <b> OK </b> status if the method returns the same 412 * URL as was passed in parameter. 413 */ _getPresentation()414 public void _getPresentation() { 415 URL url = new URL(); 416 417 url.Complete = expectedCompleteHTTP; 418 419 log.println("getPresentation('" + expectedCompleteHTTP + "', true): "); 420 String presentation = oObj.getPresentation(url, true); 421 boolean res = presentation.equals(expectedCompleteHTTP); 422 log.println("Resulted presentation: " + presentation); 423 log.println("Expected presentation: " + expectedCompleteHTTP); 424 log.println("Result: " + res); 425 426 url.Complete = expectedCompleteFTP; 427 log.println("getPresentation('" + expectedCompleteFTP + "', false): "); 428 // the password must be masqurade with <****> 429 String asterix = ""; 430 for (int n = 0 ; n < password.length(); n++){ 431 asterix += "*"; 432 } 433 asterix = "<" + asterix.substring(1,asterix.length()); 434 asterix = asterix.substring(0,asterix.length()-1) + ">"; 435 436 presentation = oObj.getPresentation(url, false); 437 String expectedPresentation = "ftp://" + user + ":" + asterix + "@" + 438 server + ":" + port + path + "/" + name; 439 res &= presentation.equals(expectedPresentation); 440 log.println("Resulted presentation: " + presentation); 441 log.println("Expected presentation: " + expectedPresentation); 442 log.println("Result: " + res); 443 444 log.println("getPresentation('" + expectedCompleteFTP + "', true): "); 445 presentation = oObj.getPresentation(url, true); 446 expectedPresentation = "ftp://" + user + ":" + password + "@" + 447 server + ":" + port + path + "/" + name; 448 res &= presentation.equals(expectedPresentation); 449 log.println("Resulted presentation: " + presentation); 450 log.println("Expected presentation: " + expectedPresentation); 451 log.println("Result: " + res); 452 453 String incorrectURL = "*bla-bla*"; 454 url.Complete = incorrectURL; 455 log.println("getPresentation('" + incorrectURL + "', false): "); 456 presentation = oObj.getPresentation(url, false); 457 expectedPresentation = ""; 458 res &= presentation.equals(expectedPresentation); 459 log.println("Resulted presentation: " + presentation); 460 log.println("Expected presentation: " + expectedPresentation); 461 log.println("Result: " + res); 462 463 tRes.tested("getPresentation()", res); 464 } 465 466 } // finish class _XURLTransformer 467 468