Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotItemObj.java
blob1d29fede2287af428434032331c730647127e3cf
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 ScDataPilotItemObj 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 XIndexAccess xIA = xDataPilotField.getItems();
283 try {
284 oObj = UnoRuntime.queryInterface(XInterface.class,
285 xIA.getByIndex(0));
286 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
287 log.println("Couldn't get DataPilotItemObj");
288 e.printStackTrace(log);
289 } catch (com.sun.star.lang.WrappedTargetException e) {
290 log.println("Couldn't get DataPilotItemObj");
291 e.printStackTrace(log);
294 TestEnvironment tEnv = new TestEnvironment(oObj);
296 log.println("Implementationname: " + util.utils.getImplName(oObj));
298 // Other parameters required for interface tests
299 tEnv.addObjRelation("NoSetName", "ScDataPilotItemObj");
300 return tEnv;
303 private void getSRange(XIndexAccess IA) {
304 int fieldsAmount = IA.getCount() + 1;
306 String[] fieldsNames = new String[fieldsAmount];
308 int i = -1;
309 int cnt = 0;
311 while ((++i) < fieldsAmount) {
312 Object field;
314 try {
315 field = IA.getByIndex(i);
316 } catch (com.sun.star.lang.WrappedTargetException e) {
317 e.printStackTrace(log);
319 return;
320 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
321 e.printStackTrace(log);
323 return;
326 XNamed named = UnoRuntime.queryInterface(XNamed.class,
327 field);
328 String name = named.getName();
330 log.println("**Field : '" + name + "' ... ");
332 if (!name.equals("Data")) {
333 fieldsNames[cnt] = name;
335 XPropertySet props = UnoRuntime.queryInterface(
336 XPropertySet.class, field);
338 try {
339 switch (cnt % 5) {
340 case 0:
341 props.setPropertyValue("Orientation",
342 DataPilotFieldOrientation.COLUMN);
343 log.println(" Column");
345 break;
347 case 1:
348 props.setPropertyValue("Orientation",
349 DataPilotFieldOrientation.ROW);
350 log.println(" Row");
352 break;
354 case 2:
355 props.setPropertyValue("Orientation",
356 DataPilotFieldOrientation.DATA);
357 log.println(" Data");
359 break;
361 case 3:
362 props.setPropertyValue("Orientation",
363 DataPilotFieldOrientation.HIDDEN);
364 log.println(" Hidden");
366 break;
368 case 4:
369 props.setPropertyValue("Orientation",
370 DataPilotFieldOrientation.PAGE);
371 log.println(" Page");
373 break;
375 } catch (com.sun.star.lang.WrappedTargetException e) {
376 e.printStackTrace(log);
378 return;
379 } catch (com.sun.star.lang.IllegalArgumentException e) {
380 e.printStackTrace(log);
382 return;
383 } catch (com.sun.star.beans.PropertyVetoException e) {
384 e.printStackTrace(log);
386 return;
387 } catch (com.sun.star.beans.UnknownPropertyException e) {
388 e.printStackTrace(log);
390 return;
393 if ((++cnt) > 4) {
394 break;
396 } else {
397 return;