bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScIndexEnumeration_DataPilotItemsEnumeration.java
blobf190da8f9ea00b734072ca439efe707dc7225e40
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.XEnumerationAccess;
22 import com.sun.star.container.XIndexAccess;
23 import com.sun.star.container.XNamed;
24 import com.sun.star.lang.XComponent;
25 import com.sun.star.sheet.DataPilotFieldOrientation;
26 import com.sun.star.sheet.XDataPilotDescriptor;
27 import com.sun.star.sheet.XDataPilotField;
28 import com.sun.star.sheet.XDataPilotTables;
29 import com.sun.star.sheet.XDataPilotTablesSupplier;
30 import com.sun.star.sheet.XSpreadsheet;
31 import com.sun.star.sheet.XSpreadsheetDocument;
32 import com.sun.star.sheet.XSpreadsheets;
33 import com.sun.star.table.CellAddress;
34 import com.sun.star.table.CellRangeAddress;
35 import com.sun.star.uno.AnyConverter;
36 import com.sun.star.uno.Type;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
40 import java.io.PrintWriter;
42 import lib.StatusException;
43 import lib.TestCase;
44 import lib.TestEnvironment;
45 import lib.TestParameters;
47 import util.SOfficeFactory;
50 /**
51 * Test for object which is represented by service
52 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
53 * Object implements the following interfaces :
54 * <ul>
55 * <li> <code>com::sun::star::container::XNamed</code></li>
56 * <li> <code>com::sun::star::sheet::DataPilotField</code></li>
57 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
58 * </ul>
59 * @see com.sun.star.sheet.DataPilotField
60 * @see com.sun.star.container.XNamed
61 * @see com.sun.star.sheet.DataPilotField
62 * @see com.sun.star.beans.XPropertySet
63 * @see ifc.container._XNamed
64 * @see ifc.sheet._DataPilotField
65 * @see ifc.beans._XPropertySet
67 public class ScIndexEnumeration_DataPilotItemsEnumeration
68 extends TestCase {
69 private XSpreadsheetDocument xSheetDoc = null;
71 /**
72 * A field is filled some values. This integer determines the size of the
73 * field in x and y direction.
75 private static final int mMaxFieldIndex = 6;
77 /**
78 * Creates Spreadsheet document.
80 @Override
81 protected void initialize(TestParameters tParam, PrintWriter log) {
82 SOfficeFactory SOF = SOfficeFactory.getFactory(
83 tParam.getMSF());
85 try {
86 log.println("creating a Spreadsheet document");
87 xSheetDoc = SOF.createCalcDoc(null);
88 } catch (com.sun.star.uno.Exception e) {
89 // Some exception occurs.FAILED
90 e.printStackTrace(log);
91 throw new StatusException("Couldn't create document", e);
95 /**
96 * Disposes Spreadsheet document.
98 @Override
99 protected void cleanup(TestParameters tParam, PrintWriter log) {
100 log.println(" disposing xSheetDoc ");
102 XComponent oComp = UnoRuntime.queryInterface(
103 XComponent.class, xSheetDoc);
104 util.DesktopTools.closeDoc(oComp);
108 * Creating a Testenvironment for the interfaces to be tested.
109 * Retrieves a collection of spreadsheets from a document
110 * and takes one of them. Fills some table in the spreadsheet.
111 * Obtains the collection of data pilot tables using the interface
112 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
113 * for the filled table and inserts new data pilot table with this descriptor
114 * to the collection. Obtains the collection of all the data pilot fields
115 * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
116 * the collection the data pilot field with index 0. This data pilot field
117 * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
118 * @see com.sun.star.sheet.DataPilotField
119 * @see com.sun.star.sheet.XDataPilotTablesSupplier
120 * @see com.sun.star.sheet.XDataPilotDescriptor
122 @Override
123 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
124 PrintWriter log) {
125 XInterface oObj = null;
128 // creation of testobject here
129 // first we write what we are intend to do to log file
130 log.println("Creating a test environment");
132 // the cell range
133 CellRangeAddress sCellRangeAdress = new CellRangeAddress();
134 sCellRangeAdress.Sheet = 0;
135 sCellRangeAdress.StartColumn = 1;
136 sCellRangeAdress.StartRow = 0;
137 sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
138 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
140 // position of the data pilot table
141 CellAddress sCellAdress = new CellAddress();
142 sCellAdress.Sheet = 0;
143 sCellAdress.Column = 7;
144 sCellAdress.Row = 8;
146 log.println("Getting a sheet");
148 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
149 XSpreadsheet oSheet = null;
150 XSpreadsheet oSheet2 = null;
151 XIndexAccess oIndexAccess = UnoRuntime.queryInterface(
152 XIndexAccess.class, xSpreadsheets);
153 // Make sure there are at least two sheets
154 xSpreadsheets.insertNewByName("Some Sheet", (short)0);
156 try {
157 oSheet = (XSpreadsheet) AnyConverter.toObject(
158 new Type(XSpreadsheet.class),
159 oIndexAccess.getByIndex(0));
160 oSheet2 = (XSpreadsheet) AnyConverter.toObject(
161 new Type(XSpreadsheet.class),
162 oIndexAccess.getByIndex(1));
163 } catch (com.sun.star.lang.WrappedTargetException e) {
164 e.printStackTrace();
165 throw new StatusException("Couldn't get a spreadsheet", e);
166 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
167 e.printStackTrace();
168 throw new StatusException("Couldn't get a spreadsheet", e);
169 } catch (com.sun.star.lang.IllegalArgumentException e) {
170 e.printStackTrace();
171 throw new StatusException("Couldn't get a spreadsheet", e);
174 try {
175 log.println("Filling a table");
177 for (int i = 1; i < mMaxFieldIndex; i++) {
178 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
179 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
180 oSheet2.getCellByPosition(i, 0).setFormula("Col" + i);
181 oSheet2.getCellByPosition(0, i).setFormula("Row" + i);
184 for (int i = 1; i < mMaxFieldIndex; i++)
185 for (int j = 1; j < mMaxFieldIndex; j++) {
186 oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
187 oSheet2.getCellByPosition(i, j).setValue(i * (j + 2));
189 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
190 e.printStackTrace();
191 throw new StatusException("Couldn't fill some cells", e);
194 try {
195 oSheet.getCellByPosition(1, 5);
197 int x = sCellAdress.Column;
198 int y = sCellAdress.Row + 3;
201 oSheet.getCellByPosition(x, y);
202 Integer.valueOf(27);
203 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
204 e.printStackTrace();
205 throw new StatusException("Couldn't get cells for changing.", e);
209 // create the test objects
210 log.println("Getting test objects");
212 XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface(
213 XDataPilotTablesSupplier.class,
214 oSheet);
215 XDataPilotTables DPT = DPTS.getDataPilotTables();
216 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
217 DPDsc.setSourceRange(sCellRangeAdress);
219 XPropertySet fieldPropSet = null;
221 try {
222 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
223 fieldPropSet = UnoRuntime.queryInterface(
224 XPropertySet.class, oDataPilotField);
225 } catch (com.sun.star.lang.WrappedTargetException e) {
226 e.printStackTrace();
227 throw new StatusException("Couldn't create a test environment", e);
228 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
229 e.printStackTrace();
230 throw new StatusException("Couldn't create a test environment", e);
233 try {
234 fieldPropSet.setPropertyValue("Function",
235 com.sun.star.sheet.GeneralFunction.SUM);
236 fieldPropSet.setPropertyValue("Orientation",
237 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
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.IllegalArgumentException e) {
242 e.printStackTrace();
243 throw new StatusException("Couldn't create a test environment", e);
244 } catch (com.sun.star.beans.PropertyVetoException e) {
245 e.printStackTrace();
246 throw new StatusException("Couldn't create a test environment", e);
247 } catch (com.sun.star.beans.UnknownPropertyException e) {
248 e.printStackTrace();
249 throw new StatusException("Couldn't create a test environment", e);
252 log.println("Insert the DataPilotTable");
254 if (DPT.hasByName("DataPilotTable")) {
255 DPT.removeByName("DataPilotTable");
258 XIndexAccess IA = DPDsc.getDataPilotFields();
259 getSRange(IA);
261 DPT.insertNewByName("DataPilotTable", sCellAdress, DPDsc);
263 try {
264 oObj = (XInterface) AnyConverter.toObject(
265 new Type(XInterface.class), IA.getByIndex(0));
266 } catch (com.sun.star.lang.WrappedTargetException e) {
267 e.printStackTrace();
268 throw new StatusException("Couldn't get data pilot field", e);
269 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
270 e.printStackTrace();
271 throw new StatusException("Couldn't get data pilot field", e);
272 } catch (com.sun.star.lang.IllegalArgumentException e) {
273 e.printStackTrace();
274 throw new StatusException("Couldn't get data pilot field", e);
277 log.println("Creating object - " +
278 ((oObj == null) ? "FAILED" : "OK"));
280 XDataPilotField xDataPilotField = UnoRuntime.queryInterface(
281 XDataPilotField.class, oObj);
283 XEnumerationAccess xEnumerationAccess = UnoRuntime.queryInterface(
284 XEnumerationAccess.class,
285 xDataPilotField.getItems());
287 oObj = xEnumerationAccess.createEnumeration();
289 TestEnvironment tEnv = new TestEnvironment(oObj);
291 log.println("Implementationname: " + util.utils.getImplName(oObj));
293 // Other parameters required for interface tests
294 return tEnv;
297 private void getSRange(XIndexAccess IA) {
298 int fieldsAmount = IA.getCount() + 1;
300 String[] fieldsNames = new String[fieldsAmount];
302 int i = -1;
303 int cnt = 0;
305 while ((++i) < fieldsAmount) {
306 Object field;
308 try {
309 field = IA.getByIndex(i);
310 } catch (com.sun.star.lang.WrappedTargetException e) {
311 e.printStackTrace(log);
313 return;
314 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
315 e.printStackTrace(log);
317 return;
320 XNamed named = UnoRuntime.queryInterface(XNamed.class,
321 field);
322 String name = named.getName();
324 log.println("**Field : '" + name + "' ... ");
326 if (!name.equals("Data")) {
327 fieldsNames[cnt] = name;
329 XPropertySet props = UnoRuntime.queryInterface(
330 XPropertySet.class, field);
332 try {
333 switch (cnt % 5) {
334 case 0:
335 props.setPropertyValue("Orientation",
336 DataPilotFieldOrientation.COLUMN);
337 log.println(" Column");
339 break;
341 case 1:
342 props.setPropertyValue("Orientation",
343 DataPilotFieldOrientation.ROW);
344 log.println(" Row");
346 break;
348 case 2:
349 props.setPropertyValue("Orientation",
350 DataPilotFieldOrientation.DATA);
351 log.println(" Data");
353 break;
355 case 3:
356 props.setPropertyValue("Orientation",
357 DataPilotFieldOrientation.HIDDEN);
358 log.println(" Hidden");
360 break;
362 case 4:
363 props.setPropertyValue("Orientation",
364 DataPilotFieldOrientation.PAGE);
365 log.println(" Page");
367 break;
369 } catch (com.sun.star.lang.WrappedTargetException e) {
370 e.printStackTrace(log);
372 return;
373 } catch (com.sun.star.lang.IllegalArgumentException e) {
374 e.printStackTrace(log);
376 return;
377 } catch (com.sun.star.beans.PropertyVetoException e) {
378 e.printStackTrace(log);
380 return;
381 } catch (com.sun.star.beans.UnknownPropertyException e) {
382 e.printStackTrace(log);
384 return;
387 if ((++cnt) > 4) {
388 break;
390 } else {
391 return;