bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScIndexEnumeration_DataPilotItemsEnumeration.java
blob22f0044e55a89f323b7946cf67c62706f3728fc7
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.lang.XMultiServiceFactory;
26 import com.sun.star.sheet.DataPilotFieldOrientation;
27 import com.sun.star.sheet.XDataPilotDescriptor;
28 import com.sun.star.sheet.XDataPilotField;
29 import com.sun.star.sheet.XDataPilotTables;
30 import com.sun.star.sheet.XDataPilotTablesSupplier;
31 import com.sun.star.sheet.XSpreadsheet;
32 import com.sun.star.sheet.XSpreadsheetDocument;
33 import com.sun.star.sheet.XSpreadsheets;
34 import com.sun.star.table.CellAddress;
35 import com.sun.star.table.CellRangeAddress;
36 import com.sun.star.uno.AnyConverter;
37 import com.sun.star.uno.Type;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
41 import java.io.PrintWriter;
43 import lib.StatusException;
44 import lib.TestCase;
45 import lib.TestEnvironment;
46 import lib.TestParameters;
48 import util.SOfficeFactory;
51 /**
52 * Test for object which is represented by service
53 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
54 * Object implements the following interfaces :
55 * <ul>
56 * <li> <code>com::sun::star::container::XNamed</code></li>
57 * <li> <code>com::sun::star::sheet::DataPilotField</code></li>
58 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
59 * </ul>
60 * @see com.sun.star.sheet.DataPilotField
61 * @see com.sun.star.container.XNamed
62 * @see com.sun.star.sheet.DataPilotField
63 * @see com.sun.star.beans.XPropertySet
64 * @see ifc.container._XNamed
65 * @see ifc.sheet._DataPilotField
66 * @see ifc.beans._XPropertySet
68 public class ScIndexEnumeration_DataPilotItemsEnumeration
69 extends TestCase {
70 private XSpreadsheetDocument xSheetDoc = null;
72 /**
73 * A field is filled some values. This integer determines the size of the
74 * field in x and y direction.
76 private int mMaxFieldIndex = 6;
78 /**
79 * Creates Spreadsheet document.
81 protected void initialize(TestParameters tParam, PrintWriter log) {
82 SOfficeFactory SOF = SOfficeFactory.getFactory(
83 (XMultiServiceFactory) 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 protected void cleanup(TestParameters tParam, PrintWriter log) {
99 log.println(" disposing xSheetDoc ");
101 XComponent oComp = UnoRuntime.queryInterface(
102 XComponent.class, xSheetDoc);
103 util.DesktopTools.closeDoc(oComp);
107 * Creating a Testenvironment for the interfaces to be tested.
108 * Retrieves a collection of spreadsheets from a document
109 * and takes one of them. Fills some table in the spreadsheet.
110 * Obtains the collection of data pilot tables using the interface
111 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
112 * for the filled table and inserts new data pilot table with this descriptor
113 * to the collection. Obtains the collection of all the data pilot fields
114 * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
115 * the collection the data pilot field with index 0. This data pilot field
116 * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
117 * @see com.sun.star.sheet.DataPilotField
118 * @see com.sun.star.sheet.XDataPilotTablesSupplier
119 * @see com.sun.star.sheet.XDataPilotDescriptor
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 new Integer(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 XEnumerationAccess xEnumerationAccess = UnoRuntime.queryInterface(
282 XEnumerationAccess.class,
283 xDataPilotField.getItems());
285 oObj = xEnumerationAccess.createEnumeration();
287 TestEnvironment tEnv = new TestEnvironment(oObj);
289 log.println("Implementationname: " + util.utils.getImplName(oObj));
291 // Other parameters required for interface tests
292 return tEnv;
295 private void getSRange(XIndexAccess IA) {
296 int fieldsAmount = IA.getCount() + 1;
298 String[] fieldsNames = new String[fieldsAmount];
300 int i = -1;
301 int cnt = 0;
303 while ((++i) < fieldsAmount) {
304 Object field;
306 try {
307 field = IA.getByIndex(i);
308 } catch (com.sun.star.lang.WrappedTargetException e) {
309 e.printStackTrace(log);
311 return;
312 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
313 e.printStackTrace(log);
315 return;
318 XNamed named = UnoRuntime.queryInterface(XNamed.class,
319 field);
320 String name = named.getName();
322 log.println("**Field : '" + name + "' ... ");
324 if (!name.equals("Data")) {
325 fieldsNames[cnt] = name;
327 XPropertySet props = UnoRuntime.queryInterface(
328 XPropertySet.class, field);
330 try {
331 switch (cnt % 5) {
332 case 0:
333 props.setPropertyValue("Orientation",
334 DataPilotFieldOrientation.COLUMN);
335 log.println(" Column");
337 break;
339 case 1:
340 props.setPropertyValue("Orientation",
341 DataPilotFieldOrientation.ROW);
342 log.println(" Row");
344 break;
346 case 2:
347 props.setPropertyValue("Orientation",
348 DataPilotFieldOrientation.DATA);
349 log.println(" Data");
351 break;
353 case 3:
354 props.setPropertyValue("Orientation",
355 DataPilotFieldOrientation.HIDDEN);
356 log.println(" Hidden");
358 break;
360 case 4:
361 props.setPropertyValue("Orientation",
362 DataPilotFieldOrientation.PAGE);
363 log.println(" Page");
365 break;
367 } catch (com.sun.star.lang.WrappedTargetException e) {
368 e.printStackTrace(log);
370 return;
371 } catch (com.sun.star.lang.IllegalArgumentException e) {
372 e.printStackTrace(log);
374 return;
375 } catch (com.sun.star.beans.PropertyVetoException e) {
376 e.printStackTrace(log);
378 return;
379 } catch (com.sun.star.beans.UnknownPropertyException e) {
380 e.printStackTrace(log);
382 return;
385 if ((++cnt) > 4) {
386 break;
388 } else {
389 return;