1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 package ifc.util;
29 
30 import com.sun.star.util.URL;
31 import com.sun.star.util.XURLTransformer;
32 import lib.MultiMethodTest;
33 
34 /**
35 * Testing <code>com.sun.star.util.XURLTransformer</code>
36 * interface methods :
37 * <ul>
38 *  <li><code> assemble() </code></li>
39 *  <li><code> parseStrict() </code></li>
40 *  <li><code> parseSmart() </code></li>
41 *  <li><code> getPresentation() </code></li>
42 * </ul> <p>
43 * Test is <b> NOT </b> multithread compilant. <p>
44 * @see com.sun.star.util.XURLTransformer
45 */
46 public class _XURLTransformer extends MultiMethodTest {
47 
48     public XURLTransformer oObj = null;
49 
50     URL url;
51 
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";
62 
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
68                 + "/" + name;
69 
70     /**
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>
77      */
78     public void _assemble(){
79         URL[] url = new URL[1];
80         url[0] = new URL();
81 
82         url[0].Protocol = "http://";
83         url[0].Server = server;
84         url[0].Port = new Integer(port).shortValue();
85         url[0].Path = path;
86         url[0].Name = name;
87         url[0].Arguments = arguments;
88         url[0].Mark = mark;
89         url[0].Main = "http://" + server + ":" +
90             port + path + "/" + name;
91 
92         boolean res = true;
93 
94         log.print("assemble http-URL: ");
95         boolean complete = oObj.assemble(url);
96         log.println(complete);
97         res &= complete;
98 
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);
103             res = false;
104         }
105 
106         url[0] = new URL();
107         url[0].Protocol = "ftp://";
108         url[0].User = user;
109         url[0].Password = password;
110         url[0].Server = server;
111         url[0].Port = new Integer(port).shortValue();
112         url[0].Path = path;
113         url[0].Name = name;
114         url[0].Main = "ftp://" + user + ":" + password + "@" + server + ":" +
115             port + path + "/" + name;
116 
117         log.print("assemble ftp-URL: ");
118         complete = oObj.assemble(url);
119         log.println(complete);
120         res &= complete;
121 
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);
126             res = false;
127         }
128 
129         URL[] incompleteUrl = new URL[1];
130         incompleteUrl[0] = new URL();
131         incompleteUrl[0].Server = server;
132 
133         log.print("assemble incomplete URL: ");
134         complete = oObj.assemble(incompleteUrl);
135         log.println(complete);
136         res &= !complete;
137 
138         // should be incomplete
139         tRes.tested("assemble()", res);
140     }
141 
142     /**
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>
149      */
150     public void _parseStrict() {
151         URL[] url = new URL[1];
152 
153         url[0] = new URL();
154         url[0].Complete = expectedCompleteHTTP;
155 
156         boolean res = true;
157         log.print("parseStrict(" + expectedCompleteHTTP + "): ");
158         boolean complete = oObj.parseStrict(url);
159         log.println(complete);
160         res &= complete;
161 
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://");
166             res = false;
167         }
168 
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);
173             res = false;
174         }
175 
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);
180             res = false;
181         }
182 
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);
187             res = false;
188         }
189 
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);
194             res = false;
195         }
196 
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);
201           res = false;
202         }
203 
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);
208             res = false;
209         }
210 
211         url[0] = new URL();
212         url[0].Complete = expectedCompleteFTP;
213 
214         log.print("parseStrict(" + expectedCompleteFTP + "): ");
215         complete = oObj.parseStrict(url);
216         log.println(complete);
217         res &= complete;
218 
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://");
223             res = false;
224         }
225 
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);
230             res = false;
231         }
232 
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);
237             res = false;
238         }
239 
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);
244             res = false;
245         }
246 
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);
251             res = false;
252         }
253 
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);
258             res = false;
259         }
260 
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);
265             res = false;
266         }
267 
268         URL[] incompleteUrl = new URL[1];
269         incompleteUrl[0] = new URL();
270         incompleteUrl[0].Complete = server;
271 
272         log.print("parseStrict(" + server + "): ");
273         complete = oObj.parseStrict(incompleteUrl);
274         log.println(complete);
275         // should be incomplete
276         res &= !complete;
277 
278         tRes.tested("parseStrict()", res);
279     }
280 
281     /**
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
285      * set properly.
286      */
287     public void _parseSmart() {
288         URL[] url = new URL[1];
289 
290         String httpURL = invalidServerPrefix + server + ":" + port + path + "/" + name + "?" +
291             arguments + "#" + mark;
292 
293         url[0] = new URL();
294         url[0].Complete = httpURL;
295 
296         boolean res = true;
297         log.print("parseSmart('" + httpURL + "', 'http://'): ");
298         boolean complete = oObj.parseSmart(url, "http://");
299         log.println(complete);
300         res &= complete;
301 
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://");
306             res = false;
307         }
308 
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);
313             res = false;
314         }
315 
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);
320             res = false;
321         }
322 
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);
327             res = false;
328         }
329 
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);
334             res = false;
335         }
336 
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);
341             res = false;
342         }
343 
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);
348             res = false;
349         }
350 
351         String ftpURL = invalidUserPrefix +user + ":" + password + "@" + server + ":" +
352             port + path + "/" + name;
353 
354         url[0] = new URL();
355         url[0].Complete = ftpURL;
356         log.print("parseSmart('" + ftpURL + "', 'ftp://'): ");
357         complete = oObj.parseSmart(url, "ftp://");
358         log.println(complete);
359         res &= complete;
360 
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://");
365             res = false;
366         }
367 
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);
372             res = false;
373         }
374 
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);
379             res = false;
380         }
381 
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);
386             res = false;
387         }
388 
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);
393             res = false;
394         }
395 
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);
400             res = false;
401         }
402 
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);
407             res = false;
408         }
409 
410         tRes.tested("parseSmart()", res);
411     }
412 
413     /**
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.
417      */
418     public void _getPresentation() {
419         URL url = new URL();
420 
421         url.Complete = expectedCompleteHTTP;
422 
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);
429 
430         url.Complete = expectedCompleteFTP;
431         log.println("getPresentation('" + expectedCompleteFTP + "', false): ");
432         // the password must be masqurade with <****>
433         String asterix = "";
434         for (int n = 0 ; n < password.length(); n++){
435             asterix += "*";
436         }
437         asterix = "<" + asterix.substring(1,asterix.length());
438         asterix = asterix.substring(0,asterix.length()-1) + ">";
439 
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);
447 
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);
456 
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);
466 
467         tRes.tested("getPresentation()", res);
468     }
469 
470 }  // finish class _XURLTransformer
471 
472