bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotFieldObj.java
blob73c2b8b8e2245e763d17b56bf423ee111a47fb89
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package mod._sc;
20 import com.sun.star.beans.XPropertySet;
21 import com.sun.star.container.XIndexAccess;
22 import com.sun.star.container.XNamed;
23 import com.sun.star.lang.XComponent;
24 import com.sun.star.lang.XMultiServiceFactory;
25 import com.sun.star.sheet.DataPilotFieldOrientation;
26 import com.sun.star.sheet.XDataPilotDescriptor;
27 import com.sun.star.sheet.XDataPilotTables;
28 import com.sun.star.sheet.XDataPilotTablesSupplier;
29 import com.sun.star.sheet.XSpreadsheet;
30 import com.sun.star.sheet.XSpreadsheetDocument;
31 import com.sun.star.sheet.XSpreadsheets;
32 import com.sun.star.table.CellAddress;
33 import com.sun.star.table.CellRangeAddress;
34 import com.sun.star.uno.AnyConverter;
35 import com.sun.star.uno.Type;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XInterface;
39 import java.io.PrintWriter;
41 import lib.StatusException;
42 import lib.TestCase;
43 import lib.TestEnvironment;
44 import lib.TestParameters;
46 import util.SOfficeFactory;
49 /**
50 * Test for object which is represented by service
51 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
52 * Object implements the following interfaces :
53 * <ul>
54 * <li> <code>com::sun::star::container::XNamed</code></li>
55 * <li> <code>com::sun::star::sheet::DataPilotField</code></li>
56 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
57 * </ul>
58 * @see com.sun.star.sheet.DataPilotField
59 * @see com.sun.star.container.XNamed
60 * @see com.sun.star.sheet.DataPilotField
61 * @see com.sun.star.beans.XPropertySet
62 * @see ifc.container._XNamed
63 * @see ifc.sheet._DataPilotField
64 * @see ifc.beans._XPropertySet
66 public class ScDataPilotFieldObj extends TestCase {
67 private XSpreadsheetDocument xSheetDoc = null;
69 /**
70 * A field is filled some values. This integer determines the size of the
71 * field in x and y direction.
73 private int mMaxFieldIndex = 6;
75 /**
76 * Creates Spreadsheet document.
78 protected void initialize(TestParameters tParam, PrintWriter log) {
79 SOfficeFactory SOF = SOfficeFactory.getFactory(
80 (XMultiServiceFactory) tParam.getMSF());
82 try {
83 log.println("creating a Spreadsheet document");
84 xSheetDoc = SOF.createCalcDoc(null);
85 } catch (com.sun.star.uno.Exception e) {
86 // Some exception occurs.FAILED
87 e.printStackTrace(log);
88 throw new StatusException("Couldn't create document", e);
92 /**
93 * Disposes Spreadsheet document.
95 protected void cleanup(TestParameters tParam, PrintWriter log) {
96 log.println(" disposing xSheetDoc ");
98 XComponent oComp = UnoRuntime.queryInterface(
99 XComponent.class, xSheetDoc);
100 util.DesktopTools.closeDoc(oComp);
104 * Creating a Testenvironment for the interfaces to be tested.
105 * Retrieves a collection of spreadsheets from a document
106 * and takes one of them. Fills some table in the spreadsheet.
107 * Obtains the collection of data pilot tables using the interface
108 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
109 * for the filled table and inserts new data pilot table with this descriptor
110 * to the collection. Obtains the collection of all the data pilot fields
111 * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
112 * the collection the data pilot field with index 0. This data pilot field
113 * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
114 * @see com.sun.star.sheet.DataPilotField
115 * @see com.sun.star.sheet.XDataPilotTablesSupplier
116 * @see com.sun.star.sheet.XDataPilotDescriptor
118 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
119 PrintWriter log) {
120 XInterface oObj = null;
123 // creation of testobject here
124 // first we write what we are intend to do to log file
125 log.println("Creating a test environment");
127 // the cell range
128 CellRangeAddress sCellRangeAdress = new CellRangeAddress();
129 sCellRangeAdress.Sheet = 0;
130 sCellRangeAdress.StartColumn = 1;
131 sCellRangeAdress.StartRow = 0;
132 sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
133 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
135 // position of the data pilot table
136 CellAddress sCellAdress = new CellAddress();
137 sCellAdress.Sheet = 0;
138 sCellAdress.Column = 7;
139 sCellAdress.Row = 8;
141 log.println("Getting a sheet");
143 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
144 XSpreadsheet oSheet = null;
145 XSpreadsheet oSheet2 = null;
146 XIndexAccess oIndexAccess = UnoRuntime.queryInterface(
147 XIndexAccess.class, xSpreadsheets);
148 // Make sure there are at least two sheets
149 xSpreadsheets.insertNewByName("Some Sheet", (short)0);
151 try {
152 oSheet = (XSpreadsheet) AnyConverter.toObject(
153 new Type(XSpreadsheet.class),
154 oIndexAccess.getByIndex(0));
155 oSheet2 = (XSpreadsheet) AnyConverter.toObject(
156 new Type(XSpreadsheet.class),
157 oIndexAccess.getByIndex(1));
158 } catch (com.sun.star.lang.WrappedTargetException e) {
159 e.printStackTrace();
160 throw new StatusException("Couldn't get a spreadsheet", e);
161 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
162 e.printStackTrace();
163 throw new StatusException("Couldn't get a spreadsheet", e);
164 } catch (com.sun.star.lang.IllegalArgumentException e) {
165 e.printStackTrace();
166 throw new StatusException("Couldn't get a spreadsheet", e);
169 try {
170 log.println("Filling a table");
172 for (int i = 1; i < mMaxFieldIndex; i++) {
173 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
174 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
175 oSheet2.getCellByPosition(i, 0).setFormula("Col" + i);
176 oSheet2.getCellByPosition(0, i).setFormula("Row" + i);
179 for (int i = 1; i < mMaxFieldIndex; i++)
180 for (int j = 1; j < mMaxFieldIndex; j++) {
181 oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
182 oSheet2.getCellByPosition(i, j).setValue(i * (j + 2));
184 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
185 e.printStackTrace();
186 throw new StatusException("Couldn't fill some cells", e);
189 try {
190 oSheet.getCellByPosition(1, 5);
192 int x = sCellAdress.Column;
193 int y = sCellAdress.Row + 3;
196 oSheet.getCellByPosition(x, y);
197 new Integer(27);
198 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
199 e.printStackTrace();
200 throw new StatusException("Couldn't get cells for changing.", e);
204 // create the test objects
205 log.println("Getting test objects");
207 XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface(
208 XDataPilotTablesSupplier.class,
209 oSheet);
210 XDataPilotTables DPT = DPTS.getDataPilotTables();
211 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
212 DPDsc.setSourceRange(sCellRangeAdress);
214 XPropertySet fieldPropSet = null;
216 try {
217 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
218 fieldPropSet = UnoRuntime.queryInterface(
219 XPropertySet.class, oDataPilotField);
220 } catch (com.sun.star.lang.WrappedTargetException e) {
221 e.printStackTrace();
222 throw new StatusException("Couldn't create a test environment", e);
223 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
224 e.printStackTrace();
225 throw new StatusException("Couldn't create a test environment", e);
228 try {
229 fieldPropSet.setPropertyValue("Function",
230 com.sun.star.sheet.GeneralFunction.SUM);
231 fieldPropSet.setPropertyValue("Orientation",
232 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
233 } catch (com.sun.star.lang.WrappedTargetException e) {
234 e.printStackTrace();
235 throw new StatusException("Couldn't create a test environment", e);
236 } catch (com.sun.star.lang.IllegalArgumentException e) {
237 e.printStackTrace();
238 throw new StatusException("Couldn't create a test environment", e);
239 } catch (com.sun.star.beans.PropertyVetoException e) {
240 e.printStackTrace();
241 throw new StatusException("Couldn't create a test environment", e);
242 } catch (com.sun.star.beans.UnknownPropertyException e) {
243 e.printStackTrace();
244 throw new StatusException("Couldn't create a test environment", e);
247 log.println("Insert the DataPilotTable");
249 if (DPT.hasByName("DataPilotTable")) {
250 DPT.removeByName("DataPilotTable");
253 XIndexAccess IA = DPDsc.getDataPilotFields();
254 getSRange(IA);
256 DPT.insertNewByName("DataPilotTable", sCellAdress, DPDsc);
258 try {
259 oObj = (XInterface) AnyConverter.toObject(
260 new Type(XInterface.class), IA.getByIndex(0));
261 } catch (com.sun.star.lang.WrappedTargetException e) {
262 e.printStackTrace();
263 throw new StatusException("Couldn't get data pilot field", e);
264 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
265 e.printStackTrace();
266 throw new StatusException("Couldn't get data pilot field", e);
267 } catch (com.sun.star.lang.IllegalArgumentException e) {
268 e.printStackTrace();
269 throw new StatusException("Couldn't get data pilot field", e);
272 log.println("Creating object - " +
273 ((oObj == null) ? "FAILED" : "OK"));
275 TestEnvironment tEnv = new TestEnvironment(oObj);
277 log.println("Implementationname: " + util.utils.getImplName(oObj));
279 // Other parameters required for interface tests
280 return tEnv;
283 private void getSRange(XIndexAccess IA) {
284 int fieldsAmount = IA.getCount() + 1;
286 String[] fieldsNames = new String[fieldsAmount];
288 int i = -1;
289 int cnt = 0;
291 while ((++i) < fieldsAmount) {
292 Object field;
294 try {
295 field = IA.getByIndex(i);
296 } catch (com.sun.star.lang.WrappedTargetException e) {
297 e.printStackTrace(log);
299 return;
300 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
301 e.printStackTrace(log);
303 return;
306 XNamed named = UnoRuntime.queryInterface(XNamed.class,
307 field);
308 String name = named.getName();
310 log.println("**Field : '" + name + "' ... ");
312 if (!name.equals("Data")) {
313 fieldsNames[cnt] = name;
315 XPropertySet props = UnoRuntime.queryInterface(
316 XPropertySet.class, field);
318 try {
319 switch (cnt % 5) {
320 case 0:
321 props.setPropertyValue("Orientation",
322 DataPilotFieldOrientation.COLUMN);
323 log.println(" Column");
325 break;
327 case 1:
328 props.setPropertyValue("Orientation",
329 DataPilotFieldOrientation.ROW);
330 log.println(" Row");
332 break;
334 case 2:
335 props.setPropertyValue("Orientation",
336 DataPilotFieldOrientation.DATA);
337 log.println(" Data");
339 break;
341 case 3:
342 props.setPropertyValue("Orientation",
343 DataPilotFieldOrientation.HIDDEN);
344 log.println(" Hidden");
346 break;
348 case 4:
349 props.setPropertyValue("Orientation",
350 DataPilotFieldOrientation.PAGE);
351 log.println(" Page");
353 break;
355 } catch (com.sun.star.lang.WrappedTargetException e) {
356 e.printStackTrace(log);
358 return;
359 } catch (com.sun.star.lang.IllegalArgumentException e) {
360 e.printStackTrace(log);
362 return;
363 } catch (com.sun.star.beans.PropertyVetoException e) {
364 e.printStackTrace(log);
366 return;
367 } catch (com.sun.star.beans.UnknownPropertyException e) {
368 e.printStackTrace(log);
370 return;
373 if ((++cnt) > 4) {
374 break;
376 } else {
377 return;