1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XSimpleFileAccess.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import lib
.MultiMethodTest
;
35 import lib
.StatusException
;
37 import com
.sun
.star
.lang
.XMultiServiceFactory
;
38 import com
.sun
.star
.task
.XInteractionHandler
;
39 import com
.sun
.star
.ucb
.XSimpleFileAccess
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.util
.DateTime
;
44 * Testing <code>com.sun.star.ucb.XSimpleFileAccess</code>
45 * interface methods. <p>
46 * The following predefined files needed to complete the test:
48 * <li> <code>XSimpleFileAccess/XSimpleFileAccess.txt</code> :
49 * text file of length 17 and 2000 year created .</li>
50 * <li> <code>XSimpleFileAccess/XSimpleFileAccess2.txt</code> :
51 * text file for <code>openFileReadWrite</code> method test.</li>
53 * This test needs the following object relations :
55 * <li> <code>'InteractionHandler'</code>
56 * (of type <code>XInteractionHandler</code>)
57 * instance of <code>com.sun.star.sdb.InteractionHandler</code>
60 * Test is <b> NOT </b> multithread compilant. <p>
61 * @see com.sun.star.ucb.XSimpleFileAccess
63 public class _XSimpleFileAccess
extends MultiMethodTest
{
65 public static XSimpleFileAccess oObj
= null;
68 * Copies <b>XSimpleFileAccess.txt</b> to a new file, checks
69 * if it was successfully copied and then deletes it. <p>
70 * Has <b> OK </b> status if after method call new copy of file
71 * exists and no exceptions were thrown. <p>
75 String copiedFile
= "";
76 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
77 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
78 String filename
= dirname
+"XSimpleFileAccess.txt";
79 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
81 if (oObj
.exists(copiedFile
))
82 oObj
.kill(copiedFile
);
84 oObj
.copy(filename
,copiedFile
);
85 tRes
.tested("copy()",oObj
.exists(copiedFile
));
86 oObj
.kill(copiedFile
);
88 catch (com
.sun
.star
.uno
.Exception ex
) {
89 log
.println("Exception occured while testing 'copy()'");
90 ex
.printStackTrace(log
);
91 tRes
.tested("copy()",false);
97 * Copies <b>XSimpleFileAccess.txt</b> to a new file, tries to
98 * rename it, then checks
99 * if it was successfully renamed and then deletes it. <p>
100 * Has <b> OK </b> status if after method call new file
101 * exists and no exceptions were thrown. <p>
103 public void _move() {
105 String copiedFile
= "";
106 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
107 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
108 String filename
= dirname
+"XSimpleFileAccess.txt";
109 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
111 if (oObj
.exists(copiedFile
))
112 oObj
.kill(copiedFile
);
114 oObj
.copy(filename
,copiedFile
);
115 filename
= copiedFile
;
116 copiedFile
= dirnameTo
+ "XSimpleFileAccess_move.txt";
117 oObj
.move(filename
,copiedFile
);
118 tRes
.tested("move()",oObj
.exists(copiedFile
));
119 oObj
.kill(copiedFile
);
121 catch (com
.sun
.star
.uno
.Exception ex
) {
122 log
.println("Exception occured while testing 'move()'");
123 ex
.printStackTrace(log
);
124 tRes
.tested("move()",false);
130 * Copies <b>XSimpleFileAccess.txt</b> to a new file, deletes it
131 * and checks if it isn't exist. <p>
132 * Has <b> OK </b> status if after method call new copy of file
133 * doesn't exist and no exceptions were thrown. <p>
135 public void _kill() {
137 String copiedFile
= "";
138 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
139 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
140 String filename
= dirname
+"XSimpleFileAccess.txt";
141 copiedFile
= dirnameTo
+ "XSimpleFileAccess_copy.txt";
143 if (oObj
.exists(copiedFile
))
144 oObj
.kill(copiedFile
);
146 oObj
.copy(filename
,copiedFile
);
147 oObj
.kill(copiedFile
);
148 tRes
.tested("kill()",!oObj
.exists(copiedFile
));
150 catch (com
.sun
.star
.uno
.Exception ex
) {
151 log
.println("Exception occured while testing 'kill()'");
152 ex
.printStackTrace(log
);
153 tRes
.tested("kill()",false);
159 * Tries to check if <b>XSimpleFileAccess</b> is folder. <p>
160 * Has <b>OK</b> status if the method returns <code>true</code>
162 public void _isFolder() {
164 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
165 tRes
.tested("isFolder()",oObj
.isFolder(dirname
));
167 catch (com
.sun
.star
.uno
.Exception ex
) {
168 log
.println("Exception occured while testing 'isFolder()'");
169 ex
.printStackTrace(log
);
170 tRes
.tested("isFolder()",false);
176 * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
177 * 'READONLY' attribute and checks it. Second clears 'READONLY'
178 * attribute and checks it again. The copy of file is deleted
181 * Has <b> OK </b> status if in the first case method returns
182 * <code></code>, and in the second case - <code>false</code>
183 * and no exceptions were thrown. <p>
185 * The following method tests are to be completed successfully before :
187 * <li> <code> setReadOnly </code> </li>
190 public void _isReadOnly() {
191 requiredMethod("setReadOnly()");
193 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
194 String filename
= dirname
+"XSimpleFileAccess.txt";
195 boolean result
= true;
197 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
198 String readonlyCopy
= dirnameTo
+ "XSimpleFileAccess_copy.txt" ;
200 if (oObj
.exists(readonlyCopy
))
201 oObj
.kill(readonlyCopy
);
203 oObj
.copy(filename
, readonlyCopy
);
205 oObj
.setReadOnly(readonlyCopy
, true);
206 result
&= oObj
.isReadOnly(readonlyCopy
);
207 oObj
.setReadOnly(readonlyCopy
, false);
208 result
&= !oObj
.isReadOnly(readonlyCopy
);
210 oObj
.kill(readonlyCopy
);
211 tRes
.tested("isReadOnly()",result
);
212 } catch (com
.sun
.star
.uno
.Exception ex
) {
213 log
.println("Exception occured while testing 'isReadOnly()'");
214 ex
.printStackTrace(log
);
215 tRes
.tested("isReadOnly()",false);
222 * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
223 * 'READONLY' attribute and checks it. Second clears 'READONLY'
224 * attribute and checks it again. The copy of file is deleted
227 * Has <b> OK </b> status if in the first case method returns
228 * <code></code>, and in the second case - <code>false</code>
229 * and no exceptions were thrown. <p>
231 * The following method tests are to be completed successfully before :
233 * <li> <code> setReadOnly </code> </li>
236 public void _setReadOnly() {
237 boolean result
= true ;
240 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
241 String filename
= dirname
+"XSimpleFileAccess.txt";
243 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
244 String readonlyCopy
= dirnameTo
+ "XSimpleFileAccess_copy.txt" ;
246 if (oObj
.exists(readonlyCopy
))
247 oObj
.kill(readonlyCopy
);
249 oObj
.copy(filename
, readonlyCopy
);
251 oObj
.setReadOnly(readonlyCopy
, true);
252 result
&= oObj
.isReadOnly(readonlyCopy
) ;
253 oObj
.setReadOnly(readonlyCopy
, false);
254 result
&= !oObj
.isReadOnly(readonlyCopy
) ;
255 tRes
.tested("setReadOnly()", result
);
257 oObj
.kill(readonlyCopy
);
259 catch (Exception ex
) {
260 log
.println("Exception occured while testing 'setReadOnly()'");
261 ex
.printStackTrace(log
);
262 tRes
.tested("setReadOnly()",false);
264 } //EOF setReadOnly()
267 * Creates folder and then checks if it was successfully created. <p>
268 * Has <b>OK</b> status if folder was created and no exceptions
271 public void _createFolder() {
273 String tmpdirname
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
274 String newFolder
= tmpdirname
+"SimpleSubFolder";
276 if (oObj
.exists(newFolder
))
277 oObj
.kill(newFolder
);
279 oObj
.createFolder(newFolder
);
280 tRes
.tested("createFolder()",oObj
.isFolder(newFolder
));
281 oObj
.kill(newFolder
);
283 catch (com
.sun
.star
.uno
.Exception ex
) {
284 log
.println("Exception occured while testing 'createFolder()'");
285 ex
.printStackTrace(log
);
286 tRes
.tested("createFolder()",false);
289 } //EOF createFolder()
292 * Test calls the method and checks return value and that
293 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
295 * Has <b> OK </b> status if the method returns <code>17</code>
296 * and no exceptions were thrown. <p>
298 public void _getSize() {
300 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
301 String filename
= dirname
+"XSimpleFileAccess.txt";
302 int fSize
= oObj
.getSize(filename
);
303 tRes
.tested("getSize()", fSize
== 17 );
305 catch (com
.sun
.star
.uno
.Exception ex
) {
306 log
.println("Exception occured while testing 'getSize()'");
307 ex
.printStackTrace(log
);
308 tRes
.tested("getSize()",false);
314 * Test calls the method and checks return value and that
315 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
317 * Has <b> OK </b> status if the method returns String
318 * <code>'application/vnd.sun.staroffice.fsys-file'</code>
319 * and no exceptions were thrown. <p>
321 public void _getContentType() {
323 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
324 String filename
= dirname
+"XSimpleFileAccess.txt";
325 String fType
= oObj
.getContentType(filename
);
326 tRes
.tested("getContentType()",
327 "application/vnd.sun.staroffice.fsys-file".equals(fType
) );
329 catch (com
.sun
.star
.uno
.Exception ex
) {
330 log
.println("Exception occured while testing 'getContentType()'");
331 ex
.printStackTrace(log
);
332 tRes
.tested("getContentType()",false);
335 } //EOF getContentType()
338 * Test calls the method and checks return value and that
339 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
341 * Has <b> OK </b> status if the method returns date with
342 * 2001 year and no exceptions were thrown. <p>
344 public void _getDateTimeModified() {
346 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
347 String filename
= dirname
+"XSimpleFileAccess.txt";
348 DateTime fTime
= oObj
.getDateTimeModified(filename
);
350 java
.io
.File the_file
= new java
.io
.File(filename
);
351 long lastModified
= the_file
.lastModified();
352 java
.util
.Date lastMod
= new java
.util
.Date(lastModified
);
354 //compare the dates gained by java with those gained by this method
356 boolean partResult
= (fTime
.Day
== lastMod
.getDay());
358 log
.println("Wrong Day");
359 log
.println("Expected: "+lastMod
.getDay());
360 log
.println("Gained: "+fTime
.Day
);
361 log
.println("------------------------------");
363 partResult
= (fTime
.Month
== lastMod
.getMonth());
365 log
.println("Wrong Month");
366 log
.println("Expected: "+lastMod
.getMonth());
367 log
.println("Gained: "+fTime
.Month
);
368 log
.println("------------------------------");
371 partResult
= (fTime
.Year
== lastMod
.getYear());
373 log
.println("Wrong Year");
374 log
.println("Expected: "+lastMod
.getYear());
375 log
.println("Gained: "+fTime
.Year
);
376 log
.println("------------------------------");
379 tRes
.tested("getDateTimeModified()", res
);
381 catch (com
.sun
.star
.uno
.Exception ex
) {
382 log
.println("Exception occured while testing 'getDateTimeModified()'");
383 ex
.printStackTrace(log
);
384 tRes
.tested("getDateTimeModified()",false);
387 } //EOF getDateTimeModified()
390 * Test calls the method and checks return value and that
391 * no exceptions were thrown. <b>XSimpleFileAccess</b>
393 * Has <b> OK </b> status if the method returns non zero length
394 * array and no exceptions were thrown. <p>
396 public void _getFolderContents() {
398 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
399 String
[] cont
= oObj
.getFolderContents(dirname
,false);
400 tRes
.tested("getFolderContents()", cont
.length
>0);
402 catch (com
.sun
.star
.uno
.Exception ex
) {
403 log
.println("Exception occured while testing 'getFolderContents()'");
404 ex
.printStackTrace(log
);
405 tRes
.tested("getFolderContents()",false);
408 } //EOF getFolderContents()
411 * First it check file <b>XSimpleFileAccess.txt</b> for
412 * existence, second file <b>I_do_not_exists.txt</b> is checked
414 * Has <b> OK </b> status if in the first case method returns
415 * <code>true</code> and in the second - <code>flase</code>
416 * and no exceptions were thrown. <p>
418 public void _exists() {
420 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
421 String filename
= dirname
+"XSimpleFileAccess.txt";
422 String wrongname
= dirname
+"I_do_not_exists.txt";
423 tRes
.tested("exists()",
424 oObj
.exists(filename
) && !oObj
.exists(wrongname
));
426 catch (com
.sun
.star
.uno
.Exception ex
) {
427 log
.println("Exception occured while testing 'exists()'");
428 ex
.printStackTrace(log
);
429 tRes
.tested("exists()",false);
435 * Test calls the method and checks return value and that
436 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
438 * Has <b> OK </b> status if the method returns not
439 * <code>null</code> value and no exceptions were thrown. <p>
441 public void _openFileRead() {
443 String dirname
= util
.utils
.getFullTestURL("XSimpleFileAccess");
444 String filename
= dirname
+"XSimpleFileAccess.txt";
445 com
.sun
.star
.io
.XInputStream iStream
= oObj
.openFileRead(filename
);
446 tRes
.tested("openFileRead()", iStream
!= null);
448 catch (com
.sun
.star
.uno
.Exception ex
) {
449 log
.println("Exception occured while testing 'openFileRead()'");
450 ex
.printStackTrace(log
);
451 tRes
.tested("openFileRead()",false);
454 } //EOF openFileRead()
457 * Test calls the method and checks return value and that
458 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
460 * Has <b> OK </b> status if the method returns not
461 * <code>null</code> value and no exceptions were thrown. <p>
463 public void _openFileWrite() {
465 String tmpdirname
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
467 String copiedFile
= tmpdirname
+"XSimpleFileAccess_openWrite.txt";
469 if (oObj
.exists(copiedFile
))
470 oObj
.kill(copiedFile
);
472 com
.sun
.star
.io
.XOutputStream oStream
=
473 oObj
.openFileWrite(copiedFile
);
474 tRes
.tested("openFileWrite()", oStream
!= null);
476 oStream
.closeOutput();
477 oObj
.kill(copiedFile
);
479 catch (com
.sun
.star
.uno
.Exception ex
) {
480 log
.println("Exception occured while testing 'openFileWrite()'");
481 ex
.printStackTrace(log
);
482 tRes
.tested("openFileWrite()",false);
485 } //EOF openFileWrite()
488 * Test calls the method and checks return value and that
489 * no exceptions were thrown. <b>XSimpleFileAccess2.txt</b>
491 * Has <b> OK </b> status if the method returns not
492 * <code>null</code> value and no exceptions were thrown. <p>
494 public void _openFileReadWrite() {
496 String dirnameTo
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
497 String copiedFile
= dirnameTo
+ "XSimpleFileAccess2.txt" ;
499 if (oObj
.exists(copiedFile
))
500 oObj
.kill(copiedFile
);
502 com
.sun
.star
.io
.XStream aStream
=
503 oObj
.openFileReadWrite(copiedFile
);
504 tRes
.tested("openFileReadWrite()", aStream
!= null);
506 aStream
.getInputStream().closeInput();
507 aStream
.getOutputStream().closeOutput();
509 oObj
.kill(copiedFile
);
511 catch (com
.sun
.star
.uno
.Exception ex
) {
512 log
.println("Exception occured while testing 'openFileReadWrite()'");
513 ex
.printStackTrace(log
);
514 tRes
.tested("openFileReadWrite()",false);
517 } //EOF openFileReadWrite()
520 * Test calls the method and checks that no exceptions were thrown.
521 * Has <b> OK </b> status if no exceptions were thrown. <p>
523 public void _setInteractionHandler() {
524 XInteractionHandler handler
= null;
525 Object oHandler
= tEnv
.getObjRelation("InteractionHandler");;
527 if (oHandler
== null)
528 throw new StatusException
529 (Status
.failed("Reelation InteractionHandler not found"));
532 handler
= (XInteractionHandler
)UnoRuntime
.queryInterface
533 (XInteractionHandler
.class, oHandler
);
534 oObj
.setInteractionHandler(handler
);
535 tRes
.tested("setInteractionHandler()", true);
536 } catch (Exception ex
) {
537 log
.println("Exception occured while testing 'setInteractionHandler()'");
538 ex
.printStackTrace(log
);
539 tRes
.tested("setInteractionHandler()", false);
542 } //EOF setInteractionHandler()
544 } // finish class _XSimpleFileAccess