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: ScDataPilotItemsObj.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
.XIndexAccess
;
34 import com
.sun
.star
.container
.XNamed
;
35 import com
.sun
.star
.lang
.XComponent
;
36 import com
.sun
.star
.lang
.XMultiServiceFactory
;
37 import com
.sun
.star
.sheet
.DataPilotFieldOrientation
;
38 import com
.sun
.star
.sheet
.XDataPilotDescriptor
;
39 import com
.sun
.star
.sheet
.XDataPilotField
;
40 import com
.sun
.star
.sheet
.XDataPilotTables
;
41 import com
.sun
.star
.sheet
.XDataPilotTablesSupplier
;
42 import com
.sun
.star
.sheet
.XSpreadsheet
;
43 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
44 import com
.sun
.star
.sheet
.XSpreadsheets
;
45 import com
.sun
.star
.table
.CellAddress
;
46 import com
.sun
.star
.table
.CellRangeAddress
;
47 import com
.sun
.star
.uno
.AnyConverter
;
48 import com
.sun
.star
.uno
.Type
;
49 import com
.sun
.star
.uno
.UnoRuntime
;
50 import com
.sun
.star
.uno
.XInterface
;
52 import java
.io
.PrintWriter
;
54 import lib
.StatusException
;
56 import lib
.TestEnvironment
;
57 import lib
.TestParameters
;
59 import util
.SOfficeFactory
;
63 * Test for object which is represented by service
64 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
65 * Object implements the following interfaces :
67 * <li> <code>com::sun::star::container::XNamed</code></li>
68 * <li> <code>com::sun::star::sheet::DataPilotField</code></li>
69 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
71 * @see com.sun.star.sheet.DataPilotField
72 * @see com.sun.star.container.XNamed
73 * @see com.sun.star.sheet.DataPilotField
74 * @see com.sun.star.beans.XPropertySet
75 * @see ifc.container._XNamed
76 * @see ifc.sheet._DataPilotField
77 * @see ifc.beans._XPropertySet
79 public class ScDataPilotItemsObj
extends TestCase
{
80 static XSpreadsheetDocument xSheetDoc
= null;
83 * A field is filled some values. This integer determines the size of the
84 * field in x and y direction.
86 private int mMaxFieldIndex
= 6;
89 * Creates Spreadsheet document.
91 protected void initialize(TestParameters tParam
, PrintWriter log
) {
92 SOfficeFactory SOF
= SOfficeFactory
.getFactory(
93 (XMultiServiceFactory
) tParam
.getMSF());
96 log
.println("creating a Spreadsheet document");
97 xSheetDoc
= SOF
.createCalcDoc(null);
98 } catch (com
.sun
.star
.uno
.Exception e
) {
99 // Some exception occures.FAILED
100 e
.printStackTrace(log
);
101 throw new StatusException("Couldn't create document", e
);
106 * Disposes Spreadsheet document.
108 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
109 log
.println(" disposing xSheetDoc ");
111 XComponent oComp
= (XComponent
) UnoRuntime
.queryInterface(
112 XComponent
.class, xSheetDoc
);
113 util
.DesktopTools
.closeDoc(oComp
);
117 * Creating a Testenvironment for the interfaces to be tested.
118 * Retrieves a collection of spreadsheets from a document
119 * and takes one of them. Fills some table in the spreadsheet.
120 * Obtains the collection of data pilot tables using the interface
121 * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
122 * for the filled table and inserts new data pilot table with this descriptor
123 * to the collection. Obtains the collection of all the data pilot fields
124 * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
125 * the collection the data pilot field with index 0. This data pilot field
126 * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
127 * @see com.sun.star.sheet.DataPilotField
128 * @see com.sun.star.sheet.XDataPilotTablesSupplier
129 * @see com.sun.star.sheet.XDataPilotDescriptor
131 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
,
133 XInterface oObj
= null;
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");
141 CellRangeAddress sCellRangeAdress
= new CellRangeAddress();
142 sCellRangeAdress
.Sheet
= 0;
143 sCellRangeAdress
.StartColumn
= 1;
144 sCellRangeAdress
.StartRow
= 0;
145 sCellRangeAdress
.EndColumn
= mMaxFieldIndex
- 1;
146 sCellRangeAdress
.EndRow
= mMaxFieldIndex
- 1;
148 // position of the data pilot table
149 CellAddress sCellAdress
= new CellAddress();
150 sCellAdress
.Sheet
= 0;
151 sCellAdress
.Column
= 7;
154 log
.println("Getting a sheet");
156 XSpreadsheets xSpreadsheets
= (XSpreadsheets
) xSheetDoc
.getSheets();
157 XSpreadsheet oSheet
= null;
158 XSpreadsheet oSheet2
= null;
159 XIndexAccess oIndexAccess
= (XIndexAccess
) UnoRuntime
.queryInterface(
160 XIndexAccess
.class, xSpreadsheets
);
163 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
164 new Type(XSpreadsheet
.class),
165 oIndexAccess
.getByIndex(0));
166 oSheet2
= (XSpreadsheet
) AnyConverter
.toObject(
167 new Type(XSpreadsheet
.class),
168 oIndexAccess
.getByIndex(1));
169 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
171 throw new StatusException("Couldn't get a spreadsheet", e
);
172 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
174 throw new StatusException("Couldn't get a spreadsheet", e
);
175 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
177 throw new StatusException("Couldn't get a spreadsheet", e
);
181 log
.println("Filling a table");
183 for (int i
= 1; i
< mMaxFieldIndex
; i
++) {
184 oSheet
.getCellByPosition(i
, 0).setFormula("Col" + i
);
185 oSheet
.getCellByPosition(0, i
).setFormula("Row" + i
);
186 oSheet2
.getCellByPosition(i
, 0).setFormula("Col" + i
);
187 oSheet2
.getCellByPosition(0, i
).setFormula("Row" + i
);
190 for (int i
= 1; i
< mMaxFieldIndex
; i
++)
191 for (int j
= 1; j
< mMaxFieldIndex
; j
++) {
192 oSheet
.getCellByPosition(i
, j
).setValue(i
* (j
+ 1));
193 oSheet2
.getCellByPosition(i
, j
).setValue(i
* (j
+ 2));
195 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
197 throw new StatusException("Couldn't fill some cells", e
);
200 // change a value of a cell and check the change in the data pilot
201 // (for the XDataPilotTable.refresh() test)
202 Object oChangeCell
= null;
203 Object oCheckCell
= null;
204 Integer aChangeValue
= null;
208 oChangeCell
= oSheet
.getCellByPosition(1, 5);
210 int x
= sCellAdress
.Column
;
211 int y
= sCellAdress
.Row
+ 3;
214 // cell of the data pilot output
215 oCheckCell
= oSheet
.getCellByPosition(x
, y
);
216 aChangeValue
= new Integer(27);
217 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
219 throw new StatusException("Couldn't get cells for changeing.", e
);
223 // create the test objects
224 log
.println("Getting test objects");
226 XDataPilotTablesSupplier DPTS
= (XDataPilotTablesSupplier
) UnoRuntime
.queryInterface(
227 XDataPilotTablesSupplier
.class,
229 XDataPilotTables DPT
= DPTS
.getDataPilotTables();
230 XDataPilotDescriptor DPDsc
= DPT
.createDataPilotDescriptor();
231 DPDsc
.setSourceRange(sCellRangeAdress
);
233 XPropertySet fieldPropSet
= null;
236 Object oDataPilotField
= DPDsc
.getDataPilotFields().getByIndex(0);
237 fieldPropSet
= (XPropertySet
) UnoRuntime
.queryInterface(
238 XPropertySet
.class, oDataPilotField
);
239 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
241 throw new StatusException("Couldn't create a test environment", e
);
242 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
244 throw new StatusException("Couldn't create a test environment", e
);
248 fieldPropSet
.setPropertyValue("Function",
249 com
.sun
.star
.sheet
.GeneralFunction
.SUM
);
250 fieldPropSet
.setPropertyValue("Orientation",
251 com
.sun
.star
.sheet
.DataPilotFieldOrientation
.DATA
);
252 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
254 throw new StatusException("Couldn't create a test environment", e
);
255 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
257 throw new StatusException("Couldn't create a test environment", e
);
258 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
260 throw new StatusException("Couldn't create a test environment", e
);
261 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
263 throw new StatusException("Couldn't create a test environment", e
);
266 log
.println("Insert the DataPilotTable");
268 if (DPT
.hasByName("DataPilotTable")) {
269 DPT
.removeByName("DataPilotTable");
272 XIndexAccess IA
= DPDsc
.getDataPilotFields();
275 DPT
.insertNewByName("DataPilotTable", sCellAdress
, DPDsc
);
278 oObj
= (XInterface
) AnyConverter
.toObject(
279 new Type(XInterface
.class), IA
.getByIndex(0));
280 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
282 throw new StatusException("Couldn't get data pilot field", e
);
283 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
285 throw new StatusException("Couldn't get data pilot field", e
);
286 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
288 throw new StatusException("Couldn't get data pilot field", e
);
291 log
.println("Creating object - " +
292 ((oObj
== null) ?
"FAILED" : "OK"));
294 XDataPilotField xDataPilotField
= (XDataPilotField
) UnoRuntime
.queryInterface(
295 XDataPilotField
.class, oObj
);
297 oObj
= xDataPilotField
.getItems();
299 TestEnvironment tEnv
= new TestEnvironment(oObj
);
301 log
.println("Implementationname: " + util
.utils
.getImplName(oObj
));
303 // Other parameters required for interface tests
307 private void getSRange(XIndexAccess IA
) {
308 int fieldsAmount
= IA
.getCount() + 1;
310 String
[] fieldsNames
= new String
[fieldsAmount
];
315 while ((++i
) < fieldsAmount
) {
319 field
= IA
.getByIndex(i
);
320 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
321 e
.printStackTrace(log
);
324 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
325 e
.printStackTrace(log
);
330 XNamed named
= (XNamed
) UnoRuntime
.queryInterface(XNamed
.class,
332 String name
= named
.getName();
334 log
.println("**Field : '" + name
+ "' ... ");
336 if (!name
.equals("Data")) {
337 fieldsNames
[cnt
] = name
;
339 XPropertySet props
= (XPropertySet
) UnoRuntime
.queryInterface(
340 XPropertySet
.class, field
);
345 props
.setPropertyValue("Orientation",
346 DataPilotFieldOrientation
.COLUMN
);
347 log
.println(" Column");
352 props
.setPropertyValue("Orientation",
353 DataPilotFieldOrientation
.ROW
);
359 props
.setPropertyValue("Orientation",
360 DataPilotFieldOrientation
.DATA
);
361 log
.println(" Data");
366 props
.setPropertyValue("Orientation",
367 DataPilotFieldOrientation
.HIDDEN
);
368 log
.println(" Hidden");
373 props
.setPropertyValue("Orientation",
374 DataPilotFieldOrientation
.PAGE
);
375 log
.println(" Page");
379 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
380 e
.printStackTrace(log
);
383 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
384 e
.printStackTrace(log
);
387 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
388 e
.printStackTrace(log
);
391 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
392 e
.printStackTrace(log
);