merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotItemObj.java
blob964399ed4c7776f1aa427c5e279c3eaa9d0f6e4e
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: ScDataPilotItemObj.java,v $
10 * $Revision: 1.6 $
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.DataPilotFieldOrientation;
38 import com.sun.star.sheet.XDataPilotDescriptor;
39 import com.sun.star.sheet.XDataPilotField;
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;
62 /**
63 * Test for object which is represented by service
64 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
65 * Object implements the following interfaces :
66 * <ul>
67 * <li> <code>com::sun::star::container::XNamed</code></li>
68 * <li> <code>com::sun::star::sheet::DataPilotField</code></li>
69 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
70 * </ul>
71 * @see com.sun.star.sheet.DataPilotField
72 * @see com.sun.star.container.XNamed
73 * @see com.sun.star.sheet.DataPilotField
74 * @see com.sun.star.beans.XPropertySet
75 * @see ifc.container._XNamed
76 * @see ifc.sheet._DataPilotField
77 * @see ifc.beans._XPropertySet
79 public class ScDataPilotItemObj extends TestCase {
80 static XSpreadsheetDocument xSheetDoc = null;
82 /**
83 * A field is filled some values. This integer determines the size of the
84 * field in x and y direction.
86 private int mMaxFieldIndex = 6;
88 /**
89 * Creates Spreadsheet document.
91 protected void initialize(TestParameters tParam, PrintWriter log) {
92 SOfficeFactory SOF = SOfficeFactory.getFactory(
93 (XMultiServiceFactory) tParam.getMSF());
95 try {
96 log.println("creating a Spreadsheet document");
97 xSheetDoc = SOF.createCalcDoc(null);
98 } catch (com.sun.star.uno.Exception e) {
99 // Some exception occures.FAILED
100 e.printStackTrace(log);
101 throw new StatusException("Couldn't create document", e);
106 * Disposes Spreadsheet document.
108 protected void cleanup(TestParameters tParam, PrintWriter log) {
109 log.println(" disposing xSheetDoc ");
111 XComponent oComp = (XComponent) UnoRuntime.queryInterface(
112 XComponent.class, xSheetDoc);
113 util.DesktopTools.closeDoc(oComp);
117 * Creating a Testenvironment for the interfaces to be tested.
118 * Retrieves a collection of spreadsheets from a document
119 * and takes one of them. Fills some table in the spreadsheet.
120 * Obtains the collection of data pilot tables using the interface
121 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
122 * for the filled table and inserts new data pilot table with this descriptor
123 * to the collection. Obtains the collection of all the data pilot fields
124 * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
125 * the collection the data pilot field with index 0. This data pilot field
126 * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
127 * @see com.sun.star.sheet.DataPilotField
128 * @see com.sun.star.sheet.XDataPilotTablesSupplier
129 * @see com.sun.star.sheet.XDataPilotDescriptor
131 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
132 PrintWriter log) {
133 XInterface oObj = null;
136 // creation of testobject here
137 // first we write what we are intend to do to log file
138 log.println("Creating a test environment");
140 // the cell range
141 CellRangeAddress sCellRangeAdress = new CellRangeAddress();
142 sCellRangeAdress.Sheet = 0;
143 sCellRangeAdress.StartColumn = 1;
144 sCellRangeAdress.StartRow = 0;
145 sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
146 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
148 // position of the data pilot table
149 CellAddress sCellAdress = new CellAddress();
150 sCellAdress.Sheet = 0;
151 sCellAdress.Column = 7;
152 sCellAdress.Row = 8;
154 log.println("Getting a sheet");
156 XSpreadsheets xSpreadsheets = (XSpreadsheets) xSheetDoc.getSheets();
157 XSpreadsheet oSheet = null;
158 XSpreadsheet oSheet2 = null;
159 XIndexAccess oIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
160 XIndexAccess.class, xSpreadsheets);
162 try {
163 oSheet = (XSpreadsheet) AnyConverter.toObject(
164 new Type(XSpreadsheet.class),
165 oIndexAccess.getByIndex(0));
166 oSheet2 = (XSpreadsheet) AnyConverter.toObject(
167 new Type(XSpreadsheet.class),
168 oIndexAccess.getByIndex(1));
169 } catch (com.sun.star.lang.WrappedTargetException e) {
170 e.printStackTrace();
171 throw new StatusException("Couldn't get a spreadsheet", e);
172 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
173 e.printStackTrace();
174 throw new StatusException("Couldn't get a spreadsheet", e);
175 } catch (com.sun.star.lang.IllegalArgumentException e) {
176 e.printStackTrace();
177 throw new StatusException("Couldn't get a spreadsheet", e);
180 try {
181 log.println("Filling a table");
183 for (int i = 1; i < mMaxFieldIndex; i++) {
184 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
185 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
186 oSheet2.getCellByPosition(i, 0).setFormula("Col" + i);
187 oSheet2.getCellByPosition(0, i).setFormula("Row" + i);
190 for (int i = 1; i < mMaxFieldIndex; i++)
191 for (int j = 1; j < mMaxFieldIndex; j++) {
192 oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
193 oSheet2.getCellByPosition(i, j).setValue(i * (j + 2));
195 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
196 e.printStackTrace();
197 throw new StatusException("Couldn't fill some cells", e);
200 // change a value of a cell and check the change in the data pilot
201 // (for the XDataPilotTable.refresh() test)
202 Object oChangeCell = null;
203 Object oCheckCell = null;
204 Integer aChangeValue = null;
206 try {
207 // cell of data
208 oChangeCell = oSheet.getCellByPosition(1, 5);
210 int x = sCellAdress.Column;
211 int y = sCellAdress.Row + 3;
214 // cell of the data pilot output
215 oCheckCell = oSheet.getCellByPosition(x, y);
216 aChangeValue = new Integer(27);
217 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
218 e.printStackTrace();
219 throw new StatusException("Couldn't get cells for changeing.", e);
223 // create the test objects
224 log.println("Getting test objects");
226 XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier) UnoRuntime.queryInterface(
227 XDataPilotTablesSupplier.class,
228 oSheet);
229 XDataPilotTables DPT = DPTS.getDataPilotTables();
230 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
231 DPDsc.setSourceRange(sCellRangeAdress);
233 XPropertySet fieldPropSet = null;
235 try {
236 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
237 fieldPropSet = (XPropertySet) UnoRuntime.queryInterface(
238 XPropertySet.class, oDataPilotField);
239 } catch (com.sun.star.lang.WrappedTargetException e) {
240 e.printStackTrace();
241 throw new StatusException("Couldn't create a test environment", e);
242 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
243 e.printStackTrace();
244 throw new StatusException("Couldn't create a test environment", e);
247 try {
248 fieldPropSet.setPropertyValue("Function",
249 com.sun.star.sheet.GeneralFunction.SUM);
250 fieldPropSet.setPropertyValue("Orientation",
251 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
252 } catch (com.sun.star.lang.WrappedTargetException e) {
253 e.printStackTrace();
254 throw new StatusException("Couldn't create a test environment", e);
255 } catch (com.sun.star.lang.IllegalArgumentException e) {
256 e.printStackTrace();
257 throw new StatusException("Couldn't create a test environment", e);
258 } catch (com.sun.star.beans.PropertyVetoException e) {
259 e.printStackTrace();
260 throw new StatusException("Couldn't create a test environment", e);
261 } catch (com.sun.star.beans.UnknownPropertyException e) {
262 e.printStackTrace();
263 throw new StatusException("Couldn't create a test environment", e);
266 log.println("Insert the DataPilotTable");
268 if (DPT.hasByName("DataPilotTable")) {
269 DPT.removeByName("DataPilotTable");
272 XIndexAccess IA = DPDsc.getDataPilotFields();
273 getSRange(IA);
275 DPT.insertNewByName("DataPilotTable", sCellAdress, DPDsc);
277 try {
278 oObj = (XInterface) AnyConverter.toObject(
279 new Type(XInterface.class), IA.getByIndex(0));
280 } catch (com.sun.star.lang.WrappedTargetException e) {
281 e.printStackTrace();
282 throw new StatusException("Couldn't get data pilot field", e);
283 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
284 e.printStackTrace();
285 throw new StatusException("Couldn't get data pilot field", e);
286 } catch (com.sun.star.lang.IllegalArgumentException e) {
287 e.printStackTrace();
288 throw new StatusException("Couldn't get data pilot field", e);
291 log.println("Creating object - " +
292 ((oObj == null) ? "FAILED" : "OK"));
294 XDataPilotField xDataPilotField = (XDataPilotField) UnoRuntime.queryInterface(
295 XDataPilotField.class, oObj);
297 XIndexAccess xIA = xDataPilotField.getItems();
299 try {
300 oObj = (XInterface) UnoRuntime.queryInterface(XInterface.class,
301 xIA.getByIndex(0));
302 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
303 log.println("Couldn't get DataPilotItemObj");
304 e.printStackTrace(log);
305 } catch (com.sun.star.lang.WrappedTargetException e) {
306 log.println("Couldn't get DataPilotItemObj");
307 e.printStackTrace(log);
310 TestEnvironment tEnv = new TestEnvironment(oObj);
312 log.println("Implementationname: " + util.utils.getImplName(oObj));
314 // Other parameters required for interface tests
315 tEnv.addObjRelation("NoSetName", "ScDataPilotItemObj");
316 return tEnv;
319 private void getSRange(XIndexAccess IA) {
320 int fieldsAmount = IA.getCount() + 1;
322 String[] fieldsNames = new String[fieldsAmount];
324 int i = -1;
325 int cnt = 0;
327 while ((++i) < fieldsAmount) {
328 Object field;
330 try {
331 field = IA.getByIndex(i);
332 } catch (com.sun.star.lang.WrappedTargetException e) {
333 e.printStackTrace(log);
335 return;
336 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
337 e.printStackTrace(log);
339 return;
342 XNamed named = (XNamed) UnoRuntime.queryInterface(XNamed.class,
343 field);
344 String name = named.getName();
346 log.println("**Field : '" + name + "' ... ");
348 if (!name.equals("Data")) {
349 fieldsNames[cnt] = name;
351 XPropertySet props = (XPropertySet) UnoRuntime.queryInterface(
352 XPropertySet.class, field);
354 try {
355 switch (cnt % 5) {
356 case 0:
357 props.setPropertyValue("Orientation",
358 DataPilotFieldOrientation.COLUMN);
359 log.println(" Column");
361 break;
363 case 1:
364 props.setPropertyValue("Orientation",
365 DataPilotFieldOrientation.ROW);
366 log.println(" Row");
368 break;
370 case 2:
371 props.setPropertyValue("Orientation",
372 DataPilotFieldOrientation.DATA);
373 log.println(" Data");
375 break;
377 case 3:
378 props.setPropertyValue("Orientation",
379 DataPilotFieldOrientation.HIDDEN);
380 log.println(" Hidden");
382 break;
384 case 4:
385 props.setPropertyValue("Orientation",
386 DataPilotFieldOrientation.PAGE);
387 log.println(" Page");
389 break;
391 } catch (com.sun.star.lang.WrappedTargetException e) {
392 e.printStackTrace(log);
394 return;
395 } catch (com.sun.star.lang.IllegalArgumentException e) {
396 e.printStackTrace(log);
398 return;
399 } catch (com.sun.star.beans.PropertyVetoException e) {
400 e.printStackTrace(log);
402 return;
403 } catch (com.sun.star.beans.UnknownPropertyException e) {
404 e.printStackTrace(log);
406 return;
409 if ((++cnt) > 4) {
410 break;
412 } else {
413 return;