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 lib
.MultiMethodTest
;
23 import lib
.StatusException
;
25 import com
.sun
.star
.lang
.XMultiServiceFactory
;
26 import com
.sun
.star
.task
.XInteractionHandler
;
27 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
28 import com
.sun
.star
.uno
.UnoRuntime
;
29 import com
.sun
.star
.util
.DateTime
;
31 import java
.util
.Calendar
;
32 import java
.util
.GregorianCalendar
;
35 * Testing <code>com.sun.star.ucb.XSimpleFileAccess</code>
36 * interface methods. <p>
37 * The following predefined files needed to complete the test:
39 * <li> <code>XSimpleFileAccess/XSimpleFileAccess.txt</code> :
40 * text file of length 17 and 2000 year created .</li>
41 * <li> <code>XSimpleFileAccess/XSimpleFileAccess2.txt</code> :
42 * text file for <code>openFileReadWrite</code> method test.</li>
44 * This test needs the following object relations :
46 * <li> <code>'InteractionHandler'</code>
47 * (of type <code>XInteractionHandler</code>)
48 * instance of <code>com.sun.star.sdb.InteractionHandler</code>
51 * Test is <b> NOT </b> multithread compilant. <p>
52 * @see com.sun.star.ucb.XSimpleFileAccess
54 public class _XSimpleFileAccess
extends MultiMethodTest
{
56 public static XSimpleFileAccess oObj
= null;
59 * Copies <b>XSimpleFileAccess.txt</b> to a new file, checks
60 * if it was successfully copied and then deletes it. <p>
61 * Has <b> OK </b> status if after method call new copy of file
62 * exists and no exceptions were thrown. <p>
66 String copiedFile
= "";
67 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
68 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
69 String filename
= dirname
+"XSimpleFileAccess.txt";
70 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
72 if (oObj
.exists(copiedFile
))
73 oObj
.kill(copiedFile
);
75 oObj
.copy(filename
,copiedFile
);
76 tRes
.tested("copy()",oObj
.exists(copiedFile
));
77 oObj
.kill(copiedFile
);
79 catch (com
.sun
.star
.uno
.Exception ex
) {
80 log
.println("Exception occurred while testing 'copy()'");
81 ex
.printStackTrace(log
);
82 tRes
.tested("copy()",false);
88 * Copies <b>XSimpleFileAccess.txt</b> to a new file, tries to
89 * rename it, then checks
90 * if it was successfully renamed and then deletes it. <p>
91 * Has <b> OK </b> status if after method call new file
92 * exists and no exceptions were thrown. <p>
96 String copiedFile
= "";
97 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
98 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
99 String filename
= dirname
+"XSimpleFileAccess.txt";
100 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
102 if (oObj
.exists(copiedFile
))
103 oObj
.kill(copiedFile
);
105 oObj
.copy(filename
,copiedFile
);
106 filename
= copiedFile
;
107 copiedFile
= dirnameTo
+ "XSimpleFileAccess_move.txt";
108 oObj
.move(filename
,copiedFile
);
109 tRes
.tested("move()",oObj
.exists(copiedFile
));
110 oObj
.kill(copiedFile
);
112 catch (com
.sun
.star
.uno
.Exception ex
) {
113 log
.println("Exception occurred while testing 'move()'");
114 ex
.printStackTrace(log
);
115 tRes
.tested("move()",false);
121 * Copies <b>XSimpleFileAccess.txt</b> to a new file, deletes it
122 * and checks if it isn't exist. <p>
123 * Has <b> OK </b> status if after method call new copy of file
124 * doesn't exist and no exceptions were thrown. <p>
126 public void _kill() {
128 String copiedFile
= "";
129 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
130 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
131 String filename
= dirname
+"XSimpleFileAccess.txt";
132 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
134 if (oObj
.exists(copiedFile
))
135 oObj
.kill(copiedFile
);
137 oObj
.copy(filename
,copiedFile
);
138 oObj
.kill(copiedFile
);
139 tRes
.tested("kill()",!oObj
.exists(copiedFile
));
141 catch (com
.sun
.star
.uno
.Exception ex
) {
142 log
.println("Exception occurred while testing 'kill()'");
143 ex
.printStackTrace(log
);
144 tRes
.tested("kill()",false);
150 * Tries to check if <b>XSimpleFileAccess</b> is folder. <p>
151 * Has <b>OK</b> status if the method returns <code>true</code>
153 public void _isFolder() {
155 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
156 tRes
.tested("isFolder()",oObj
.isFolder(dirname
));
158 catch (com
.sun
.star
.uno
.Exception ex
) {
159 log
.println("Exception occurred while testing 'isFolder()'");
160 ex
.printStackTrace(log
);
161 tRes
.tested("isFolder()",false);
167 * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
168 * 'READONLY' attribute and checks it. Second clears 'READONLY'
169 * attribute and checks it again. The copy of file is deleted
172 * Has <b> OK </b> status if in the first case method returns
173 * <code></code>, and in the second case - <code>false</code>
174 * and no exceptions were thrown. <p>
176 * The following method tests are to be completed successfully before :
178 * <li> <code> setReadOnly </code> </li>
181 public void _isReadOnly() {
182 requiredMethod("setReadOnly()");
184 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
185 String filename
= dirname
+"XSimpleFileAccess.txt";
186 boolean result
= true;
188 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
189 String readonlyCopy
= dirnameTo
+ "XSimpleFileAccess_copy.txt" ;
191 if (oObj
.exists(readonlyCopy
))
192 oObj
.kill(readonlyCopy
);
194 oObj
.copy(filename
, readonlyCopy
);
196 oObj
.setReadOnly(readonlyCopy
, true);
197 result
&= oObj
.isReadOnly(readonlyCopy
);
198 oObj
.setReadOnly(readonlyCopy
, false);
199 result
&= !oObj
.isReadOnly(readonlyCopy
);
201 oObj
.kill(readonlyCopy
);
202 tRes
.tested("isReadOnly()",result
);
203 } catch (com
.sun
.star
.uno
.Exception ex
) {
204 log
.println("Exception occurred while testing 'isReadOnly()'");
205 ex
.printStackTrace(log
);
206 tRes
.tested("isReadOnly()",false);
213 * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
214 * 'READONLY' attribute and checks it. Second clears 'READONLY'
215 * attribute and checks it again. The copy of file is deleted
218 * Has <b> OK </b> status if in the first case method returns
219 * <code></code>, and in the second case - <code>false</code>
220 * and no exceptions were thrown. <p>
222 * The following method tests are to be completed successfully before :
224 * <li> <code> setReadOnly </code> </li>
227 public void _setReadOnly() {
228 boolean result
= true ;
231 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
232 String filename
= dirname
+"XSimpleFileAccess.txt";
234 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
235 String readonlyCopy
= dirnameTo
+ "XSimpleFileAccess_copy.txt" ;
237 if (oObj
.exists(readonlyCopy
))
238 oObj
.kill(readonlyCopy
);
240 oObj
.copy(filename
, readonlyCopy
);
242 oObj
.setReadOnly(readonlyCopy
, true);
243 result
&= oObj
.isReadOnly(readonlyCopy
) ;
244 oObj
.setReadOnly(readonlyCopy
, false);
245 result
&= !oObj
.isReadOnly(readonlyCopy
) ;
246 tRes
.tested("setReadOnly()", result
);
248 oObj
.kill(readonlyCopy
);
250 catch (Exception ex
) {
251 log
.println("Exception occurred while testing 'setReadOnly()'");
252 ex
.printStackTrace(log
);
253 tRes
.tested("setReadOnly()",false);
255 } //EOF setReadOnly()
258 * Creates folder and then checks if it was successfully created. <p>
259 * Has <b>OK</b> status if folder was created and no exceptions
262 public void _createFolder() {
264 String tmpdirname
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
265 String newFolder
= tmpdirname
+"SimpleSubFolder";
267 if (oObj
.exists(newFolder
))
268 oObj
.kill(newFolder
);
270 oObj
.createFolder(newFolder
);
271 tRes
.tested("createFolder()",oObj
.isFolder(newFolder
));
272 oObj
.kill(newFolder
);
274 catch (com
.sun
.star
.uno
.Exception ex
) {
275 log
.println("Exception occurred while testing 'createFolder()'");
276 ex
.printStackTrace(log
);
277 tRes
.tested("createFolder()",false);
280 } //EOF createFolder()
283 * Test calls the method and checks return value and that
284 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
286 * Has <b> OK </b> status if the method returns <code>17</code>
287 * and no exceptions were thrown. <p>
289 public void _getSize() {
291 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
292 String filename
= dirname
+"XSimpleFileAccess.txt";
293 int fSize
= oObj
.getSize(filename
);
294 tRes
.tested("getSize()", fSize
== 17 );
296 catch (com
.sun
.star
.uno
.Exception ex
) {
297 log
.println("Exception occurred while testing 'getSize()'");
298 ex
.printStackTrace(log
);
299 tRes
.tested("getSize()",false);
305 * Test calls the method and checks return value and that
306 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
308 * Has <b> OK </b> status if the method returns String
309 * <code>'application/vnd.sun.staroffice.fsys-file'</code>
310 * and no exceptions were thrown. <p>
312 public void _getContentType() {
314 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
315 String filename
= dirname
+"XSimpleFileAccess.txt";
316 String fType
= oObj
.getContentType(filename
);
317 tRes
.tested("getContentType()",
318 "application/vnd.sun.staroffice.fsys-file".equals(fType
) );
320 catch (com
.sun
.star
.uno
.Exception ex
) {
321 log
.println("Exception occurred while testing 'getContentType()'");
322 ex
.printStackTrace(log
);
323 tRes
.tested("getContentType()",false);
326 } //EOF getContentType()
329 * Test calls the method and checks return value and that
330 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
332 * Has <b> OK </b> status if the method returns date with
333 * 2001 year and no exceptions were thrown. <p>
335 public void _getDateTimeModified() {
337 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
338 String filename
= dirname
+"XSimpleFileAccess.txt";
339 DateTime fTime
= oObj
.getDateTimeModified(filename
);
341 java
.io
.File the_file
= new java
.io
.File(filename
);
342 long lastModified
= the_file
.lastModified();
343 java
.util
.Date lastMod
= new java
.util
.Date(lastModified
);
344 GregorianCalendar lastModCal
= new GregorianCalendar();
345 lastModCal
.setTime(lastMod
);
347 //compare the dates gained by java with those gained by this method
349 boolean partResult
= (fTime
.Day
== lastModCal
.get(Calendar
.DAY_OF_WEEK
));
351 log
.println("Wrong Day");
352 log
.println("Expected: "+lastModCal
.get(Calendar
.DAY_OF_WEEK
));
353 log
.println("Gained: "+fTime
.Day
);
354 log
.println("------------------------------");
356 partResult
= (fTime
.Month
== lastModCal
.get(Calendar
.MONTH
));
358 log
.println("Wrong Month");
359 log
.println("Expected: "+lastModCal
.get(Calendar
.MONTH
));
360 log
.println("Gained: "+fTime
.Month
);
361 log
.println("------------------------------");
364 partResult
= (fTime
.Year
== (lastModCal
.get(Calendar
.MONTH
) - 1900));
366 log
.println("Wrong Year");
367 log
.println("Expected: "+(lastModCal
.get(Calendar
.MONTH
) - 1900));
368 log
.println("Gained: "+fTime
.Year
);
369 log
.println("------------------------------");
372 tRes
.tested("getDateTimeModified()", res
);
374 catch (com
.sun
.star
.uno
.Exception ex
) {
375 log
.println("Exception occurred while testing 'getDateTimeModified()'");
376 ex
.printStackTrace(log
);
377 tRes
.tested("getDateTimeModified()",false);
380 } //EOF getDateTimeModified()
383 * Test calls the method and checks return value and that
384 * no exceptions were thrown. <b>XSimpleFileAccess</b>
386 * Has <b> OK </b> status if the method returns non zero length
387 * array and no exceptions were thrown. <p>
389 public void _getFolderContents() {
391 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
392 String
[] cont
= oObj
.getFolderContents(dirname
,false);
393 tRes
.tested("getFolderContents()", cont
.length
>0);
395 catch (com
.sun
.star
.uno
.Exception ex
) {
396 log
.println("Exception occurred while testing 'getFolderContents()'");
397 ex
.printStackTrace(log
);
398 tRes
.tested("getFolderContents()",false);
401 } //EOF getFolderContents()
404 * First it check file <b>XSimpleFileAccess.txt</b> for
405 * existence, second file <b>I_do_not_exists.txt</b> is checked
407 * Has <b> OK </b> status if in the first case method returns
408 * <code>true</code> and in the second - <code>flase</code>
409 * and no exceptions were thrown. <p>
411 public void _exists() {
413 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
414 String filename
= dirname
+"XSimpleFileAccess.txt";
415 String wrongname
= dirname
+"I_do_not_exists.txt";
416 tRes
.tested("exists()",
417 oObj
.exists(filename
) && !oObj
.exists(wrongname
));
419 catch (com
.sun
.star
.uno
.Exception ex
) {
420 log
.println("Exception occurred while testing 'exists()'");
421 ex
.printStackTrace(log
);
422 tRes
.tested("exists()",false);
428 * Test calls the method and checks return value and that
429 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
431 * Has <b> OK </b> status if the method returns not
432 * <code>null</code> value and no exceptions were thrown. <p>
434 public void _openFileRead() {
436 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
437 String filename
= dirname
+"XSimpleFileAccess.txt";
438 com
.sun
.star
.io
.XInputStream iStream
= oObj
.openFileRead(filename
);
439 tRes
.tested("openFileRead()", iStream
!= null);
441 catch (com
.sun
.star
.uno
.Exception ex
) {
442 log
.println("Exception occurred while testing 'openFileRead()'");
443 ex
.printStackTrace(log
);
444 tRes
.tested("openFileRead()",false);
447 } //EOF openFileRead()
450 * Test calls the method and checks return value and that
451 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
453 * Has <b> OK </b> status if the method returns not
454 * <code>null</code> value and no exceptions were thrown. <p>
456 public void _openFileWrite() {
458 String tmpdirname
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
460 String copiedFile
= tmpdirname
+"XSimpleFileAccess_openWrite.txt";
462 if (oObj
.exists(copiedFile
))
463 oObj
.kill(copiedFile
);
465 com
.sun
.star
.io
.XOutputStream oStream
=
466 oObj
.openFileWrite(copiedFile
);
467 tRes
.tested("openFileWrite()", oStream
!= null);
469 oStream
.closeOutput();
470 oObj
.kill(copiedFile
);
472 catch (com
.sun
.star
.uno
.Exception ex
) {
473 log
.println("Exception occurred while testing 'openFileWrite()'");
474 ex
.printStackTrace(log
);
475 tRes
.tested("openFileWrite()",false);
478 } //EOF openFileWrite()
481 * Test calls the method and checks return value and that
482 * no exceptions were thrown. <b>XSimpleFileAccess2.txt</b>
484 * Has <b> OK </b> status if the method returns not
485 * <code>null</code> value and no exceptions were thrown. <p>
487 public void _openFileReadWrite() {
489 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
490 String copiedFile
= dirnameTo
+ "XSimpleFileAccess2.txt" ;
492 if (oObj
.exists(copiedFile
))
493 oObj
.kill(copiedFile
);
495 com
.sun
.star
.io
.XStream aStream
=
496 oObj
.openFileReadWrite(copiedFile
);
497 tRes
.tested("openFileReadWrite()", aStream
!= null);
499 aStream
.getInputStream().closeInput();
500 aStream
.getOutputStream().closeOutput();
502 oObj
.kill(copiedFile
);
504 catch (com
.sun
.star
.uno
.Exception ex
) {
505 log
.println("Exception occurred while testing 'openFileReadWrite()'");
506 ex
.printStackTrace(log
);
507 tRes
.tested("openFileReadWrite()",false);
510 } //EOF openFileReadWrite()
513 * Test calls the method and checks that no exceptions were thrown.
514 * Has <b> OK </b> status if no exceptions were thrown. <p>
516 public void _setInteractionHandler() {
517 XInteractionHandler handler
= null;
518 Object oHandler
= tEnv
.getObjRelation("InteractionHandler");
520 if (oHandler
== null)
521 throw new StatusException
522 (Status
.failed("Reelation InteractionHandler not found"));
525 handler
= UnoRuntime
.queryInterface
526 (XInteractionHandler
.class, oHandler
);
527 oObj
.setInteractionHandler(handler
);
528 tRes
.tested("setInteractionHandler()", true);
529 } catch (Exception ex
) {
530 log
.println("Exception occurred while testing 'setInteractionHandler()'");
531 ex
.printStackTrace(log
);
532 tRes
.tested("setInteractionHandler()", false);
535 } //EOF setInteractionHandler()
537 } // finish class _XSimpleFileAccess