1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 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. 14 * 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). 20 * 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. 25 * 26 ************************************************************************/ 27 28 package ifc.ucb; 29 30 import lib.MultiMethodTest; 31 import lib.Status; 32 import lib.StatusException; 33 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; 39 40 /** 41 * Testing <code>com.sun.star.ucb.XSimpleFileAccess</code> 42 * interface methods. <p> 43 * The following predefined files needed to complete the test: 44 * <ul> 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> 49 * <ul> <p> 50 * This test needs the following object relations : 51 * <ul> 52 * <li> <code>'InteractionHandler'</code> 53 * (of type <code>XInteractionHandler</code>) 54 * instance of <code>com.sun.star.sdb.InteractionHandler</code> 55 * </li> 56 * </ul> <p> 57 * Test is <b> NOT </b> multithread compilant. <p> 58 * @see com.sun.star.ucb.XSimpleFileAccess 59 */ 60 public class _XSimpleFileAccess extends MultiMethodTest { 61 62 public static XSimpleFileAccess oObj = null; 63 64 /** 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> 69 */ 70 public void _copy() { 71 try { 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"; 77 78 if (oObj.exists(copiedFile)) 79 oObj.kill(copiedFile); 80 81 oObj.copy(filename,copiedFile); 82 tRes.tested("copy()",oObj.exists(copiedFile)); 83 oObj.kill(copiedFile); 84 } 85 catch (com.sun.star.uno.Exception ex) { 86 log.println("Exception occured while testing 'copy()'"); 87 ex.printStackTrace(log); 88 tRes.tested("copy()",false); 89 } 90 91 } //EOF copy() 92 93 /** 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> 99 */ 100 public void _move() { 101 try { 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"; 107 108 if (oObj.exists(copiedFile)) 109 oObj.kill(copiedFile); 110 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); 117 } 118 catch (com.sun.star.uno.Exception ex) { 119 log.println("Exception occured while testing 'move()'"); 120 ex.printStackTrace(log); 121 tRes.tested("move()",false); 122 } 123 124 } //EOF move() 125 126 /** 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> 131 */ 132 public void _kill() { 133 try { 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"; 139 140 if (oObj.exists(copiedFile)) 141 oObj.kill(copiedFile); 142 143 oObj.copy(filename,copiedFile); 144 oObj.kill(copiedFile); 145 tRes.tested("kill()",!oObj.exists(copiedFile)); 146 } 147 catch (com.sun.star.uno.Exception ex) { 148 log.println("Exception occured while testing 'kill()'"); 149 ex.printStackTrace(log); 150 tRes.tested("kill()",false); 151 } 152 153 } //EOF kill() 154 155 /** 156 * Tries to check if <b>XSimpleFileAccess</b> is folder. <p> 157 * Has <b>OK</b> status if the method returns <code>true</code> 158 */ 159 public void _isFolder() { 160 try { 161 String dirname = util.utils.getFullTestURL("XSimpleFileAccess"); 162 tRes.tested("isFolder()",oObj.isFolder(dirname)); 163 } 164 catch (com.sun.star.uno.Exception ex) { 165 log.println("Exception occured while testing 'isFolder()'"); 166 ex.printStackTrace(log); 167 tRes.tested("isFolder()",false); 168 } 169 170 } //EOF isFolder() 171 172 /** 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 176 * finally.<p> 177 * 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> 181 * 182 * The following method tests are to be completed successfully before : 183 * <ul> 184 * <li> <code> setReadOnly </code> </li> 185 * </ul> 186 */ 187 public void _isReadOnly() { 188 requiredMethod("setReadOnly()"); 189 try { 190 String dirname = util.utils.getFullTestURL("XSimpleFileAccess"); 191 String filename = dirname+"XSimpleFileAccess.txt"; 192 boolean result = true; 193 194 String dirnameTo = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ; 195 String readonlyCopy = dirnameTo + "XSimpleFileAccess_copy.txt" ; 196 197 if (oObj.exists(readonlyCopy)) 198 oObj.kill(readonlyCopy); 199 200 oObj.copy(filename, readonlyCopy); 201 202 oObj.setReadOnly(readonlyCopy, true); 203 result &= oObj.isReadOnly(readonlyCopy); 204 oObj.setReadOnly(readonlyCopy, false); 205 result &= !oObj.isReadOnly(readonlyCopy); 206 207 oObj.kill(readonlyCopy); 208 tRes.tested("isReadOnly()",result); 209 } catch (com.sun.star.uno.Exception ex) { 210 log.println("Exception occured while testing 'isReadOnly()'"); 211 ex.printStackTrace(log); 212 tRes.tested("isReadOnly()",false); 213 } 214 215 } //EOF isReadOnly() 216 217 218 /** 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 222 * finally.<p> 223 * 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> 227 * 228 * The following method tests are to be completed successfully before : 229 * <ul> 230 * <li> <code> setReadOnly </code> </li> 231 * </ul> 232 */ 233 public void _setReadOnly() { 234 boolean result = true ; 235 236 try { 237 String dirname = util.utils.getFullTestURL("XSimpleFileAccess"); 238 String filename = dirname+"XSimpleFileAccess.txt"; 239 240 String dirnameTo = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ; 241 String readonlyCopy = dirnameTo + "XSimpleFileAccess_copy.txt" ; 242 243 if (oObj.exists(readonlyCopy)) 244 oObj.kill(readonlyCopy); 245 246 oObj.copy(filename, readonlyCopy); 247 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); 253 254 oObj.kill(readonlyCopy); 255 } 256 catch (Exception ex) { 257 log.println("Exception occured while testing 'setReadOnly()'"); 258 ex.printStackTrace(log); 259 tRes.tested("setReadOnly()",false); 260 } 261 } //EOF setReadOnly() 262 263 /** 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 266 * were thrown. 267 */ 268 public void _createFolder() { 269 try { 270 String tmpdirname = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ; 271 String newFolder = tmpdirname+"SimpleSubFolder"; 272 273 if (oObj.exists(newFolder)) 274 oObj.kill(newFolder); 275 276 oObj.createFolder(newFolder); 277 tRes.tested("createFolder()",oObj.isFolder(newFolder)); 278 oObj.kill(newFolder); 279 } 280 catch (com.sun.star.uno.Exception ex) { 281 log.println("Exception occured while testing 'createFolder()'"); 282 ex.printStackTrace(log); 283 tRes.tested("createFolder()",false); 284 } 285 286 } //EOF createFolder() 287 288 /** 289 * Test calls the method and checks return value and that 290 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b> 291 * file tested.<p> 292 * Has <b> OK </b> status if the method returns <code>17</code> 293 * and no exceptions were thrown. <p> 294 */ 295 public void _getSize() { 296 try { 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 ); 301 } 302 catch (com.sun.star.uno.Exception ex) { 303 log.println("Exception occured while testing 'getSize()'"); 304 ex.printStackTrace(log); 305 tRes.tested("getSize()",false); 306 } 307 308 } //EOF getSize() 309 310 /** 311 * Test calls the method and checks return value and that 312 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b> 313 * file tested.<p> 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> 317 */ 318 public void _getContentType() { 319 try { 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) ); 325 } 326 catch (com.sun.star.uno.Exception ex) { 327 log.println("Exception occured while testing 'getContentType()'"); 328 ex.printStackTrace(log); 329 tRes.tested("getContentType()",false); 330 } 331 332 } //EOF getContentType() 333 334 /** 335 * Test calls the method and checks return value and that 336 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b> 337 * file tested.<p> 338 * Has <b> OK </b> status if the method returns date with 339 * 2001 year and no exceptions were thrown. <p> 340 */ 341 public void _getDateTimeModified() { 342 try { 343 String dirname = util.utils.getFullTestURL("XSimpleFileAccess"); 344 String filename = dirname+"XSimpleFileAccess.txt"; 345 DateTime fTime = oObj.getDateTimeModified(filename); 346 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); 350 351 //compare the dates gained by java with those gained by this method 352 boolean res = true; 353 boolean partResult = (fTime.Day == lastMod.getDay()); 354 if (!partResult) { 355 log.println("Wrong Day"); 356 log.println("Expected: "+lastMod.getDay()); 357 log.println("Gained: "+fTime.Day); 358 log.println("------------------------------"); 359 } 360 partResult = (fTime.Month == lastMod.getMonth()); 361 if (!partResult) { 362 log.println("Wrong Month"); 363 log.println("Expected: "+lastMod.getMonth()); 364 log.println("Gained: "+fTime.Month); 365 log.println("------------------------------"); 366 } 367 368 partResult = (fTime.Year == lastMod.getYear()); 369 if (!partResult) { 370 log.println("Wrong Year"); 371 log.println("Expected: "+lastMod.getYear()); 372 log.println("Gained: "+fTime.Year); 373 log.println("------------------------------"); 374 } 375 376 tRes.tested("getDateTimeModified()", res); 377 } 378 catch (com.sun.star.uno.Exception ex) { 379 log.println("Exception occured while testing 'getDateTimeModified()'"); 380 ex.printStackTrace(log); 381 tRes.tested("getDateTimeModified()",false); 382 } 383 384 } //EOF getDateTimeModified() 385 386 /** 387 * Test calls the method and checks return value and that 388 * no exceptions were thrown. <b>XSimpleFileAccess</b> 389 * directory used.<p> 390 * Has <b> OK </b> status if the method returns non zero length 391 * array and no exceptions were thrown. <p> 392 */ 393 public void _getFolderContents() { 394 try { 395 String dirname = util.utils.getFullTestURL("XSimpleFileAccess"); 396 String[] cont = oObj.getFolderContents(dirname,false); 397 tRes.tested("getFolderContents()", cont.length>0); 398 } 399 catch (com.sun.star.uno.Exception ex) { 400 log.println("Exception occured while testing 'getFolderContents()'"); 401 ex.printStackTrace(log); 402 tRes.tested("getFolderContents()",false); 403 } 404 405 } //EOF getFolderContents() 406 407 /** 408 * First it check file <b>XSimpleFileAccess.txt</b> for 409 * existence, second file <b>I_do_not_exists.txt</b> is checked 410 * for existence. <p> 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> 414 */ 415 public void _exists() { 416 try { 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)); 422 } 423 catch (com.sun.star.uno.Exception ex) { 424 log.println("Exception occured while testing 'exists()'"); 425 ex.printStackTrace(log); 426 tRes.tested("exists()",false); 427 } 428 429 } //EOF exists() 430 431 /** 432 * Test calls the method and checks return value and that 433 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b> 434 * file used.<p> 435 * Has <b> OK </b> status if the method returns not 436 * <code>null</code> value and no exceptions were thrown. <p> 437 */ 438 public void _openFileRead() { 439 try { 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); 444 } 445 catch (com.sun.star.uno.Exception ex) { 446 log.println("Exception occured while testing 'openFileRead()'"); 447 ex.printStackTrace(log); 448 tRes.tested("openFileRead()",false); 449 } 450 451 } //EOF openFileRead() 452 453 /** 454 * Test calls the method and checks return value and that 455 * no exceptions were thrown. <b>XSimpleFileAccess.txt</b> 456 * file used.<p> 457 * Has <b> OK </b> status if the method returns not 458 * <code>null</code> value and no exceptions were thrown. <p> 459 */ 460 public void _openFileWrite() { 461 try { 462 String tmpdirname = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ; 463 464 String copiedFile = tmpdirname+"XSimpleFileAccess_openWrite.txt"; 465 466 if (oObj.exists(copiedFile)) 467 oObj.kill(copiedFile); 468 469 com.sun.star.io.XOutputStream oStream = 470 oObj.openFileWrite(copiedFile); 471 tRes.tested("openFileWrite()", oStream != null); 472 473 oStream.closeOutput(); 474 oObj.kill(copiedFile); 475 } 476 catch (com.sun.star.uno.Exception ex) { 477 log.println("Exception occured while testing 'openFileWrite()'"); 478 ex.printStackTrace(log); 479 tRes.tested("openFileWrite()",false); 480 } 481 482 } //EOF openFileWrite() 483 484 /** 485 * Test calls the method and checks return value and that 486 * no exceptions were thrown. <b>XSimpleFileAccess2.txt</b> 487 * file used.<p> 488 * Has <b> OK </b> status if the method returns not 489 * <code>null</code> value and no exceptions were thrown. <p> 490 */ 491 public void _openFileReadWrite() { 492 try { 493 String dirnameTo = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ; 494 String copiedFile = dirnameTo + "XSimpleFileAccess2.txt" ; 495 496 if (oObj.exists(copiedFile)) 497 oObj.kill(copiedFile); 498 499 com.sun.star.io.XStream aStream = 500 oObj.openFileReadWrite(copiedFile); 501 tRes.tested("openFileReadWrite()", aStream != null); 502 503 aStream.getInputStream().closeInput(); 504 aStream.getOutputStream().closeOutput(); 505 506 oObj.kill(copiedFile); 507 } 508 catch (com.sun.star.uno.Exception ex) { 509 log.println("Exception occured while testing 'openFileReadWrite()'"); 510 ex.printStackTrace(log); 511 tRes.tested("openFileReadWrite()",false); 512 } 513 514 } //EOF openFileReadWrite() 515 516 /** 517 * Test calls the method and checks that no exceptions were thrown. 518 * Has <b> OK </b> status if no exceptions were thrown. <p> 519 */ 520 public void _setInteractionHandler() { 521 XInteractionHandler handler = null; 522 Object oHandler = tEnv.getObjRelation("InteractionHandler");; 523 524 if (oHandler == null) 525 throw new StatusException 526 (Status.failed("Reelation InteractionHandler not found")); 527 528 try { 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 occured while testing 'setInteractionHandler()'"); 535 ex.printStackTrace(log); 536 tRes.tested("setInteractionHandler()", false); 537 } 538 539 } //EOF setInteractionHandler() 540 541 } // finish class _XSimpleFileAccess 542 543