merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotTableObj.java
blobadf04855bf08ae0381af27cdb6432c9c767a04cb
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScDataPilotTableObj.java,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package mod._sc;
33 import java.io.PrintWriter;
35 import lib.StatusException;
36 import lib.TestCase;
37 import lib.TestEnvironment;
38 import lib.TestParameters;
39 import util.SOfficeFactory;
41 import com.sun.star.beans.XPropertySet;
42 import com.sun.star.container.XIndexAccess;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.sheet.TableFilterField;
46 import com.sun.star.sheet.XDataPilotDescriptor;
47 import com.sun.star.sheet.XDataPilotTables;
48 import com.sun.star.sheet.XDataPilotTablesSupplier;
49 import com.sun.star.sheet.XSheetFilterDescriptor;
50 import com.sun.star.sheet.XSpreadsheet;
51 import com.sun.star.sheet.XSpreadsheetDocument;
52 import com.sun.star.sheet.XSpreadsheets;
53 import com.sun.star.table.CellAddress;
54 import com.sun.star.table.CellRangeAddress;
55 import com.sun.star.uno.AnyConverter;
56 import com.sun.star.uno.Type;
57 import com.sun.star.uno.UnoRuntime;
58 import com.sun.star.uno.XInterface;
60 /**
61 * Test for object which is represented by service
62 * <code>com.sun.star.sheet.DataPilotTable</code>. <p>
63 * Object implements the following interfaces :
64 * <ul>
65 * <li> <code>com::sun::star::container::XNamed</code></li>
66 * <li> <code>com::sun::star::sheet::XDataPilotTable</code></li>
67 * <li> <code>com::sun::star::sheet::XDataPilotDescriptor</code></li>
68 * </ul>
69 * @see com.sun.star.sheet.DataPilotTable
70 * @see com.sun.star.container.XNamed
71 * @see com.sun.star.sheet.XDataPilotTable
72 * @see com.sun.star.sheet.XDataPilotDescriptor
73 * @see ifc.container._XNamed
74 * @see ifc.sheet._XDataPilotTable
75 * @see ifc.sheet._XDataPilotDescriptor
77 public class ScDataPilotTableObj extends TestCase {
78 static XSpreadsheetDocument xSheetDoc = null;
80 /**
81 * Creates Spreadsheet document.
83 protected void initialize( TestParameters tParam, PrintWriter log ) {
84 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
86 try {
87 log.println( "creating a Spreadsheet document" );
88 xSheetDoc = SOF.createCalcDoc(null);
89 } catch ( com.sun.star.uno.Exception e ) {
90 // Some exception occures.FAILED
91 e.printStackTrace( log );
92 throw new StatusException( "Couldn't create document", e );
97 /**
98 * Disposes Spreadsheet document.
100 protected void cleanup( TestParameters tParam, PrintWriter log ) {
101 log.println( " disposing xSheetDoc " );
102 XComponent oComp = (XComponent)
103 UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ;
104 util.DesktopTools.closeDoc(oComp);
108 * Creating a Testenvironment for the interfaces to be tested.
109 * Retrieves a collection of spreadsheets from a document
110 * and takes one of them. Fills some table in the spreadsheet.
111 * Obtains the collection of data pilot tables using the interface
112 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
113 * for the filled table. Obtains the collection of all the data pilot fields
114 * using the interface <code>XDataPilotDescriptor</code>. Sets field
115 * orientation for every of the data pilot fields. Sets the definitions of
116 * the filter fields for the created description. Inserts new data pilot
117 * table with this descriptor to the collection.
118 * This new data pilot table is the instance of the service
119 * <code>com.sun.star.sheet.DataPilotTable</code>.
120 * Object relations created :
121 * <ul>
122 * <li> <code>'OUTPUTRANGE'</code> for
123 * {@link ifc.sheet._XDataPilotTable}(the cell range address of the
124 * created data pilot table) </li>
125 * <li> <code>'FIELDSAMOUNT'</code> for
126 * {@link ifc.sheet._XDataPilotDescriptor}(the number of filled fields
127 * which descriptor was created for) </li>
128 * <li> <code>'CELLFORCHANGE'</code> for
129 * {@link ifc.sheet._XDataPilotTable}(value of this cell will be changed)</li>
130 * <li> <code>'CELLFORCHECK'</code> for
131 * {@link ifc.sheet._XDataPilotTable}(value of this cell must be changed
132 * after refresh call)</li>
133 * </ul>
134 * @see com.sun.star.sheet.DataPilotTable
135 * @see com.sun.star.sheet.XDataPilotTablesSupplier
136 * @see com.sun.star.sheet.XDataPilotDescriptor
138 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
140 XInterface oObj = null;
142 CellAddress sCellAddress = new CellAddress();
143 sCellAddress.Sheet = 0;
144 sCellAddress.Column = 7;
145 sCellAddress.Row = 8;
147 // creation of testobject here
148 // first we write what we are intend to do to log file
149 log.println( "Creating a test environment" );
150 log.println("getting sheets");
151 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
152 XIndexAccess oIndexAccess = (XIndexAccess)
153 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
154 XSpreadsheet oSheet = null;
155 Object oChangeCell = null;
156 Object oCheckCell = null;
157 try {
158 oSheet = (XSpreadsheet) AnyConverter.toObject(
159 new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
160 oChangeCell = oSheet.getCellByPosition(1, 5);
161 oCheckCell = oSheet.getCellByPosition(
162 sCellAddress.Column, sCellAddress.Row + 3);
163 } catch (com.sun.star.lang.WrappedTargetException e) {
164 e.printStackTrace(log);
165 throw new StatusException( "Couldn't get a spreadsheet", e);
166 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
167 e.printStackTrace(log);
168 throw new StatusException( "Couldn't get a spreadsheet", e);
169 } catch (com.sun.star.lang.IllegalArgumentException e) {
170 e.printStackTrace(log);
171 throw new StatusException( "Couldn't get a spreadsheet", e);
173 log.println("Getting sheet - " + (oSheet == null ? "FAILED" : "OK"));
174 try {
175 log.println("Filing a table");
176 for (int i = 1; i < 6; i++) {
177 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
178 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
181 for (int i = 1; i < 6; i++)
182 for (int j = 1; j < 6; j++) {
183 oSheet.getCellByPosition(i, j).setValue(2.5 * j + i);
185 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
186 e.printStackTrace(log);
187 throw new StatusException("Couldn't fill some cells", e);
190 CellRangeAddress sCellRangeAddress = new CellRangeAddress();
191 sCellRangeAddress.Sheet = 0;
192 sCellRangeAddress.StartColumn = 1;
193 sCellRangeAddress.StartRow = 0;
194 sCellRangeAddress.EndColumn = 5;
195 sCellRangeAddress.EndRow = 5;
197 TableFilterField[] filterFields = new TableFilterField[2];
198 filterFields[0] = new TableFilterField();
199 filterFields[0].Connection = com.sun.star.sheet.FilterConnection.AND;
200 filterFields[0].Field = 1;
201 filterFields[0].IsNumeric = true;
202 filterFields[0].NumericValue = 4;
203 filterFields[0].Operator = com.sun.star.sheet.FilterOperator.GREATER;
204 filterFields[1] = new TableFilterField();
205 filterFields[1].Connection = com.sun.star.sheet.FilterConnection.AND;
206 filterFields[1].Field = 1;
207 filterFields[1].IsNumeric = true;
208 filterFields[1].NumericValue = 12;
209 filterFields[1].Operator = com.sun.star.sheet.FilterOperator.LESS_EQUAL;
211 XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier)
212 UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
213 log.println("Getting test object");
214 XDataPilotTables DPT = DPTS.getDataPilotTables();
215 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
216 DPDsc.setSourceRange(sCellRangeAddress);
218 XSheetFilterDescriptor SFD = DPDsc.getFilterDescriptor();
219 SFD.setFilterFields(filterFields);
221 XPropertySet fieldPropSet = null;
222 try {
223 Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
224 fieldPropSet = (XPropertySet)
225 UnoRuntime.queryInterface(XPropertySet.class, oDataPilotField);
226 } catch (com.sun.star.lang.WrappedTargetException e) {
227 e.printStackTrace(log);
228 throw new StatusException("Couldn't create a test environment", e);
229 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
230 e.printStackTrace(log);
231 throw new StatusException("Couldn't create a test environment", e);
234 try {
235 fieldPropSet.setPropertyValue("Function",
236 com.sun.star.sheet.GeneralFunction.SUM);
237 fieldPropSet.setPropertyValue("Orientation",
238 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
239 } catch(com.sun.star.lang.WrappedTargetException e) {
240 e.printStackTrace(log);
241 throw new StatusException("Couldn't create a test environment", e);
242 } catch(com.sun.star.lang.IllegalArgumentException e) {
243 e.printStackTrace(log);
244 throw new StatusException("Couldn't create a test environment", e);
245 } catch(com.sun.star.beans.PropertyVetoException e) {
246 e.printStackTrace(log);
247 throw new StatusException("Couldn't create a test environment", e);
248 } catch(com.sun.star.beans.UnknownPropertyException e) {
249 e.printStackTrace(log);
250 throw new StatusException("Couldn't create a test environment", e);
253 if (DPT.hasByName("DataPilotTable")) {
254 DPT.removeByName("DataPilotTable");
256 DPT.insertNewByName("DataPilotTable", sCellAddress, DPDsc);
257 try {
258 oObj = (XInterface) AnyConverter.toObject(
259 new Type(XInterface.class),DPT.getByName(DPT.getElementNames()[0]));
260 } catch (com.sun.star.lang.WrappedTargetException e) {
261 e.printStackTrace(log);
262 throw new StatusException("Couldn't create a test environment", e);
263 } catch (com.sun.star.container.NoSuchElementException e) {
264 e.printStackTrace(log);
265 throw new StatusException("Couldn't create a test environment", e);
266 } catch (com.sun.star.lang.IllegalArgumentException e) {
267 e.printStackTrace(log);
268 throw new StatusException("Couldn't create a test environment", e);
271 log.println("Creating object - " +
272 ((oObj == null) ? "FAILED" : "OK"));
274 TestEnvironment tEnv = new TestEnvironment( oObj );
276 // Other parameters required for interface tests
277 tEnv.addObjRelation("OUTPUTRANGE", sCellAddress);
278 tEnv.addObjRelation("CELLFORCHANGE", oChangeCell);
279 tEnv.addObjRelation("CELLFORCHECK", oCheckCell);
280 tEnv.addObjRelation("FIELDSAMOUNT", new Integer(5));
281 tEnv.addObjRelation("SHEETDOCUMENT", xSheetDoc);
283 createTable2(oSheet, sCellRangeAddress, tEnv);
285 return tEnv;
288 /**
289 * Create a new DataPilot table output for use with testing XDataPilotTable2
290 * interface.
292 * @param oSheet current sheet instance
293 * @param srcRange source range
294 * @param tEnv test environment instance
296 private void createTable2(XSpreadsheet oSheet, CellRangeAddress srcRange, TestEnvironment tEnv)
298 XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier)
299 UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
300 log.println("Creating test table object");
301 XDataPilotTables DPT = DPTS.getDataPilotTables();
302 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
303 DPDsc.setSourceRange(srcRange);
305 XIndexAccess xIA = DPDsc.getDataPilotFields();
306 int fieldCount = xIA.getCount() - 1; // skip the last field because it's always hidden.
309 for (int i = 0; i < fieldCount; ++i)
311 Object o = xIA.getByIndex(i);
312 XPropertySet fieldPropSet = (XPropertySet)UnoRuntime.queryInterface(
313 XPropertySet.class, o);
315 if (i == fieldCount - 1)
317 // last field
318 fieldPropSet.setPropertyValue(
319 "Function", com.sun.star.sheet.GeneralFunction.SUM);
320 fieldPropSet.setPropertyValue(
321 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.DATA);
323 else if (i%2 == 0)
325 // even number fields
326 fieldPropSet.setPropertyValue(
327 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.COLUMN);
329 else if (i%2 == 1)
331 // odd number fields
332 fieldPropSet.setPropertyValue(
333 "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.ROW);
337 if (DPT.hasByName("DataPilotTable2"))
338 DPT.removeByName("DataPilotTable2");
340 CellAddress destAddr = new CellAddress();
341 destAddr.Sheet = 0;
342 destAddr.Column = 0;
343 destAddr.Row = 14;
344 DPT.insertNewByName("DataPilotTable2", destAddr, DPDsc);
346 Object o = DPT.getByName("DataPilotTable2");
347 tEnv.addObjRelation("DATAPILOTTABLE2", o);
349 catch (com.sun.star.uno.Exception e)
351 e.printStackTrace(log);
352 throw new StatusException("Couldn't create a test environment", e);