merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sc / ScIndexEnumeration_DataPilotItemsEnumeration.java
blob5367ed41380c7d42304e518b70403115641a0cac
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: ScIndexEnumeration_DataPilotItemsEnumeration.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.XEnumerationAccess;
34 import com.sun.star.container.XIndexAccess;
35 import com.sun.star.container.XNamed;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.DataPilotFieldOrientation;
39 import com.sun.star.sheet.XDataPilotDescriptor;
40 import com.sun.star.sheet.XDataPilotField;
41 import com.sun.star.sheet.XDataPilotTables;
42 import com.sun.star.sheet.XDataPilotTablesSupplier;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.sheet.XSpreadsheets;
46 import com.sun.star.table.CellAddress;
47 import com.sun.star.table.CellRangeAddress;
48 import com.sun.star.uno.AnyConverter;
49 import com.sun.star.uno.Type;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XInterface;
53 import java.io.PrintWriter;
55 import lib.StatusException;
56 import lib.TestCase;
57 import lib.TestEnvironment;
58 import lib.TestParameters;
60 import util.SOfficeFactory;
63 /**
64 * Test for object which is represented by service
65 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
66 * Object implements the following interfaces :
67 * <ul>
68 * <li> <code>com::sun::star::container::XNamed</code></li>
69 * <li> <code>com::sun::star::sheet::DataPilotField</code></li>
70 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
71 * </ul>
72 * @see com.sun.star.sheet.DataPilotField
73 * @see com.sun.star.container.XNamed
74 * @see com.sun.star.sheet.DataPilotField
75 * @see com.sun.star.beans.XPropertySet
76 * @see ifc.container._XNamed
77 * @see ifc.sheet._DataPilotField
78 * @see ifc.beans._XPropertySet
80 public class ScIndexEnumeration_DataPilotItemsEnumeration
81 extends TestCase {
82 static XSpreadsheetDocument xSheetDoc = null;
84 /**
85 * A field is filled some values. This integer determines the size of the
86 * field in x and y direction.
88 private int mMaxFieldIndex = 6;
90 /**
91 * Creates Spreadsheet document.
93 protected void initialize(TestParameters tParam, PrintWriter log) {
94 SOfficeFactory SOF = SOfficeFactory.getFactory(
95 (XMultiServiceFactory) tParam.getMSF());
97 try {
98 log.println("creating a Spreadsheet document");
99 xSheetDoc = SOF.createCalcDoc(null);
100 } catch (com.sun.star.uno.Exception e) {
101 // Some exception occures.FAILED
102 e.printStackTrace(log);
103 throw new StatusException("Couldn't create document", e);
108 * Disposes Spreadsheet document.
110 protected void cleanup(TestParameters tParam, PrintWriter log) {
111 log.println(" disposing xSheetDoc ");
113 XComponent oComp = (XComponent) UnoRuntime.queryInterface(
114 XComponent.class, xSheetDoc);
115 util.DesktopTools.closeDoc(oComp);
119 * Creating a Testenvironment for the interfaces to be tested.
120 * Retrieves a collection of spreadsheets from a document
121 * and takes one of them. Fills some table in the spreadsheet.
122 * Obtains the collection of data pilot tables using the interface
123 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
124 * for the filled table and inserts new data pilot table with this descriptor
125 * to the collection. Obtains the collection of all the data pilot fields
126 * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
127 * the collection the data pilot field with index 0. This data pilot field
128 * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
129 * @see com.sun.star.sheet.DataPilotField
130 * @see com.sun.star.sheet.XDataPilotTablesSupplier
131 * @see com.sun.star.sheet.XDataPilotDescriptor
133 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
134 PrintWriter log) {
135 XInterface oObj = null;
138 // creation of testobject here
139 // first we write what we are intend to do to log file
140 log.println("Creating a test environment");
142 // the cell range
143 CellRangeAddress sCellRangeAdress = new CellRangeAddress();
144 sCellRangeAdress.Sheet = 0;
145 sCellRangeAdress.StartColumn = 1;
146 sCellRangeAdress.StartRow = 0;
147 sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
148 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
150 // position of the data pilot table
151 CellAddress sCellAdress = new CellAddress();
152 sCellAdress.Sheet = 0;
153 sCellAdress.Column = 7;
154 sCellAdress.Row = 8;
156 log.println("Getting a sheet");
158 XSpreadsheets xSpreadsheets = (XSpreadsheets) xSheetDoc.getSheets();
159 XSpreadsheet oSheet = null;
160 XSpreadsheet oSheet2 = null;
161 XIndexAccess oIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
162 XIndexAccess.class, xSpreadsheets);
164 try {
165 oSheet = (XSpreadsheet) AnyConverter.toObject(
166 new Type(XSpreadsheet.class),
167 oIndexAccess.getByIndex(0));
168 oSheet2 = (XSpreadsheet) AnyConverter.toObject(
169 new Type(XSpreadsheet.class),
170 oIndexAccess.getByIndex(1));
171 } catch (com.sun.star.lang.WrappedTargetException e) {
172 e.printStackTrace();
173 throw new StatusException("Couldn't get a spreadsheet", e);
174 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
175 e.printStackTrace();
176 throw new StatusException("Couldn't get a spreadsheet", e);
177 } catch (com.sun.star.lang.IllegalArgumentException e) {
178 e.printStackTrace();
179 throw new StatusException("Couldn't get a spreadsheet", e);
182 try {
183 log.println("Filling a table");
185 for (int i = 1; i < mMaxFieldIndex; i++) {
186 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
187 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
188 oSheet2.getCellByPosition(i, 0).setFormula("Col" + i);
189 oSheet2.getCellByPosition(0, i).setFormula("Row" + i);
192 for (int i = 1; i < mMaxFieldIndex; i++)
193 for (int j = 1; j < mMaxFieldIndex; j++) {
194 oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
195 oSheet2.getCellByPosition(i, j).setValue(i * (j + 2));
197 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
198 e.printStackTrace();
199 throw new StatusException("Couldn't fill some cells", e);
202 // change a value of a cell and check the change in the data pilot
203 // (for the XDataPilotTable.refresh() test)
204 Object oChangeCell = null;
205 Object oCheckCell = null;
206 Integer aChangeValue = null;
208 try {
209 // cell of data
210 oChangeCell = oSheet.getCellByPosition(1, 5);
212 int x = sCellAdress.Column;
213 int y = sCellAdress.Row + 3;
216 // cell of the data pilot output
217 oCheckCell = oSheet.getCellByPosition(x, y);
218 aChangeValue = new Integer(27);
219 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
220 e.printStackTrace();
221 throw new StatusException("Couldn't get cells for changeing.", e);
225 // create the test objects
226 log.println("Getting test objects");
228 XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier) UnoRuntime.queryInterface(
229 XDataPilotTablesSupplier.class,
230 oSheet);
231 XDataPilotTables DPT = DPTS.getDataPilotTables();
232 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
233 DPDsc.setSourceRange(sCellRangeAdress);
235 XPropertySet fieldPropSet = null;
237 try {
238 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
239 fieldPropSet = (XPropertySet) UnoRuntime.queryInterface(
240 XPropertySet.class, oDataPilotField);
241 } catch (com.sun.star.lang.WrappedTargetException e) {
242 e.printStackTrace();
243 throw new StatusException("Couldn't create a test environment", e);
244 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
245 e.printStackTrace();
246 throw new StatusException("Couldn't create a test environment", e);
249 try {
250 fieldPropSet.setPropertyValue("Function",
251 com.sun.star.sheet.GeneralFunction.SUM);
252 fieldPropSet.setPropertyValue("Orientation",
253 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
254 } catch (com.sun.star.lang.WrappedTargetException e) {
255 e.printStackTrace();
256 throw new StatusException("Couldn't create a test environment", e);
257 } catch (com.sun.star.lang.IllegalArgumentException e) {
258 e.printStackTrace();
259 throw new StatusException("Couldn't create a test environment", e);
260 } catch (com.sun.star.beans.PropertyVetoException e) {
261 e.printStackTrace();
262 throw new StatusException("Couldn't create a test environment", e);
263 } catch (com.sun.star.beans.UnknownPropertyException e) {
264 e.printStackTrace();
265 throw new StatusException("Couldn't create a test environment", e);
268 log.println("Insert the DataPilotTable");
270 if (DPT.hasByName("DataPilotTable")) {
271 DPT.removeByName("DataPilotTable");
274 XIndexAccess IA = DPDsc.getDataPilotFields();
275 getSRange(IA);
277 DPT.insertNewByName("DataPilotTable", sCellAdress, DPDsc);
279 try {
280 oObj = (XInterface) AnyConverter.toObject(
281 new Type(XInterface.class), IA.getByIndex(0));
282 } catch (com.sun.star.lang.WrappedTargetException e) {
283 e.printStackTrace();
284 throw new StatusException("Couldn't get data pilot field", e);
285 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
286 e.printStackTrace();
287 throw new StatusException("Couldn't get data pilot field", e);
288 } catch (com.sun.star.lang.IllegalArgumentException e) {
289 e.printStackTrace();
290 throw new StatusException("Couldn't get data pilot field", e);
293 log.println("Creating object - " +
294 ((oObj == null) ? "FAILED" : "OK"));
296 XDataPilotField xDataPilotField = (XDataPilotField) UnoRuntime.queryInterface(
297 XDataPilotField.class, oObj);
299 XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime.queryInterface(
300 XEnumerationAccess.class,
301 xDataPilotField.getItems());
303 oObj = xEnumerationAccess.createEnumeration();
305 TestEnvironment tEnv = new TestEnvironment(oObj);
307 log.println("Implementationname: " + util.utils.getImplName(oObj));
309 // Other parameters required for interface tests
310 return tEnv;
313 private void getSRange(XIndexAccess IA) {
314 int fieldsAmount = IA.getCount() + 1;
316 String[] fieldsNames = new String[fieldsAmount];
318 int i = -1;
319 int cnt = 0;
321 while ((++i) < fieldsAmount) {
322 Object field;
324 try {
325 field = IA.getByIndex(i);
326 } catch (com.sun.star.lang.WrappedTargetException e) {
327 e.printStackTrace(log);
329 return;
330 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
331 e.printStackTrace(log);
333 return;
336 XNamed named = (XNamed) UnoRuntime.queryInterface(XNamed.class,
337 field);
338 String name = named.getName();
340 log.println("**Field : '" + name + "' ... ");
342 if (!name.equals("Data")) {
343 fieldsNames[cnt] = name;
345 XPropertySet props = (XPropertySet) UnoRuntime.queryInterface(
346 XPropertySet.class, field);
348 try {
349 switch (cnt % 5) {
350 case 0:
351 props.setPropertyValue("Orientation",
352 DataPilotFieldOrientation.COLUMN);
353 log.println(" Column");
355 break;
357 case 1:
358 props.setPropertyValue("Orientation",
359 DataPilotFieldOrientation.ROW);
360 log.println(" Row");
362 break;
364 case 2:
365 props.setPropertyValue("Orientation",
366 DataPilotFieldOrientation.DATA);
367 log.println(" Data");
369 break;
371 case 3:
372 props.setPropertyValue("Orientation",
373 DataPilotFieldOrientation.HIDDEN);
374 log.println(" Hidden");
376 break;
378 case 4:
379 props.setPropertyValue("Orientation",
380 DataPilotFieldOrientation.PAGE);
381 log.println(" Page");
383 break;
385 } catch (com.sun.star.lang.WrappedTargetException e) {
386 e.printStackTrace(log);
388 return;
389 } catch (com.sun.star.lang.IllegalArgumentException e) {
390 e.printStackTrace(log);
392 return;
393 } catch (com.sun.star.beans.PropertyVetoException e) {
394 e.printStackTrace(log);
396 return;
397 } catch (com.sun.star.beans.UnknownPropertyException e) {
398 e.printStackTrace(log);
400 return;
403 if ((++cnt) > 4) {
404 break;
406 } else {
407 return;