Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotTableObj.java
blob03677927043f3e187a48f26152e55e0c21030df6
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 .
19 package mod._sc;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.container.XIndexAccess;
31 import com.sun.star.lang.XComponent;
32 import com.sun.star.sheet.TableFilterField;
33 import com.sun.star.sheet.XDataPilotDescriptor;
34 import com.sun.star.sheet.XDataPilotTables;
35 import com.sun.star.sheet.XDataPilotTablesSupplier;
36 import com.sun.star.sheet.XSheetFilterDescriptor;
37 import com.sun.star.sheet.XSpreadsheet;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.sheet.XSpreadsheets;
40 import com.sun.star.table.CellAddress;
41 import com.sun.star.table.CellRangeAddress;
42 import com.sun.star.uno.AnyConverter;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
47 /**
48 * Test for object which is represented by service
49 * <code>com.sun.star.sheet.DataPilotTable</code>. <p>
50 * Object implements the following interfaces :
51 * <ul>
52 * <li> <code>com::sun::star::container::XNamed</code></li>
53 * <li> <code>com::sun::star::sheet::XDataPilotTable</code></li>
54 * <li> <code>com::sun::star::sheet::XDataPilotDescriptor</code></li>
55 * </ul>
56 * @see com.sun.star.sheet.DataPilotTable
57 * @see com.sun.star.container.XNamed
58 * @see com.sun.star.sheet.XDataPilotTable
59 * @see com.sun.star.sheet.XDataPilotDescriptor
60 * @see ifc.container._XNamed
61 * @see ifc.sheet._XDataPilotTable
62 * @see ifc.sheet._XDataPilotDescriptor
64 public class ScDataPilotTableObj extends TestCase {
65 private XSpreadsheetDocument xSheetDoc = null;
67 /**
68 * Creates Spreadsheet document.
70 @Override
71 protected void initialize( TestParameters tParam, PrintWriter log ) {
72 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
74 try {
75 log.println( "creating a Spreadsheet document" );
76 xSheetDoc = SOF.createCalcDoc(null);
77 } catch ( com.sun.star.uno.Exception e ) {
78 // Some exception occurs.FAILED
79 e.printStackTrace( log );
80 throw new StatusException( "Couldn't create document", e );
85 /**
86 * Disposes Spreadsheet document.
88 @Override
89 protected void cleanup( TestParameters tParam, PrintWriter log ) {
90 log.println( " disposing xSheetDoc " );
91 XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ;
92 util.DesktopTools.closeDoc(oComp);
95 /**
96 * Creating a Testenvironment for the interfaces to be tested.
97 * Retrieves a collection of spreadsheets from a document
98 * and takes one of them. Fills some table in the spreadsheet.
99 * Obtains the collection of data pilot tables using the interface
100 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
101 * for the filled table. Obtains the collection of all the data pilot fields
102 * using the interface <code>XDataPilotDescriptor</code>. Sets field
103 * orientation for every of the data pilot fields. Sets the definitions of
104 * the filter fields for the created description. Inserts new data pilot
105 * table with this descriptor to the collection.
106 * This new data pilot table is the instance of the service
107 * <code>com.sun.star.sheet.DataPilotTable</code>.
108 * Object relations created :
109 * <ul>
110 * <li> <code>'OUTPUTRANGE'</code> for
111 * {@link ifc.sheet._XDataPilotTable}(the cell range address of the
112 * created data pilot table) </li>
113 * <li> <code>'FIELDSAMOUNT'</code> for
114 * {@link ifc.sheet._XDataPilotDescriptor}(the number of filled fields
115 * which descriptor was created for) </li>
116 * <li> <code>'CELLFORCHANGE'</code> for
117 * {@link ifc.sheet._XDataPilotTable}(value of this cell will be changed)</li>
118 * <li> <code>'CELLFORCHECK'</code> for
119 * {@link ifc.sheet._XDataPilotTable}(value of this cell must be changed
120 * after refresh call)</li>
121 * </ul>
122 * @see com.sun.star.sheet.DataPilotTable
123 * @see com.sun.star.sheet.XDataPilotTablesSupplier
124 * @see com.sun.star.sheet.XDataPilotDescriptor
126 @Override
127 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
129 XInterface oObj = null;
131 CellAddress sCellAddress = new CellAddress();
132 sCellAddress.Sheet = 0;
133 sCellAddress.Column = 7;
134 sCellAddress.Row = 8;
136 // creation of testobject here
137 // first we write what we are intend to do to log file
138 log.println( "Creating a test environment" );
139 log.println("getting sheets");
140 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
141 XIndexAccess oIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
142 XSpreadsheet oSheet = null;
143 Object oChangeCell = null;
144 Object oCheckCell = null;
145 try {
146 oSheet = (XSpreadsheet) AnyConverter.toObject(
147 new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
148 oChangeCell = oSheet.getCellByPosition(1, 5);
149 oCheckCell = oSheet.getCellByPosition(
150 sCellAddress.Column, sCellAddress.Row + 3);
151 } catch (com.sun.star.lang.WrappedTargetException e) {
152 e.printStackTrace(log);
153 throw new StatusException( "Couldn't get a spreadsheet", e);
154 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
155 e.printStackTrace(log);
156 throw new StatusException( "Couldn't get a spreadsheet", e);
157 } catch (com.sun.star.lang.IllegalArgumentException e) {
158 e.printStackTrace(log);
159 throw new StatusException( "Couldn't get a spreadsheet", e);
161 try {
162 log.println("Filing a table");
163 for (int i = 1; i < 6; i++) {
164 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
165 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
168 for (int i = 1; i < 6; i++)
169 for (int j = 1; j < 6; j++) {
170 oSheet.getCellByPosition(i, j).setValue(2.5 * j + i);
172 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
173 e.printStackTrace(log);
174 throw new StatusException("Couldn't fill some cells", e);
177 CellRangeAddress sCellRangeAddress = new CellRangeAddress();
178 sCellRangeAddress.Sheet = 0;
179 sCellRangeAddress.StartColumn = 1;
180 sCellRangeAddress.StartRow = 0;
181 sCellRangeAddress.EndColumn = 5;
182 sCellRangeAddress.EndRow = 5;
184 TableFilterField[] filterFields = new TableFilterField[2];
185 filterFields[0] = new TableFilterField();
186 filterFields[0].Connection = com.sun.star.sheet.FilterConnection.AND;
187 filterFields[0].Field = 1;
188 filterFields[0].IsNumeric = true;
189 filterFields[0].NumericValue = 4;
190 filterFields[0].Operator = com.sun.star.sheet.FilterOperator.GREATER;
191 filterFields[1] = new TableFilterField();
192 filterFields[1].Connection = com.sun.star.sheet.FilterConnection.AND;
193 filterFields[1].Field = 1;
194 filterFields[1].IsNumeric = true;
195 filterFields[1].NumericValue = 12;
196 filterFields[1].Operator = com.sun.star.sheet.FilterOperator.LESS_EQUAL;
198 XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
199 log.println("Getting test object");
200 XDataPilotTables DPT = DPTS.getDataPilotTables();
201 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
202 DPDsc.setSourceRange(sCellRangeAddress);
204 XSheetFilterDescriptor SFD = DPDsc.getFilterDescriptor();
205 SFD.setFilterFields(filterFields);
207 XPropertySet fieldPropSet = null;
208 try {
209 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
210 fieldPropSet = UnoRuntime.queryInterface(XPropertySet.class, oDataPilotField);
211 } catch (com.sun.star.lang.WrappedTargetException e) {
212 e.printStackTrace(log);
213 throw new StatusException("Couldn't create a test environment", e);
214 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
215 e.printStackTrace(log);
216 throw new StatusException("Couldn't create a test environment", e);
219 try {
220 fieldPropSet.setPropertyValue("Function",
221 com.sun.star.sheet.GeneralFunction.SUM);
222 fieldPropSet.setPropertyValue("Orientation",
223 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
224 } catch(com.sun.star.lang.WrappedTargetException e) {
225 e.printStackTrace(log);
226 throw new StatusException("Couldn't create a test environment", e);
227 } catch(com.sun.star.lang.IllegalArgumentException e) {
228 e.printStackTrace(log);
229 throw new StatusException("Couldn't create a test environment", e);
230 } catch(com.sun.star.beans.PropertyVetoException e) {
231 e.printStackTrace(log);
232 throw new StatusException("Couldn't create a test environment", e);
233 } catch(com.sun.star.beans.UnknownPropertyException e) {
234 e.printStackTrace(log);
235 throw new StatusException("Couldn't create a test environment", e);
238 if (DPT.hasByName("DataPilotTable")) {
239 DPT.removeByName("DataPilotTable");
241 DPT.insertNewByName("DataPilotTable", sCellAddress, DPDsc);
242 try {
243 oObj = (XInterface) AnyConverter.toObject(
244 new Type(XInterface.class),DPT.getByName(DPT.getElementNames()[0]));
245 } catch (com.sun.star.lang.WrappedTargetException e) {
246 e.printStackTrace(log);
247 throw new StatusException("Couldn't create a test environment", e);
248 } catch (com.sun.star.container.NoSuchElementException e) {
249 e.printStackTrace(log);
250 throw new StatusException("Couldn't create a test environment", e);
251 } catch (com.sun.star.lang.IllegalArgumentException e) {
252 e.printStackTrace(log);
253 throw new StatusException("Couldn't create a test environment", e);
256 log.println("Creating object - " +
257 ((oObj == null) ? "FAILED" : "OK"));
259 TestEnvironment tEnv = new TestEnvironment( oObj );
261 // Other parameters required for interface tests
262 tEnv.addObjRelation("OUTPUTRANGE", sCellAddress);
263 tEnv.addObjRelation("CELLFORCHANGE", oChangeCell);
264 tEnv.addObjRelation("CELLFORCHECK", oCheckCell);
265 tEnv.addObjRelation("FIELDSAMOUNT", Integer.valueOf(5));
266 tEnv.addObjRelation("SHEETDOCUMENT", xSheetDoc);
268 createTable2(oSheet, sCellRangeAddress, tEnv);
270 return tEnv;
274 * Create a new DataPilot table output for use with testing XDataPilotTable2
275 * interface.
277 * @param oSheet current sheet instance
278 * @param srcRange source range
279 * @param tEnv test environment instance
281 private void createTable2(XSpreadsheet oSheet, CellRangeAddress srcRange, TestEnvironment tEnv)
283 XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
284 log.println("Creating test table object");
285 XDataPilotTables DPT = DPTS.getDataPilotTables();
286 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
287 DPDsc.setSourceRange(srcRange);
289 XIndexAccess xIA = DPDsc.getDataPilotFields();
290 int fieldCount = xIA.getCount() - 1; // skip the last field because it's always hidden.
293 for (int i = 0; i < fieldCount; ++i)
295 Object o = xIA.getByIndex(i);
296 XPropertySet fieldPropSet = UnoRuntime.queryInterface(
297 XPropertySet.class, o);
299 if (i == fieldCount - 1)
301 // last field
302 fieldPropSet.setPropertyValue(
303 "Function", com.sun.star.sheet.GeneralFunction.SUM);
304 fieldPropSet.setPropertyValue(
305 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.DATA);
307 else if (i%2 == 0)
309 // even number fields
310 fieldPropSet.setPropertyValue(
311 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.COLUMN);
313 else if (i%2 == 1)
315 // odd number fields
316 fieldPropSet.setPropertyValue(
317 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.ROW);
321 if (DPT.hasByName("DataPilotTable2"))
322 DPT.removeByName("DataPilotTable2");
324 CellAddress destAddr = new CellAddress();
325 destAddr.Sheet = 0;
326 destAddr.Column = 0;
327 destAddr.Row = 14;
328 DPT.insertNewByName("DataPilotTable2", destAddr, DPDsc);
330 Object o = DPT.getByName("DataPilotTable2");
331 tEnv.addObjRelation("DATAPILOTTABLE2", o);
333 catch (com.sun.star.uno.Exception e)
335 e.printStackTrace(log);
336 throw new StatusException("Couldn't create a test environment", e);