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.sheet; 29 30 import java.util.Random; 31 import java.util.StringTokenizer; 32 33 import lib.MultiMethodTest; 34 import lib.Status; 35 import lib.StatusException; 36 37 import com.sun.star.container.XIndexAccess; 38 import com.sun.star.sheet.Border; 39 import com.sun.star.sheet.NamedRangeFlag; 40 import com.sun.star.sheet.XCellRangeAddressable; 41 import com.sun.star.sheet.XCellRangeReferrer; 42 import com.sun.star.sheet.XNamedRanges; 43 import com.sun.star.sheet.XSpreadsheet; 44 import com.sun.star.table.CellAddress; 45 import com.sun.star.table.CellRangeAddress; 46 import com.sun.star.table.XCell; 47 import com.sun.star.table.XCellRange; 48 import com.sun.star.text.XTextRange; 49 import com.sun.star.uno.UnoRuntime; 50 51 /** 52 * Testing <code>com.sun.star.sheet.XNamedRanges</code> 53 * interface methods : 54 * <ul> 55 * <li><code> addNewByName()</code></li> 56 * <li><code> addNewFromTitles()</code></li> 57 * <li><code> removeByName()</code></li> 58 * <li><code> outputList()</code></li> 59 * </ul> <p> 60 * This test needs the following object relations : 61 * <ul> 62 * <li> <code>'SHEET'</code> (of type <code>XSpreadsheet</code>): 63 * to have a spreadsheet </li> 64 * <ul> <p> 65 * @see com.sun.star.sheet.XNamedRanges 66 * @see com.sun.star.sheet.XSpreadsheet 67 */ 68 public class _XNamedRanges extends MultiMethodTest { 69 70 public XNamedRanges oObj = null; 71 String name = "_XNamedRanges"; 72 XSpreadsheet oSheet = null; 73 74 /** 75 * Retrieves object relations. 76 * @throws StatusException If one of relations not found. 77 */ 78 protected void before() { 79 oSheet = (XSpreadsheet)tEnv.getObjRelation("SHEET"); 80 if (oSheet == null) throw new StatusException(Status.failed 81 ("Relation 'SHEET' not found")); 82 } 83 84 /** 85 * Test creates and stores random content and random type, calls the method 86 * and checks that new range exists in collection using method 87 * <code>hasByName()</code>. <p> 88 * Has <b> OK </b> status if new range exists in collection 89 * and no exceptions were thrown. <p> 90 */ 91 public void _addNewByName() { 92 boolean bResult = true; 93 CellAddress aPosition = new CellAddress((short)0, 2, 2); 94 int nType = getRandomType(); 95 String sContent = getRandomContent("D3;A6:B9;=F12"); 96 name += sContent; 97 log.println("Adding new range with name=\"" + name + 98 "\", sContent = \"" + sContent + 99 "\", aPosition = (" + aPosition.Sheet + ", " 100 + aPosition.Column + ", " 101 + aPosition.Row + 102 "), Type = " + nType + "."); 103 104 oObj.addNewByName(name, sContent, aPosition, nType); 105 106 //inserted for a bug 107 CellAddress listOutputPosition = new CellAddress((short)0, 1, 1); 108 oObj.outputList(listOutputPosition); 109 String s = null; 110 String s1 = null; 111 try { 112 s = oSheet.getCellByPosition(1, 1).getFormula(); 113 s1 = oSheet.getCellByPosition(2, 1).getFormula(); 114 } catch(com.sun.star.lang.IndexOutOfBoundsException e) { 115 log.println("Can't get cell by position: " + e); 116 bResult = false; 117 } 118 log.println("Outputlist returns: " + s + " " + s1); 119 //end of insertion 120 121 bResult &= oObj.hasByName(name); 122 123 tRes.tested("addNewByName()", bResult); 124 } 125 126 /** 127 * Test creates a table with left and top titles, creates new ranges from 128 * top titles and left titles, checks all new title ranges. <p> 129 * Has <b> OK </b> status if all required title ranges are present 130 * in collection, if each of them have valid size and position and 131 * no exceptions were thrown. <p> 132 */ 133 public void _addNewFromTitles() { 134 boolean bResult = true; 135 136 // First, create a small table. 137 log.println("Creating a small table."); 138 try { 139 XCell cell = null; 140 XTextRange textrange = null; 141 142 for (int i = 1; i < 4; i++) { 143 cell = oSheet.getCellByPosition(0, i); 144 textrange = (XTextRange)UnoRuntime. 145 queryInterface(XTextRange.class, cell); 146 textrange.setString("Row" + i); 147 148 cell = oSheet.getCellByPosition(i, 0); 149 textrange = (XTextRange)UnoRuntime. 150 queryInterface(XTextRange.class, cell); 151 textrange.setString("Column" + i); 152 } 153 154 for (int i = 1; i < 4; i++) 155 for (int j = 1; j < 4; j++) { 156 cell = oSheet.getCellByPosition(i, j); 157 textrange = (XTextRange)UnoRuntime. 158 queryInterface(XTextRange.class, cell); 159 textrange.setString("Val" + ((j - 1) * 3 + i)); 160 } 161 log.println("Finished creating table."); 162 log.println("Creating new ranges from titles"); 163 164 CellRangeAddress CRA = new CellRangeAddress((short)0, 0, 0, 3, 3); 165 Border border = Border.TOP; 166 oObj.addNewFromTitles(CRA, border); 167 for (int i = 1; i < 4; i++) { 168 bResult &= oObj.hasByName("Column" + i); 169 170 Object range = oObj.getByName("Column" + i); 171 XCellRangeReferrer CRR = (XCellRangeReferrer)UnoRuntime. 172 queryInterface(XCellRangeReferrer.class,range); 173 174 XCellRange CR = CRR.getReferredCells(); 175 XCellRangeAddressable xCRA = (XCellRangeAddressable) 176 UnoRuntime.queryInterface(XCellRangeAddressable.class, CR); 177 178 CellRangeAddress objCRA = xCRA.getRangeAddress(); 179 180 bResult &= (objCRA.EndColumn == i && objCRA.StartColumn == i); 181 bResult &= objCRA.StartRow == 1; 182 bResult &= objCRA.EndRow == 3; 183 bResult &= objCRA.Sheet == 0; 184 } 185 186 border = Border.LEFT; 187 oObj.addNewFromTitles(CRA, border); 188 for (int i = 1; i < 4; i++) { 189 bResult &= oObj.hasByName("Row" + i); 190 191 Object range = oObj.getByName("Row" + i); 192 XCellRangeReferrer CRR = (XCellRangeReferrer)UnoRuntime. 193 queryInterface(XCellRangeReferrer.class,range); 194 195 XCellRange CR = CRR.getReferredCells(); 196 XCellRangeAddressable xCRA = (XCellRangeAddressable) 197 UnoRuntime.queryInterface(XCellRangeAddressable.class, CR); 198 199 CellRangeAddress objCRA = xCRA.getRangeAddress(); 200 201 bResult &= (objCRA.EndRow == i && objCRA.StartRow == i); 202 bResult &= objCRA.StartColumn == 1; 203 bResult &= objCRA.EndColumn == 3; 204 bResult &= objCRA.Sheet == 0; 205 } 206 207 oObj.outputList(new CellAddress((short)0, 5, 5)); 208 209 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 210 e.printStackTrace(log); 211 bResult = false; 212 } catch (com.sun.star.lang.WrappedTargetException e) { 213 e.printStackTrace(log); 214 bResult = false; 215 } catch (com.sun.star.container.NoSuchElementException e) { 216 e.printStackTrace(log); 217 bResult = false; 218 } 219 220 tRes.tested("addNewFromTitles()", bResult); 221 } 222 223 /** 224 * Test calls the method and checks existing of named ranges obtained 225 * by relation <code>'SHEET'</code>. <p> 226 * Has <b> OK </b> status if all output named ranges exist 227 * and no exceptions were thrown. <p> 228 */ 229 public void _outputList() { 230 boolean bResult = true; 231 CellAddress CA = new CellAddress((short)0, 0, 0); 232 233 XIndexAccess IA = (XIndexAccess)UnoRuntime. 234 queryInterface(XIndexAccess.class, oObj); 235 236 int elementsCount = IA.getCount(); 237 238 oObj.outputList(CA); 239 240 try { 241 for (int i = 0; i < elementsCount; i++) { 242 XCell cell = oSheet.getCellByPosition(0, i); 243 XTextRange textrange = (XTextRange) 244 UnoRuntime.queryInterface(XTextRange.class, cell); 245 String str = textrange.getString(); 246 bResult &= oObj.hasByName(str); 247 } 248 } catch(com.sun.star.lang.IndexOutOfBoundsException e) { 249 e.printStackTrace(log); 250 bResult = false; 251 } 252 253 tRes.tested("outputList()", bResult); 254 } 255 256 /** 257 * Test calls the method for existing range, checks number of ranges in 258 * collection after method call, calls method for non-existent named range. 259 * <p>Has <b> OK </b> status if number of named ranges is less by one than 260 * before method call and exception was thrown during second call. <p> 261 * The following method tests are to be completed successfully before : 262 * <ul> 263 * <li> <code> addNewByName() </code> : to have name of existent 264 * named range </li> 265 * </ul> 266 */ 267 public void _removeByName() { 268 requiredMethod("addNewByName()"); 269 boolean bResult = true; 270 XIndexAccess IA = (XIndexAccess)UnoRuntime. 271 queryInterface(XIndexAccess.class, oObj); 272 273 int elementsCount = IA.getCount(); 274 275 // Removing existent element 276 oObj.removeByName(name); 277 bResult = elementsCount == IA.getCount() + 1; 278 279 try { 280 // Removing unexistent element. 281 oObj.removeByName(name); 282 log.println("Exception expected when removed unexistent element!"); 283 bResult = false; 284 } catch (com.sun.star.uno.RuntimeException e) { 285 log.println("Expected exception occured while testing" + 286 "removeByName() when removed unexistent element."); 287 288 } 289 290 tRes.tested("removeByName()", bResult); 291 } 292 293 /** 294 * Method make string of random content. 295 * @return string of random content 296 */ 297 String getRandomContent(String str) { 298 String gRS = "none"; 299 Random rnd = new Random(); 300 301 StringTokenizer ST = new StringTokenizer(str, ";"); 302 int nr = rnd.nextInt(ST.countTokens()); 303 if (nr < 1) 304 nr++; 305 306 for (int i=1; i < nr + 1; i++) 307 gRS = ST.nextToken(); 308 309 return gRS; 310 } 311 312 /** 313 * Returns random value of named range flag. 314 */ 315 int getRandomType(){ 316 int types[] = { 0, 317 NamedRangeFlag.COLUMN_HEADER, 318 NamedRangeFlag.FILTER_CRITERIA, 319 NamedRangeFlag.PRINT_AREA, 320 NamedRangeFlag.ROW_HEADER 321 }; 322 323 Random rnd = new Random(); 324 return types[rnd.nextInt(5)]; 325 } 326 } 327 328