Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotFieldObj.java
blob89164816c6c051ecb1b317f9396d5e358cd37d87
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.XDataPilotTables;
27 import com.sun.star.sheet.XDataPilotTablesSupplier;
28 import com.sun.star.sheet.XSpreadsheet;
29 import com.sun.star.sheet.XSpreadsheetDocument;
30 import com.sun.star.sheet.XSpreadsheets;
31 import com.sun.star.table.CellAddress;
32 import com.sun.star.table.CellRangeAddress;
33 import com.sun.star.uno.AnyConverter;
34 import com.sun.star.uno.Type;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
38 import java.io.PrintWriter;
40 import lib.StatusException;
41 import lib.TestCase;
42 import lib.TestEnvironment;
43 import lib.TestParameters;
45 import util.SOfficeFactory;
48 /**
49 * Test for object which is represented by service
50 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
51 * Object implements the following interfaces :
52 * <ul>
53 * <li> <code>com::sun::star::container::XNamed</code></li>
54 * <li> <code>com::sun::star::sheet::DataPilotField</code></li>
55 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
56 * </ul>
57 * @see com.sun.star.sheet.DataPilotField
58 * @see com.sun.star.container.XNamed
59 * @see com.sun.star.sheet.DataPilotField
60 * @see com.sun.star.beans.XPropertySet
61 * @see ifc.container._XNamed
62 * @see ifc.sheet._DataPilotField
63 * @see ifc.beans._XPropertySet
65 public class ScDataPilotFieldObj extends TestCase {
66 private XSpreadsheetDocument xSheetDoc = null;
68 /**
69 * A field is filled some values. This integer determines the size of the
70 * field in x and y direction.
72 private static final int mMaxFieldIndex = 6;
74 /**
75 * Creates Spreadsheet document.
77 @Override
78 protected void initialize(TestParameters tParam, PrintWriter log) {
79 SOfficeFactory SOF = SOfficeFactory.getFactory(
80 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 @Override
96 protected void cleanup(TestParameters tParam, PrintWriter log) {
97 log.println(" disposing xSheetDoc ");
99 XComponent oComp = UnoRuntime.queryInterface(
100 XComponent.class, xSheetDoc);
101 util.DesktopTools.closeDoc(oComp);
105 * Creating a Testenvironment for the interfaces to be tested.
106 * Retrieves a collection of spreadsheets from a document
107 * and takes one of them. Fills some table in the spreadsheet.
108 * Obtains the collection of data pilot tables using the interface
109 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
110 * for the filled table and inserts new data pilot table with this descriptor
111 * to the collection. Obtains the collection of all the data pilot fields
112 * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
113 * the collection the data pilot field with index 0. This data pilot field
114 * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
115 * @see com.sun.star.sheet.DataPilotField
116 * @see com.sun.star.sheet.XDataPilotTablesSupplier
117 * @see com.sun.star.sheet.XDataPilotDescriptor
119 @Override
120 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
121 PrintWriter log) {
122 XInterface oObj = null;
125 // creation of testobject here
126 // first we write what we are intend to do to log file
127 log.println("Creating a test environment");
129 // the cell range
130 CellRangeAddress sCellRangeAdress = new CellRangeAddress();
131 sCellRangeAdress.Sheet = 0;
132 sCellRangeAdress.StartColumn = 1;
133 sCellRangeAdress.StartRow = 0;
134 sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
135 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
137 // position of the data pilot table
138 CellAddress sCellAdress = new CellAddress();
139 sCellAdress.Sheet = 0;
140 sCellAdress.Column = 7;
141 sCellAdress.Row = 8;
143 log.println("Getting a sheet");
145 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
146 XSpreadsheet oSheet = null;
147 XSpreadsheet oSheet2 = null;
148 XIndexAccess oIndexAccess = UnoRuntime.queryInterface(
149 XIndexAccess.class, xSpreadsheets);
150 // Make sure there are at least two sheets
151 xSpreadsheets.insertNewByName("Some Sheet", (short)0);
153 try {
154 oSheet = (XSpreadsheet) AnyConverter.toObject(
155 new Type(XSpreadsheet.class),
156 oIndexAccess.getByIndex(0));
157 oSheet2 = (XSpreadsheet) AnyConverter.toObject(
158 new Type(XSpreadsheet.class),
159 oIndexAccess.getByIndex(1));
160 } catch (com.sun.star.lang.WrappedTargetException e) {
161 e.printStackTrace();
162 throw new StatusException("Couldn't get a spreadsheet", e);
163 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
164 e.printStackTrace();
165 throw new StatusException("Couldn't get a spreadsheet", e);
166 } catch (com.sun.star.lang.IllegalArgumentException e) {
167 e.printStackTrace();
168 throw new StatusException("Couldn't get a spreadsheet", e);
171 try {
172 log.println("Filling a table");
174 for (int i = 1; i < mMaxFieldIndex; i++) {
175 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
176 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
177 oSheet2.getCellByPosition(i, 0).setFormula("Col" + i);
178 oSheet2.getCellByPosition(0, i).setFormula("Row" + i);
181 for (int i = 1; i < mMaxFieldIndex; i++)
182 for (int j = 1; j < mMaxFieldIndex; j++) {
183 oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
184 oSheet2.getCellByPosition(i, j).setValue(i * (j + 2));
186 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
187 e.printStackTrace();
188 throw new StatusException("Couldn't fill some cells", e);
191 try {
192 oSheet.getCellByPosition(1, 5);
194 int x = sCellAdress.Column;
195 int y = sCellAdress.Row + 3;
198 oSheet.getCellByPosition(x, y);
199 Integer.valueOf(27);
200 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
201 e.printStackTrace();
202 throw new StatusException("Couldn't get cells for changing.", e);
206 // create the test objects
207 log.println("Getting test objects");
209 XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface(
210 XDataPilotTablesSupplier.class,
211 oSheet);
212 XDataPilotTables DPT = DPTS.getDataPilotTables();
213 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
214 DPDsc.setSourceRange(sCellRangeAdress);
216 XPropertySet fieldPropSet = null;
218 try {
219 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
220 fieldPropSet = UnoRuntime.queryInterface(
221 XPropertySet.class, oDataPilotField);
222 } catch (com.sun.star.lang.WrappedTargetException e) {
223 e.printStackTrace();
224 throw new StatusException("Couldn't create a test environment", e);
225 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
226 e.printStackTrace();
227 throw new StatusException("Couldn't create a test environment", e);
230 try {
231 fieldPropSet.setPropertyValue("Function",
232 com.sun.star.sheet.GeneralFunction.SUM);
233 fieldPropSet.setPropertyValue("Orientation",
234 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
235 } catch (com.sun.star.lang.WrappedTargetException e) {
236 e.printStackTrace();
237 throw new StatusException("Couldn't create a test environment", e);
238 } catch (com.sun.star.lang.IllegalArgumentException e) {
239 e.printStackTrace();
240 throw new StatusException("Couldn't create a test environment", e);
241 } catch (com.sun.star.beans.PropertyVetoException e) {
242 e.printStackTrace();
243 throw new StatusException("Couldn't create a test environment", e);
244 } catch (com.sun.star.beans.UnknownPropertyException e) {
245 e.printStackTrace();
246 throw new StatusException("Couldn't create a test environment", e);
249 log.println("Insert the DataPilotTable");
251 if (DPT.hasByName("DataPilotTable")) {
252 DPT.removeByName("DataPilotTable");
255 XIndexAccess IA = DPDsc.getDataPilotFields();
256 getSRange(IA);
258 DPT.insertNewByName("DataPilotTable", sCellAdress, DPDsc);
260 try {
261 oObj = (XInterface) AnyConverter.toObject(
262 new Type(XInterface.class), IA.getByIndex(0));
263 } catch (com.sun.star.lang.WrappedTargetException e) {
264 e.printStackTrace();
265 throw new StatusException("Couldn't get data pilot field", e);
266 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
267 e.printStackTrace();
268 throw new StatusException("Couldn't get data pilot field", e);
269 } catch (com.sun.star.lang.IllegalArgumentException e) {
270 e.printStackTrace();
271 throw new StatusException("Couldn't get data pilot field", e);
274 log.println("Creating object - " +
275 ((oObj == null) ? "FAILED" : "OK"));
277 TestEnvironment tEnv = new TestEnvironment(oObj);
279 log.println("Implementationname: " + util.utils.getImplName(oObj));
281 // Other parameters required for interface tests
282 return tEnv;
285 private void getSRange(XIndexAccess IA) {
286 int fieldsAmount = IA.getCount() + 1;
288 String[] fieldsNames = new String[fieldsAmount];
290 int i = -1;
291 int cnt = 0;
293 while ((++i) < fieldsAmount) {
294 Object field;
296 try {
297 field = IA.getByIndex(i);
298 } catch (com.sun.star.lang.WrappedTargetException e) {
299 e.printStackTrace(log);
301 return;
302 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
303 e.printStackTrace(log);
305 return;
308 XNamed named = UnoRuntime.queryInterface(XNamed.class,
309 field);
310 String name = named.getName();
312 log.println("**Field : '" + name + "' ... ");
314 if (!name.equals("Data")) {
315 fieldsNames[cnt] = name;
317 XPropertySet props = UnoRuntime.queryInterface(
318 XPropertySet.class, field);
320 try {
321 switch (cnt % 5) {
322 case 0:
323 props.setPropertyValue("Orientation",
324 DataPilotFieldOrientation.COLUMN);
325 log.println(" Column");
327 break;
329 case 1:
330 props.setPropertyValue("Orientation",
331 DataPilotFieldOrientation.ROW);
332 log.println(" Row");
334 break;
336 case 2:
337 props.setPropertyValue("Orientation",
338 DataPilotFieldOrientation.DATA);
339 log.println(" Data");
341 break;
343 case 3:
344 props.setPropertyValue("Orientation",
345 DataPilotFieldOrientation.HIDDEN);
346 log.println(" Hidden");
348 break;
350 case 4:
351 props.setPropertyValue("Orientation",
352 DataPilotFieldOrientation.PAGE);
353 log.println(" Page");
355 break;
357 } catch (com.sun.star.lang.WrappedTargetException e) {
358 e.printStackTrace(log);
360 return;
361 } catch (com.sun.star.lang.IllegalArgumentException e) {
362 e.printStackTrace(log);
364 return;
365 } catch (com.sun.star.beans.PropertyVetoException e) {
366 e.printStackTrace(log);
368 return;
369 } catch (com.sun.star.beans.UnknownPropertyException e) {
370 e.printStackTrace(log);
372 return;
375 if ((++cnt) > 4) {
376 break;
378 } else {
379 return;