summaryrefslogtreecommitdiff
path: root/qadevOOo/tests/java/ifc/ucb/_XSimpleFileAccess.java
blob: 0d34af24ba6bbf77a8c4f9098529a27d30632389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package ifc.ucb;

import lib.MultiMethodTest;
import lib.Status;
import lib.StatusException;

import com.sun.star.task.XInteractionHandler;
import com.sun.star.ucb.XSimpleFileAccess;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.DateTime;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
* Testing <code>com.sun.star.ucb.XSimpleFileAccess</code>
* interface methods. <p>
* The following predefined files needed to complete the test:
* <ul>
*  <li> <code>XSimpleFileAccess/XSimpleFileAccess.txt</code> :
*   text file of length 17 and 2000 year created .</li>
*  <li> <code>XSimpleFileAccess/XSimpleFileAccess2.txt</code> :
*   text file for <code>openFileReadWrite</code> method test.</li>
* <ul> <p>
* This test needs the following object relations :
* <ul>
*  <li> <code>'InteractionHandler'</code>
*  (of type <code>XInteractionHandler</code>)
*  instance of <code>com.sun.star.sdb.InteractionHandler</code>
*  </li>
* </ul> <p>
* Test is <b> NOT </b> multithread compliant. <p>
* @see com.sun.star.ucb.XSimpleFileAccess
*/
public class _XSimpleFileAccess extends MultiMethodTest {

    public static XSimpleFileAccess oObj = null;

    /**
    * Copies <b>XSimpleFileAccess.txt</b> to a new file, checks
    * if it was successfully copied and then deletes it. <p>
    * Has <b> OK </b> status if after method call new copy of file
    * exists and no exceptions were thrown. <p>
    */
    public void _copy() {
        try {
            String copiedFile = "";
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String dirnameTo = util.utils.getOfficeTemp(tParam.getMSF()) ;
            String filename = dirname+"XSimpleFileAccess.txt";
            copiedFile = dirnameTo + "XSimpleFileAccess_copy.txt";

            if (oObj.exists(copiedFile))
                oObj.kill(copiedFile);

            oObj.copy(filename,copiedFile);
            tRes.tested("copy()",oObj.exists(copiedFile));
            oObj.kill(copiedFile);
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'copy()'");
            ex.printStackTrace(log);
            tRes.tested("copy()",false);
        }

    } //EOF copy()

    /**
    * Copies <b>XSimpleFileAccess.txt</b> to a new file, tries to
    * rename it, then checks
    * if it was successfully renamed and then deletes it. <p>
    * Has <b> OK </b> status if after method call new file
    * exists and no exceptions were thrown. <p>
    */
    public void _move() {
        try {
            String copiedFile = "";
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String dirnameTo = util.utils.getOfficeTemp(tParam.getMSF()) ;
            String filename = dirname+"XSimpleFileAccess.txt";
            copiedFile = dirnameTo + "XSimpleFileAccess_copy.txt";

            if (oObj.exists(copiedFile))
                oObj.kill(copiedFile);

            oObj.copy(filename,copiedFile);
            filename = copiedFile;
            copiedFile = dirnameTo + "XSimpleFileAccess_move.txt";
            oObj.move(filename,copiedFile);
            tRes.tested("move()",oObj.exists(copiedFile));
            oObj.kill(copiedFile);
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'move()'");
            ex.printStackTrace(log);
            tRes.tested("move()",false);
        }

    } //EOF move()

    /**
    * Copies <b>XSimpleFileAccess.txt</b> to a new file, deletes it
    * and checks if it isn't exist. <p>
    * Has <b> OK </b> status if after method call new copy of file
    * doesn't exist and no exceptions were thrown. <p>
    */
    public void _kill() {
        try {
            String copiedFile = "";
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String dirnameTo = util.utils.getOfficeTemp(tParam.getMSF()) ;
            String filename = dirname+"XSimpleFileAccess.txt";
            copiedFile = dirnameTo + "XSimpleFileAccess_copy.txt";

            if (oObj.exists(copiedFile))
                oObj.kill(copiedFile);

            oObj.copy(filename,copiedFile);
            oObj.kill(copiedFile);
            tRes.tested("kill()",!oObj.exists(copiedFile));
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'kill()'");
            ex.printStackTrace(log);
            tRes.tested("kill()",false);
        }

    } //EOF kill()

    /**
    * Tries to check if <b>XSimpleFileAccess</b> is folder. <p>
    * Has <b>OK</b> status if the method returns <code>true</code>
    */
    public void _isFolder() {
        try {
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            tRes.tested("isFolder()",oObj.isFolder(dirname));
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'isFolder()'");
            ex.printStackTrace(log);
            tRes.tested("isFolder()",false);
        }

    } //EOF isFolder()

    /**
     * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
     * 'READONLY' attribute and checks it. Second clears 'READONLY'
     * attribute and checks it again. The copy of file is deleted
     * finally.<p>
     *
     * Has <b> OK </b> status if in the first case method returns
     * <code></code>, and in the second case - <code>false</code>
     * and no exceptions were thrown. <p>
     *
     * The following method tests are to be completed successfully before :
     * <ul>
     *  <li> <code> setReadOnly </code> </li>
     * </ul>
     */
    public void _isReadOnly() {
        requiredMethod("setReadOnly()");
        try {
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String filename = dirname+"XSimpleFileAccess.txt";
            boolean result = true;

            String dirnameTo = util.utils.getOfficeTemp(tParam.getMSF()) ;
            String readonlyCopy = dirnameTo + "XSimpleFileAccess_copy.txt" ;

            if (oObj.exists(readonlyCopy))
                oObj.kill(readonlyCopy);

            oObj.copy(filename, readonlyCopy);

            oObj.setReadOnly(readonlyCopy, true);
            result &= oObj.isReadOnly(readonlyCopy);
            oObj.setReadOnly(readonlyCopy, false);
            result &= !oObj.isReadOnly(readonlyCopy);

            oObj.kill(readonlyCopy);
            tRes.tested("isReadOnly()",result);
        } catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'isReadOnly()'");
            ex.printStackTrace(log);
            tRes.tested("isReadOnly()",false);
        }

    } //EOF isReadOnly()


    /**
     * Copies <b>XSimpleFileAccess.txt</b> to a new file, sets
     * 'READONLY' attribute and checks it. Second clears 'READONLY'
     * attribute and checks it again. The copy of file is deleted
     * finally.<p>
     *
     * Has <b> OK </b> status if in the first case method returns
     * <code></code>, and in the second case - <code>false</code>
     * and no exceptions were thrown. <p>
     *
     * The following method tests are to be completed successfully before :
     * <ul>
     *  <li> <code> setReadOnly </code> </li>
     * </ul>
     */
    public void _setReadOnly() {
        boolean result = true ;

        try {
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String filename = dirname+"XSimpleFileAccess.txt";

            String dirnameTo = util.utils.getOfficeTemp(tParam.getMSF()) ;
            String readonlyCopy = dirnameTo + "XSimpleFileAccess_copy.txt" ;

            if (oObj.exists(readonlyCopy))
                oObj.kill(readonlyCopy);

            oObj.copy(filename, readonlyCopy);

            oObj.setReadOnly(readonlyCopy, true);
            result &= oObj.isReadOnly(readonlyCopy) ;
            oObj.setReadOnly(readonlyCopy, false);
            result &= !oObj.isReadOnly(readonlyCopy) ;
            tRes.tested("setReadOnly()", result);

            oObj.kill(readonlyCopy);
        }
        catch (Exception ex) {
            log.println("Exception occurred while testing 'setReadOnly()'");
            ex.printStackTrace(log);
            tRes.tested("setReadOnly()",false);
        }
    } //EOF setReadOnly()

    /**
     * Creates folder and then checks if it was successfully created. <p>
     * Has <b>OK</b> status if folder was created and no exceptions
     * were thrown.
     */
    public void _createFolder() {
        try {
            String tmpdirname = util.utils.getOfficeTemp(tParam.getMSF()) ;
            String newFolder = tmpdirname+"SimpleSubFolder";

            if (oObj.exists(newFolder))
                oObj.kill(newFolder);

            oObj.createFolder(newFolder);
            tRes.tested("createFolder()",oObj.isFolder(newFolder));
            oObj.kill(newFolder);
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'createFolder()'");
            ex.printStackTrace(log);
            tRes.tested("createFolder()",false);
        }

    } //EOF createFolder()

    /**
    * Test calls the method and checks return value and that
    * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
    * file tested.<p>
    * Has <b> OK </b> status if the method returns <code>17</code>
    * and no exceptions were thrown. <p>
    */
    public void _getSize() {
        try {
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String filename = dirname+"XSimpleFileAccess.txt";
            int fSize = oObj.getSize(filename);
            tRes.tested("getSize()", fSize == 17 );
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'getSize()'");
            ex.printStackTrace(log);
            tRes.tested("getSize()",false);
        }

    } //EOF getSize()

    /**
    * Test calls the method and checks return value and that
    * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
    * file tested.<p>
    * Has <b> OK </b> status if the method returns String
    * <code>'application/vnd.sun.staroffice.fsys-file'</code>
    * and no exceptions were thrown. <p>
    */
    public void _getContentType() {
        try {
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String filename = dirname+"XSimpleFileAccess.txt";
            String fType = oObj.getContentType(filename);
            tRes.tested("getContentType()",
                "application/vnd.sun.staroffice.fsys-file".equals(fType) );
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'getContentType()'");
            ex.printStackTrace(log);
            tRes.tested("getContentType()",false);
        }

    } //EOF getContentType()

    /**
    * Test calls the method and checks return value and that
    * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
    * file tested.<p>
    * Has <b> OK </b> status if the method returns date with
    * 2001 year and no exceptions were thrown. <p>
    */
    public void _getDateTimeModified() {
        try {
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String filename = dirname+"XSimpleFileAccess.txt";
            DateTime fTime = oObj.getDateTimeModified(filename);

            java.io.File the_file = new java.io.File(filename);
            long lastModified = the_file.lastModified();
            java.util.Date lastMod = new java.util.Date(lastModified);
            GregorianCalendar lastModCal = new GregorianCalendar();
            lastModCal.setTime(lastMod);

            //compare the dates gained by java with those gained by this method
            boolean res = true;
            boolean partResult = (fTime.Day == lastModCal.get(Calendar.DAY_OF_WEEK));
            if (!partResult) {
                log.println("Wrong Day");
                log.println("Expected: "+lastModCal.get(Calendar.DAY_OF_WEEK));
                log.println("Gained: "+fTime.Day);
                log.println("------------------------------");
            }
            partResult = (fTime.Month == lastModCal.get(Calendar.MONTH));
            if (!partResult) {
                log.println("Wrong Month");
                log.println("Expected: "+lastModCal.get(Calendar.MONTH));
                log.println("Gained: "+fTime.Month);
                log.println("------------------------------");
            }

            partResult = (fTime.Year == (lastModCal.get(Calendar.MONTH) - 1900));
            if (!partResult) {
                log.println("Wrong Year");
                log.println("Expected: "+(lastModCal.get(Calendar.MONTH) - 1900));
                log.println("Gained: "+fTime.Year);
                log.println("------------------------------");
            }

            tRes.tested("getDateTimeModified()", res);
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'getDateTimeModified()'");
            ex.printStackTrace(log);
            tRes.tested("getDateTimeModified()",false);
        }

    } //EOF getDateTimeModified()

    /**
    * Test calls the method and checks return value and that
    * no exceptions were thrown. <b>XSimpleFileAccess</b>
    * directory used.<p>
    * Has <b> OK </b> status if the method returns non zero length
    * array and no exceptions were thrown. <p>
    */
    public void _getFolderContents() {
        try {
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String[] cont = oObj.getFolderContents(dirname,false);
            tRes.tested("getFolderContents()", cont.length>0);
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'getFolderContents()'");
            ex.printStackTrace(log);
            tRes.tested("getFolderContents()",false);
        }

    } //EOF getFolderContents()

    /**
    * First it check file <b>XSimpleFileAccess.txt</b> for
    * existence, second file <b>I_do_not_exists.txt</b> is checked
    * for existence. <p>
    * Has <b> OK </b> status if in the first case method returns
    * <code>true</code> and in the second - <code>flase</code>
    * and no exceptions were thrown. <p>
    */
    public void _exists() {
        try {
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String filename = dirname+"XSimpleFileAccess.txt";
            String wrongname = dirname+"I_do_not_exists.txt";
            tRes.tested("exists()",
                oObj.exists(filename) && !oObj.exists(wrongname));
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'exists()'");
            ex.printStackTrace(log);
            tRes.tested("exists()",false);
        }

    } //EOF exists()

    /**
    * Test calls the method and checks return value and that
    * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
    * file used.<p>
    * Has <b> OK </b> status if the method returns not
    * <code>null</code> value and no exceptions were thrown. <p>
    */
    public void _openFileRead() {
        try {
            String dirname = util.utils.getFullTestURL("XSimpleFileAccess");
            String filename = dirname+"XSimpleFileAccess.txt";
            com.sun.star.io.XInputStream iStream = oObj.openFileRead(filename);
            tRes.tested("openFileRead()", iStream != null);
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'openFileRead()'");
            ex.printStackTrace(log);
            tRes.tested("openFileRead()",false);
        }

    } //EOF openFileRead()

    /**
    * Test calls the method and checks return value and that
    * no exceptions were thrown. <b>XSimpleFileAccess.txt</b>
    * file used.<p>
    * Has <b> OK </b> status if the method returns not
    * <code>null</code> value and no exceptions were thrown. <p>
    */
    public void _openFileWrite() {
        try {
            String tmpdirname = util.utils.getOfficeTemp(tParam.getMSF()) ;

            String copiedFile = tmpdirname+"XSimpleFileAccess_openWrite.txt";

            if (oObj.exists(copiedFile))
                oObj.kill(copiedFile);

            com.sun.star.io.XOutputStream oStream =
                oObj.openFileWrite(copiedFile);

            oStream.closeOutput();
            oObj.kill(copiedFile);

            tRes.tested("openFileWrite()", true);
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'openFileWrite()'");
            ex.printStackTrace(log);
            tRes.tested("openFileWrite()", false);
        }

    } //EOF openFileWrite()

    /**
    * Test calls the method and checks return value and that
    * no exceptions were thrown. <b>XSimpleFileAccess2.txt</b>
    * file used.<p>
    * Has <b> OK </b> status if the method returns not
    * <code>null</code> value and no exceptions were thrown. <p>
    */
    public void _openFileReadWrite() {
        try {
            String dirnameTo = util.utils.getOfficeTemp(tParam.getMSF()) ;
            String copiedFile = dirnameTo + "XSimpleFileAccess2.txt" ;

            if (oObj.exists(copiedFile))
                oObj.kill(copiedFile);

            com.sun.star.io.XStream aStream =
                oObj.openFileReadWrite(copiedFile);

            aStream.getInputStream().closeInput();
            aStream.getOutputStream().closeOutput();

            oObj.kill(copiedFile);

            tRes.tested("openFileReadWrite()", true);
        }
        catch (com.sun.star.uno.Exception ex) {
            log.println("Exception occurred while testing 'openFileReadWrite()'");
            ex.printStackTrace(log);
            tRes.tested("openFileReadWrite()", false);
        }

    } //EOF openFileReadWrite()

    /**
    * Test calls the method and checks that no exceptions were thrown.
    * Has <b> OK </b> status if no exceptions were thrown. <p>
    */
    public void _setInteractionHandler() {
        XInteractionHandler handler = null;
        Object oHandler = tEnv.getObjRelation("InteractionHandler");

        if (oHandler == null)
            throw new StatusException
                (Status.failed("Reelation InteractionHandler not found"));

        try {
            handler = UnoRuntime.queryInterface
                (XInteractionHandler.class, oHandler);
            oObj.setInteractionHandler(handler);
            tRes.tested("setInteractionHandler()", true);
        } catch (Exception ex) {
            log.println("Exception occurred while testing 'setInteractionHandler()'");
            ex.printStackTrace(log);
            tRes.tested("setInteractionHandler()", false);
        }

    } //EOF setInteractionHandler()

}  // finish class _XSimpleFileAccess