1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
30 import lib
.MultiMethodTest
;
32 import lib
.StatusException
;
34 import com
.sun
.star
.lang
.XMultiServiceFactory
;
35 import com
.sun
.star
.task
.XInteractionHandler
;
36 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 import com
.sun
.star
.util
.DateTime
;
41 * Testing <code>com.sun.star.ucb.XSimpleFileAccess</code>
42 * interface methods. <p>
43 * The following predefined files needed to complete the test:
45 * <li> <code>XSimpleFileAccess/XSimpleFileAccess.txt</code> :
46 * text file of length 17 and 2000 year created .</li>
47 * <li> <code>XSimpleFileAccess/XSimpleFileAccess2.txt</code> :
48 * text file for <code>openFileReadWrite</code> method test.</li>
50 * This test needs the following object relations :
52 * <li> <code>'InteractionHandler'</code>
53 * (of type <code>XInteractionHandler</code>)
54 * instance of <code>com.sun.star.sdb.InteractionHandler</code>
57 * Test is <b> NOT </b> multithread compilant. <p>
58 * @see com.sun.star.ucb.XSimpleFileAccess
60 public class _XSimpleFileAccess
extends MultiMethodTest
{
62 public static XSimpleFileAccess oObj
= null;
65 * Copies <b>XSimpleFileAccess.txt</b> to a new file, checks
66 * if it was successfully copied and then deletes it. <p>
67 * Has <b> OK </b> status if after method call new copy of file
68 * exists and no exceptions were thrown. <p>
72 String copiedFile
= "";
73 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
74 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
75 String filename
= dirname
+"XSimpleFileAccess.txt";
76 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
78 if (oObj
.exists(copiedFile
))
79 oObj
.kill(copiedFile
);
81 oObj
.copy(filename
,copiedFile
);
82 tRes
.tested("copy()",oObj
.exists(copiedFile
));
83 oObj
.kill(copiedFile
);
85 catch (com
.sun
.star
.uno
.Exception ex
) {
86 log
.println("Exception occurred while testing 'copy()'");
87 ex
.printStackTrace(log
);
88 tRes
.tested("copy()",false);
94 * Copies <b>XSimpleFileAccess.txt</b> to a new file, tries to
95 * rename it, then checks
96 * if it was successfully renamed and then deletes it. <p>
97 * Has <b> OK </b> status if after method call new file
98 * exists and no exceptions were thrown. <p>
100 public void _move() {
102 String copiedFile
= "";
103 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
104 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
105 String filename
= dirname
+"XSimpleFileAccess.txt";
106 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
108 if (oObj
.exists(copiedFile
))
109 oObj
.kill(copiedFile
);
111 oObj
.copy(filename
,copiedFile
);
112 filename
= copiedFile
;
113 copiedFile
= dirnameTo
+ "XSimpleFileAccess_move.txt";
114 oObj
.move(filename
,copiedFile
);
115 tRes
.tested("move()",oObj
.exists(copiedFile
));
116 oObj
.kill(copiedFile
);
118 catch (com
.sun
.star
.uno
.Exception ex
) {
119 log
.println("Exception occurred while testing 'move()'");
120 ex
.printStackTrace(log
);
121 tRes
.tested("move()",false);
127 * Copies <b>XSimpleFileAccess.txt</b> to a new file, deletes it
128 * and checks if it isn't exist. <p>
129 * Has <b> OK </b> status if after method call new copy of file
130 * doesn't exist and no exceptions were thrown. <p>
132 public void _kill() {
134 String copiedFile
= "";
135 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
136 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
137 String filename
= dirname
+"XSimpleFileAccess.txt";
138 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
140 if (oObj
.exists(copiedFile
))
141 oObj
.kill(copiedFile
);
143 oObj
.copy(filename
,copiedFile
);
144 oObj
.kill(copiedFile
);
145 tRes
.tested("kill()",!oObj
.exists(copiedFile
));
147 catch (com
.sun
.star
.uno
.Exception ex
) {
148 log
.println("Exception occurred while testing 'kill()'");
149 ex
.printStackTrace(log
);
150 tRes
.tested("kill()",false);
156 * Tries to check if <b>XSimpleFileAccess</b> is folder. <p>
157 * Has <b>OK</b> status if the method returns <code>true</code>
159 public void _isFolder() {
161 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
162 tRes
.tested("isFolder()",oObj
.isFolder(dirname
));
164 catch (com
.sun
.star
.uno
.Exception ex
) {
165 log
.println("Exception occurred while testing 'isFolder()'");
166 ex
.printStackTrace(log
);
167 tRes
.tested("isFolder()",false);
173 * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
174 * 'READONLY' attribute and checks it. Second clears 'READONLY'
175 * attribute and checks it again. The copy of file is deleted
178 * Has <b> OK </b> status if in the first case method returns
179 * <code></code>, and in the second case - <code>false</code>
180 * and no exceptions were thrown. <p>
182 * The following method tests are to be completed successfully before :
184 * <li> <code> setReadOnly </code> </li>
187 public void _isReadOnly() {
188 requiredMethod("setReadOnly()");
190 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
191 String filename
= dirname
+"XSimpleFileAccess.txt";
192 boolean result
= true;
194 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
195 String readonlyCopy
= dirnameTo
+ "XSimpleFileAccess_copy.txt" ;
197 if (oObj
.exists(readonlyCopy
))
198 oObj
.kill(readonlyCopy
);
200 oObj
.copy(filename
, readonlyCopy
);
202 oObj
.setReadOnly(readonlyCopy
, true);
203 result
&= oObj
.isReadOnly(readonlyCopy
);
204 oObj
.setReadOnly(readonlyCopy
, false);
205 result
&= !oObj
.isReadOnly(readonlyCopy
);
207 oObj
.kill(readonlyCopy
);
208 tRes
.tested("isReadOnly()",result
);
209 } catch (com
.sun
.star
.uno
.Exception ex
) {
210 log
.println("Exception occurred while testing 'isReadOnly()'");
211 ex
.printStackTrace(log
);
212 tRes
.tested("isReadOnly()",false);
219 * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
220 * 'READONLY' attribute and checks it. Second clears 'READONLY'
221 * attribute and checks it again. The copy of file is deleted
224 * Has <b> OK </b> status if in the first case method returns
225 * <code></code>, and in the second case - <code>false</code>
226 * and no exceptions were thrown. <p>
228 * The following method tests are to be completed successfully before :
230 * <li> <code> setReadOnly </code> </li>
233 public void _setReadOnly() {
234 boolean result
= true ;
237 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
238 String filename
= dirname
+"XSimpleFileAccess.txt";
240 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
241 String readonlyCopy
= dirnameTo
+ "XSimpleFileAccess_copy.txt" ;
243 if (oObj
.exists(readonlyCopy
))
244 oObj
.kill(readonlyCopy
);
246 oObj
.copy(filename
, readonlyCopy
);
248 oObj
.setReadOnly(readonlyCopy
, true);
249 result
&= oObj
.isReadOnly(readonlyCopy
) ;
250 oObj
.setReadOnly(readonlyCopy
, false);
251 result
&= !oObj
.isReadOnly(readonlyCopy
) ;
252 tRes
.tested("setReadOnly()", result
);
254 oObj
.kill(readonlyCopy
);
256 catch (Exception ex
) {
257 log
.println("Exception occurred while testing 'setReadOnly()'");
258 ex
.printStackTrace(log
);
259 tRes
.tested("setReadOnly()",false);
261 } //EOF setReadOnly()
264 * Creates folder and then checks if it was successfully created. <p>
265 * Has <b>OK</b> status if folder was created and no exceptions
268 public void _createFolder() {
270 String tmpdirname
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
271 String newFolder
= tmpdirname
+"SimpleSubFolder";
273 if (oObj
.exists(newFolder
))
274 oObj
.kill(newFolder
);
276 oObj
.createFolder(newFolder
);
277 tRes
.tested("createFolder()",oObj
.isFolder(newFolder
));
278 oObj
.kill(newFolder
);
280 catch (com
.sun
.star
.uno
.Exception ex
) {
281 log
.println("Exception occurred while testing 'createFolder()'");
282 ex
.printStackTrace(log
);
283 tRes
.tested("createFolder()",false);
286 } //EOF createFolder()
289 * Test calls the method and checks return value and that
290 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
292 * Has <b> OK </b> status if the method returns <code>17</code>
293 * and no exceptions were thrown. <p>
295 public void _getSize() {
297 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
298 String filename
= dirname
+"XSimpleFileAccess.txt";
299 int fSize
= oObj
.getSize(filename
);
300 tRes
.tested("getSize()", fSize
== 17 );
302 catch (com
.sun
.star
.uno
.Exception ex
) {
303 log
.println("Exception occurred while testing 'getSize()'");
304 ex
.printStackTrace(log
);
305 tRes
.tested("getSize()",false);
311 * Test calls the method and checks return value and that
312 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
314 * Has <b> OK </b> status if the method returns String
315 * <code>'application/vnd.sun.staroffice.fsys-file'</code>
316 * and no exceptions were thrown. <p>
318 public void _getContentType() {
320 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
321 String filename
= dirname
+"XSimpleFileAccess.txt";
322 String fType
= oObj
.getContentType(filename
);
323 tRes
.tested("getContentType()",
324 "application/vnd.sun.staroffice.fsys-file".equals(fType
) );
326 catch (com
.sun
.star
.uno
.Exception ex
) {
327 log
.println("Exception occurred while testing 'getContentType()'");
328 ex
.printStackTrace(log
);
329 tRes
.tested("getContentType()",false);
332 } //EOF getContentType()
335 * Test calls the method and checks return value and that
336 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
338 * Has <b> OK </b> status if the method returns date with
339 * 2001 year and no exceptions were thrown. <p>
341 public void _getDateTimeModified() {
343 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
344 String filename
= dirname
+"XSimpleFileAccess.txt";
345 DateTime fTime
= oObj
.getDateTimeModified(filename
);
347 java
.io
.File the_file
= new java
.io
.File(filename
);
348 long lastModified
= the_file
.lastModified();
349 java
.util
.Date lastMod
= new java
.util
.Date(lastModified
);
351 //compare the dates gained by java with those gained by this method
353 boolean partResult
= (fTime
.Day
== lastMod
.getDay());
355 log
.println("Wrong Day");
356 log
.println("Expected: "+lastMod
.getDay());
357 log
.println("Gained: "+fTime
.Day
);
358 log
.println("------------------------------");
360 partResult
= (fTime
.Month
== lastMod
.getMonth());
362 log
.println("Wrong Month");
363 log
.println("Expected: "+lastMod
.getMonth());
364 log
.println("Gained: "+fTime
.Month
);
365 log
.println("------------------------------");
368 partResult
= (fTime
.Year
== lastMod
.getYear());
370 log
.println("Wrong Year");
371 log
.println("Expected: "+lastMod
.getYear());
372 log
.println("Gained: "+fTime
.Year
);
373 log
.println("------------------------------");
376 tRes
.tested("getDateTimeModified()", res
);
378 catch (com
.sun
.star
.uno
.Exception ex
) {
379 log
.println("Exception occurred while testing 'getDateTimeModified()'");
380 ex
.printStackTrace(log
);
381 tRes
.tested("getDateTimeModified()",false);
384 } //EOF getDateTimeModified()
387 * Test calls the method and checks return value and that
388 * no exceptions were thrown. <b>XSimpleFileAccess</b>
390 * Has <b> OK </b> status if the method returns non zero length
391 * array and no exceptions were thrown. <p>
393 public void _getFolderContents() {
395 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
396 String
[] cont
= oObj
.getFolderContents(dirname
,false);
397 tRes
.tested("getFolderContents()", cont
.length
>0);
399 catch (com
.sun
.star
.uno
.Exception ex
) {
400 log
.println("Exception occurred while testing 'getFolderContents()'");
401 ex
.printStackTrace(log
);
402 tRes
.tested("getFolderContents()",false);
405 } //EOF getFolderContents()
408 * First it check file <b>XSimpleFileAccess.txt</b> for
409 * existence, second file <b>I_do_not_exists.txt</b> is checked
411 * Has <b> OK </b> status if in the first case method returns
412 * <code>true</code> and in the second - <code>flase</code>
413 * and no exceptions were thrown. <p>
415 public void _exists() {
417 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
418 String filename
= dirname
+"XSimpleFileAccess.txt";
419 String wrongname
= dirname
+"I_do_not_exists.txt";
420 tRes
.tested("exists()",
421 oObj
.exists(filename
) && !oObj
.exists(wrongname
));
423 catch (com
.sun
.star
.uno
.Exception ex
) {
424 log
.println("Exception occurred while testing 'exists()'");
425 ex
.printStackTrace(log
);
426 tRes
.tested("exists()",false);
432 * Test calls the method and checks return value and that
433 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
435 * Has <b> OK </b> status if the method returns not
436 * <code>null</code> value and no exceptions were thrown. <p>
438 public void _openFileRead() {
440 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
441 String filename
= dirname
+"XSimpleFileAccess.txt";
442 com
.sun
.star
.io
.XInputStream iStream
= oObj
.openFileRead(filename
);
443 tRes
.tested("openFileRead()", iStream
!= null);
445 catch (com
.sun
.star
.uno
.Exception ex
) {
446 log
.println("Exception occurred while testing 'openFileRead()'");
447 ex
.printStackTrace(log
);
448 tRes
.tested("openFileRead()",false);
451 } //EOF openFileRead()
454 * Test calls the method and checks return value and that
455 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
457 * Has <b> OK </b> status if the method returns not
458 * <code>null</code> value and no exceptions were thrown. <p>
460 public void _openFileWrite() {
462 String tmpdirname
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
464 String copiedFile
= tmpdirname
+"XSimpleFileAccess_openWrite.txt";
466 if (oObj
.exists(copiedFile
))
467 oObj
.kill(copiedFile
);
469 com
.sun
.star
.io
.XOutputStream oStream
=
470 oObj
.openFileWrite(copiedFile
);
471 tRes
.tested("openFileWrite()", oStream
!= null);
473 oStream
.closeOutput();
474 oObj
.kill(copiedFile
);
476 catch (com
.sun
.star
.uno
.Exception ex
) {
477 log
.println("Exception occurred while testing 'openFileWrite()'");
478 ex
.printStackTrace(log
);
479 tRes
.tested("openFileWrite()",false);
482 } //EOF openFileWrite()
485 * Test calls the method and checks return value and that
486 * no exceptions were thrown. <b>XSimpleFileAccess2.txt</b>
488 * Has <b> OK </b> status if the method returns not
489 * <code>null</code> value and no exceptions were thrown. <p>
491 public void _openFileReadWrite() {
493 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
494 String copiedFile
= dirnameTo
+ "XSimpleFileAccess2.txt" ;
496 if (oObj
.exists(copiedFile
))
497 oObj
.kill(copiedFile
);
499 com
.sun
.star
.io
.XStream aStream
=
500 oObj
.openFileReadWrite(copiedFile
);
501 tRes
.tested("openFileReadWrite()", aStream
!= null);
503 aStream
.getInputStream().closeInput();
504 aStream
.getOutputStream().closeOutput();
506 oObj
.kill(copiedFile
);
508 catch (com
.sun
.star
.uno
.Exception ex
) {
509 log
.println("Exception occurred while testing 'openFileReadWrite()'");
510 ex
.printStackTrace(log
);
511 tRes
.tested("openFileReadWrite()",false);
514 } //EOF openFileReadWrite()
517 * Test calls the method and checks that no exceptions were thrown.
518 * Has <b> OK </b> status if no exceptions were thrown. <p>
520 public void _setInteractionHandler() {
521 XInteractionHandler handler
= null;
522 Object oHandler
= tEnv
.getObjRelation("InteractionHandler");;
524 if (oHandler
== null)
525 throw new StatusException
526 (Status
.failed("Reelation InteractionHandler not found"));
529 handler
= (XInteractionHandler
)UnoRuntime
.queryInterface
530 (XInteractionHandler
.class, oHandler
);
531 oObj
.setInteractionHandler(handler
);
532 tRes
.tested("setInteractionHandler()", true);
533 } catch (Exception ex
) {
534 log
.println("Exception occurred while testing 'setInteractionHandler()'");
535 ex
.printStackTrace(log
);
536 tRes
.tested("setInteractionHandler()", false);
539 } //EOF setInteractionHandler()
541 } // finish class _XSimpleFileAccess