merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotFieldObj.java
blob3149df05d74c0b8ce7f3c57d6ec69493ec3564b7
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: ScDataPilotFieldObj.java,v $
10 * $Revision: 1.10 $
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.XDataPilotTables;
40 import com.sun.star.sheet.XDataPilotTablesSupplier;
41 import com.sun.star.sheet.XSpreadsheet;
42 import com.sun.star.sheet.XSpreadsheetDocument;
43 import com.sun.star.sheet.XSpreadsheets;
44 import com.sun.star.table.CellAddress;
45 import com.sun.star.table.CellRangeAddress;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.Type;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
51 import java.io.PrintWriter;
53 import lib.StatusException;
54 import lib.TestCase;
55 import lib.TestEnvironment;
56 import lib.TestParameters;
58 import util.SOfficeFactory;
61 /**
62 * Test for object which is represented by service
63 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
64 * Object implements the following interfaces :
65 * <ul>
66 * <li> <code>com::sun::star::container::XNamed</code></li>
67 * <li> <code>com::sun::star::sheet::DataPilotField</code></li>
68 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
69 * </ul>
70 * @see com.sun.star.sheet.DataPilotField
71 * @see com.sun.star.container.XNamed
72 * @see com.sun.star.sheet.DataPilotField
73 * @see com.sun.star.beans.XPropertySet
74 * @see ifc.container._XNamed
75 * @see ifc.sheet._DataPilotField
76 * @see ifc.beans._XPropertySet
78 public class ScDataPilotFieldObj extends TestCase {
79 static XSpreadsheetDocument xSheetDoc = null;
81 /**
82 * A field is filled some values. This integer determines the size of the
83 * field in x and y direction.
85 private int mMaxFieldIndex = 6;
87 /**
88 * Creates Spreadsheet document.
90 protected void initialize(TestParameters tParam, PrintWriter log) {
91 SOfficeFactory SOF = SOfficeFactory.getFactory(
92 (XMultiServiceFactory) tParam.getMSF());
94 try {
95 log.println("creating a Spreadsheet document");
96 xSheetDoc = SOF.createCalcDoc(null);
97 } catch (com.sun.star.uno.Exception e) {
98 // Some exception occures.FAILED
99 e.printStackTrace(log);
100 throw new StatusException("Couldn't create document", e);
105 * Disposes Spreadsheet document.
107 protected void cleanup(TestParameters tParam, PrintWriter log) {
108 log.println(" disposing xSheetDoc ");
110 XComponent oComp = (XComponent) UnoRuntime.queryInterface(
111 XComponent.class, xSheetDoc);
112 util.DesktopTools.closeDoc(oComp);
116 * Creating a Testenvironment for the interfaces to be tested.
117 * Retrieves a collection of spreadsheets from a document
118 * and takes one of them. Fills some table in the spreadsheet.
119 * Obtains the collection of data pilot tables using the interface
120 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
121 * for the filled table and inserts new data pilot table with this descriptor
122 * to the collection. Obtains the collection of all the data pilot fields
123 * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
124 * the collection the data pilot field with index 0. This data pilot field
125 * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
126 * @see com.sun.star.sheet.DataPilotField
127 * @see com.sun.star.sheet.XDataPilotTablesSupplier
128 * @see com.sun.star.sheet.XDataPilotDescriptor
130 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
131 PrintWriter log) {
132 XInterface oObj = null;
135 // creation of testobject here
136 // first we write what we are intend to do to log file
137 log.println("Creating a test environment");
139 // the cell range
140 CellRangeAddress sCellRangeAdress = new CellRangeAddress();
141 sCellRangeAdress.Sheet = 0;
142 sCellRangeAdress.StartColumn = 1;
143 sCellRangeAdress.StartRow = 0;
144 sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
145 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
147 // position of the data pilot table
148 CellAddress sCellAdress = new CellAddress();
149 sCellAdress.Sheet = 0;
150 sCellAdress.Column = 7;
151 sCellAdress.Row = 8;
153 log.println("Getting a sheet");
155 XSpreadsheets xSpreadsheets = (XSpreadsheets) xSheetDoc.getSheets();
156 XSpreadsheet oSheet = null;
157 XSpreadsheet oSheet2 = null;
158 XIndexAccess oIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
159 XIndexAccess.class, xSpreadsheets);
161 try {
162 oSheet = (XSpreadsheet) AnyConverter.toObject(
163 new Type(XSpreadsheet.class),
164 oIndexAccess.getByIndex(0));
165 oSheet2 = (XSpreadsheet) AnyConverter.toObject(
166 new Type(XSpreadsheet.class),
167 oIndexAccess.getByIndex(1));
168 } catch (com.sun.star.lang.WrappedTargetException e) {
169 e.printStackTrace();
170 throw new StatusException("Couldn't get a spreadsheet", e);
171 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
172 e.printStackTrace();
173 throw new StatusException("Couldn't get a spreadsheet", e);
174 } catch (com.sun.star.lang.IllegalArgumentException e) {
175 e.printStackTrace();
176 throw new StatusException("Couldn't get a spreadsheet", e);
179 try {
180 log.println("Filling a table");
182 for (int i = 1; i < mMaxFieldIndex; i++) {
183 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
184 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
185 oSheet2.getCellByPosition(i, 0).setFormula("Col" + i);
186 oSheet2.getCellByPosition(0, i).setFormula("Row" + i);
189 for (int i = 1; i < mMaxFieldIndex; i++)
190 for (int j = 1; j < mMaxFieldIndex; j++) {
191 oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
192 oSheet2.getCellByPosition(i, j).setValue(i * (j + 2));
194 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
195 e.printStackTrace();
196 throw new StatusException("Couldn't fill some cells", e);
199 // change a value of a cell and check the change in the data pilot
200 // (for the XDataPilotTable.refresh() test)
201 Object oChangeCell = null;
202 Object oCheckCell = null;
203 Integer aChangeValue = null;
205 try {
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);
216 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
217 e.printStackTrace();
218 throw new StatusException("Couldn't get cells for changeing.", e);
222 // create the test objects
223 log.println("Getting test objects");
225 XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier) UnoRuntime.queryInterface(
226 XDataPilotTablesSupplier.class,
227 oSheet);
228 XDataPilotTables DPT = DPTS.getDataPilotTables();
229 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
230 DPDsc.setSourceRange(sCellRangeAdress);
232 XPropertySet fieldPropSet = null;
234 try {
235 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
236 fieldPropSet = (XPropertySet) UnoRuntime.queryInterface(
237 XPropertySet.class, oDataPilotField);
238 } catch (com.sun.star.lang.WrappedTargetException e) {
239 e.printStackTrace();
240 throw new StatusException("Couldn't create a test environment", e);
241 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
242 e.printStackTrace();
243 throw new StatusException("Couldn't create a test environment", e);
246 try {
247 fieldPropSet.setPropertyValue("Function",
248 com.sun.star.sheet.GeneralFunction.SUM);
249 fieldPropSet.setPropertyValue("Orientation",
250 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
251 } catch (com.sun.star.lang.WrappedTargetException e) {
252 e.printStackTrace();
253 throw new StatusException("Couldn't create a test environment", e);
254 } catch (com.sun.star.lang.IllegalArgumentException e) {
255 e.printStackTrace();
256 throw new StatusException("Couldn't create a test environment", e);
257 } catch (com.sun.star.beans.PropertyVetoException e) {
258 e.printStackTrace();
259 throw new StatusException("Couldn't create a test environment", e);
260 } catch (com.sun.star.beans.UnknownPropertyException e) {
261 e.printStackTrace();
262 throw new StatusException("Couldn't create a test environment", e);
265 log.println("Insert the DataPilotTable");
267 if (DPT.hasByName("DataPilotTable")) {
268 DPT.removeByName("DataPilotTable");
271 XIndexAccess IA = DPDsc.getDataPilotFields();
272 getSRange(IA);
274 DPT.insertNewByName("DataPilotTable", sCellAdress, DPDsc);
276 try {
277 oObj = (XInterface) AnyConverter.toObject(
278 new Type(XInterface.class), IA.getByIndex(0));
279 } catch (com.sun.star.lang.WrappedTargetException e) {
280 e.printStackTrace();
281 throw new StatusException("Couldn't get data pilot field", e);
282 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
283 e.printStackTrace();
284 throw new StatusException("Couldn't get data pilot field", e);
285 } catch (com.sun.star.lang.IllegalArgumentException e) {
286 e.printStackTrace();
287 throw new StatusException("Couldn't get data pilot field", e);
290 log.println("Creating object - " +
291 ((oObj == null) ? "FAILED" : "OK"));
293 TestEnvironment tEnv = new TestEnvironment(oObj);
295 log.println("Implementationname: " + util.utils.getImplName(oObj));
297 // Other parameters required for interface tests
298 return tEnv;
301 private void getSRange(XIndexAccess IA) {
302 int fieldsAmount = IA.getCount() + 1;
304 String[] fieldsNames = new String[fieldsAmount];
306 int i = -1;
307 int cnt = 0;
309 while ((++i) < fieldsAmount) {
310 Object field;
312 try {
313 field = IA.getByIndex(i);
314 } catch (com.sun.star.lang.WrappedTargetException e) {
315 e.printStackTrace(log);
317 return;
318 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
319 e.printStackTrace(log);
321 return;
324 XNamed named = (XNamed) UnoRuntime.queryInterface(XNamed.class,
325 field);
326 String name = named.getName();
328 log.println("**Field : '" + name + "' ... ");
330 if (!name.equals("Data")) {
331 fieldsNames[cnt] = name;
333 XPropertySet props = (XPropertySet) UnoRuntime.queryInterface(
334 XPropertySet.class, field);
336 try {
337 switch (cnt % 5) {
338 case 0:
339 props.setPropertyValue("Orientation",
340 DataPilotFieldOrientation.COLUMN);
341 log.println(" Column");
343 break;
345 case 1:
346 props.setPropertyValue("Orientation",
347 DataPilotFieldOrientation.ROW);
348 log.println(" Row");
350 break;
352 case 2:
353 props.setPropertyValue("Orientation",
354 DataPilotFieldOrientation.DATA);
355 log.println(" Data");
357 break;
359 case 3:
360 props.setPropertyValue("Orientation",
361 DataPilotFieldOrientation.HIDDEN);
362 log.println(" Hidden");
364 break;
366 case 4:
367 props.setPropertyValue("Orientation",
368 DataPilotFieldOrientation.PAGE);
369 log.println(" Page");
371 break;
373 } catch (com.sun.star.lang.WrappedTargetException e) {
374 e.printStackTrace(log);
376 return;
377 } catch (com.sun.star.lang.IllegalArgumentException e) {
378 e.printStackTrace(log);
380 return;
381 } catch (com.sun.star.beans.PropertyVetoException e) {
382 e.printStackTrace(log);
384 return;
385 } catch (com.sun.star.beans.UnknownPropertyException e) {
386 e.printStackTrace(log);
388 return;
391 if ((++cnt) > 4) {
392 break;
394 } else {
395 return;