bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotItemsObj.java
blob3c4708a60cdf7fcf16e9379b1114bd5bb5a7838d
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.sheet.DataPilotFieldOrientation;
25 import com.sun.star.sheet.XDataPilotDescriptor;
26 import com.sun.star.sheet.XDataPilotField;
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 ScDataPilotItemsObj 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 static final int mMaxFieldIndex = 6;
75 /**
76 * Creates Spreadsheet document.
78 @Override
79 protected void initialize(TestParameters tParam, PrintWriter log) {
80 SOfficeFactory SOF = SOfficeFactory.getFactory(
81 tParam.getMSF());
83 try {
84 log.println("creating a Spreadsheet document");
85 xSheetDoc = SOF.createCalcDoc(null);
86 } catch (com.sun.star.uno.Exception e) {
87 // Some exception occurs.FAILED
88 e.printStackTrace(log);
89 throw new StatusException("Couldn't create document", e);
93 /**
94 * Disposes Spreadsheet document.
96 @Override
97 protected void cleanup(TestParameters tParam, PrintWriter log) {
98 log.println(" disposing xSheetDoc ");
100 XComponent oComp = UnoRuntime.queryInterface(
101 XComponent.class, xSheetDoc);
102 util.DesktopTools.closeDoc(oComp);
106 * Creating a Testenvironment for the interfaces to be tested.
107 * Retrieves a collection of spreadsheets from a document
108 * and takes one of them. Fills some table in the spreadsheet.
109 * Obtains the collection of data pilot tables using the interface
110 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
111 * for the filled table and inserts new data pilot table with this descriptor
112 * to the collection. Obtains the collection of all the data pilot fields
113 * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
114 * the collection the data pilot field with index 0. This data pilot field
115 * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
116 * @see com.sun.star.sheet.DataPilotField
117 * @see com.sun.star.sheet.XDataPilotTablesSupplier
118 * @see com.sun.star.sheet.XDataPilotDescriptor
120 @Override
121 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
122 PrintWriter log) {
123 XInterface oObj = null;
126 // creation of testobject here
127 // first we write what we are intend to do to log file
128 log.println("Creating a test environment");
130 // the cell range
131 CellRangeAddress sCellRangeAdress = new CellRangeAddress();
132 sCellRangeAdress.Sheet = 0;
133 sCellRangeAdress.StartColumn = 1;
134 sCellRangeAdress.StartRow = 0;
135 sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
136 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
138 // position of the data pilot table
139 CellAddress sCellAdress = new CellAddress();
140 sCellAdress.Sheet = 0;
141 sCellAdress.Column = 7;
142 sCellAdress.Row = 8;
144 log.println("Getting a sheet");
146 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
147 XSpreadsheet oSheet = null;
148 XSpreadsheet oSheet2 = null;
149 XIndexAccess oIndexAccess = UnoRuntime.queryInterface(
150 XIndexAccess.class, xSpreadsheets);
151 // Make sure there are at least two sheets
152 xSpreadsheets.insertNewByName("Some Sheet", (short)0);
154 try {
155 oSheet = (XSpreadsheet) AnyConverter.toObject(
156 new Type(XSpreadsheet.class),
157 oIndexAccess.getByIndex(0));
158 oSheet2 = (XSpreadsheet) AnyConverter.toObject(
159 new Type(XSpreadsheet.class),
160 oIndexAccess.getByIndex(1));
161 } catch (com.sun.star.lang.WrappedTargetException e) {
162 e.printStackTrace();
163 throw new StatusException("Couldn't get a spreadsheet", e);
164 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
165 e.printStackTrace();
166 throw new StatusException("Couldn't get a spreadsheet", e);
167 } catch (com.sun.star.lang.IllegalArgumentException e) {
168 e.printStackTrace();
169 throw new StatusException("Couldn't get a spreadsheet", e);
172 try {
173 log.println("Filling a table");
175 for (int i = 1; i < mMaxFieldIndex; i++) {
176 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
177 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
178 oSheet2.getCellByPosition(i, 0).setFormula("Col" + i);
179 oSheet2.getCellByPosition(0, i).setFormula("Row" + i);
182 for (int i = 1; i < mMaxFieldIndex; i++)
183 for (int j = 1; j < mMaxFieldIndex; j++) {
184 oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
185 oSheet2.getCellByPosition(i, j).setValue(i * (j + 2));
187 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
188 e.printStackTrace();
189 throw new StatusException("Couldn't fill some cells", e);
192 try {
193 oSheet.getCellByPosition(1, 5);
195 int x = sCellAdress.Column;
196 int y = sCellAdress.Row + 3;
199 oSheet.getCellByPosition(x, y);
200 Integer.valueOf(27);
201 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
202 e.printStackTrace();
203 throw new StatusException("Couldn't get cells for changing.", e);
207 // create the test objects
208 log.println("Getting test objects");
210 XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface(
211 XDataPilotTablesSupplier.class,
212 oSheet);
213 XDataPilotTables DPT = DPTS.getDataPilotTables();
214 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
215 DPDsc.setSourceRange(sCellRangeAdress);
217 XPropertySet fieldPropSet = null;
219 try {
220 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
221 fieldPropSet = UnoRuntime.queryInterface(
222 XPropertySet.class, oDataPilotField);
223 } catch (com.sun.star.lang.WrappedTargetException e) {
224 e.printStackTrace();
225 throw new StatusException("Couldn't create a test environment", e);
226 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
227 e.printStackTrace();
228 throw new StatusException("Couldn't create a test environment", e);
231 try {
232 fieldPropSet.setPropertyValue("Function",
233 com.sun.star.sheet.GeneralFunction.SUM);
234 fieldPropSet.setPropertyValue("Orientation",
235 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
236 } catch (com.sun.star.lang.WrappedTargetException e) {
237 e.printStackTrace();
238 throw new StatusException("Couldn't create a test environment", e);
239 } catch (com.sun.star.lang.IllegalArgumentException e) {
240 e.printStackTrace();
241 throw new StatusException("Couldn't create a test environment", e);
242 } catch (com.sun.star.beans.PropertyVetoException e) {
243 e.printStackTrace();
244 throw new StatusException("Couldn't create a test environment", e);
245 } catch (com.sun.star.beans.UnknownPropertyException e) {
246 e.printStackTrace();
247 throw new StatusException("Couldn't create a test environment", e);
250 log.println("Insert the DataPilotTable");
252 if (DPT.hasByName("DataPilotTable")) {
253 DPT.removeByName("DataPilotTable");
256 XIndexAccess IA = DPDsc.getDataPilotFields();
257 getSRange(IA);
259 DPT.insertNewByName("DataPilotTable", sCellAdress, DPDsc);
261 try {
262 oObj = (XInterface) AnyConverter.toObject(
263 new Type(XInterface.class), IA.getByIndex(0));
264 } catch (com.sun.star.lang.WrappedTargetException e) {
265 e.printStackTrace();
266 throw new StatusException("Couldn't get data pilot field", e);
267 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
268 e.printStackTrace();
269 throw new StatusException("Couldn't get data pilot field", e);
270 } catch (com.sun.star.lang.IllegalArgumentException e) {
271 e.printStackTrace();
272 throw new StatusException("Couldn't get data pilot field", e);
275 log.println("Creating object - " +
276 ((oObj == null) ? "FAILED" : "OK"));
278 XDataPilotField xDataPilotField = UnoRuntime.queryInterface(
279 XDataPilotField.class, oObj);
281 oObj = xDataPilotField.getItems();
283 TestEnvironment tEnv = new TestEnvironment(oObj);
285 log.println("Implementationname: " + util.utils.getImplName(oObj));
287 // Other parameters required for interface tests
288 return tEnv;
291 private void getSRange(XIndexAccess IA) {
292 int fieldsAmount = IA.getCount() + 1;
294 String[] fieldsNames = new String[fieldsAmount];
296 int i = -1;
297 int cnt = 0;
299 while ((++i) < fieldsAmount) {
300 Object field;
302 try {
303 field = IA.getByIndex(i);
304 } catch (com.sun.star.lang.WrappedTargetException e) {
305 e.printStackTrace(log);
307 return;
308 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
309 e.printStackTrace(log);
311 return;
314 XNamed named = UnoRuntime.queryInterface(XNamed.class,
315 field);
316 String name = named.getName();
318 log.println("**Field : '" + name + "' ... ");
320 if (!name.equals("Data")) {
321 fieldsNames[cnt] = name;
323 XPropertySet props = UnoRuntime.queryInterface(
324 XPropertySet.class, field);
326 try {
327 switch (cnt % 5) {
328 case 0:
329 props.setPropertyValue("Orientation",
330 DataPilotFieldOrientation.COLUMN);
331 log.println(" Column");
333 break;
335 case 1:
336 props.setPropertyValue("Orientation",
337 DataPilotFieldOrientation.ROW);
338 log.println(" Row");
340 break;
342 case 2:
343 props.setPropertyValue("Orientation",
344 DataPilotFieldOrientation.DATA);
345 log.println(" Data");
347 break;
349 case 3:
350 props.setPropertyValue("Orientation",
351 DataPilotFieldOrientation.HIDDEN);
352 log.println(" Hidden");
354 break;
356 case 4:
357 props.setPropertyValue("Orientation",
358 DataPilotFieldOrientation.PAGE);
359 log.println(" Page");
361 break;
363 } catch (com.sun.star.lang.WrappedTargetException e) {
364 e.printStackTrace(log);
366 return;
367 } catch (com.sun.star.lang.IllegalArgumentException e) {
368 e.printStackTrace(log);
370 return;
371 } catch (com.sun.star.beans.PropertyVetoException e) {
372 e.printStackTrace(log);
374 return;
375 } catch (com.sun.star.beans.UnknownPropertyException e) {
376 e.printStackTrace(log);
378 return;
381 if ((++cnt) > 4) {
382 break;
384 } else {
385 return;