1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir package mod._sc; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 26cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 27cdf0e10cSrcweir import com.sun.star.container.XNamed; 28cdf0e10cSrcweir import com.sun.star.lang.XComponent; 29cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 30cdf0e10cSrcweir import com.sun.star.sheet.DataPilotFieldGroupInfo; 31cdf0e10cSrcweir import com.sun.star.sheet.XDataPilotDescriptor; 32cdf0e10cSrcweir import com.sun.star.sheet.XDataPilotFieldGrouping; 33cdf0e10cSrcweir import com.sun.star.sheet.XDataPilotTables; 34cdf0e10cSrcweir import com.sun.star.sheet.XDataPilotTablesSupplier; 35cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet; 36cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 37cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets; 38cdf0e10cSrcweir import com.sun.star.table.CellAddress; 39cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress; 40cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 41cdf0e10cSrcweir import com.sun.star.uno.Type; 42cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 43cdf0e10cSrcweir import com.sun.star.uno.XInterface; 44cdf0e10cSrcweir 45cdf0e10cSrcweir import java.io.PrintWriter; 46cdf0e10cSrcweir 47cdf0e10cSrcweir import lib.StatusException; 48cdf0e10cSrcweir import lib.TestCase; 49cdf0e10cSrcweir import lib.TestEnvironment; 50cdf0e10cSrcweir import lib.TestParameters; 51cdf0e10cSrcweir 52cdf0e10cSrcweir import util.SOfficeFactory; 53cdf0e10cSrcweir 54cdf0e10cSrcweir 55cdf0e10cSrcweir 56cdf0e10cSrcweir public class ScDataPilotFieldGroupsObj extends TestCase 57cdf0e10cSrcweir { 58cdf0e10cSrcweir static XSpreadsheetDocument xSheetDoc = null; 59cdf0e10cSrcweir 60cdf0e10cSrcweir /** 61cdf0e10cSrcweir * A field is filled some values. This integer determines the size of the 62cdf0e10cSrcweir * field in x and y direction. 63cdf0e10cSrcweir */ 64cdf0e10cSrcweir private int mMaxFieldIndex = 6; 65cdf0e10cSrcweir 66cdf0e10cSrcweir /** 67cdf0e10cSrcweir * Creates Spreadsheet document. 68cdf0e10cSrcweir */ 69cdf0e10cSrcweir protected void initialize (TestParameters tParam, PrintWriter log) 70cdf0e10cSrcweir { 71cdf0e10cSrcweir SOfficeFactory SOF = SOfficeFactory.getFactory ( 72cdf0e10cSrcweir (XMultiServiceFactory) tParam.getMSF ()); 73cdf0e10cSrcweir 74cdf0e10cSrcweir try 75cdf0e10cSrcweir { 76cdf0e10cSrcweir log.println ("creating a Spreadsheet document"); 77cdf0e10cSrcweir xSheetDoc = SOF.createCalcDoc (null); 78cdf0e10cSrcweir } 79cdf0e10cSrcweir catch (com.sun.star.uno.Exception e) 80cdf0e10cSrcweir { 81cdf0e10cSrcweir // Some exception occures.FAILED 82cdf0e10cSrcweir e.printStackTrace (log); 83cdf0e10cSrcweir throw new StatusException ("Couldn't create document", e); 84cdf0e10cSrcweir } 85cdf0e10cSrcweir } 86cdf0e10cSrcweir 87cdf0e10cSrcweir /** 88cdf0e10cSrcweir * Disposes Spreadsheet document. 89cdf0e10cSrcweir */ 90cdf0e10cSrcweir protected void cleanup (TestParameters tParam, PrintWriter log) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir log.println (" disposing xSheetDoc "); 93cdf0e10cSrcweir 94cdf0e10cSrcweir XComponent oComp = (XComponent) UnoRuntime.queryInterface ( 95cdf0e10cSrcweir XComponent.class, xSheetDoc); 96cdf0e10cSrcweir util.DesktopTools.closeDoc (oComp); 97cdf0e10cSrcweir } 98cdf0e10cSrcweir 99cdf0e10cSrcweir protected synchronized TestEnvironment createTestEnvironment (TestParameters Param, 100cdf0e10cSrcweir PrintWriter log) 101cdf0e10cSrcweir { 102cdf0e10cSrcweir XInterface oObj = null; 103cdf0e10cSrcweir XInterface datapilotfield = null; 104cdf0e10cSrcweir 105cdf0e10cSrcweir 106cdf0e10cSrcweir // creation of testobject here 107cdf0e10cSrcweir // first we write what we are intend to do to log file 108cdf0e10cSrcweir log.println ("Creating a test environment"); 109cdf0e10cSrcweir 110cdf0e10cSrcweir // the cell range 111cdf0e10cSrcweir CellRangeAddress sCellRangeAdress = new CellRangeAddress (); 112cdf0e10cSrcweir sCellRangeAdress.Sheet = 0; 113cdf0e10cSrcweir sCellRangeAdress.StartColumn = 1; 114cdf0e10cSrcweir sCellRangeAdress.StartRow = 0; 115cdf0e10cSrcweir sCellRangeAdress.EndColumn = mMaxFieldIndex - 1; 116cdf0e10cSrcweir sCellRangeAdress.EndRow = mMaxFieldIndex - 1; 117cdf0e10cSrcweir 118cdf0e10cSrcweir // position of the data pilot table 119cdf0e10cSrcweir CellAddress sCellAdress = new CellAddress (); 120cdf0e10cSrcweir sCellAdress.Sheet = 0; 121cdf0e10cSrcweir sCellAdress.Column = 7; 122cdf0e10cSrcweir sCellAdress.Row = 8; 123cdf0e10cSrcweir 124cdf0e10cSrcweir log.println ("Getting a sheet"); 125cdf0e10cSrcweir 126cdf0e10cSrcweir XSpreadsheets xSpreadsheets = (XSpreadsheets) xSheetDoc.getSheets (); 127cdf0e10cSrcweir XSpreadsheet oSheet = null; 128cdf0e10cSrcweir XSpreadsheet oSheet2 = null; 129cdf0e10cSrcweir XIndexAccess oIndexAccess = (XIndexAccess) UnoRuntime.queryInterface ( 130cdf0e10cSrcweir XIndexAccess.class, xSpreadsheets); 131cdf0e10cSrcweir 132cdf0e10cSrcweir try 133cdf0e10cSrcweir { 134cdf0e10cSrcweir oSheet = (XSpreadsheet) AnyConverter.toObject ( 135cdf0e10cSrcweir new Type (XSpreadsheet.class), 136cdf0e10cSrcweir oIndexAccess.getByIndex (0)); 137cdf0e10cSrcweir oSheet2 = (XSpreadsheet) AnyConverter.toObject ( 138cdf0e10cSrcweir new Type (XSpreadsheet.class), 139cdf0e10cSrcweir oIndexAccess.getByIndex (1)); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir catch (com.sun.star.lang.WrappedTargetException e) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir e.printStackTrace (); 144cdf0e10cSrcweir throw new StatusException ("Couldn't get a spreadsheet", e); 145cdf0e10cSrcweir } 146cdf0e10cSrcweir catch (com.sun.star.lang.IndexOutOfBoundsException e) 147cdf0e10cSrcweir { 148cdf0e10cSrcweir e.printStackTrace (); 149cdf0e10cSrcweir throw new StatusException ("Couldn't get a spreadsheet", e); 150cdf0e10cSrcweir } 151cdf0e10cSrcweir catch (com.sun.star.lang.IllegalArgumentException e) 152cdf0e10cSrcweir { 153cdf0e10cSrcweir e.printStackTrace (); 154cdf0e10cSrcweir throw new StatusException ("Couldn't get a spreadsheet", e); 155cdf0e10cSrcweir } 156cdf0e10cSrcweir 157cdf0e10cSrcweir try 158cdf0e10cSrcweir { 159cdf0e10cSrcweir log.println ("Filling a table"); 160cdf0e10cSrcweir 161cdf0e10cSrcweir for (int i = 1; i < mMaxFieldIndex; i++) 162cdf0e10cSrcweir { 163cdf0e10cSrcweir oSheet.getCellByPosition (i, 0).setFormula ("Col" + i); 164cdf0e10cSrcweir oSheet.getCellByPosition (0, i).setFormula ("Row" + i); 165cdf0e10cSrcweir oSheet2.getCellByPosition (i, 0).setFormula ("Col" + i); 166cdf0e10cSrcweir oSheet2.getCellByPosition (0, i).setFormula ("Row" + i); 167cdf0e10cSrcweir } 168cdf0e10cSrcweir 169cdf0e10cSrcweir for (int i = 1; i < mMaxFieldIndex; i++) 170cdf0e10cSrcweir { 171cdf0e10cSrcweir for (int j = 1; j < mMaxFieldIndex; j++) 172cdf0e10cSrcweir { 173cdf0e10cSrcweir oSheet.getCellByPosition (i, j).setValue (i * (j + 1)); 174cdf0e10cSrcweir oSheet2.getCellByPosition (i, j).setValue (i * (j + 2)); 175cdf0e10cSrcweir } 176cdf0e10cSrcweir } 177cdf0e10cSrcweir 178cdf0e10cSrcweir oSheet.getCellByPosition (1, 1).setFormula ("aName"); 179cdf0e10cSrcweir oSheet.getCellByPosition (1, 2).setFormula ("otherName"); 180cdf0e10cSrcweir oSheet.getCellByPosition (1, 3).setFormula ("una"); 181cdf0e10cSrcweir oSheet.getCellByPosition (1, 4).setFormula ("otherName"); 182cdf0e10cSrcweir oSheet.getCellByPosition (1, 5).setFormula ("somethingelse"); 183cdf0e10cSrcweir 184cdf0e10cSrcweir } 185cdf0e10cSrcweir catch (com.sun.star.lang.IndexOutOfBoundsException e) 186cdf0e10cSrcweir { 187cdf0e10cSrcweir e.printStackTrace (); 188cdf0e10cSrcweir throw new StatusException ("Couldn't fill some cells", e); 189cdf0e10cSrcweir } 190cdf0e10cSrcweir 191cdf0e10cSrcweir // change a value of a cell and check the change in the data pilot 192cdf0e10cSrcweir // (for the XDataPilotTable.refresh() test) 193cdf0e10cSrcweir Object oChangeCell = null; 194cdf0e10cSrcweir Object oCheckCell = null; 195cdf0e10cSrcweir Integer aChangeValue = null; 196cdf0e10cSrcweir 197cdf0e10cSrcweir try 198cdf0e10cSrcweir { 199cdf0e10cSrcweir // cell of data 200cdf0e10cSrcweir oChangeCell = oSheet.getCellByPosition (1, 5); 201cdf0e10cSrcweir 202cdf0e10cSrcweir int x = sCellAdress.Column; 203cdf0e10cSrcweir int y = sCellAdress.Row + 3; 204cdf0e10cSrcweir 205cdf0e10cSrcweir 206cdf0e10cSrcweir // cell of the data pilot output 207cdf0e10cSrcweir oCheckCell = oSheet.getCellByPosition (x, y); 208cdf0e10cSrcweir aChangeValue = new Integer (27); 209cdf0e10cSrcweir } 210cdf0e10cSrcweir catch (com.sun.star.lang.IndexOutOfBoundsException e) 211cdf0e10cSrcweir { 212cdf0e10cSrcweir e.printStackTrace (); 213cdf0e10cSrcweir throw new StatusException ("Couldn't get cells for changeing.", e); 214cdf0e10cSrcweir } 215cdf0e10cSrcweir 216cdf0e10cSrcweir 217cdf0e10cSrcweir // create the test objects 218cdf0e10cSrcweir log.println ("Getting test objects"); 219cdf0e10cSrcweir 220cdf0e10cSrcweir XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier) UnoRuntime.queryInterface ( 221cdf0e10cSrcweir XDataPilotTablesSupplier.class, 222cdf0e10cSrcweir oSheet); 223cdf0e10cSrcweir XDataPilotTables DPT = DPTS.getDataPilotTables (); 224cdf0e10cSrcweir XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor (); 225cdf0e10cSrcweir DPDsc.setSourceRange (sCellRangeAdress); 226cdf0e10cSrcweir 227cdf0e10cSrcweir XPropertySet fieldPropSet = null; 228cdf0e10cSrcweir 229cdf0e10cSrcweir try 230cdf0e10cSrcweir { 231cdf0e10cSrcweir Object oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (0); 232cdf0e10cSrcweir fieldPropSet = (XPropertySet) UnoRuntime.queryInterface ( 233cdf0e10cSrcweir XPropertySet.class, oDataPilotField); 234cdf0e10cSrcweir fieldPropSet.setPropertyValue ("Orientation", 235cdf0e10cSrcweir com.sun.star.sheet.DataPilotFieldOrientation.ROW); 236cdf0e10cSrcweir oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (1); 237cdf0e10cSrcweir fieldPropSet = (XPropertySet) UnoRuntime.queryInterface ( 238cdf0e10cSrcweir XPropertySet.class, oDataPilotField); 239cdf0e10cSrcweir fieldPropSet.setPropertyValue ("Function", 240cdf0e10cSrcweir com.sun.star.sheet.GeneralFunction.SUM); 241cdf0e10cSrcweir fieldPropSet.setPropertyValue ("Orientation", 242cdf0e10cSrcweir com.sun.star.sheet.DataPilotFieldOrientation.DATA); 243cdf0e10cSrcweir oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (2); 244cdf0e10cSrcweir fieldPropSet = (XPropertySet) UnoRuntime.queryInterface ( 245cdf0e10cSrcweir XPropertySet.class, oDataPilotField); 246cdf0e10cSrcweir fieldPropSet.setPropertyValue ("Orientation", 247cdf0e10cSrcweir com.sun.star.sheet.DataPilotFieldOrientation.COLUMN); 248cdf0e10cSrcweir } 249cdf0e10cSrcweir catch (com.sun.star.lang.WrappedTargetException e) 250cdf0e10cSrcweir { 251cdf0e10cSrcweir e.printStackTrace (); 252cdf0e10cSrcweir throw new StatusException ("Couldn't create a test environment", e); 253cdf0e10cSrcweir } 254cdf0e10cSrcweir catch (com.sun.star.lang.IllegalArgumentException e) 255cdf0e10cSrcweir { 256cdf0e10cSrcweir e.printStackTrace (); 257cdf0e10cSrcweir throw new StatusException ("Couldn't create a test environment", e); 258cdf0e10cSrcweir } 259cdf0e10cSrcweir catch (com.sun.star.beans.PropertyVetoException e) 260cdf0e10cSrcweir { 261cdf0e10cSrcweir e.printStackTrace (); 262cdf0e10cSrcweir throw new StatusException ("Couldn't create a test environment", e); 263cdf0e10cSrcweir } 264cdf0e10cSrcweir catch (com.sun.star.beans.UnknownPropertyException e) 265cdf0e10cSrcweir { 266cdf0e10cSrcweir e.printStackTrace (); 267cdf0e10cSrcweir throw new StatusException ("Couldn't create a test environment", e); 268cdf0e10cSrcweir } 269cdf0e10cSrcweir catch (com.sun.star.lang.IndexOutOfBoundsException e) 270cdf0e10cSrcweir { 271cdf0e10cSrcweir e.printStackTrace (); 272cdf0e10cSrcweir throw new StatusException ("Couldn't create a test environment", e); 273cdf0e10cSrcweir } 274cdf0e10cSrcweir 275cdf0e10cSrcweir log.println ("Insert the DataPilotTable"); 276cdf0e10cSrcweir 277cdf0e10cSrcweir if (DPT.hasByName ("DataPilotTable")) 278cdf0e10cSrcweir { 279cdf0e10cSrcweir DPT.removeByName ("DataPilotTable"); 280cdf0e10cSrcweir } 281cdf0e10cSrcweir 282cdf0e10cSrcweir DPT.insertNewByName ("DataPilotTable", sCellAdress, DPDsc); 283cdf0e10cSrcweir XIndexAccess xIA = (XIndexAccess) UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ()); 284cdf0e10cSrcweir XIndexAccess IA = null; 285cdf0e10cSrcweir try 286cdf0e10cSrcweir { 287cdf0e10cSrcweir XDataPilotDescriptor xDPT = (XDataPilotDescriptor) UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0)); 288cdf0e10cSrcweir IA = xDPT.getRowFields (); 289cdf0e10cSrcweir //getSRange(IA); 290cdf0e10cSrcweir System.out.println ("COUNT: "+IA.getCount ()); 291cdf0e10cSrcweir datapilotfield = (XInterface) AnyConverter.toObject ( 292cdf0e10cSrcweir new Type (XInterface.class), IA.getByIndex (0)); 293cdf0e10cSrcweir } 294cdf0e10cSrcweir catch (com.sun.star.lang.WrappedTargetException e) 295cdf0e10cSrcweir { 296cdf0e10cSrcweir e.printStackTrace (); 297cdf0e10cSrcweir throw new StatusException ("Couldn't get data pilot field", e); 298cdf0e10cSrcweir } 299cdf0e10cSrcweir catch (com.sun.star.lang.IndexOutOfBoundsException e) 300cdf0e10cSrcweir { 301cdf0e10cSrcweir e.printStackTrace (); 302cdf0e10cSrcweir throw new StatusException ("Couldn't get data pilot field", e); 303cdf0e10cSrcweir } 304cdf0e10cSrcweir catch (com.sun.star.lang.IllegalArgumentException e) 305cdf0e10cSrcweir { 306cdf0e10cSrcweir e.printStackTrace (); 307cdf0e10cSrcweir throw new StatusException ("Couldn't get data pilot field", e); 308cdf0e10cSrcweir } 309cdf0e10cSrcweir 310cdf0e10cSrcweir try 311cdf0e10cSrcweir { 312cdf0e10cSrcweir XDataPilotFieldGrouping dpfg = (XDataPilotFieldGrouping) UnoRuntime.queryInterface (XDataPilotFieldGrouping.class, datapilotfield); 313cdf0e10cSrcweir String[] elements = new String[]{"aName","otherName"}; 314cdf0e10cSrcweir dpfg.createNameGroup (elements); 315cdf0e10cSrcweir DataPilotFieldGroupInfo dpgi=null; 316cdf0e10cSrcweir xIA = (XIndexAccess) UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ()); 317cdf0e10cSrcweir try 318cdf0e10cSrcweir { 319cdf0e10cSrcweir XDataPilotDescriptor xDPT = (XDataPilotDescriptor) UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0)); 320cdf0e10cSrcweir IA = xDPT.getRowFields (); 321cdf0e10cSrcweir } 322cdf0e10cSrcweir catch (com.sun.star.lang.WrappedTargetException e) 323cdf0e10cSrcweir { 324cdf0e10cSrcweir e.printStackTrace (); 325cdf0e10cSrcweir throw new StatusException ("Couldn't get data pilot field", e); 326cdf0e10cSrcweir } 327cdf0e10cSrcweir catch (com.sun.star.lang.IndexOutOfBoundsException e) 328cdf0e10cSrcweir { 329cdf0e10cSrcweir e.printStackTrace (); 330cdf0e10cSrcweir throw new StatusException ("Couldn't get data pilot field", e); 331cdf0e10cSrcweir } 332cdf0e10cSrcweir for (int i=0;i<IA.getCount ();i++) 333cdf0e10cSrcweir { 334cdf0e10cSrcweir datapilotfield = (XInterface) AnyConverter.toObject ( 335cdf0e10cSrcweir new Type (XInterface.class), IA.getByIndex (i)); 336cdf0e10cSrcweir XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface (XPropertySet.class, IA.getByIndex (i)); 337cdf0e10cSrcweir if (((Boolean)xPropertySet.getPropertyValue ("IsGroupField")).booleanValue ()) 338cdf0e10cSrcweir { 339cdf0e10cSrcweir xPropertySet = (XPropertySet) UnoRuntime.queryInterface (XPropertySet.class, datapilotfield); 340cdf0e10cSrcweir XNamed xNamed = (XNamed) UnoRuntime.queryInterface (XNamed.class, IA.getByIndex (i)); 341cdf0e10cSrcweir System.out.println ("name: "+xNamed.getName ()); 342cdf0e10cSrcweir dpgi = (DataPilotFieldGroupInfo) xPropertySet.getPropertyValue ("GroupInfo"); 343cdf0e10cSrcweir } 344cdf0e10cSrcweir } 345cdf0e10cSrcweir oObj = dpgi.Groups; 346cdf0e10cSrcweir } 347cdf0e10cSrcweir catch (Exception e) 348cdf0e10cSrcweir { 349cdf0e10cSrcweir e.printStackTrace (); 350cdf0e10cSrcweir } 351cdf0e10cSrcweir 352cdf0e10cSrcweir log.println ("Creating object - " + 353cdf0e10cSrcweir ((oObj == null) ? "FAILED" : "OK")); 354cdf0e10cSrcweir 355cdf0e10cSrcweir TestEnvironment tEnv = new TestEnvironment (oObj); 356cdf0e10cSrcweir 357cdf0e10cSrcweir log.println ("Implementationname: " + util.utils.getImplName (oObj)); 358cdf0e10cSrcweir 359cdf0e10cSrcweir // Other parameters required for interface tests 360cdf0e10cSrcweir return tEnv; 361cdf0e10cSrcweir } 362cdf0e10cSrcweir 363cdf0e10cSrcweir } 364