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 package ifc.sheet; 28 29 import com.sun.star.sheet.XSpreadsheets; 30 31 import lib.MultiMethodTest; 32 33 34 /** 35 * Testing <code>com.sun.star.sheet.XSpreadsheets</code> 36 * interface methods : 37 * <ul> 38 * <li><code> insertNewByName()</code></li> 39 * <li><code> moveByName()</code></li> 40 * <li><code> copyByName()</code></li> 41 * </ul> <p> 42 * Test is multithread compilant. <p> 43 * @see com.sun.star.sheet.XSpreadsheets 44 */ 45 public class _XSpreadsheets extends MultiMethodTest { 46 protected static int uniqCount = 0; 47 public XSpreadsheets oObj = null; 48 protected int uniqNumber = 0; 49 50 /** 51 * Sets the unique number for the current test. 52 */ 53 protected synchronized void before() { 54 uniqNumber = uniqCount++; 55 } 56 57 /** 58 * Test inserts new sheet using the name returned by the method 59 * <code>newName</code>, copies inserted sheet with the new name, 60 * checks existence of the sheet with this name in collection and removes 61 * the both sheets from the collection. <p> 62 * Has <b> OK </b> status if the sheet with the name of the copy exists 63 * in the collection and no exceptions were thrown. <p> 64 */ 65 public void _copyByName() { 66 boolean result = true; 67 68 //first insert one that should be copied 69 String iS = newName("copyFrom"); 70 log.println("Inserting sheet '" + iS + "'"); 71 oObj.insertNewByName(iS, (short) 0); 72 73 String[] eNames = oObj.getElementNames(); 74 String NewSheet = newName("copyTo"); 75 log.println("Try to copy " + eNames[0] + " to " + NewSheet); 76 oObj.copyByName(eNames[0], NewSheet, (short) 0); 77 result = oObj.hasByName(NewSheet); 78 79 //remove all inserted sheets 80 try { 81 oObj.removeByName(NewSheet); 82 oObj.removeByName(iS); 83 } catch (com.sun.star.lang.WrappedTargetException e) { 84 log.print("Can't remove sheet by name"); 85 e.printStackTrace(log); 86 result = false; 87 } catch (com.sun.star.container.NoSuchElementException e) { 88 log.print("Can't remove sheet by name"); 89 e.printStackTrace(log); 90 result = false; 91 } 92 93 tRes.tested("copyByName()", result); 94 } // finished _copyByName 95 96 /** 97 * Test inserts new sheet using the name returned by the method 98 * <code>newName</code>, moves the inserted sheet to the new position 99 * in collection, gets all element names in collection and checks the name 100 * of the sheet in the new position. <p> 101 * Has <b> OK </b> status if the sheet name in the new position is equal to 102 * the name of the sheet that was moved. <p> 103 */ 104 public void _moveByName() { 105 //first insert one that should be moved 106 String iS = newName("move"); 107 oObj.insertNewByName(iS, (short) 0); 108 109 String[] eNames = oObj.getElementNames(); 110 String sheetToMove = eNames[0]; 111 log.println("Try to move " + sheetToMove); 112 oObj.moveByName(sheetToMove, (short) 2); 113 eNames = oObj.getElementNames(); 114 tRes.tested("moveByName()", sheetToMove.equals(eNames[1])); 115 } // finished _moveByName 116 117 /** 118 * Test inserts new sheet using the name returned by the method 119 * <code>newName</code>, checks the existence of the inserted sheet in 120 * the collection, removes the sheet, tries to insert the sheet with the 121 * bad name returned by method <code>badName()</code>. <p> 122 * Has <b> OK </b> status if the inserted sheet exists in the collection 123 * after first method call and if exception occured during the second call. <p> 124 */ 125 public void _insertNewByName() { 126 boolean result = false; 127 128 String NewSheet = newName("insert"); 129 log.println("Try to insert " + NewSheet); 130 oObj.insertNewByName(NewSheet, (short) 0); 131 result = oObj.hasByName(NewSheet); 132 133 try { 134 oObj.removeByName(NewSheet); 135 } catch (com.sun.star.lang.WrappedTargetException e) { 136 log.print("Can't remove sheet '" + NewSheet + "':"); 137 e.printStackTrace(log); 138 result = false; 139 } catch (com.sun.star.container.NoSuchElementException e) { 140 log.print("Can't remove sheet '" + NewSheet + "':"); 141 e.printStackTrace(log); 142 result = false; 143 } 144 145 try { 146 NewSheet = badName(); 147 log.println("Try to insert " + NewSheet); 148 oObj.insertNewByName(NewSheet, (short) 0); 149 log.println( 150 "No Exception thrown while inserting sheet with invalid name"); 151 result &= false; 152 oObj.removeByName(NewSheet); 153 } catch (com.sun.star.uno.RuntimeException e) { 154 log.println( 155 "Expected exception occured during testing 'insertNewByName'"); 156 result &= true; 157 } catch (com.sun.star.lang.WrappedTargetException e) { 158 log.print("Can't remove sheet '" + NewSheet + "':"); 159 e.printStackTrace(log); 160 result = false; 161 } catch (com.sun.star.container.NoSuchElementException e) { 162 log.print("Can't remove sheet '" + NewSheet + "':"); 163 e.printStackTrace(log); 164 result = false; 165 } 166 167 tRes.tested("insertNewByName()", result); 168 } // finished _insertByName 169 170 /** 171 * Method returns unique new name using passed prefix and unique number 172 * of the current test. 173 */ 174 public String newName(String prefix) { 175 return prefix + uniqNumber; 176 } // finished newName 177 178 /** 179 * Method return bad name for a sheet using the name of the current thread. 180 */ 181 public String badName() { 182 return "$%#/?\\"; 183 } // finished badName 184 } //finish class _XSpreadsheets 185