bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotTableObj.java
blobb2d83385149a0d9dc13f43ab3e1affc3418d327a
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.lang.XMultiServiceFactory;
33 import com.sun.star.sheet.TableFilterField;
34 import com.sun.star.sheet.XDataPilotDescriptor;
35 import com.sun.star.sheet.XDataPilotTables;
36 import com.sun.star.sheet.XDataPilotTablesSupplier;
37 import com.sun.star.sheet.XSheetFilterDescriptor;
38 import com.sun.star.sheet.XSpreadsheet;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.sheet.XSpreadsheets;
41 import com.sun.star.table.CellAddress;
42 import com.sun.star.table.CellRangeAddress;
43 import com.sun.star.uno.AnyConverter;
44 import com.sun.star.uno.Type;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
48 /**
49 * Test for object which is represented by service
50 * <code>com.sun.star.sheet.DataPilotTable</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::XDataPilotTable</code></li>
55 * <li> <code>com::sun::star::sheet::XDataPilotDescriptor</code></li>
56 * </ul>
57 * @see com.sun.star.sheet.DataPilotTable
58 * @see com.sun.star.container.XNamed
59 * @see com.sun.star.sheet.XDataPilotTable
60 * @see com.sun.star.sheet.XDataPilotDescriptor
61 * @see ifc.container._XNamed
62 * @see ifc.sheet._XDataPilotTable
63 * @see ifc.sheet._XDataPilotDescriptor
65 public class ScDataPilotTableObj extends TestCase {
66 private XSpreadsheetDocument xSheetDoc = null;
68 /**
69 * Creates Spreadsheet document.
71 protected void initialize( TestParameters tParam, PrintWriter log ) {
72 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)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 protected void cleanup( TestParameters tParam, PrintWriter log ) {
89 log.println( " disposing xSheetDoc " );
90 XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ;
91 util.DesktopTools.closeDoc(oComp);
94 /**
95 * Creating a Testenvironment for the interfaces to be tested.
96 * Retrieves a collection of spreadsheets from a document
97 * and takes one of them. Fills some table in the spreadsheet.
98 * Obtains the collection of data pilot tables using the interface
99 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
100 * for the filled table. Obtains the collection of all the data pilot fields
101 * using the interface <code>XDataPilotDescriptor</code>. Sets field
102 * orientation for every of the data pilot fields. Sets the definitions of
103 * the filter fields for the created description. Inserts new data pilot
104 * table with this descriptor to the collection.
105 * This new data pilot table is the instance of the service
106 * <code>com.sun.star.sheet.DataPilotTable</code>.
107 * Object relations created :
108 * <ul>
109 * <li> <code>'OUTPUTRANGE'</code> for
110 * {@link ifc.sheet._XDataPilotTable}(the cell range address of the
111 * created data pilot table) </li>
112 * <li> <code>'FIELDSAMOUNT'</code> for
113 * {@link ifc.sheet._XDataPilotDescriptor}(the number of filled fields
114 * which descriptor was created for) </li>
115 * <li> <code>'CELLFORCHANGE'</code> for
116 * {@link ifc.sheet._XDataPilotTable}(value of this cell will be changed)</li>
117 * <li> <code>'CELLFORCHECK'</code> for
118 * {@link ifc.sheet._XDataPilotTable}(value of this cell must be changed
119 * after refresh call)</li>
120 * </ul>
121 * @see com.sun.star.sheet.DataPilotTable
122 * @see com.sun.star.sheet.XDataPilotTablesSupplier
123 * @see com.sun.star.sheet.XDataPilotDescriptor
125 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
127 XInterface oObj = null;
129 CellAddress sCellAddress = new CellAddress();
130 sCellAddress.Sheet = 0;
131 sCellAddress.Column = 7;
132 sCellAddress.Row = 8;
134 // creation of testobject here
135 // first we write what we are intend to do to log file
136 log.println( "Creating a test environment" );
137 log.println("getting sheets");
138 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
139 XIndexAccess oIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
140 XSpreadsheet oSheet = null;
141 Object oChangeCell = null;
142 Object oCheckCell = null;
143 try {
144 oSheet = (XSpreadsheet) AnyConverter.toObject(
145 new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
146 oChangeCell = oSheet.getCellByPosition(1, 5);
147 oCheckCell = oSheet.getCellByPosition(
148 sCellAddress.Column, sCellAddress.Row + 3);
149 } catch (com.sun.star.lang.WrappedTargetException e) {
150 e.printStackTrace(log);
151 throw new StatusException( "Couldn't get a spreadsheet", e);
152 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
153 e.printStackTrace(log);
154 throw new StatusException( "Couldn't get a spreadsheet", e);
155 } catch (com.sun.star.lang.IllegalArgumentException e) {
156 e.printStackTrace(log);
157 throw new StatusException( "Couldn't get a spreadsheet", e);
159 log.println("Getting sheet - " + (oSheet == null ? "FAILED" : "OK"));
160 try {
161 log.println("Filing a table");
162 for (int i = 1; i < 6; i++) {
163 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
164 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
167 for (int i = 1; i < 6; i++)
168 for (int j = 1; j < 6; j++) {
169 oSheet.getCellByPosition(i, j).setValue(2.5 * j + i);
171 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
172 e.printStackTrace(log);
173 throw new StatusException("Couldn't fill some cells", e);
176 CellRangeAddress sCellRangeAddress = new CellRangeAddress();
177 sCellRangeAddress.Sheet = 0;
178 sCellRangeAddress.StartColumn = 1;
179 sCellRangeAddress.StartRow = 0;
180 sCellRangeAddress.EndColumn = 5;
181 sCellRangeAddress.EndRow = 5;
183 TableFilterField[] filterFields = new TableFilterField[2];
184 filterFields[0] = new TableFilterField();
185 filterFields[0].Connection = com.sun.star.sheet.FilterConnection.AND;
186 filterFields[0].Field = 1;
187 filterFields[0].IsNumeric = true;
188 filterFields[0].NumericValue = 4;
189 filterFields[0].Operator = com.sun.star.sheet.FilterOperator.GREATER;
190 filterFields[1] = new TableFilterField();
191 filterFields[1].Connection = com.sun.star.sheet.FilterConnection.AND;
192 filterFields[1].Field = 1;
193 filterFields[1].IsNumeric = true;
194 filterFields[1].NumericValue = 12;
195 filterFields[1].Operator = com.sun.star.sheet.FilterOperator.LESS_EQUAL;
197 XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
198 log.println("Getting test object");
199 XDataPilotTables DPT = DPTS.getDataPilotTables();
200 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
201 DPDsc.setSourceRange(sCellRangeAddress);
203 XSheetFilterDescriptor SFD = DPDsc.getFilterDescriptor();
204 SFD.setFilterFields(filterFields);
206 XPropertySet fieldPropSet = null;
207 try {
208 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
209 fieldPropSet = UnoRuntime.queryInterface(XPropertySet.class, oDataPilotField);
210 } catch (com.sun.star.lang.WrappedTargetException e) {
211 e.printStackTrace(log);
212 throw new StatusException("Couldn't create a test environment", e);
213 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
214 e.printStackTrace(log);
215 throw new StatusException("Couldn't create a test environment", e);
218 try {
219 fieldPropSet.setPropertyValue("Function",
220 com.sun.star.sheet.GeneralFunction.SUM);
221 fieldPropSet.setPropertyValue("Orientation",
222 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
223 } catch(com.sun.star.lang.WrappedTargetException e) {
224 e.printStackTrace(log);
225 throw new StatusException("Couldn't create a test environment", e);
226 } catch(com.sun.star.lang.IllegalArgumentException e) {
227 e.printStackTrace(log);
228 throw new StatusException("Couldn't create a test environment", e);
229 } catch(com.sun.star.beans.PropertyVetoException e) {
230 e.printStackTrace(log);
231 throw new StatusException("Couldn't create a test environment", e);
232 } catch(com.sun.star.beans.UnknownPropertyException e) {
233 e.printStackTrace(log);
234 throw new StatusException("Couldn't create a test environment", e);
237 if (DPT.hasByName("DataPilotTable")) {
238 DPT.removeByName("DataPilotTable");
240 DPT.insertNewByName("DataPilotTable", sCellAddress, DPDsc);
241 try {
242 oObj = (XInterface) AnyConverter.toObject(
243 new Type(XInterface.class),DPT.getByName(DPT.getElementNames()[0]));
244 } catch (com.sun.star.lang.WrappedTargetException e) {
245 e.printStackTrace(log);
246 throw new StatusException("Couldn't create a test environment", e);
247 } catch (com.sun.star.container.NoSuchElementException e) {
248 e.printStackTrace(log);
249 throw new StatusException("Couldn't create a test environment", e);
250 } catch (com.sun.star.lang.IllegalArgumentException e) {
251 e.printStackTrace(log);
252 throw new StatusException("Couldn't create a test environment", e);
255 log.println("Creating object - " +
256 ((oObj == null) ? "FAILED" : "OK"));
258 TestEnvironment tEnv = new TestEnvironment( oObj );
260 // Other parameters required for interface tests
261 tEnv.addObjRelation("OUTPUTRANGE", sCellAddress);
262 tEnv.addObjRelation("CELLFORCHANGE", oChangeCell);
263 tEnv.addObjRelation("CELLFORCHECK", oCheckCell);
264 tEnv.addObjRelation("FIELDSAMOUNT", new Integer(5));
265 tEnv.addObjRelation("SHEETDOCUMENT", xSheetDoc);
267 createTable2(oSheet, sCellRangeAddress, tEnv);
269 return tEnv;
273 * Create a new DataPilot table output for use with testing XDataPilotTable2
274 * interface.
276 * @param oSheet current sheet instance
277 * @param srcRange source range
278 * @param tEnv test environment instance
280 private void createTable2(XSpreadsheet oSheet, CellRangeAddress srcRange, TestEnvironment tEnv)
282 XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
283 log.println("Creating test table object");
284 XDataPilotTables DPT = DPTS.getDataPilotTables();
285 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
286 DPDsc.setSourceRange(srcRange);
288 XIndexAccess xIA = DPDsc.getDataPilotFields();
289 int fieldCount = xIA.getCount() - 1; // skip the last field because it's always hidden.
292 for (int i = 0; i < fieldCount; ++i)
294 Object o = xIA.getByIndex(i);
295 XPropertySet fieldPropSet = UnoRuntime.queryInterface(
296 XPropertySet.class, o);
298 if (i == fieldCount - 1)
300 // last field
301 fieldPropSet.setPropertyValue(
302 "Function", com.sun.star.sheet.GeneralFunction.SUM);
303 fieldPropSet.setPropertyValue(
304 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.DATA);
306 else if (i%2 == 0)
308 // even number fields
309 fieldPropSet.setPropertyValue(
310 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.COLUMN);
312 else if (i%2 == 1)
314 // odd number fields
315 fieldPropSet.setPropertyValue(
316 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.ROW);
320 if (DPT.hasByName("DataPilotTable2"))
321 DPT.removeByName("DataPilotTable2");
323 CellAddress destAddr = new CellAddress();
324 destAddr.Sheet = 0;
325 destAddr.Column = 0;
326 destAddr.Row = 14;
327 DPT.insertNewByName("DataPilotTable2", destAddr, DPDsc);
329 Object o = DPT.getByName("DataPilotTable2");
330 tEnv.addObjRelation("DATAPILOTTABLE2", o);
332 catch (com.sun.star.uno.Exception e)
334 e.printStackTrace(log);
335 throw new StatusException("Couldn't create a test environment", e);