merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotFieldGroupObj.java
blobbdb5c93324b005b1283c9b5cda3fcbbb776bb55a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScDataPilotFieldGroupObj.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package mod._sc;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.container.XIndexAccess;
34 import com.sun.star.container.XNamed;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sheet.DataPilotFieldGroupInfo;
38 import com.sun.star.sheet.XDataPilotDescriptor;
39 import com.sun.star.sheet.XDataPilotFieldGrouping;
40 import com.sun.star.sheet.XDataPilotTables;
41 import com.sun.star.sheet.XDataPilotTablesSupplier;
42 import com.sun.star.sheet.XSpreadsheet;
43 import com.sun.star.sheet.XSpreadsheetDocument;
44 import com.sun.star.sheet.XSpreadsheets;
45 import com.sun.star.table.CellAddress;
46 import com.sun.star.table.CellRangeAddress;
47 import com.sun.star.uno.AnyConverter;
48 import com.sun.star.uno.Type;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XInterface;
52 import java.io.PrintWriter;
54 import lib.StatusException;
55 import lib.TestCase;
56 import lib.TestEnvironment;
57 import lib.TestParameters;
59 import util.SOfficeFactory;
63 public class ScDataPilotFieldGroupObj extends TestCase
65 static XSpreadsheetDocument xSheetDoc = null;
67 /**
68 * A field is filled some values. This integer determines the size of the
69 * field in x and y direction.
71 private int mMaxFieldIndex = 6;
73 /**
74 * Creates Spreadsheet document.
76 protected void initialize (TestParameters tParam, PrintWriter log)
78 SOfficeFactory SOF = SOfficeFactory.getFactory (
79 (XMultiServiceFactory) tParam.getMSF ());
81 try
83 log.println ("creating a Spreadsheet document");
84 xSheetDoc = SOF.createCalcDoc (null);
86 catch (com.sun.star.uno.Exception e)
88 // Some exception occures.FAILED
89 e.printStackTrace (log);
90 throw new StatusException ("Couldn't create document", e);
94 /**
95 * Disposes Spreadsheet document.
97 protected void cleanup (TestParameters tParam, PrintWriter log)
99 log.println (" disposing xSheetDoc ");
101 XComponent oComp = (XComponent) UnoRuntime.queryInterface (
102 XComponent.class, xSheetDoc);
103 util.DesktopTools.closeDoc (oComp);
106 protected synchronized TestEnvironment createTestEnvironment (TestParameters Param,
107 PrintWriter log)
109 XInterface oObj = null;
110 XInterface datapilotfield = null;
111 XInterface groups = null;
113 // creation of testobject here
114 // first we write what we are intend to do to log file
115 log.println ("Creating a test environment");
117 // the cell range
118 CellRangeAddress sCellRangeAdress = new CellRangeAddress ();
119 sCellRangeAdress.Sheet = 0;
120 sCellRangeAdress.StartColumn = 1;
121 sCellRangeAdress.StartRow = 0;
122 sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
123 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
125 // position of the data pilot table
126 CellAddress sCellAdress = new CellAddress ();
127 sCellAdress.Sheet = 0;
128 sCellAdress.Column = 7;
129 sCellAdress.Row = 8;
131 log.println ("Getting a sheet");
133 XSpreadsheets xSpreadsheets = (XSpreadsheets) xSheetDoc.getSheets ();
134 XSpreadsheet oSheet = null;
135 XSpreadsheet oSheet2 = null;
136 XIndexAccess oIndexAccess = (XIndexAccess) UnoRuntime.queryInterface (
137 XIndexAccess.class, xSpreadsheets);
141 oSheet = (XSpreadsheet) AnyConverter.toObject (
142 new Type (XSpreadsheet.class),
143 oIndexAccess.getByIndex (0));
144 oSheet2 = (XSpreadsheet) AnyConverter.toObject (
145 new Type (XSpreadsheet.class),
146 oIndexAccess.getByIndex (1));
148 catch (com.sun.star.lang.WrappedTargetException e)
150 e.printStackTrace ();
151 throw new StatusException ("Couldn't get a spreadsheet", e);
153 catch (com.sun.star.lang.IndexOutOfBoundsException e)
155 e.printStackTrace ();
156 throw new StatusException ("Couldn't get a spreadsheet", e);
158 catch (com.sun.star.lang.IllegalArgumentException e)
160 e.printStackTrace ();
161 throw new StatusException ("Couldn't get a spreadsheet", e);
166 log.println ("Filling a table");
168 for (int i = 1; i < mMaxFieldIndex; i++)
170 oSheet.getCellByPosition (i, 0).setFormula ("Col" + i);
171 oSheet.getCellByPosition (0, i).setFormula ("Row" + i);
172 oSheet2.getCellByPosition (i, 0).setFormula ("Col" + i);
173 oSheet2.getCellByPosition (0, i).setFormula ("Row" + i);
176 for (int i = 1; i < mMaxFieldIndex; i++)
178 for (int j = 1; j < mMaxFieldIndex; j++)
180 oSheet.getCellByPosition (i, j).setValue (i * (j + 1));
181 oSheet2.getCellByPosition (i, j).setValue (i * (j + 2));
185 oSheet.getCellByPosition (1, 1).setFormula ("aName");
186 oSheet.getCellByPosition (1, 2).setFormula ("otherName");
187 oSheet.getCellByPosition (1, 3).setFormula ("una");
188 oSheet.getCellByPosition (1, 4).setFormula ("otherName");
189 oSheet.getCellByPosition (1, 5).setFormula ("somethingelse");
192 catch (com.sun.star.lang.IndexOutOfBoundsException e)
194 e.printStackTrace ();
195 throw new StatusException ("Couldn't fill some cells", e);
198 // change a value of a cell and check the change in the data pilot
199 // (for the XDataPilotTable.refresh() test)
200 Object oChangeCell = null;
201 Object oCheckCell = null;
202 Integer aChangeValue = null;
206 // cell of data
207 oChangeCell = oSheet.getCellByPosition (1, 5);
209 int x = sCellAdress.Column;
210 int y = sCellAdress.Row + 3;
213 // cell of the data pilot output
214 oCheckCell = oSheet.getCellByPosition (x, y);
215 aChangeValue = new Integer (27);
217 catch (com.sun.star.lang.IndexOutOfBoundsException e)
219 e.printStackTrace ();
220 throw new StatusException ("Couldn't get cells for changeing.", e);
224 // create the test objects
225 log.println ("Getting test objects");
227 XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier) UnoRuntime.queryInterface (
228 XDataPilotTablesSupplier.class,
229 oSheet);
230 XDataPilotTables DPT = DPTS.getDataPilotTables ();
231 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor ();
232 DPDsc.setSourceRange (sCellRangeAdress);
234 XPropertySet fieldPropSet = null;
238 Object oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (0);
239 fieldPropSet = (XPropertySet) UnoRuntime.queryInterface (
240 XPropertySet.class, oDataPilotField);
241 fieldPropSet.setPropertyValue ("Orientation",
242 com.sun.star.sheet.DataPilotFieldOrientation.ROW);
243 oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (1);
244 fieldPropSet = (XPropertySet) UnoRuntime.queryInterface (
245 XPropertySet.class, oDataPilotField);
246 fieldPropSet.setPropertyValue ("Function",
247 com.sun.star.sheet.GeneralFunction.SUM);
248 fieldPropSet.setPropertyValue ("Orientation",
249 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
250 oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (2);
251 fieldPropSet = (XPropertySet) UnoRuntime.queryInterface (
252 XPropertySet.class, oDataPilotField);
253 fieldPropSet.setPropertyValue ("Orientation",
254 com.sun.star.sheet.DataPilotFieldOrientation.COLUMN);
256 catch (com.sun.star.lang.WrappedTargetException e)
258 e.printStackTrace ();
259 throw new StatusException ("Couldn't create a test environment", e);
261 catch (com.sun.star.lang.IllegalArgumentException e)
263 e.printStackTrace ();
264 throw new StatusException ("Couldn't create a test environment", e);
266 catch (com.sun.star.beans.PropertyVetoException e)
268 e.printStackTrace ();
269 throw new StatusException ("Couldn't create a test environment", e);
271 catch (com.sun.star.beans.UnknownPropertyException e)
273 e.printStackTrace ();
274 throw new StatusException ("Couldn't create a test environment", e);
276 catch (com.sun.star.lang.IndexOutOfBoundsException e)
278 e.printStackTrace ();
279 throw new StatusException ("Couldn't create a test environment", e);
282 log.println ("Insert the DataPilotTable");
284 if (DPT.hasByName ("DataPilotTable"))
286 DPT.removeByName ("DataPilotTable");
289 DPT.insertNewByName ("DataPilotTable", sCellAdress, DPDsc);
290 XIndexAccess xIA = (XIndexAccess) UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ());
291 XIndexAccess IA = null;
294 XDataPilotDescriptor xDPT = (XDataPilotDescriptor) UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0));
295 IA = xDPT.getRowFields ();
296 //getSRange(IA);
297 System.out.println ("COUNT: "+IA.getCount ());
298 datapilotfield = (XInterface) AnyConverter.toObject (
299 new Type (XInterface.class), IA.getByIndex (0));
301 catch (com.sun.star.lang.WrappedTargetException e)
303 e.printStackTrace ();
304 throw new StatusException ("Couldn't get data pilot field", e);
306 catch (com.sun.star.lang.IndexOutOfBoundsException e)
308 e.printStackTrace ();
309 throw new StatusException ("Couldn't get data pilot field", e);
311 catch (com.sun.star.lang.IllegalArgumentException e)
313 e.printStackTrace ();
314 throw new StatusException ("Couldn't get data pilot field", e);
319 XDataPilotFieldGrouping dpfg = (XDataPilotFieldGrouping) UnoRuntime.queryInterface (XDataPilotFieldGrouping.class, datapilotfield);
320 String[] elements = new String[]{"aName","otherName"};
321 dpfg.createNameGroup (elements);
322 DataPilotFieldGroupInfo dpgi=null;
323 xIA = (XIndexAccess) UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ());
326 XDataPilotDescriptor xDPT = (XDataPilotDescriptor) UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0));
327 IA = xDPT.getRowFields ();
329 catch (com.sun.star.lang.WrappedTargetException e)
331 e.printStackTrace ();
332 throw new StatusException ("Couldn't get data pilot field", e);
334 catch (com.sun.star.lang.IndexOutOfBoundsException e)
336 e.printStackTrace ();
337 throw new StatusException ("Couldn't get data pilot field", e);
339 for (int i=0;i<IA.getCount ();i++)
341 datapilotfield = (XInterface) AnyConverter.toObject (
342 new Type (XInterface.class), IA.getByIndex (i));
343 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface (XPropertySet.class, IA.getByIndex (i));
344 if (((Boolean)xPropertySet.getPropertyValue ("IsGroupField")).booleanValue ())
346 xPropertySet = (XPropertySet) UnoRuntime.queryInterface (XPropertySet.class, datapilotfield);
347 XNamed xNamed = (XNamed) UnoRuntime.queryInterface (XNamed.class, IA.getByIndex (i));
348 System.out.println ("name: "+xNamed.getName ());
349 dpgi = (DataPilotFieldGroupInfo) xPropertySet.getPropertyValue ("GroupInfo");
352 groups = dpgi.Groups;
353 XIndexAccess groupAccess = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, groups);
354 oObj = (XInterface) UnoRuntime.queryInterface(XInterface.class, groupAccess.getByIndex(0));
356 catch (Exception e)
358 e.printStackTrace ();
361 log.println ("Creating object - " +
362 ((oObj == null) ? "FAILED" : "OK"));
364 TestEnvironment tEnv = new TestEnvironment (oObj);
366 log.println ("Implementationname: " + util.utils.getImplName (oObj));
368 // Other parameters required for interface tests
369 return tEnv;