1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org 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 Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import com
.sun
.star
.util
.URL
;
31 import com
.sun
.star
.util
.XURLTransformer
;
32 import lib
.MultiMethodTest
;
35 * Testing <code>com.sun.star.util.XURLTransformer</code>
38 * <li><code> assemble() </code></li>
39 * <li><code> parseStrict() </code></li>
40 * <li><code> parseSmart() </code></li>
41 * <li><code> getPresentation() </code></li>
43 * Test is <b> NOT </b> multithread compilant. <p>
44 * @see com.sun.star.util.XURLTransformer
46 public class _XURLTransformer
extends MultiMethodTest
{
48 public XURLTransformer oObj
= null;
52 final static String user
= "user";
53 final static String invalidUserPrefix
= "1";
54 final static String password
= "password";
55 final static String server
= "server";
56 final static String invalidServerPrefix
= "1";
57 final static String port
= "8080";
58 final static String path
= "/pub/path";
59 final static String name
= "file.txt";
60 final static String arguments
= "a=b";
61 final static String mark
= "mark";
63 final static String expectedCompleteHTTP
= "http://"
64 + server
+ ":" + port
+ path
65 + "/" + name
+ "?" + arguments
+ "#" + mark
;
66 final static String expectedCompleteFTP
= "ftp://"
67 + user
+ ":" + password
+ "@" + server
+ ":" + port
+ path
71 * First the complete URL (all URL fields are filled) is
72 * passed and assembled. Then incomplete URL (only
73 * <code>Server</code> field is set) is passed. <p>
74 * Has <b> OK </b> status if in the first case <code>true</code>
75 * retruned and <code>Complete</code> field is set and in the
76 * second case <code>false</code> is returned. <p>
78 public void _assemble(){
79 URL
[] url
= new URL
[1];
82 url
[0].Protocol
= "http://";
83 url
[0].Server
= server
;
84 url
[0].Port
= new Integer(port
).shortValue();
87 url
[0].Arguments
= arguments
;
89 url
[0].Main
= "http://" + server
+ ":" +
90 port
+ path
+ "/" + name
;
94 log
.print("assemble http-URL: ");
95 boolean complete
= oObj
.assemble(url
);
96 log
.println(complete
);
99 if (!expectedCompleteHTTP
.equals(url
[0].Complete
)) {
100 log
.println("assemble works wrong");
101 log
.println("complete field : " + url
[0].Complete
);
102 log
.println("expected : " + expectedCompleteHTTP
);
107 url
[0].Protocol
= "ftp://";
109 url
[0].Password
= password
;
110 url
[0].Server
= server
;
111 url
[0].Port
= new Integer(port
).shortValue();
114 url
[0].Main
= "ftp://" + user
+ ":" + password
+ "@" + server
+ ":" +
115 port
+ path
+ "/" + name
;
117 log
.print("assemble ftp-URL: ");
118 complete
= oObj
.assemble(url
);
119 log
.println(complete
);
122 if (!expectedCompleteFTP
.equals(url
[0].Complete
)) {
123 log
.println("assemble works wrong");
124 log
.println("complete field : " + url
[0].Complete
);
125 log
.println("expected : " + expectedCompleteFTP
);
129 URL
[] incompleteUrl
= new URL
[1];
130 incompleteUrl
[0] = new URL();
131 incompleteUrl
[0].Server
= server
;
133 log
.print("assemble incomplete URL: ");
134 complete
= oObj
.assemble(incompleteUrl
);
135 log
.println(complete
);
138 // should be incomplete
139 tRes
.tested("assemble()", res
);
143 * First the complete URL (<code>Complete</code> field is set
144 * to proper URL) is passed and parsed. Then incomplete URL (only
145 * <code>Server</code> field is set) is passed. <p>
146 * Has <b> OK </b> status if in the first case <code>true</code>
147 * retruned and all URL fields are set to proper values and in the
148 * second case <code>false</code> is returned. <p>
150 public void _parseStrict() {
151 URL
[] url
= new URL
[1];
154 url
[0].Complete
= expectedCompleteHTTP
;
157 log
.print("parseStrict(" + expectedCompleteHTTP
+ "): ");
158 boolean complete
= oObj
.parseStrict(url
);
159 log
.println(complete
);
162 if (!url
[0].Protocol
.equals("http://")) {
163 log
.println("parseStrict works wrong");
164 log
.println("protocol field : " + url
[0].Protocol
);
165 log
.println("expected : http://");
169 if (!url
[0].Server
.equals(server
)) {
170 log
.println("parseStrict works wrong");
171 log
.println("server field : " + url
[0].Server
);
172 log
.println("expected : " + server
);
176 if (url
[0].Port
!= new Integer(port
).shortValue()) {
177 log
.println("parseStrict works wrong");
178 log
.println("port field : " + url
[0].Port
);
179 log
.println("expected : " + port
);
183 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
184 log
.println("parseStrict works wrong");
185 log
.println("path field : " + url
[0].Path
);
186 log
.println("expected : " + path
);
190 if (!url
[0].Name
.equals(name
)) {
191 log
.println("parseStrict works wrong");
192 log
.println("name field : " + url
[0].Name
);
193 log
.println("expected : " + name
);
197 if (!url
[0].Arguments
.equals(arguments
)) {
198 log
.println("parseStrict works wrong");
199 log
.println("arguments field : " + url
[0].Arguments
);
200 log
.println("expected : " + arguments
);
204 if (!url
[0].Mark
.equals(mark
)) {
205 log
.println("parseStrict works wrong");
206 log
.println("mark field : " + url
[0].Mark
);
207 log
.println("expected : " + mark
);
212 url
[0].Complete
= expectedCompleteFTP
;
214 log
.print("parseStrict(" + expectedCompleteFTP
+ "): ");
215 complete
= oObj
.parseStrict(url
);
216 log
.println(complete
);
219 if (!url
[0].Protocol
.equals("ftp://")) {
220 log
.println("parseStrict works wrong");
221 log
.println("protocol field : " + url
[0].Protocol
);
222 log
.println("expected : ftp://");
226 if (!url
[0].User
.equals(user
)) {
227 log
.println("parseStrict works wrong");
228 log
.println("user field : " + url
[0].User
);
229 log
.println("expected : " + user
);
233 if (!url
[0].Password
.equals(password
)) {
234 log
.println("parseStrict works wrong");
235 log
.println("password field : " + url
[0].Password
);
236 log
.println("expected : " + password
);
240 if (!url
[0].Server
.equals(server
)) {
241 log
.println("parseStrict works wrong");
242 log
.println("server field : " + url
[0].Server
);
243 log
.println("expected : " + server
);
247 if (url
[0].Port
!= new Integer(port
).shortValue()) {
248 log
.println("parseStrict works wrong");
249 log
.println("port field : " + url
[0].Port
);
250 log
.println("expected : " + port
);
254 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
255 log
.println("parseStrict works wrong");
256 log
.println("path field : " + url
[0].Path
);
257 log
.println("expected : " + path
);
261 if (!url
[0].Name
.equals(name
)) {
262 log
.println("parseStrict works wrong");
263 log
.println("name field : " + url
[0].Name
);
264 log
.println("expected : " + name
);
268 URL
[] incompleteUrl
= new URL
[1];
269 incompleteUrl
[0] = new URL();
270 incompleteUrl
[0].Complete
= server
;
272 log
.print("parseStrict(" + server
+ "): ");
273 complete
= oObj
.parseStrict(incompleteUrl
);
274 log
.println(complete
);
275 // should be incomplete
278 tRes
.tested("parseStrict()", res
);
282 * Tries to parse WWW server name. <p>
283 * Has <b> OK </b> status if the method return <code>true</code>
284 * value and <code>Protocol, Server, Port</code> URL fields are
287 public void _parseSmart() {
288 URL
[] url
= new URL
[1];
290 String httpURL
= invalidServerPrefix
+ server
+ ":" + port
+ path
+ "/" + name
+ "?" +
291 arguments
+ "#" + mark
;
294 url
[0].Complete
= httpURL
;
297 log
.print("parseSmart('" + httpURL
+ "', 'http://'): ");
298 boolean complete
= oObj
.parseSmart(url
, "http://");
299 log
.println(complete
);
302 if (!url
[0].Protocol
.equals("http://")) {
303 log
.println("parseSmart works wrong");
304 log
.println("protocol field : " + url
[0].Protocol
);
305 log
.println("expected : http://");
309 if (!url
[0].Server
.equals(invalidServerPrefix
+server
)) {
310 log
.println("parseSmart works wrong");
311 log
.println("server field : " + url
[0].Server
);
312 log
.println("expected : " + server
);
316 if (url
[0].Port
!= new Integer(port
).shortValue()) {
317 log
.println("parseSmart works wrong");
318 log
.println("port field : " + url
[0].Port
);
319 log
.println("expected : " + port
);
323 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
324 log
.println("parseSmart works wrong");
325 log
.println("path field : " + url
[0].Path
);
326 log
.println("expected : " + path
);
330 if (!url
[0].Name
.equals(name
)) {
331 log
.println("parseSmart works wrong");
332 log
.println("name field : " + url
[0].Name
);
333 log
.println("expected : " + name
);
337 if (!url
[0].Arguments
.equals(arguments
)) {
338 log
.println("parseSmart works wrong");
339 log
.println("arguments field : " + url
[0].Arguments
);
340 log
.println("expected : " + arguments
);
344 if (!url
[0].Mark
.equals(mark
)) {
345 log
.println("parseSmart works wrong");
346 log
.println("mark field : " + url
[0].Mark
);
347 log
.println("expected : " + mark
);
351 String ftpURL
= invalidUserPrefix
+user
+ ":" + password
+ "@" + server
+ ":" +
352 port
+ path
+ "/" + name
;
355 url
[0].Complete
= ftpURL
;
356 log
.print("parseSmart('" + ftpURL
+ "', 'ftp://'): ");
357 complete
= oObj
.parseSmart(url
, "ftp://");
358 log
.println(complete
);
361 if (!url
[0].Protocol
.equals("ftp://")) {
362 log
.println("parseSmart works wrong");
363 log
.println("protocol field : " + url
[0].Protocol
);
364 log
.println("expected : ftp://");
368 if (!url
[0].User
.equals(invalidUserPrefix
+user
)) {
369 log
.println("parseSmart works wrong");
370 log
.println("user field : " + url
[0].User
);
371 log
.println("expected : " + user
);
375 if (!url
[0].Password
.equals(password
)) {
376 log
.println("parseSmart works wrong");
377 log
.println("password field : " + url
[0].Password
);
378 log
.println("expected : " + password
);
382 if (!url
[0].Server
.equals(server
)) {
383 log
.println("parseSmart works wrong");
384 log
.println("server field : " + url
[0].Server
);
385 log
.println("expected : " + server
);
389 if (url
[0].Port
!= new Integer(port
).shortValue()) {
390 log
.println("parseSmart works wrong");
391 log
.println("port field : " + url
[0].Port
);
392 log
.println("expected : " + port
);
396 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
397 log
.println("parseSmart works wrong");
398 log
.println("path field : " + url
[0].Path
);
399 log
.println("expected : " + path
);
403 if (!url
[0].Name
.equals(name
)) {
404 log
.println("parseSmart works wrong");
405 log
.println("name field : " + url
[0].Name
);
406 log
.println("expected : " + name
);
410 tRes
.tested("parseSmart()", res
);
414 * Gets the presentation of a URL. <p>
415 * Has <b> OK </b> status if the method returns the same
416 * URL as was passed in parameter.
418 public void _getPresentation() {
421 url
.Complete
= expectedCompleteHTTP
;
423 log
.println("getPresentation('" + expectedCompleteHTTP
+ "', true): ");
424 String presentation
= oObj
.getPresentation(url
, true);
425 boolean res
= presentation
.equals(expectedCompleteHTTP
);
426 log
.println("Resulted presentation: " + presentation
);
427 log
.println("Expected presentation: " + expectedCompleteHTTP
);
428 log
.println("Result: " + res
);
430 url
.Complete
= expectedCompleteFTP
;
431 log
.println("getPresentation('" + expectedCompleteFTP
+ "', false): ");
432 // the password must be masqurade with <****>
434 for (int n
= 0 ; n
< password
.length(); n
++){
437 asterix
= "<" + asterix
.substring(1,asterix
.length());
438 asterix
= asterix
.substring(0,asterix
.length()-1) + ">";
440 presentation
= oObj
.getPresentation(url
, false);
441 String expectedPresentation
= "ftp://" + user
+ ":" + asterix
+ "@" +
442 server
+ ":" + port
+ path
+ "/" + name
;
443 res
&= presentation
.equals(expectedPresentation
);
444 log
.println("Resulted presentation: " + presentation
);
445 log
.println("Expected presentation: " + expectedPresentation
);
446 log
.println("Result: " + res
);
448 log
.println("getPresentation('" + expectedCompleteFTP
+ "', true): ");
449 presentation
= oObj
.getPresentation(url
, true);
450 expectedPresentation
= "ftp://" + user
+ ":" + password
+ "@" +
451 server
+ ":" + port
+ path
+ "/" + name
;
452 res
&= presentation
.equals(expectedPresentation
);
453 log
.println("Resulted presentation: " + presentation
);
454 log
.println("Expected presentation: " + expectedPresentation
);
455 log
.println("Result: " + res
);
457 String incorrectURL
= "*bla-bla*";
458 url
.Complete
= incorrectURL
;
459 log
.println("getPresentation('" + incorrectURL
+ "', false): ");
460 presentation
= oObj
.getPresentation(url
, false);
461 expectedPresentation
= "";
462 res
&= presentation
.equals(expectedPresentation
);
463 log
.println("Resulted presentation: " + presentation
);
464 log
.println("Expected presentation: " + expectedPresentation
);
465 log
.println("Result: " + res
);
467 tRes
.tested("getPresentation()", res
);
470 } // finish class _XURLTransformer