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 mod._sc; 29 30 import java.io.PrintWriter; 31 32 import lib.StatusException; 33 import lib.TestCase; 34 import lib.TestEnvironment; 35 import lib.TestParameters; 36 import util.SOfficeFactory; 37 38 import com.sun.star.beans.XPropertySet; 39 import com.sun.star.container.XIndexAccess; 40 import com.sun.star.lang.XComponent; 41 import com.sun.star.lang.XMultiServiceFactory; 42 import com.sun.star.sheet.TableFilterField; 43 import com.sun.star.sheet.XDataPilotDescriptor; 44 import com.sun.star.sheet.XDataPilotTables; 45 import com.sun.star.sheet.XDataPilotTablesSupplier; 46 import com.sun.star.sheet.XSheetFilterDescriptor; 47 import com.sun.star.sheet.XSpreadsheet; 48 import com.sun.star.sheet.XSpreadsheetDocument; 49 import com.sun.star.sheet.XSpreadsheets; 50 import com.sun.star.table.CellAddress; 51 import com.sun.star.table.CellRangeAddress; 52 import com.sun.star.uno.AnyConverter; 53 import com.sun.star.uno.Type; 54 import com.sun.star.uno.UnoRuntime; 55 import com.sun.star.uno.XInterface; 56 57 /** 58 * Test for object which is represented by service 59 * <code>com.sun.star.sheet.DataPilotTable</code>. <p> 60 * Object implements the following interfaces : 61 * <ul> 62 * <li> <code>com::sun::star::container::XNamed</code></li> 63 * <li> <code>com::sun::star::sheet::XDataPilotTable</code></li> 64 * <li> <code>com::sun::star::sheet::XDataPilotDescriptor</code></li> 65 * </ul> 66 * @see com.sun.star.sheet.DataPilotTable 67 * @see com.sun.star.container.XNamed 68 * @see com.sun.star.sheet.XDataPilotTable 69 * @see com.sun.star.sheet.XDataPilotDescriptor 70 * @see ifc.container._XNamed 71 * @see ifc.sheet._XDataPilotTable 72 * @see ifc.sheet._XDataPilotDescriptor 73 */ 74 public class ScDataPilotTableObj extends TestCase { 75 static XSpreadsheetDocument xSheetDoc = null; 76 77 /** 78 * Creates Spreadsheet document. 79 */ 80 protected void initialize( TestParameters tParam, PrintWriter log ) { 81 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() ); 82 83 try { 84 log.println( "creating a Spreadsheet document" ); 85 xSheetDoc = SOF.createCalcDoc(null); 86 } catch ( com.sun.star.uno.Exception e ) { 87 // Some exception occures.FAILED 88 e.printStackTrace( log ); 89 throw new StatusException( "Couldn't create document", e ); 90 } 91 92 } 93 94 /** 95 * Disposes Spreadsheet document. 96 */ 97 protected void cleanup( TestParameters tParam, PrintWriter log ) { 98 log.println( " disposing xSheetDoc " ); 99 XComponent oComp = (XComponent) 100 UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ; 101 util.DesktopTools.closeDoc(oComp); 102 } 103 104 /** 105 * Creating a Testenvironment for the interfaces to be tested. 106 * Retrieves a collection of spreadsheets from a document 107 * and takes one of them. Fills some table in the spreadsheet. 108 * Obtains the collection of data pilot tables using the interface 109 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor 110 * for the filled table. Obtains the collection of all the data pilot fields 111 * using the interface <code>XDataPilotDescriptor</code>. Sets field 112 * orientation for every of the data pilot fields. Sets the definitions of 113 * the filter fields for the created description. Inserts new data pilot 114 * table with this descriptor to the collection. 115 * This new data pilot table is the instance of the service 116 * <code>com.sun.star.sheet.DataPilotTable</code>. 117 * Object relations created : 118 * <ul> 119 * <li> <code>'OUTPUTRANGE'</code> for 120 * {@link ifc.sheet._XDataPilotTable}(the cell range address of the 121 * created data pilot table) </li> 122 * <li> <code>'FIELDSAMOUNT'</code> for 123 * {@link ifc.sheet._XDataPilotDescriptor}(the number of filled fields 124 * which descriptor was created for) </li> 125 * <li> <code>'CELLFORCHANGE'</code> for 126 * {@link ifc.sheet._XDataPilotTable}(value of this cell will be changed)</li> 127 * <li> <code>'CELLFORCHECK'</code> for 128 * {@link ifc.sheet._XDataPilotTable}(value of this cell must be changed 129 * after refresh call)</li> 130 * </ul> 131 * @see com.sun.star.sheet.DataPilotTable 132 * @see com.sun.star.sheet.XDataPilotTablesSupplier 133 * @see com.sun.star.sheet.XDataPilotDescriptor 134 */ 135 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) { 136 137 XInterface oObj = null; 138 139 CellAddress sCellAddress = new CellAddress(); 140 sCellAddress.Sheet = 0; 141 sCellAddress.Column = 7; 142 sCellAddress.Row = 8; 143 144 // creation of testobject here 145 // first we write what we are intend to do to log file 146 log.println( "Creating a test environment" ); 147 log.println("getting sheets"); 148 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets(); 149 XIndexAccess oIndexAccess = (XIndexAccess) 150 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets); 151 XSpreadsheet oSheet = null; 152 Object oChangeCell = null; 153 Object oCheckCell = null; 154 try { 155 oSheet = (XSpreadsheet) AnyConverter.toObject( 156 new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0)); 157 oChangeCell = oSheet.getCellByPosition(1, 5); 158 oCheckCell = oSheet.getCellByPosition( 159 sCellAddress.Column, sCellAddress.Row + 3); 160 } catch (com.sun.star.lang.WrappedTargetException e) { 161 e.printStackTrace(log); 162 throw new StatusException( "Couldn't get a spreadsheet", e); 163 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 164 e.printStackTrace(log); 165 throw new StatusException( "Couldn't get a spreadsheet", e); 166 } catch (com.sun.star.lang.IllegalArgumentException e) { 167 e.printStackTrace(log); 168 throw new StatusException( "Couldn't get a spreadsheet", e); 169 } 170 log.println("Getting sheet - " + (oSheet == null ? "FAILED" : "OK")); 171 try { 172 log.println("Filing a table"); 173 for (int i = 1; i < 6; i++) { 174 oSheet.getCellByPosition(0, i).setFormula("Row" + i); 175 oSheet.getCellByPosition(i, 0).setFormula("Col" + i); 176 } 177 178 for (int i = 1; i < 6; i++) 179 for (int j = 1; j < 6; j++) { 180 oSheet.getCellByPosition(i, j).setValue(2.5 * j + i); 181 } 182 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 183 e.printStackTrace(log); 184 throw new StatusException("Couldn't fill some cells", e); 185 } 186 187 CellRangeAddress sCellRangeAddress = new CellRangeAddress(); 188 sCellRangeAddress.Sheet = 0; 189 sCellRangeAddress.StartColumn = 1; 190 sCellRangeAddress.StartRow = 0; 191 sCellRangeAddress.EndColumn = 5; 192 sCellRangeAddress.EndRow = 5; 193 194 TableFilterField[] filterFields = new TableFilterField[2]; 195 filterFields[0] = new TableFilterField(); 196 filterFields[0].Connection = com.sun.star.sheet.FilterConnection.AND; 197 filterFields[0].Field = 1; 198 filterFields[0].IsNumeric = true; 199 filterFields[0].NumericValue = 4; 200 filterFields[0].Operator = com.sun.star.sheet.FilterOperator.GREATER; 201 filterFields[1] = new TableFilterField(); 202 filterFields[1].Connection = com.sun.star.sheet.FilterConnection.AND; 203 filterFields[1].Field = 1; 204 filterFields[1].IsNumeric = true; 205 filterFields[1].NumericValue = 12; 206 filterFields[1].Operator = com.sun.star.sheet.FilterOperator.LESS_EQUAL; 207 208 XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier) 209 UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet); 210 log.println("Getting test object"); 211 XDataPilotTables DPT = DPTS.getDataPilotTables(); 212 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor(); 213 DPDsc.setSourceRange(sCellRangeAddress); 214 215 XSheetFilterDescriptor SFD = DPDsc.getFilterDescriptor(); 216 SFD.setFilterFields(filterFields); 217 218 XPropertySet fieldPropSet = null; 219 try { 220 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0); 221 fieldPropSet = (XPropertySet) 222 UnoRuntime.queryInterface(XPropertySet.class, oDataPilotField); 223 } catch (com.sun.star.lang.WrappedTargetException e) { 224 e.printStackTrace(log); 225 throw new StatusException("Couldn't create a test environment", e); 226 } catch(com.sun.star.lang.IndexOutOfBoundsException e) { 227 e.printStackTrace(log); 228 throw new StatusException("Couldn't create a test environment", e); 229 } 230 231 try { 232 fieldPropSet.setPropertyValue("Function", 233 com.sun.star.sheet.GeneralFunction.SUM); 234 fieldPropSet.setPropertyValue("Orientation", 235 com.sun.star.sheet.DataPilotFieldOrientation.DATA); 236 } catch(com.sun.star.lang.WrappedTargetException e) { 237 e.printStackTrace(log); 238 throw new StatusException("Couldn't create a test environment", e); 239 } catch(com.sun.star.lang.IllegalArgumentException e) { 240 e.printStackTrace(log); 241 throw new StatusException("Couldn't create a test environment", e); 242 } catch(com.sun.star.beans.PropertyVetoException e) { 243 e.printStackTrace(log); 244 throw new StatusException("Couldn't create a test environment", e); 245 } catch(com.sun.star.beans.UnknownPropertyException e) { 246 e.printStackTrace(log); 247 throw new StatusException("Couldn't create a test environment", e); 248 } 249 250 if (DPT.hasByName("DataPilotTable")) { 251 DPT.removeByName("DataPilotTable"); 252 } 253 DPT.insertNewByName("DataPilotTable", sCellAddress, DPDsc); 254 try { 255 oObj = (XInterface) AnyConverter.toObject( 256 new Type(XInterface.class),DPT.getByName(DPT.getElementNames()[0])); 257 } catch (com.sun.star.lang.WrappedTargetException e) { 258 e.printStackTrace(log); 259 throw new StatusException("Couldn't create a test environment", e); 260 } catch (com.sun.star.container.NoSuchElementException e) { 261 e.printStackTrace(log); 262 throw new StatusException("Couldn't create a test environment", e); 263 } catch (com.sun.star.lang.IllegalArgumentException e) { 264 e.printStackTrace(log); 265 throw new StatusException("Couldn't create a test environment", e); 266 } 267 268 log.println("Creating object - " + 269 ((oObj == null) ? "FAILED" : "OK")); 270 271 TestEnvironment tEnv = new TestEnvironment( oObj ); 272 273 // Other parameters required for interface tests 274 tEnv.addObjRelation("OUTPUTRANGE", sCellAddress); 275 tEnv.addObjRelation("CELLFORCHANGE", oChangeCell); 276 tEnv.addObjRelation("CELLFORCHECK", oCheckCell); 277 tEnv.addObjRelation("FIELDSAMOUNT", new Integer(5)); 278 tEnv.addObjRelation("SHEETDOCUMENT", xSheetDoc); 279 280 createTable2(oSheet, sCellRangeAddress, tEnv); 281 282 return tEnv; 283 } 284 285 /** 286 * Create a new DataPilot table output for use with testing XDataPilotTable2 287 * interface. 288 * 289 * @param oSheet current sheet instance 290 * @param srcRange source range 291 * @param tEnv test environment instance 292 */ 293 private void createTable2(XSpreadsheet oSheet, CellRangeAddress srcRange, TestEnvironment tEnv) 294 { 295 XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier) 296 UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet); 297 log.println("Creating test table object"); 298 XDataPilotTables DPT = DPTS.getDataPilotTables(); 299 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor(); 300 DPDsc.setSourceRange(srcRange); 301 302 XIndexAccess xIA = DPDsc.getDataPilotFields(); 303 int fieldCount = xIA.getCount() - 1; // skip the last field because it's always hidden. 304 try 305 { 306 for (int i = 0; i < fieldCount; ++i) 307 { 308 Object o = xIA.getByIndex(i); 309 XPropertySet fieldPropSet = (XPropertySet)UnoRuntime.queryInterface( 310 XPropertySet.class, o); 311 312 if (i == fieldCount - 1) 313 { 314 // last field 315 fieldPropSet.setPropertyValue( 316 "Function", com.sun.star.sheet.GeneralFunction.SUM); 317 fieldPropSet.setPropertyValue( 318 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.DATA); 319 } 320 else if (i%2 == 0) 321 { 322 // even number fields 323 fieldPropSet.setPropertyValue( 324 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.COLUMN); 325 } 326 else if (i%2 == 1) 327 { 328 // odd number fields 329 fieldPropSet.setPropertyValue( 330 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.ROW); 331 } 332 } 333 334 if (DPT.hasByName("DataPilotTable2")) 335 DPT.removeByName("DataPilotTable2"); 336 337 CellAddress destAddr = new CellAddress(); 338 destAddr.Sheet = 0; 339 destAddr.Column = 0; 340 destAddr.Row = 14; 341 DPT.insertNewByName("DataPilotTable2", destAddr, DPDsc); 342 343 Object o = DPT.getByName("DataPilotTable2"); 344 tEnv.addObjRelation("DATAPILOTTABLE2", o); 345 } 346 catch (com.sun.star.uno.Exception e) 347 { 348 e.printStackTrace(log); 349 throw new StatusException("Couldn't create a test environment", e); 350 } 351 } 352 353 } 354 355 356