1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.ucb;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import lib.StatusException;
29 
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.task.XInteractionHandler;
32 import com.sun.star.ucb.XSimpleFileAccess;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.util.DateTime;
35 
36 /**
37 * Testing <code>com.sun.star.ucb.XSimpleFileAccess</code>
38 * interface methods. <p>
39 * The following predefined files needed to complete the test:
40 * <ul>
41 *  <li> <code>XSimpleFileAccess/XSimpleFileAccess.txt</code> :
42 *   text file of length 17 and 2000 year created .</li>
43 *  <li> <code>XSimpleFileAccess/XSimpleFileAccess2.txt</code> :
44 *   text file for <code>openFileReadWrite</code> method test.</li>
45 * <ul> <p>
46 * This test needs the following object relations :
47 * <ul>
48 *  <li> <code>'InteractionHandler'</code>
49 *  (of type <code>XInteractionHandler</code>)
50 *  instance of <code>com.sun.star.sdb.InteractionHandler</code>
51 *  </li>
52 * </ul> <p>
53 * Test is <b> NOT </b> multithread compilant. <p>
54 * @see com.sun.star.ucb.XSimpleFileAccess
55 */
56 public class _XSimpleFileAccess extends MultiMethodTest {
57 
58     public static XSimpleFileAccess oObj = null;
59 
60     /**
61     * Copies <b>XSimpleFileAccess.txt</b> to a new file, checks
62     * if it was successfully copied and then deletes it. <p>
63     * Has <b> OK </b> status if after method call new copy of file
64     * exists and no exceptions were thrown. <p>
65     */
_copy()66     public void _copy() {
67         try {
68             String copiedFile = "";
69             String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
70             String dirnameTo = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ;
71             String filename = dirname+"XSimpleFileAccess.txt";
72             copiedFile = dirnameTo + "XSimpleFileAccess_copy.txt";
73 
74             if (oObj.exists(copiedFile))
75                 oObj.kill(copiedFile);
76 
77             oObj.copy(filename,copiedFile);
78             tRes.tested("copy()",oObj.exists(copiedFile));
79             oObj.kill(copiedFile);
80         }
81         catch (com.sun.star.uno.Exception ex) {
82             log.println("Exception occured while testing 'copy()'");
83             ex.printStackTrace(log);
84             tRes.tested("copy()",false);
85         }
86 
87     } //EOF copy()
88 
89     /**
90     * Copies <b>XSimpleFileAccess.txt</b> to a new file, tries to
91     * rename it, then checks
92     * if it was successfully renamed and then deletes it. <p>
93     * Has <b> OK </b> status if after method call new file
94     * exists and no exceptions were thrown. <p>
95     */
_move()96     public void _move() {
97         try {
98             String copiedFile = "";
99             String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
100             String dirnameTo = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ;
101             String filename = dirname+"XSimpleFileAccess.txt";
102             copiedFile = dirnameTo + "XSimpleFileAccess_copy.txt";
103 
104             if (oObj.exists(copiedFile))
105                 oObj.kill(copiedFile);
106 
107             oObj.copy(filename,copiedFile);
108             filename = copiedFile;
109             copiedFile = dirnameTo + "XSimpleFileAccess_move.txt";
110             oObj.move(filename,copiedFile);
111             tRes.tested("move()",oObj.exists(copiedFile));
112             oObj.kill(copiedFile);
113         }
114         catch (com.sun.star.uno.Exception ex) {
115             log.println("Exception occured while testing 'move()'");
116             ex.printStackTrace(log);
117             tRes.tested("move()",false);
118         }
119 
120     } //EOF move()
121 
122     /**
123     * Copies <b>XSimpleFileAccess.txt</b> to a new file, deletes it
124     * and checks if it isn't exist. <p>
125     * Has <b> OK </b> status if after method call new copy of file
126     * doesn't exist and no exceptions were thrown. <p>
127     */
_kill()128     public void _kill() {
129         try {
130             String copiedFile = "";
131             String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
132             String dirnameTo = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ;
133             String filename = dirname+"XSimpleFileAccess.txt";
134             copiedFile = dirnameTo + "XSimpleFileAccess_copy.txt";
135 
136             if (oObj.exists(copiedFile))
137                 oObj.kill(copiedFile);
138 
139             oObj.copy(filename,copiedFile);
140             oObj.kill(copiedFile);
141             tRes.tested("kill()",!oObj.exists(copiedFile));
142         }
143         catch (com.sun.star.uno.Exception ex) {
144             log.println("Exception occured while testing 'kill()'");
145             ex.printStackTrace(log);
146             tRes.tested("kill()",false);
147         }
148 
149     } //EOF kill()
150 
151     /**
152     * Tries to check if <b>XSimpleFileAccess</b> is folder. <p>
153     * Has <b>OK</b> status if the method returns <code>true</code>
154     */
_isFolder()155     public void _isFolder() {
156         try {
157             String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
158             tRes.tested("isFolder()",oObj.isFolder(dirname));
159         }
160         catch (com.sun.star.uno.Exception ex) {
161             log.println("Exception occured while testing 'isFolder()'");
162             ex.printStackTrace(log);
163             tRes.tested("isFolder()",false);
164         }
165 
166     } //EOF isFolder()
167 
168     /**
169      * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
170      * 'READONLY' attribute and checks it. Second clears 'READONLY'
171      * attribute and checks it again. The copy of file is deleted
172      * finally.<p>
173      *
174      * Has <b> OK </b> status if in the first case method returns
175      * <code></code>, and in the second case - <code>false</code>
176      * and no exceptions were thrown. <p>
177      *
178      * The following method tests are to be completed successfully before :
179      * <ul>
180      *  <li> <code> setReadOnly </code> </li>
181      * </ul>
182      */
_isReadOnly()183     public void _isReadOnly() {
184         requiredMethod("setReadOnly()");
185         try {
186             String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
187             String filename = dirname+"XSimpleFileAccess.txt";
188             boolean result = true;
189 
190             String dirnameTo = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ;
191             String readonlyCopy = dirnameTo + "XSimpleFileAccess_copy.txt" ;
192 
193             if (oObj.exists(readonlyCopy))
194                 oObj.kill(readonlyCopy);
195 
196             oObj.copy(filename, readonlyCopy);
197 
198             oObj.setReadOnly(readonlyCopy, true);
199             result &= oObj.isReadOnly(readonlyCopy);
200             oObj.setReadOnly(readonlyCopy, false);
201             result &= !oObj.isReadOnly(readonlyCopy);
202 
203             oObj.kill(readonlyCopy);
204             tRes.tested("isReadOnly()",result);
205         } catch (com.sun.star.uno.Exception ex) {
206             log.println("Exception occured while testing 'isReadOnly()'");
207             ex.printStackTrace(log);
208             tRes.tested("isReadOnly()",false);
209         }
210 
211     } //EOF isReadOnly()
212 
213 
214     /**
215      * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
216      * 'READONLY' attribute and checks it. Second clears 'READONLY'
217      * attribute and checks it again. The copy of file is deleted
218      * finally.<p>
219      *
220      * Has <b> OK </b> status if in the first case method returns
221      * <code></code>, and in the second case - <code>false</code>
222      * and no exceptions were thrown. <p>
223      *
224      * The following method tests are to be completed successfully before :
225      * <ul>
226      *  <li> <code> setReadOnly </code> </li>
227      * </ul>
228      */
_setReadOnly()229     public void _setReadOnly() {
230         boolean result = true ;
231 
232         try {
233             String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
234             String filename = dirname+"XSimpleFileAccess.txt";
235 
236             String dirnameTo = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ;
237             String readonlyCopy = dirnameTo + "XSimpleFileAccess_copy.txt" ;
238 
239             if (oObj.exists(readonlyCopy))
240                 oObj.kill(readonlyCopy);
241 
242             oObj.copy(filename, readonlyCopy);
243 
244             oObj.setReadOnly(readonlyCopy, true);
245             result &= oObj.isReadOnly(readonlyCopy) ;
246             oObj.setReadOnly(readonlyCopy, false);
247             result &= !oObj.isReadOnly(readonlyCopy) ;
248             tRes.tested("setReadOnly()", result);
249 
250             oObj.kill(readonlyCopy);
251         }
252         catch (Exception ex) {
253             log.println("Exception occured while testing 'setReadOnly()'");
254             ex.printStackTrace(log);
255             tRes.tested("setReadOnly()",false);
256         }
257     } //EOF setReadOnly()
258 
259     /**
260      * Creates folder and then checks if it was successfully created. <p>
261      * Has <b>OK</b> status if folder was created and no exceptions
262      * were thrown.
263      */
_createFolder()264     public void _createFolder() {
265         try {
266             String tmpdirname = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ;
267             String newFolder = tmpdirname+"SimpleSubFolder";
268 
269             if (oObj.exists(newFolder))
270                 oObj.kill(newFolder);
271 
272             oObj.createFolder(newFolder);
273             tRes.tested("createFolder()",oObj.isFolder(newFolder));
274             oObj.kill(newFolder);
275         }
276         catch (com.sun.star.uno.Exception ex) {
277             log.println("Exception occured while testing 'createFolder()'");
278             ex.printStackTrace(log);
279             tRes.tested("createFolder()",false);
280         }
281 
282     } //EOF createFolder()
283 
284     /**
285     * Test calls the method and checks return value and that
286     * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
287     * file tested.<p>
288     * Has <b> OK </b> status if the method returns <code>17</code>
289     * and no exceptions were thrown. <p>
290     */
_getSize()291     public void _getSize() {
292         try {
293             String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
294             String filename = dirname+"XSimpleFileAccess.txt";
295             int fSize = oObj.getSize(filename);
296             tRes.tested("getSize()", fSize == 17 );
297         }
298         catch (com.sun.star.uno.Exception ex) {
299             log.println("Exception occured while testing 'getSize()'");
300             ex.printStackTrace(log);
301             tRes.tested("getSize()",false);
302         }
303 
304     } //EOF getSize()
305 
306     /**
307     * Test calls the method and checks return value and that
308     * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
309     * file tested.<p>
310     * Has <b> OK </b> status if the method returns String
311     * <code>'application/vnd.sun.staroffice.fsys-file'</code>
312     * and no exceptions were thrown. <p>
313     */
_getContentType()314     public void _getContentType() {
315         try {
316             String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
317             String filename = dirname+"XSimpleFileAccess.txt";
318             String fType = oObj.getContentType(filename);
319             tRes.tested("getContentType()",
320                 "application/vnd.sun.staroffice.fsys-file".equals(fType) );
321         }
322         catch (com.sun.star.uno.Exception ex) {
323             log.println("Exception occured while testing 'getContentType()'");
324             ex.printStackTrace(log);
325             tRes.tested("getContentType()",false);
326         }
327 
328     } //EOF getContentType()
329 
330     /**
331     * Test calls the method and checks return value and that
332     * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
333     * file tested.<p>
334     * Has <b> OK </b> status if the method returns date with
335     * 2001 year and no exceptions were thrown. <p>
336     */
_getDateTimeModified()337     public void _getDateTimeModified() {
338         try {
339             String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
340             String filename = dirname+"XSimpleFileAccess.txt";
341             DateTime fTime = oObj.getDateTimeModified(filename);
342 
343             java.io.File the_file = new java.io.File(filename);
344             long lastModified = the_file.lastModified();
345             java.util.Date lastMod = new java.util.Date(lastModified);
346 
347             //compare the dates gained by java with those gained by this method
348             boolean res = true;
349             boolean partResult = (fTime.Day == lastMod.getDay());
350             if (!partResult) {
351                 log.println("Wrong Day");
352                 log.println("Expected: "+lastMod.getDay());
353                 log.println("Gained: "+fTime.Day);
354                 log.println("------------------------------");
355             }
356             partResult = (fTime.Month == lastMod.getMonth());
357             if (!partResult) {
358                 log.println("Wrong Month");
359                 log.println("Expected: "+lastMod.getMonth());
360                 log.println("Gained: "+fTime.Month);
361                 log.println("------------------------------");
362             }
363 
364             partResult = (fTime.Year == lastMod.getYear());
365             if (!partResult) {
366                 log.println("Wrong Year");
367                 log.println("Expected: "+lastMod.getYear());
368                 log.println("Gained: "+fTime.Year);
369                 log.println("------------------------------");
370             }
371 
372             tRes.tested("getDateTimeModified()", res);
373         }
374         catch (com.sun.star.uno.Exception ex) {
375             log.println("Exception occured while testing 'getDateTimeModified()'");
376             ex.printStackTrace(log);
377             tRes.tested("getDateTimeModified()",false);
378         }
379 
380     } //EOF getDateTimeModified()
381 
382     /**
383     * Test calls the method and checks return value and that
384     * no exceptions were thrown. <b>XSimpleFileAccess</b>
385     * directory used.<p>
386     * Has <b> OK </b> status if the method returns non zero length
387     * array and no exceptions were thrown. <p>
388     */
_getFolderContents()389     public void _getFolderContents() {
390         try {
391             String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
392             String[] cont = oObj.getFolderContents(dirname,false);
393             tRes.tested("getFolderContents()", cont.length>0);
394         }
395         catch (com.sun.star.uno.Exception ex) {
396             log.println("Exception occured while testing 'getFolderContents()'");
397             ex.printStackTrace(log);
398             tRes.tested("getFolderContents()",false);
399         }
400 
401     } //EOF getFolderContents()
402 
403     /**
404     * First it check file <b>XSimpleFileAccess.txt</b> for
405     * existence, second file <b>I_do_not_exists.txt</b> is checked
406     * for existence. <p>
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>
410     */
_exists()411     public void _exists() {
412         try {
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));
418         }
419         catch (com.sun.star.uno.Exception ex) {
420             log.println("Exception occured while testing 'exists()'");
421             ex.printStackTrace(log);
422             tRes.tested("exists()",false);
423         }
424 
425     } //EOF exists()
426 
427     /**
428     * Test calls the method and checks return value and that
429     * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
430     * file used.<p>
431     * Has <b> OK </b> status if the method returns not
432     * <code>null</code> value and no exceptions were thrown. <p>
433     */
_openFileRead()434     public void _openFileRead() {
435         try {
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);
440         }
441         catch (com.sun.star.uno.Exception ex) {
442             log.println("Exception occured while testing 'openFileRead()'");
443             ex.printStackTrace(log);
444             tRes.tested("openFileRead()",false);
445         }
446 
447     } //EOF openFileRead()
448 
449     /**
450     * Test calls the method and checks return value and that
451     * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
452     * file used.<p>
453     * Has <b> OK </b> status if the method returns not
454     * <code>null</code> value and no exceptions were thrown. <p>
455     */
_openFileWrite()456     public void _openFileWrite() {
457         try {
458             String tmpdirname = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ;
459 
460             String copiedFile = tmpdirname+"XSimpleFileAccess_openWrite.txt";
461 
462             if (oObj.exists(copiedFile))
463                 oObj.kill(copiedFile);
464 
465             com.sun.star.io.XOutputStream oStream =
466                 oObj.openFileWrite(copiedFile);
467             tRes.tested("openFileWrite()", oStream != null);
468 
469             oStream.closeOutput();
470             oObj.kill(copiedFile);
471         }
472         catch (com.sun.star.uno.Exception ex) {
473             log.println("Exception occured while testing 'openFileWrite()'");
474             ex.printStackTrace(log);
475             tRes.tested("openFileWrite()",false);
476         }
477 
478     } //EOF openFileWrite()
479 
480     /**
481     * Test calls the method and checks return value and that
482     * no exceptions were thrown. <b>XSimpleFileAccess2.txt</b>
483     * file used.<p>
484     * Has <b> OK </b> status if the method returns not
485     * <code>null</code> value and no exceptions were thrown. <p>
486     */
_openFileReadWrite()487     public void _openFileReadWrite() {
488         try {
489             String dirnameTo = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ;
490             String copiedFile = dirnameTo + "XSimpleFileAccess2.txt" ;
491 
492             if (oObj.exists(copiedFile))
493                 oObj.kill(copiedFile);
494 
495             com.sun.star.io.XStream aStream =
496                 oObj.openFileReadWrite(copiedFile);
497             tRes.tested("openFileReadWrite()", aStream != null);
498 
499             aStream.getInputStream().closeInput();
500             aStream.getOutputStream().closeOutput();
501 
502             oObj.kill(copiedFile);
503         }
504         catch (com.sun.star.uno.Exception ex) {
505             log.println("Exception occured while testing 'openFileReadWrite()'");
506             ex.printStackTrace(log);
507             tRes.tested("openFileReadWrite()",false);
508         }
509 
510     } //EOF openFileReadWrite()
511 
512     /**
513     * Test calls the method and checks that no exceptions were thrown.
514     * Has <b> OK </b> status if no exceptions were thrown. <p>
515     */
_setInteractionHandler()516     public void _setInteractionHandler() {
517         XInteractionHandler handler = null;
518         Object oHandler = tEnv.getObjRelation("InteractionHandler");
519 
520         if (oHandler == null)
521             throw new StatusException
522                 (Status.failed("Reelation InteractionHandler not found"));
523 
524         try {
525             handler = (XInteractionHandler)UnoRuntime.queryInterface
526                 (XInteractionHandler.class, oHandler);
527             oObj.setInteractionHandler(handler);
528             tRes.tested("setInteractionHandler()", true);
529         } catch (Exception ex) {
530             log.println("Exception occured while testing 'setInteractionHandler()'");
531             ex.printStackTrace(log);
532             tRes.tested("setInteractionHandler()", false);
533         }
534 
535     } //EOF setInteractionHandler()
536 
537 }  // finish class _XSimpleFileAccess
538 
539