1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScIndexEnumeration_DataPilotItemsEnumeration.java,v $
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 ************************************************************************/
32 import com
.sun
.star
.beans
.XPropertySet
;
33 import com
.sun
.star
.container
.XEnumerationAccess
;
34 import com
.sun
.star
.container
.XIndexAccess
;
35 import com
.sun
.star
.container
.XNamed
;
36 import com
.sun
.star
.lang
.XComponent
;
37 import com
.sun
.star
.lang
.XMultiServiceFactory
;
38 import com
.sun
.star
.sheet
.DataPilotFieldOrientation
;
39 import com
.sun
.star
.sheet
.XDataPilotDescriptor
;
40 import com
.sun
.star
.sheet
.XDataPilotField
;
41 import com
.sun
.star
.sheet
.XDataPilotTables
;
42 import com
.sun
.star
.sheet
.XDataPilotTablesSupplier
;
43 import com
.sun
.star
.sheet
.XSpreadsheet
;
44 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
45 import com
.sun
.star
.sheet
.XSpreadsheets
;
46 import com
.sun
.star
.table
.CellAddress
;
47 import com
.sun
.star
.table
.CellRangeAddress
;
48 import com
.sun
.star
.uno
.AnyConverter
;
49 import com
.sun
.star
.uno
.Type
;
50 import com
.sun
.star
.uno
.UnoRuntime
;
51 import com
.sun
.star
.uno
.XInterface
;
53 import java
.io
.PrintWriter
;
55 import lib
.StatusException
;
57 import lib
.TestEnvironment
;
58 import lib
.TestParameters
;
60 import util
.SOfficeFactory
;
64 * Test for object which is represented by service
65 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
66 * Object implements the following interfaces :
68 * <li> <code>com::sun::star::container::XNamed</code></li>
69 * <li> <code>com::sun::star::sheet::DataPilotField</code></li>
70 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
72 * @see com.sun.star.sheet.DataPilotField
73 * @see com.sun.star.container.XNamed
74 * @see com.sun.star.sheet.DataPilotField
75 * @see com.sun.star.beans.XPropertySet
76 * @see ifc.container._XNamed
77 * @see ifc.sheet._DataPilotField
78 * @see ifc.beans._XPropertySet
80 public class ScIndexEnumeration_DataPilotItemsEnumeration
82 static XSpreadsheetDocument xSheetDoc
= null;
85 * A field is filled some values. This integer determines the size of the
86 * field in x and y direction.
88 private int mMaxFieldIndex
= 6;
91 * Creates Spreadsheet document.
93 protected void initialize(TestParameters tParam
, PrintWriter log
) {
94 SOfficeFactory SOF
= SOfficeFactory
.getFactory(
95 (XMultiServiceFactory
) tParam
.getMSF());
98 log
.println("creating a Spreadsheet document");
99 xSheetDoc
= SOF
.createCalcDoc(null);
100 } catch (com
.sun
.star
.uno
.Exception e
) {
101 // Some exception occures.FAILED
102 e
.printStackTrace(log
);
103 throw new StatusException("Couldn't create document", e
);
108 * Disposes Spreadsheet document.
110 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
111 log
.println(" disposing xSheetDoc ");
113 XComponent oComp
= (XComponent
) UnoRuntime
.queryInterface(
114 XComponent
.class, xSheetDoc
);
115 util
.DesktopTools
.closeDoc(oComp
);
119 * Creating a Testenvironment for the interfaces to be tested.
120 * Retrieves a collection of spreadsheets from a document
121 * and takes one of them. Fills some table in the spreadsheet.
122 * Obtains the collection of data pilot tables using the interface
123 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
124 * for the filled table and inserts new data pilot table with this descriptor
125 * to the collection. Obtains the collection of all the data pilot fields
126 * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
127 * the collection the data pilot field with index 0. This data pilot field
128 * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
129 * @see com.sun.star.sheet.DataPilotField
130 * @see com.sun.star.sheet.XDataPilotTablesSupplier
131 * @see com.sun.star.sheet.XDataPilotDescriptor
133 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
,
135 XInterface oObj
= null;
138 // creation of testobject here
139 // first we write what we are intend to do to log file
140 log
.println("Creating a test environment");
143 CellRangeAddress sCellRangeAdress
= new CellRangeAddress();
144 sCellRangeAdress
.Sheet
= 0;
145 sCellRangeAdress
.StartColumn
= 1;
146 sCellRangeAdress
.StartRow
= 0;
147 sCellRangeAdress
.EndColumn
= mMaxFieldIndex
- 1;
148 sCellRangeAdress
.EndRow
= mMaxFieldIndex
- 1;
150 // position of the data pilot table
151 CellAddress sCellAdress
= new CellAddress();
152 sCellAdress
.Sheet
= 0;
153 sCellAdress
.Column
= 7;
156 log
.println("Getting a sheet");
158 XSpreadsheets xSpreadsheets
= (XSpreadsheets
) xSheetDoc
.getSheets();
159 XSpreadsheet oSheet
= null;
160 XSpreadsheet oSheet2
= null;
161 XIndexAccess oIndexAccess
= (XIndexAccess
) UnoRuntime
.queryInterface(
162 XIndexAccess
.class, xSpreadsheets
);
165 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
166 new Type(XSpreadsheet
.class),
167 oIndexAccess
.getByIndex(0));
168 oSheet2
= (XSpreadsheet
) AnyConverter
.toObject(
169 new Type(XSpreadsheet
.class),
170 oIndexAccess
.getByIndex(1));
171 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
173 throw new StatusException("Couldn't get a spreadsheet", e
);
174 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
176 throw new StatusException("Couldn't get a spreadsheet", e
);
177 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
179 throw new StatusException("Couldn't get a spreadsheet", e
);
183 log
.println("Filling a table");
185 for (int i
= 1; i
< mMaxFieldIndex
; i
++) {
186 oSheet
.getCellByPosition(i
, 0).setFormula("Col" + i
);
187 oSheet
.getCellByPosition(0, i
).setFormula("Row" + i
);
188 oSheet2
.getCellByPosition(i
, 0).setFormula("Col" + i
);
189 oSheet2
.getCellByPosition(0, i
).setFormula("Row" + i
);
192 for (int i
= 1; i
< mMaxFieldIndex
; i
++)
193 for (int j
= 1; j
< mMaxFieldIndex
; j
++) {
194 oSheet
.getCellByPosition(i
, j
).setValue(i
* (j
+ 1));
195 oSheet2
.getCellByPosition(i
, j
).setValue(i
* (j
+ 2));
197 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
199 throw new StatusException("Couldn't fill some cells", e
);
202 // change a value of a cell and check the change in the data pilot
203 // (for the XDataPilotTable.refresh() test)
204 Object oChangeCell
= null;
205 Object oCheckCell
= null;
206 Integer aChangeValue
= null;
210 oChangeCell
= oSheet
.getCellByPosition(1, 5);
212 int x
= sCellAdress
.Column
;
213 int y
= sCellAdress
.Row
+ 3;
216 // cell of the data pilot output
217 oCheckCell
= oSheet
.getCellByPosition(x
, y
);
218 aChangeValue
= new Integer(27);
219 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
221 throw new StatusException("Couldn't get cells for changeing.", e
);
225 // create the test objects
226 log
.println("Getting test objects");
228 XDataPilotTablesSupplier DPTS
= (XDataPilotTablesSupplier
) UnoRuntime
.queryInterface(
229 XDataPilotTablesSupplier
.class,
231 XDataPilotTables DPT
= DPTS
.getDataPilotTables();
232 XDataPilotDescriptor DPDsc
= DPT
.createDataPilotDescriptor();
233 DPDsc
.setSourceRange(sCellRangeAdress
);
235 XPropertySet fieldPropSet
= null;
238 Object oDataPilotField
= DPDsc
.getDataPilotFields().getByIndex(0);
239 fieldPropSet
= (XPropertySet
) UnoRuntime
.queryInterface(
240 XPropertySet
.class, oDataPilotField
);
241 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
243 throw new StatusException("Couldn't create a test environment", e
);
244 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
246 throw new StatusException("Couldn't create a test environment", e
);
250 fieldPropSet
.setPropertyValue("Function",
251 com
.sun
.star
.sheet
.GeneralFunction
.SUM
);
252 fieldPropSet
.setPropertyValue("Orientation",
253 com
.sun
.star
.sheet
.DataPilotFieldOrientation
.DATA
);
254 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
256 throw new StatusException("Couldn't create a test environment", e
);
257 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
259 throw new StatusException("Couldn't create a test environment", e
);
260 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
262 throw new StatusException("Couldn't create a test environment", e
);
263 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
265 throw new StatusException("Couldn't create a test environment", e
);
268 log
.println("Insert the DataPilotTable");
270 if (DPT
.hasByName("DataPilotTable")) {
271 DPT
.removeByName("DataPilotTable");
274 XIndexAccess IA
= DPDsc
.getDataPilotFields();
277 DPT
.insertNewByName("DataPilotTable", sCellAdress
, DPDsc
);
280 oObj
= (XInterface
) AnyConverter
.toObject(
281 new Type(XInterface
.class), IA
.getByIndex(0));
282 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
284 throw new StatusException("Couldn't get data pilot field", e
);
285 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
287 throw new StatusException("Couldn't get data pilot field", e
);
288 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
290 throw new StatusException("Couldn't get data pilot field", e
);
293 log
.println("Creating object - " +
294 ((oObj
== null) ?
"FAILED" : "OK"));
296 XDataPilotField xDataPilotField
= (XDataPilotField
) UnoRuntime
.queryInterface(
297 XDataPilotField
.class, oObj
);
299 XEnumerationAccess xEnumerationAccess
= (XEnumerationAccess
) UnoRuntime
.queryInterface(
300 XEnumerationAccess
.class,
301 xDataPilotField
.getItems());
303 oObj
= xEnumerationAccess
.createEnumeration();
305 TestEnvironment tEnv
= new TestEnvironment(oObj
);
307 log
.println("Implementationname: " + util
.utils
.getImplName(oObj
));
309 // Other parameters required for interface tests
313 private void getSRange(XIndexAccess IA
) {
314 int fieldsAmount
= IA
.getCount() + 1;
316 String
[] fieldsNames
= new String
[fieldsAmount
];
321 while ((++i
) < fieldsAmount
) {
325 field
= IA
.getByIndex(i
);
326 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
327 e
.printStackTrace(log
);
330 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
331 e
.printStackTrace(log
);
336 XNamed named
= (XNamed
) UnoRuntime
.queryInterface(XNamed
.class,
338 String name
= named
.getName();
340 log
.println("**Field : '" + name
+ "' ... ");
342 if (!name
.equals("Data")) {
343 fieldsNames
[cnt
] = name
;
345 XPropertySet props
= (XPropertySet
) UnoRuntime
.queryInterface(
346 XPropertySet
.class, field
);
351 props
.setPropertyValue("Orientation",
352 DataPilotFieldOrientation
.COLUMN
);
353 log
.println(" Column");
358 props
.setPropertyValue("Orientation",
359 DataPilotFieldOrientation
.ROW
);
365 props
.setPropertyValue("Orientation",
366 DataPilotFieldOrientation
.DATA
);
367 log
.println(" Data");
372 props
.setPropertyValue("Orientation",
373 DataPilotFieldOrientation
.HIDDEN
);
374 log
.println(" Hidden");
379 props
.setPropertyValue("Orientation",
380 DataPilotFieldOrientation
.PAGE
);
381 log
.println(" Page");
385 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
386 e
.printStackTrace(log
);
389 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
390 e
.printStackTrace(log
);
393 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
394 e
.printStackTrace(log
);
397 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
398 e
.printStackTrace(log
);