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;
41 static final String user
= "user";
42 static final String invalidUserPrefix
= "1";
43 static final String password
= "password";
44 static final String server
= "server";
45 static final String invalidServerPrefix
= "1";
46 static final String port
= "8080";
47 static final String path
= "/pub/path";
48 static final String name
= "file.txt";
49 static final String arguments
= "a=b";
50 static final String mark
= "mark";
52 static final String expectedCompleteHTTP
= "http://"
53 + server
+ ":" + port
+ path
54 + "/" + name
+ "?" + arguments
+ "#" + mark
;
55 static final String expectedCompleteFTP
= "ftp://"
56 + user
+ ":" + password
+ "@" + server
+ ":" + port
+ path
60 * First the complete URL (all URL fields are filled) is
61 * passed and assembled. Then incomplete URL (only
62 * <code>Server</code> field is set) is passed. <p>
63 * Has <b> OK </b> status if in the first case <code>true</code>
64 * returned and <code>Complete</code> field is set and in the
65 * second case <code>false</code> is returned. <p>
67 public void _assemble(){
68 URL
[] url
= new URL
[1];
71 url
[0].Protocol
= "http://";
72 url
[0].Server
= server
;
73 url
[0].Port
= Short
.parseShort(port
);
76 url
[0].Arguments
= arguments
;
78 url
[0].Main
= "http://" + server
+ ":" +
79 port
+ path
+ "/" + name
;
83 log
.print("assemble http-URL: ");
84 boolean complete
= oObj
.assemble(url
);
85 log
.println(complete
);
88 if (!expectedCompleteHTTP
.equals(url
[0].Complete
)) {
89 log
.println("assemble works wrong");
90 log
.println("complete field : " + url
[0].Complete
);
91 log
.println("expected : " + expectedCompleteHTTP
);
96 url
[0].Protocol
= "ftp://";
98 url
[0].Password
= password
;
99 url
[0].Server
= server
;
100 url
[0].Port
= Short
.parseShort(port
);
103 url
[0].Main
= "ftp://" + user
+ ":" + password
+ "@" + server
+ ":" +
104 port
+ path
+ "/" + name
;
106 log
.print("assemble ftp-URL: ");
107 complete
= oObj
.assemble(url
);
108 log
.println(complete
);
111 if (!expectedCompleteFTP
.equals(url
[0].Complete
)) {
112 log
.println("assemble works wrong");
113 log
.println("complete field : " + url
[0].Complete
);
114 log
.println("expected : " + expectedCompleteFTP
);
118 URL
[] incompleteUrl
= new URL
[1];
119 incompleteUrl
[0] = new URL();
120 incompleteUrl
[0].Server
= server
;
122 log
.print("assemble incomplete URL: ");
123 complete
= oObj
.assemble(incompleteUrl
);
124 log
.println(complete
);
127 // should be incomplete
128 tRes
.tested("assemble()", res
);
132 * First the complete URL (<code>Complete</code> field is set
133 * to proper URL) is passed and parsed. Then incomplete URL (only
134 * <code>Server</code> field is set) is passed. <p>
135 * Has <b> OK </b> status if in the first case <code>true</code>
136 * returned and all URL fields are set to proper values and in the
137 * second case <code>false</code> is returned. <p>
139 public void _parseStrict() {
140 URL
[] url
= new URL
[1];
143 url
[0].Complete
= expectedCompleteHTTP
;
146 log
.print("parseStrict(" + expectedCompleteHTTP
+ "): ");
147 boolean complete
= oObj
.parseStrict(url
);
148 log
.println(complete
);
151 if (!url
[0].Protocol
.equals("http://")) {
152 log
.println("parseStrict works wrong");
153 log
.println("protocol field : " + url
[0].Protocol
);
154 log
.println("expected : http://");
158 if (!url
[0].Server
.equals(server
)) {
159 log
.println("parseStrict works wrong");
160 log
.println("server field : " + url
[0].Server
);
161 log
.println("expected : " + server
);
165 if (url
[0].Port
!= Short
.parseShort(port
)) {
166 log
.println("parseStrict works wrong");
167 log
.println("port field : " + url
[0].Port
);
168 log
.println("expected : " + port
);
172 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
173 log
.println("parseStrict works wrong");
174 log
.println("path field : " + url
[0].Path
);
175 log
.println("expected : " + path
);
179 if (!url
[0].Name
.equals(name
)) {
180 log
.println("parseStrict works wrong");
181 log
.println("name field : " + url
[0].Name
);
182 log
.println("expected : " + name
);
186 if (!url
[0].Arguments
.equals(arguments
)) {
187 log
.println("parseStrict works wrong");
188 log
.println("arguments field : " + url
[0].Arguments
);
189 log
.println("expected : " + arguments
);
193 if (!url
[0].Mark
.equals(mark
)) {
194 log
.println("parseStrict works wrong");
195 log
.println("mark field : " + url
[0].Mark
);
196 log
.println("expected : " + mark
);
201 url
[0].Complete
= expectedCompleteFTP
;
203 log
.print("parseStrict(" + expectedCompleteFTP
+ "): ");
204 complete
= oObj
.parseStrict(url
);
205 log
.println(complete
);
208 if (!url
[0].Protocol
.equals("ftp://")) {
209 log
.println("parseStrict works wrong");
210 log
.println("protocol field : " + url
[0].Protocol
);
211 log
.println("expected : ftp://");
215 if (!url
[0].User
.equals(user
)) {
216 log
.println("parseStrict works wrong");
217 log
.println("user field : " + url
[0].User
);
218 log
.println("expected : " + user
);
222 if (!url
[0].Password
.equals(password
)) {
223 log
.println("parseStrict works wrong");
224 log
.println("password field : " + url
[0].Password
);
225 log
.println("expected : " + password
);
229 if (!url
[0].Server
.equals(server
)) {
230 log
.println("parseStrict works wrong");
231 log
.println("server field : " + url
[0].Server
);
232 log
.println("expected : " + server
);
236 if (url
[0].Port
!= Short
.parseShort(port
)) {
237 log
.println("parseStrict works wrong");
238 log
.println("port field : " + url
[0].Port
);
239 log
.println("expected : " + port
);
243 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
244 log
.println("parseStrict works wrong");
245 log
.println("path field : " + url
[0].Path
);
246 log
.println("expected : " + path
);
250 if (!url
[0].Name
.equals(name
)) {
251 log
.println("parseStrict works wrong");
252 log
.println("name field : " + url
[0].Name
);
253 log
.println("expected : " + name
);
257 URL
[] incompleteUrl
= new URL
[1];
258 incompleteUrl
[0] = new URL();
259 incompleteUrl
[0].Complete
= server
;
261 log
.print("parseStrict(" + server
+ "): ");
262 complete
= oObj
.parseStrict(incompleteUrl
);
263 log
.println(complete
);
264 // should be incomplete
267 tRes
.tested("parseStrict()", res
);
271 * Tries to parse WWW server name. <p>
272 * Has <b> OK </b> status if the method return <code>true</code>
273 * value and <code>Protocol, Server, Port</code> URL fields are
276 public void _parseSmart() {
277 URL
[] url
= new URL
[1];
279 String httpURL
= invalidServerPrefix
+ server
+ ":" + port
+ path
+ "/" + name
+ "?" +
280 arguments
+ "#" + mark
;
283 url
[0].Complete
= httpURL
;
286 log
.print("parseSmart('" + httpURL
+ "', 'http://'): ");
287 boolean complete
= oObj
.parseSmart(url
, "http://");
288 log
.println(complete
);
291 if (!url
[0].Protocol
.equals("http://")) {
292 log
.println("parseSmart works wrong");
293 log
.println("protocol field : " + url
[0].Protocol
);
294 log
.println("expected : http://");
298 if (!url
[0].Server
.equals(invalidServerPrefix
+server
)) {
299 log
.println("parseSmart works wrong");
300 log
.println("server field : " + url
[0].Server
);
301 log
.println("expected : " + server
);
305 if (url
[0].Port
!= Short
.parseShort(port
)) {
306 log
.println("parseSmart works wrong");
307 log
.println("port field : " + url
[0].Port
);
308 log
.println("expected : " + port
);
312 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
313 log
.println("parseSmart works wrong");
314 log
.println("path field : " + url
[0].Path
);
315 log
.println("expected : " + path
);
319 if (!url
[0].Name
.equals(name
)) {
320 log
.println("parseSmart works wrong");
321 log
.println("name field : " + url
[0].Name
);
322 log
.println("expected : " + name
);
326 if (!url
[0].Arguments
.equals(arguments
)) {
327 log
.println("parseSmart works wrong");
328 log
.println("arguments field : " + url
[0].Arguments
);
329 log
.println("expected : " + arguments
);
333 if (!url
[0].Mark
.equals(mark
)) {
334 log
.println("parseSmart works wrong");
335 log
.println("mark field : " + url
[0].Mark
);
336 log
.println("expected : " + mark
);
340 String ftpURL
= invalidUserPrefix
+user
+ ":" + password
+ "@" + server
+ ":" +
341 port
+ path
+ "/" + name
;
344 url
[0].Complete
= ftpURL
;
345 log
.print("parseSmart('" + ftpURL
+ "', 'ftp://'): ");
346 complete
= oObj
.parseSmart(url
, "ftp://");
347 log
.println(complete
);
350 if (!url
[0].Protocol
.equals("ftp://")) {
351 log
.println("parseSmart works wrong");
352 log
.println("protocol field : " + url
[0].Protocol
);
353 log
.println("expected : ftp://");
357 if (!url
[0].User
.equals(invalidUserPrefix
+user
)) {
358 log
.println("parseSmart works wrong");
359 log
.println("user field : " + url
[0].User
);
360 log
.println("expected : " + user
);
364 if (!url
[0].Password
.equals(password
)) {
365 log
.println("parseSmart works wrong");
366 log
.println("password field : " + url
[0].Password
);
367 log
.println("expected : " + password
);
371 if (!url
[0].Server
.equals(server
)) {
372 log
.println("parseSmart works wrong");
373 log
.println("server field : " + url
[0].Server
);
374 log
.println("expected : " + server
);
378 if (url
[0].Port
!= Short
.parseShort(port
)) {
379 log
.println("parseSmart works wrong");
380 log
.println("port field : " + url
[0].Port
);
381 log
.println("expected : " + port
);
385 if ((!url
[0].Path
.equals(path
)) && (!url
[0].Path
.equals(path
+ "/"))) {
386 log
.println("parseSmart works wrong");
387 log
.println("path field : " + url
[0].Path
);
388 log
.println("expected : " + path
);
392 if (!url
[0].Name
.equals(name
)) {
393 log
.println("parseSmart works wrong");
394 log
.println("name field : " + url
[0].Name
);
395 log
.println("expected : " + name
);
399 tRes
.tested("parseSmart()", res
);
403 * Gets the presentation of a URL. <p>
404 * Has <b> OK </b> status if the method returns the same
405 * URL as was passed in parameter.
407 public void _getPresentation() {
410 url
.Complete
= expectedCompleteHTTP
;
412 log
.println("getPresentation('" + expectedCompleteHTTP
+ "', true): ");
413 String presentation
= oObj
.getPresentation(url
, true);
414 boolean res
= presentation
.equals(expectedCompleteHTTP
);
415 log
.println("Resulted presentation: " + presentation
);
416 log
.println("Expected presentation: " + expectedCompleteHTTP
);
417 log
.println("Result: " + res
);
419 url
.Complete
= expectedCompleteFTP
;
420 log
.println("getPresentation('" + expectedCompleteFTP
+ "', false): ");
421 // the password must be masquerade with <****>
422 StringBuilder sb
= new StringBuilder();
423 for (int n
= 0 ; n
< password
.length(); n
++){
426 String asterisk
= sb
.toString();
427 asterisk
= "<" + asterisk
.substring(1,asterisk
.length());
428 asterisk
= asterisk
.substring(0,asterisk
.length()-1) + ">";
430 presentation
= oObj
.getPresentation(url
, false);
431 String expectedPresentation
= "ftp://" + user
+ ":" + asterisk
+ "@" +
432 server
+ ":" + port
+ path
+ "/" + name
;
433 res
&= presentation
.equals(expectedPresentation
);
434 log
.println("Resulted presentation: " + presentation
);
435 log
.println("Expected presentation: " + expectedPresentation
);
436 log
.println("Result: " + res
);
438 log
.println("getPresentation('" + expectedCompleteFTP
+ "', true): ");
439 presentation
= oObj
.getPresentation(url
, true);
440 expectedPresentation
= "ftp://" + user
+ ":" + password
+ "@" +
441 server
+ ":" + port
+ path
+ "/" + name
;
442 res
&= presentation
.equals(expectedPresentation
);
443 log
.println("Resulted presentation: " + presentation
);
444 log
.println("Expected presentation: " + expectedPresentation
);
445 log
.println("Result: " + res
);
447 String incorrectURL
= "*bla-bla*";
448 url
.Complete
= incorrectURL
;
449 log
.println("getPresentation('" + incorrectURL
+ "', false): ");
450 presentation
= oObj
.getPresentation(url
, false);
451 expectedPresentation
= "";
452 res
&= presentation
.equals(expectedPresentation
);
453 log
.println("Resulted presentation: " + presentation
);
454 log
.println("Expected presentation: " + expectedPresentation
);
455 log
.println("Result: " + res
);
457 tRes
.tested("getPresentation()", res
);
460 } // finish class _XURLTransformer