2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import com
.sun
.star
.util
.URL
;
22 import com
.sun
.star
.util
.XURLTransformer
;
23 import lib
.MultiMethodTest
;
26 * Testing <code>com.sun.star.util.XURLTransformer</code>
29 * <li><code> assemble() </code></li>
30 * <li><code> parseStrict() </code></li>
31 * <li><code> parseSmart() </code></li>
32 * <li><code> getPresentation() </code></li>
34 * Test is <b> NOT </b> multithread compliant. <p>
35 * @see com.sun.star.util.XURLTransformer
37 public class _XURLTransformer
extends MultiMethodTest
{
39 public XURLTransformer oObj
= null;
43 final static String user
= "user";
44 final static String invalidUserPrefix
= "1";
45 final static String password
= "password";
46 final static String server
= "server";
47 final static String invalidServerPrefix
= "1";
48 final static String port
= "8080";
49 final static String path
= "/pub/path";
50 final static String name
= "file.txt";
51 final static String arguments
= "a=b";
52 final static String mark
= "mark";
54 final static String expectedCompleteHTTP
= "http://"
55 + server
+ ":" + port
+ path
56 + "/" + name
+ "?" + arguments
+ "#" + mark
;
57 final static String expectedCompleteFTP
= "ftp://"
58 + user
+ ":" + password
+ "@" + server
+ ":" + port
+ path
62 * First the complete URL (all URL fields are filled) is
63 * passed and assembled. Then incomplete URL (only
64 * <code>Server</code> field is set) is passed. <p>
65 * Has <b> OK </b> status if in the first case <code>true</code>
66 * retruned and <code>Complete</code> field is set and in the
67 * second case <code>false</code> is returned. <p>
69 public void _assemble(){
70 URL
[] url
= new URL
[1];
73 url
[0].Protocol
= "http://";
74 url
[0].Server
= server
;
75 url
[0].Port
= Short
.parseShort(port
);
78 url
[0].Arguments
= arguments
;
80 url
[0].Main
= "http://" + server
+ ":" +
81 port
+ path
+ "/" + name
;
85 log
.print("assemble http-URL: ");
86 boolean complete
= oObj
.assemble(url
);
87 log
.println(complete
);
90 if (!expectedCompleteHTTP
.equals(url
[0].Complete
)) {
91 log
.println("assemble works wrong");
92 log
.println("complete field : " + url
[0].Complete
);
93 log
.println("expected : " + expectedCompleteHTTP
);
98 url
[0].Protocol
= "ftp://";
100 url
[0].Password
= password
;
101 url
[0].Server
= server
;
102 url
[0].Port
= Short
.parseShort(port
);
105 url
[0].Main
= "ftp://" + user
+ ":" + password
+ "@" + server
+ ":" +
106 port
+ path
+ "/" + name
;
108 log
.print("assemble ftp-URL: ");
109 complete
= oObj
.assemble(url
);
110 log
.println(complete
);
113 if (!expectedCompleteFTP
.equals(url
[0].Complete
)) {
114 log
.println("assemble works wrong");
115 log
.println("complete field : " + url
[0].Complete
);
116 log
.println("expected : " + expectedCompleteFTP
);
120 URL
[] incompleteUrl
= new URL
[1];
121 incompleteUrl
[0] = new URL();
122 incompleteUrl
[0].Server
= server
;
124 log
.print("assemble incomplete URL: ");
125 complete
= oObj
.assemble(incompleteUrl
);
126 log
.println(complete
);
129 // should be incomplete
130 tRes
.tested("assemble()", res
);
134 * First the complete URL (<code>Complete</code> field is set
135 * to proper URL) is passed and parsed. Then incomplete URL (only
136 * <code>Server</code> field is set) is passed. <p>
137 * Has <b> OK </b> status if in the first case <code>true</code>
138 * retruned and all URL fields are set to proper values and in the
139 * second case <code>false</code> is returned. <p>
141 public void _parseStrict() {
142 URL
[] url
= new URL
[1];
145 url
[0].Complete
= expectedCompleteHTTP
;
148 log
.print("parseStrict(" + expectedCompleteHTTP
+ "): ");
149 boolean complete
= oObj
.parseStrict(url
);
150 log
.println(complete
);
153 if (!url
[0].Protocol
.equals("http://")) {
154 log
.println("parseStrict works wrong");
155 log
.println("protocol field : " + url
[0].Protocol
);
156 log
.println("expected : http://");
160 if (!url
[0].Server
.equals(server
)) {
161 log
.println("parseStrict works wrong");
162 log
.println("server field : " + url
[0].Server
);
163 log
.println("expected : " + server
);
167 if (url
[0].Port
!= Short
.parseShort(port
)) {
168 log
.println("parseStrict works wrong");
169 log
.println("port field : " + url
[0].Port
);
170 log
.println("expected : " + port
);
174 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
175 log
.println("parseStrict works wrong");
176 log
.println("path field : " + url
[0].Path
);
177 log
.println("expected : " + path
);
181 if (!url
[0].Name
.equals(name
)) {
182 log
.println("parseStrict works wrong");
183 log
.println("name field : " + url
[0].Name
);
184 log
.println("expected : " + name
);
188 if (!url
[0].Arguments
.equals(arguments
)) {
189 log
.println("parseStrict works wrong");
190 log
.println("arguments field : " + url
[0].Arguments
);
191 log
.println("expected : " + arguments
);
195 if (!url
[0].Mark
.equals(mark
)) {
196 log
.println("parseStrict works wrong");
197 log
.println("mark field : " + url
[0].Mark
);
198 log
.println("expected : " + mark
);
203 url
[0].Complete
= expectedCompleteFTP
;
205 log
.print("parseStrict(" + expectedCompleteFTP
+ "): ");
206 complete
= oObj
.parseStrict(url
);
207 log
.println(complete
);
210 if (!url
[0].Protocol
.equals("ftp://")) {
211 log
.println("parseStrict works wrong");
212 log
.println("protocol field : " + url
[0].Protocol
);
213 log
.println("expected : ftp://");
217 if (!url
[0].User
.equals(user
)) {
218 log
.println("parseStrict works wrong");
219 log
.println("user field : " + url
[0].User
);
220 log
.println("expected : " + user
);
224 if (!url
[0].Password
.equals(password
)) {
225 log
.println("parseStrict works wrong");
226 log
.println("password field : " + url
[0].Password
);
227 log
.println("expected : " + password
);
231 if (!url
[0].Server
.equals(server
)) {
232 log
.println("parseStrict works wrong");
233 log
.println("server field : " + url
[0].Server
);
234 log
.println("expected : " + server
);
238 if (url
[0].Port
!= Short
.parseShort(port
)) {
239 log
.println("parseStrict works wrong");
240 log
.println("port field : " + url
[0].Port
);
241 log
.println("expected : " + port
);
245 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
246 log
.println("parseStrict works wrong");
247 log
.println("path field : " + url
[0].Path
);
248 log
.println("expected : " + path
);
252 if (!url
[0].Name
.equals(name
)) {
253 log
.println("parseStrict works wrong");
254 log
.println("name field : " + url
[0].Name
);
255 log
.println("expected : " + name
);
259 URL
[] incompleteUrl
= new URL
[1];
260 incompleteUrl
[0] = new URL();
261 incompleteUrl
[0].Complete
= server
;
263 log
.print("parseStrict(" + server
+ "): ");
264 complete
= oObj
.parseStrict(incompleteUrl
);
265 log
.println(complete
);
266 // should be incomplete
269 tRes
.tested("parseStrict()", res
);
273 * Tries to parse WWW server name. <p>
274 * Has <b> OK </b> status if the method return <code>true</code>
275 * value and <code>Protocol, Server, Port</code> URL fields are
278 public void _parseSmart() {
279 URL
[] url
= new URL
[1];
281 String httpURL
= invalidServerPrefix
+ server
+ ":" + port
+ path
+ "/" + name
+ "?" +
282 arguments
+ "#" + mark
;
285 url
[0].Complete
= httpURL
;
288 log
.print("parseSmart('" + httpURL
+ "', 'http://'): ");
289 boolean complete
= oObj
.parseSmart(url
, "http://");
290 log
.println(complete
);
293 if (!url
[0].Protocol
.equals("http://")) {
294 log
.println("parseSmart works wrong");
295 log
.println("protocol field : " + url
[0].Protocol
);
296 log
.println("expected : http://");
300 if (!url
[0].Server
.equals(invalidServerPrefix
+server
)) {
301 log
.println("parseSmart works wrong");
302 log
.println("server field : " + url
[0].Server
);
303 log
.println("expected : " + server
);
307 if (url
[0].Port
!= Short
.parseShort(port
)) {
308 log
.println("parseSmart works wrong");
309 log
.println("port field : " + url
[0].Port
);
310 log
.println("expected : " + port
);
314 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
315 log
.println("parseSmart works wrong");
316 log
.println("path field : " + url
[0].Path
);
317 log
.println("expected : " + path
);
321 if (!url
[0].Name
.equals(name
)) {
322 log
.println("parseSmart works wrong");
323 log
.println("name field : " + url
[0].Name
);
324 log
.println("expected : " + name
);
328 if (!url
[0].Arguments
.equals(arguments
)) {
329 log
.println("parseSmart works wrong");
330 log
.println("arguments field : " + url
[0].Arguments
);
331 log
.println("expected : " + arguments
);
335 if (!url
[0].Mark
.equals(mark
)) {
336 log
.println("parseSmart works wrong");
337 log
.println("mark field : " + url
[0].Mark
);
338 log
.println("expected : " + mark
);
342 String ftpURL
= invalidUserPrefix
+user
+ ":" + password
+ "@" + server
+ ":" +
343 port
+ path
+ "/" + name
;
346 url
[0].Complete
= ftpURL
;
347 log
.print("parseSmart('" + ftpURL
+ "', 'ftp://'): ");
348 complete
= oObj
.parseSmart(url
, "ftp://");
349 log
.println(complete
);
352 if (!url
[0].Protocol
.equals("ftp://")) {
353 log
.println("parseSmart works wrong");
354 log
.println("protocol field : " + url
[0].Protocol
);
355 log
.println("expected : ftp://");
359 if (!url
[0].User
.equals(invalidUserPrefix
+user
)) {
360 log
.println("parseSmart works wrong");
361 log
.println("user field : " + url
[0].User
);
362 log
.println("expected : " + user
);
366 if (!url
[0].Password
.equals(password
)) {
367 log
.println("parseSmart works wrong");
368 log
.println("password field : " + url
[0].Password
);
369 log
.println("expected : " + password
);
373 if (!url
[0].Server
.equals(server
)) {
374 log
.println("parseSmart works wrong");
375 log
.println("server field : " + url
[0].Server
);
376 log
.println("expected : " + server
);
380 if (url
[0].Port
!= Short
.parseShort(port
)) {
381 log
.println("parseSmart works wrong");
382 log
.println("port field : " + url
[0].Port
);
383 log
.println("expected : " + port
);
387 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
388 log
.println("parseSmart works wrong");
389 log
.println("path field : " + url
[0].Path
);
390 log
.println("expected : " + path
);
394 if (!url
[0].Name
.equals(name
)) {
395 log
.println("parseSmart works wrong");
396 log
.println("name field : " + url
[0].Name
);
397 log
.println("expected : " + name
);
401 tRes
.tested("parseSmart()", res
);
405 * Gets the presentation of a URL. <p>
406 * Has <b> OK </b> status if the method returns the same
407 * URL as was passed in parameter.
409 public void _getPresentation() {
412 url
.Complete
= expectedCompleteHTTP
;
414 log
.println("getPresentation('" + expectedCompleteHTTP
+ "', true): ");
415 String presentation
= oObj
.getPresentation(url
, true);
416 boolean res
= presentation
.equals(expectedCompleteHTTP
);
417 log
.println("Resulted presentation: " + presentation
);
418 log
.println("Expected presentation: " + expectedCompleteHTTP
);
419 log
.println("Result: " + res
);
421 url
.Complete
= expectedCompleteFTP
;
422 log
.println("getPresentation('" + expectedCompleteFTP
+ "', false): ");
423 // the password must be masqurade with <****>
424 String asterisk
= "";
425 for (int n
= 0 ; n
< password
.length(); n
++){
428 asterisk
= "<" + asterisk
.substring(1,asterisk
.length());
429 asterisk
= asterisk
.substring(0,asterisk
.length()-1) + ">";
431 presentation
= oObj
.getPresentation(url
, false);
432 String expectedPresentation
= "ftp://" + user
+ ":" + asterisk
+ "@" +
433 server
+ ":" + port
+ path
+ "/" + name
;
434 res
&= presentation
.equals(expectedPresentation
);
435 log
.println("Resulted presentation: " + presentation
);
436 log
.println("Expected presentation: " + expectedPresentation
);
437 log
.println("Result: " + res
);
439 log
.println("getPresentation('" + expectedCompleteFTP
+ "', true): ");
440 presentation
= oObj
.getPresentation(url
, true);
441 expectedPresentation
= "ftp://" + user
+ ":" + password
+ "@" +
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 String incorrectURL
= "*bla-bla*";
449 url
.Complete
= incorrectURL
;
450 log
.println("getPresentation('" + incorrectURL
+ "', false): ");
451 presentation
= oObj
.getPresentation(url
, false);
452 expectedPresentation
= "";
453 res
&= presentation
.equals(expectedPresentation
);
454 log
.println("Resulted presentation: " + presentation
);
455 log
.println("Expected presentation: " + expectedPresentation
);
456 log
.println("Result: " + res
);
458 tRes
.tested("getPresentation()", res
);
461 } // finish class _XURLTransformer