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
.task
.XInteractionHandler
;
26 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
27 import com
.sun
.star
.uno
.UnoRuntime
;
28 import com
.sun
.star
.util
.DateTime
;
30 import java
.util
.Calendar
;
31 import java
.util
.GregorianCalendar
;
34 * Testing <code>com.sun.star.ucb.XSimpleFileAccess</code>
35 * interface methods. <p>
36 * The following predefined files needed to complete the test:
38 * <li> <code>XSimpleFileAccess/XSimpleFileAccess.txt</code> :
39 * text file of length 17 and 2000 year created .</li>
40 * <li> <code>XSimpleFileAccess/XSimpleFileAccess2.txt</code> :
41 * text file for <code>openFileReadWrite</code> method test.</li>
43 * This test needs the following object relations :
45 * <li> <code>'InteractionHandler'</code>
46 * (of type <code>XInteractionHandler</code>)
47 * instance of <code>com.sun.star.sdb.InteractionHandler</code>
50 * Test is <b> NOT </b> multithread compliant. <p>
51 * @see com.sun.star.ucb.XSimpleFileAccess
53 public class _XSimpleFileAccess
extends MultiMethodTest
{
55 public static XSimpleFileAccess oObj
= null;
58 * Copies <b>XSimpleFileAccess.txt</b> to a new file, checks
59 * if it was successfully copied and then deletes it. <p>
60 * Has <b> OK </b> status if after method call new copy of file
61 * exists and no exceptions were thrown. <p>
65 String copiedFile
= "";
66 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
67 String dirnameTo
= util
.utils
.getOfficeTemp(tParam
.getMSF()) ;
68 String filename
= dirname
+"XSimpleFileAccess.txt";
69 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
71 if (oObj
.exists(copiedFile
))
72 oObj
.kill(copiedFile
);
74 oObj
.copy(filename
,copiedFile
);
75 tRes
.tested("copy()",oObj
.exists(copiedFile
));
76 oObj
.kill(copiedFile
);
78 catch (com
.sun
.star
.uno
.Exception ex
) {
79 log
.println("Exception occurred while testing 'copy()'");
80 ex
.printStackTrace(log
);
81 tRes
.tested("copy()",false);
87 * Copies <b>XSimpleFileAccess.txt</b> to a new file, tries to
88 * rename it, then checks
89 * if it was successfully renamed and then deletes it. <p>
90 * Has <b> OK </b> status if after method call new file
91 * exists and no exceptions were thrown. <p>
95 String copiedFile
= "";
96 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
97 String dirnameTo
= util
.utils
.getOfficeTemp(tParam
.getMSF()) ;
98 String filename
= dirname
+"XSimpleFileAccess.txt";
99 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
101 if (oObj
.exists(copiedFile
))
102 oObj
.kill(copiedFile
);
104 oObj
.copy(filename
,copiedFile
);
105 filename
= copiedFile
;
106 copiedFile
= dirnameTo
+ "XSimpleFileAccess_move.txt";
107 oObj
.move(filename
,copiedFile
);
108 tRes
.tested("move()",oObj
.exists(copiedFile
));
109 oObj
.kill(copiedFile
);
111 catch (com
.sun
.star
.uno
.Exception ex
) {
112 log
.println("Exception occurred while testing 'move()'");
113 ex
.printStackTrace(log
);
114 tRes
.tested("move()",false);
120 * Copies <b>XSimpleFileAccess.txt</b> to a new file, deletes it
121 * and checks if it isn't exist. <p>
122 * Has <b> OK </b> status if after method call new copy of file
123 * doesn't exist and no exceptions were thrown. <p>
125 public void _kill() {
127 String copiedFile
= "";
128 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
129 String dirnameTo
= util
.utils
.getOfficeTemp(tParam
.getMSF()) ;
130 String filename
= dirname
+"XSimpleFileAccess.txt";
131 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
133 if (oObj
.exists(copiedFile
))
134 oObj
.kill(copiedFile
);
136 oObj
.copy(filename
,copiedFile
);
137 oObj
.kill(copiedFile
);
138 tRes
.tested("kill()",!oObj
.exists(copiedFile
));
140 catch (com
.sun
.star
.uno
.Exception ex
) {
141 log
.println("Exception occurred while testing 'kill()'");
142 ex
.printStackTrace(log
);
143 tRes
.tested("kill()",false);
149 * Tries to check if <b>XSimpleFileAccess</b> is folder. <p>
150 * Has <b>OK</b> status if the method returns <code>true</code>
152 public void _isFolder() {
154 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
155 tRes
.tested("isFolder()",oObj
.isFolder(dirname
));
157 catch (com
.sun
.star
.uno
.Exception ex
) {
158 log
.println("Exception occurred while testing 'isFolder()'");
159 ex
.printStackTrace(log
);
160 tRes
.tested("isFolder()",false);
166 * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
167 * 'READONLY' attribute and checks it. Second clears 'READONLY'
168 * attribute and checks it again. The copy of file is deleted
171 * Has <b> OK </b> status if in the first case method returns
172 * <code></code>, and in the second case - <code>false</code>
173 * and no exceptions were thrown. <p>
175 * The following method tests are to be completed successfully before :
177 * <li> <code> setReadOnly </code> </li>
180 public void _isReadOnly() {
181 requiredMethod("setReadOnly()");
183 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
184 String filename
= dirname
+"XSimpleFileAccess.txt";
185 boolean result
= true;
187 String dirnameTo
= util
.utils
.getOfficeTemp(tParam
.getMSF()) ;
188 String readonlyCopy
= dirnameTo
+ "XSimpleFileAccess_copy.txt" ;
190 if (oObj
.exists(readonlyCopy
))
191 oObj
.kill(readonlyCopy
);
193 oObj
.copy(filename
, readonlyCopy
);
195 oObj
.setReadOnly(readonlyCopy
, true);
196 result
&= oObj
.isReadOnly(readonlyCopy
);
197 oObj
.setReadOnly(readonlyCopy
, false);
198 result
&= !oObj
.isReadOnly(readonlyCopy
);
200 oObj
.kill(readonlyCopy
);
201 tRes
.tested("isReadOnly()",result
);
202 } catch (com
.sun
.star
.uno
.Exception ex
) {
203 log
.println("Exception occurred while testing 'isReadOnly()'");
204 ex
.printStackTrace(log
);
205 tRes
.tested("isReadOnly()",false);
212 * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
213 * 'READONLY' attribute and checks it. Second clears 'READONLY'
214 * attribute and checks it again. The copy of file is deleted
217 * Has <b> OK </b> status if in the first case method returns
218 * <code></code>, and in the second case - <code>false</code>
219 * and no exceptions were thrown. <p>
221 * The following method tests are to be completed successfully before :
223 * <li> <code> setReadOnly </code> </li>
226 public void _setReadOnly() {
227 boolean result
= true ;
230 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
231 String filename
= dirname
+"XSimpleFileAccess.txt";
233 String dirnameTo
= util
.utils
.getOfficeTemp(tParam
.getMSF()) ;
234 String readonlyCopy
= dirnameTo
+ "XSimpleFileAccess_copy.txt" ;
236 if (oObj
.exists(readonlyCopy
))
237 oObj
.kill(readonlyCopy
);
239 oObj
.copy(filename
, readonlyCopy
);
241 oObj
.setReadOnly(readonlyCopy
, true);
242 result
&= oObj
.isReadOnly(readonlyCopy
) ;
243 oObj
.setReadOnly(readonlyCopy
, false);
244 result
&= !oObj
.isReadOnly(readonlyCopy
) ;
245 tRes
.tested("setReadOnly()", result
);
247 oObj
.kill(readonlyCopy
);
249 catch (Exception ex
) {
250 log
.println("Exception occurred while testing 'setReadOnly()'");
251 ex
.printStackTrace(log
);
252 tRes
.tested("setReadOnly()",false);
254 } //EOF setReadOnly()
257 * Creates folder and then checks if it was successfully created. <p>
258 * Has <b>OK</b> status if folder was created and no exceptions
261 public void _createFolder() {
263 String tmpdirname
= util
.utils
.getOfficeTemp(tParam
.getMSF()) ;
264 String newFolder
= tmpdirname
+"SimpleSubFolder";
266 if (oObj
.exists(newFolder
))
267 oObj
.kill(newFolder
);
269 oObj
.createFolder(newFolder
);
270 tRes
.tested("createFolder()",oObj
.isFolder(newFolder
));
271 oObj
.kill(newFolder
);
273 catch (com
.sun
.star
.uno
.Exception ex
) {
274 log
.println("Exception occurred while testing 'createFolder()'");
275 ex
.printStackTrace(log
);
276 tRes
.tested("createFolder()",false);
279 } //EOF createFolder()
282 * Test calls the method and checks return value and that
283 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
285 * Has <b> OK </b> status if the method returns <code>17</code>
286 * and no exceptions were thrown. <p>
288 public void _getSize() {
290 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
291 String filename
= dirname
+"XSimpleFileAccess.txt";
292 int fSize
= oObj
.getSize(filename
);
293 tRes
.tested("getSize()", fSize
== 17 );
295 catch (com
.sun
.star
.uno
.Exception ex
) {
296 log
.println("Exception occurred while testing 'getSize()'");
297 ex
.printStackTrace(log
);
298 tRes
.tested("getSize()",false);
304 * Test calls the method and checks return value and that
305 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
307 * Has <b> OK </b> status if the method returns String
308 * <code>'application/vnd.sun.staroffice.fsys-file'</code>
309 * and no exceptions were thrown. <p>
311 public void _getContentType() {
313 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
314 String filename
= dirname
+"XSimpleFileAccess.txt";
315 String fType
= oObj
.getContentType(filename
);
316 tRes
.tested("getContentType()",
317 "application/vnd.sun.staroffice.fsys-file".equals(fType
) );
319 catch (com
.sun
.star
.uno
.Exception ex
) {
320 log
.println("Exception occurred while testing 'getContentType()'");
321 ex
.printStackTrace(log
);
322 tRes
.tested("getContentType()",false);
325 } //EOF getContentType()
328 * Test calls the method and checks return value and that
329 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
331 * Has <b> OK </b> status if the method returns date with
332 * 2001 year and no exceptions were thrown. <p>
334 public void _getDateTimeModified() {
336 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
337 String filename
= dirname
+"XSimpleFileAccess.txt";
338 DateTime fTime
= oObj
.getDateTimeModified(filename
);
340 java
.io
.File the_file
= new java
.io
.File(filename
);
341 long lastModified
= the_file
.lastModified();
342 java
.util
.Date lastMod
= new java
.util
.Date(lastModified
);
343 GregorianCalendar lastModCal
= new GregorianCalendar();
344 lastModCal
.setTime(lastMod
);
346 //compare the dates gained by java with those gained by this method
348 boolean partResult
= (fTime
.Day
== lastModCal
.get(Calendar
.DAY_OF_WEEK
));
350 log
.println("Wrong Day");
351 log
.println("Expected: "+lastModCal
.get(Calendar
.DAY_OF_WEEK
));
352 log
.println("Gained: "+fTime
.Day
);
353 log
.println("------------------------------");
355 partResult
= (fTime
.Month
== lastModCal
.get(Calendar
.MONTH
));
357 log
.println("Wrong Month");
358 log
.println("Expected: "+lastModCal
.get(Calendar
.MONTH
));
359 log
.println("Gained: "+fTime
.Month
);
360 log
.println("------------------------------");
363 partResult
= (fTime
.Year
== (lastModCal
.get(Calendar
.MONTH
) - 1900));
365 log
.println("Wrong Year");
366 log
.println("Expected: "+(lastModCal
.get(Calendar
.MONTH
) - 1900));
367 log
.println("Gained: "+fTime
.Year
);
368 log
.println("------------------------------");
371 tRes
.tested("getDateTimeModified()", res
);
373 catch (com
.sun
.star
.uno
.Exception ex
) {
374 log
.println("Exception occurred while testing 'getDateTimeModified()'");
375 ex
.printStackTrace(log
);
376 tRes
.tested("getDateTimeModified()",false);
379 } //EOF getDateTimeModified()
382 * Test calls the method and checks return value and that
383 * no exceptions were thrown. <b>XSimpleFileAccess</b>
385 * Has <b> OK </b> status if the method returns non zero length
386 * array and no exceptions were thrown. <p>
388 public void _getFolderContents() {
390 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
391 String
[] cont
= oObj
.getFolderContents(dirname
,false);
392 tRes
.tested("getFolderContents()", cont
.length
>0);
394 catch (com
.sun
.star
.uno
.Exception ex
) {
395 log
.println("Exception occurred while testing 'getFolderContents()'");
396 ex
.printStackTrace(log
);
397 tRes
.tested("getFolderContents()",false);
400 } //EOF getFolderContents()
403 * First it check file <b>XSimpleFileAccess.txt</b> for
404 * existence, second file <b>I_do_not_exists.txt</b> is checked
406 * Has <b> OK </b> status if in the first case method returns
407 * <code>true</code> and in the second - <code>flase</code>
408 * and no exceptions were thrown. <p>
410 public void _exists() {
412 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
413 String filename
= dirname
+"XSimpleFileAccess.txt";
414 String wrongname
= dirname
+"I_do_not_exists.txt";
415 tRes
.tested("exists()",
416 oObj
.exists(filename
) && !oObj
.exists(wrongname
));
418 catch (com
.sun
.star
.uno
.Exception ex
) {
419 log
.println("Exception occurred while testing 'exists()'");
420 ex
.printStackTrace(log
);
421 tRes
.tested("exists()",false);
427 * Test calls the method and checks return value and that
428 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
430 * Has <b> OK </b> status if the method returns not
431 * <code>null</code> value and no exceptions were thrown. <p>
433 public void _openFileRead() {
435 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
436 String filename
= dirname
+"XSimpleFileAccess.txt";
437 com
.sun
.star
.io
.XInputStream iStream
= oObj
.openFileRead(filename
);
438 tRes
.tested("openFileRead()", iStream
!= null);
440 catch (com
.sun
.star
.uno
.Exception ex
) {
441 log
.println("Exception occurred while testing 'openFileRead()'");
442 ex
.printStackTrace(log
);
443 tRes
.tested("openFileRead()",false);
446 } //EOF openFileRead()
449 * Test calls the method and checks return value and that
450 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
452 * Has <b> OK </b> status if the method returns not
453 * <code>null</code> value and no exceptions were thrown. <p>
455 public void _openFileWrite() {
457 String tmpdirname
= util
.utils
.getOfficeTemp(tParam
.getMSF()) ;
459 String copiedFile
= tmpdirname
+"XSimpleFileAccess_openWrite.txt";
461 if (oObj
.exists(copiedFile
))
462 oObj
.kill(copiedFile
);
464 com
.sun
.star
.io
.XOutputStream oStream
=
465 oObj
.openFileWrite(copiedFile
);
466 tRes
.tested("openFileWrite()", oStream
!= null);
468 oStream
.closeOutput();
469 oObj
.kill(copiedFile
);
471 catch (com
.sun
.star
.uno
.Exception ex
) {
472 log
.println("Exception occurred while testing 'openFileWrite()'");
473 ex
.printStackTrace(log
);
474 tRes
.tested("openFileWrite()",false);
477 } //EOF openFileWrite()
480 * Test calls the method and checks return value and that
481 * no exceptions were thrown. <b>XSimpleFileAccess2.txt</b>
483 * Has <b> OK </b> status if the method returns not
484 * <code>null</code> value and no exceptions were thrown. <p>
486 public void _openFileReadWrite() {
488 String dirnameTo
= util
.utils
.getOfficeTemp(tParam
.getMSF()) ;
489 String copiedFile
= dirnameTo
+ "XSimpleFileAccess2.txt" ;
491 if (oObj
.exists(copiedFile
))
492 oObj
.kill(copiedFile
);
494 com
.sun
.star
.io
.XStream aStream
=
495 oObj
.openFileReadWrite(copiedFile
);
496 tRes
.tested("openFileReadWrite()", aStream
!= null);
498 aStream
.getInputStream().closeInput();
499 aStream
.getOutputStream().closeOutput();
501 oObj
.kill(copiedFile
);
503 catch (com
.sun
.star
.uno
.Exception ex
) {
504 log
.println("Exception occurred while testing 'openFileReadWrite()'");
505 ex
.printStackTrace(log
);
506 tRes
.tested("openFileReadWrite()",false);
509 } //EOF openFileReadWrite()
512 * Test calls the method and checks that no exceptions were thrown.
513 * Has <b> OK </b> status if no exceptions were thrown. <p>
515 public void _setInteractionHandler() {
516 XInteractionHandler handler
= null;
517 Object oHandler
= tEnv
.getObjRelation("InteractionHandler");
519 if (oHandler
== null)
520 throw new StatusException
521 (Status
.failed("Reelation InteractionHandler not found"));
524 handler
= UnoRuntime
.queryInterface
525 (XInteractionHandler
.class, oHandler
);
526 oObj
.setInteractionHandler(handler
);
527 tRes
.tested("setInteractionHandler()", true);
528 } catch (Exception ex
) {
529 log
.println("Exception occurred while testing 'setInteractionHandler()'");
530 ex
.printStackTrace(log
);
531 tRes
.tested("setInteractionHandler()", false);
534 } //EOF setInteractionHandler()
536 } // finish class _XSimpleFileAccess