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 complex
.dataPilot
;
21 import com
.sun
.star
.beans
.XPropertySet
;
22 import com
.sun
.star
.container
.XIndexAccess
;
23 import com
.sun
.star
.container
.XNamed
;
24 import com
.sun
.star
.lang
.XMultiServiceFactory
;
25 import com
.sun
.star
.sheet
.XDataPilotDescriptor
;
26 import com
.sun
.star
.sheet
.XDataPilotTable
;
27 import com
.sun
.star
.sheet
.XDataPilotTables
;
28 import com
.sun
.star
.sheet
.XDataPilotTablesSupplier
;
29 import com
.sun
.star
.sheet
.XSpreadsheet
;
30 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
31 import com
.sun
.star
.sheet
.XSpreadsheets
;
32 import com
.sun
.star
.table
.CellAddress
;
33 import com
.sun
.star
.table
.CellRangeAddress
;
34 import com
.sun
.star
.uno
.AnyConverter
;
35 import com
.sun
.star
.uno
.Type
;
36 import com
.sun
.star
.uno
.UnoRuntime
;
37 import com
.sun
.star
.uno
.XInterface
;
38 import com
.sun
.star
.util
.XCloseable
;
39 import lib
.StatusException
;
40 import lib
.TestParameters
;
41 import util
.SOfficeFactory
;
43 import org
.junit
.After
;
44 import org
.junit
.AfterClass
;
45 import org
.junit
.Before
;
46 import org
.junit
.BeforeClass
;
47 import org
.junit
.Test
;
48 import org
.openoffice
.test
.OfficeConnection
;
49 import static org
.junit
.Assert
.*;
53 * check the DataPilot of Calc.
55 public class CheckDataPilot
{
56 /** The data pilot field object **/
57 private XInterface mDataPilotFieldObject
= null;
58 /** The data pilot table object **/
59 private XInterface mDataPilotTableObject
= null;
62 private XSpreadsheetDocument xSheetDoc
= null;
65 * A field is filled some values. This integer determines the size of the
66 * field in x and y direction.
68 private static final int mMaxFieldIndex
= 6;
73 private static TestParameters param
= null;
76 * Get all test methods
77 * @return The test methods
79 public String
[] getTestMethodNames() {
80 return new String
[]{"testDataPilotTableObject",
81 "testDataPilotFieldObject"};
85 * Test the data pilot field object:
86 * simply execute the interface tests in a row
88 @Test public void testDataPilotFieldObject() {
89 System
.out
.println("Starting 'testDataPilotFieldObject'");
91 XNamed xNamed
= UnoRuntime
.queryInterface(
92 XNamed
.class, mDataPilotFieldObject
);
94 _XNamed _xNamed
= new _XNamed(xNamed
/*, log*/, param
);
95 assertTrue("_getName failed.",_xNamed
._getName());
96 assertTrue("_setName failed.",_xNamed
._setName());
99 XPropertySet xProp
= UnoRuntime
.queryInterface(
100 XPropertySet
.class, mDataPilotFieldObject
);
101 _XPropertySet _xProp
= new _XPropertySet(xProp
/*, log*/, param
);
102 assertTrue("_getPropertySetInfo failed.",_xProp
._getPropertySetInfo());
103 assertTrue("_addPropertyChangeListener failed.",_xProp
._addPropertyChangeListener());
104 assertTrue("_addVetoableChangeListener failed.",_xProp
._addVetoableChangeListener());
105 assertTrue("_setPropertyValue failed.",_xProp
._setPropertyValue());
106 assertTrue("_getPropertyValue failed.",_xProp
._getPropertyValue());
107 assertTrue("_removePropertyChangeListener failed.",_xProp
._removePropertyChangeListener());
108 assertTrue("_removeVetoableChangeListener failed.",_xProp
._removeVetoableChangeListener());
113 * Test the data pilot table object:
114 * simply execute the interface tests in a row
116 @Test public void testDataPilotTableObject() {
117 System
.out
.println("Starting 'testDataPilotTableObject'");
119 XNamed xNamed
= UnoRuntime
.queryInterface(
120 XNamed
.class, mDataPilotTableObject
);
121 _XNamed _xNamed
= new _XNamed(xNamed
/*, log*/, param
);
122 assertTrue("_getName failed.",_xNamed
._getName());
123 assertTrue("_setName failed.",_xNamed
._setName());
126 XDataPilotTable xDataPilotTable
=
127 UnoRuntime
.queryInterface(XDataPilotTable
.class,
128 mDataPilotTableObject
);
129 _XDataPilotTable _xDataPilotTable
=
130 new _XDataPilotTable(xDataPilotTable
/*, log*/, param
);
131 assertTrue("before failed.", _xDataPilotTable
.before());
132 assertTrue("_getOutputRange failed.", _xDataPilotTable
._getOutputRange()) ;
134 // _XDataPilotDescriptor
135 XDataPilotDescriptor xDataPilotDescriptor
=
136 UnoRuntime
.queryInterface(XDataPilotDescriptor
.class,
137 mDataPilotTableObject
);
138 _XDataPilotDescriptor _xDataPilotDescriptor
=
139 new _XDataPilotDescriptor(xDataPilotDescriptor
/*, log*/, param
);
140 assertTrue("before failed.", _xDataPilotDescriptor
.before());
141 assertTrue("_setTag failed.", _xDataPilotDescriptor
._setTag()) ;
142 assertTrue("_getTag failed.", _xDataPilotDescriptor
._getTag()) ;
143 assertTrue("_getFilterDescriptor failed.", _xDataPilotDescriptor
._getFilterDescriptor()) ;
144 assertTrue("_getDataPilotFields failed.", _xDataPilotDescriptor
._getDataPilotFields()) ;
145 assertTrue("_getColumnFields failed.", _xDataPilotDescriptor
._getColumnFields()) ;
146 assertTrue("_getRowFields failed.", _xDataPilotDescriptor
._getRowFields()) ;
147 assertTrue("_getDataFields failed.", _xDataPilotDescriptor
._getDataFields()) ;
148 assertTrue("_getHiddenFields failed.", _xDataPilotDescriptor
._getHiddenFields()) ;
149 assertTrue("_getPageFields failed.", _xDataPilotDescriptor
._getPageFields()) ;
150 assertTrue("_setSourceRange failed.", _xDataPilotDescriptor
._setSourceRange()) ;
151 assertTrue("_getSourceRange failed.", _xDataPilotDescriptor
._getSourceRange()) ;
155 * create an environment for the test
157 @Before public void before() {
159 final XMultiServiceFactory xMsf
= UnoRuntime
.queryInterface(XMultiServiceFactory
.class, connection
.getComponentContext().getServiceManager());
160 SOfficeFactory SOF
= SOfficeFactory
.getFactory(xMsf
);
162 param
= new TestParameters();
163 param
.put("ServiceFactory", xMsf
);
166 CellRangeAddress sCellRangeAdress
= new CellRangeAddress();
167 sCellRangeAdress
.Sheet
= 0;
168 sCellRangeAdress
.StartColumn
= 1;
169 sCellRangeAdress
.StartRow
= 0;
170 sCellRangeAdress
.EndColumn
= mMaxFieldIndex
-1;
171 sCellRangeAdress
.EndRow
= mMaxFieldIndex
- 1;
173 // position of the data pilot table
174 CellAddress sCellAdress
= new CellAddress();
175 sCellAdress
.Sheet
= 0;
176 sCellAdress
.Column
= 7;
180 System
.out
.println( "Creating a Spreadsheet document" );
181 xSheetDoc
= SOF
.createCalcDoc(null);
182 } catch (com
.sun
.star
.uno
.Exception e
) {
183 // Some exception occurs.FAILED
185 throw new StatusException( "Couldn't create document", e
);
188 System
.out
.println("Getting a sheet");
189 XSpreadsheets xSpreadsheets
= xSheetDoc
.getSheets();
190 XSpreadsheet oSheet
= null;
191 XSpreadsheet oSheet2
= null;
192 XIndexAccess oIndexAccess
=
193 UnoRuntime
.queryInterface(XIndexAccess
.class, xSpreadsheets
);
195 // Per default there's now just one sheet, make sure we have at least two, then
196 xSpreadsheets
.insertNewByName("Some Sheet", (short)0);
199 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
200 new Type(XSpreadsheet
.class),oIndexAccess
.getByIndex(0));
201 oSheet2
= (XSpreadsheet
) AnyConverter
.toObject(
202 new Type(XSpreadsheet
.class),oIndexAccess
.getByIndex(1));
203 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
205 throw new StatusException( "Couldn't get a spreadsheet", e
);
206 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
208 throw new StatusException( "Couldn't get a spreadsheet", e
);
209 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
211 throw new StatusException( "Couldn't get a spreadsheet", e
);
215 System
.out
.println("Filling a table");
216 for (int i
= 1; i
< mMaxFieldIndex
; i
++) {
217 oSheet
.getCellByPosition(i
, 0).setFormula("Col" + i
);
218 oSheet
.getCellByPosition(0, i
).setFormula("Row" + i
);
219 oSheet2
.getCellByPosition(i
, 0).setFormula("Col" + i
);
220 oSheet2
.getCellByPosition(0, i
).setFormula("Row" + i
);
223 for (int i
= 1; i
< mMaxFieldIndex
; i
++)
225 for (int j
= 1; j
< mMaxFieldIndex
; j
++)
227 oSheet
.getCellByPosition(i
, j
).setValue(i
* (j
+ 1));
228 oSheet2
.getCellByPosition(i
, j
).setValue(i
* (j
+ 2));
231 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
233 throw new StatusException("Couldn't fill some cells", e
);
236 // change a value of a cell and check the change in the data pilot
237 // (for the XDataPilotTable.refresh() test)
238 Object oChangeCell
= null;
239 Object oCheckCell
= null;
240 Integer aChangeValue
= null;
243 oChangeCell
= oSheet
.getCellByPosition(1, 5);
244 int x
= sCellAdress
.Column
;
245 int y
= sCellAdress
.Row
+ 3;
246 // cell of the data pilot output
247 oCheckCell
= oSheet
.getCellByPosition(x
, y
);
248 aChangeValue
= Integer
.valueOf(27);
249 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
251 throw new StatusException( "Couldn't get cells for changing.", e
);
255 // create the test objects
256 System
.out
.println("Getting test objects") ;
257 XDataPilotTablesSupplier DPTS
=
258 UnoRuntime
.queryInterface(XDataPilotTablesSupplier
.class, oSheet
);
259 XDataPilotTables DPT
= DPTS
.getDataPilotTables();
260 XDataPilotDescriptor DPDsc
= DPT
.createDataPilotDescriptor();
261 DPDsc
.setSourceRange(sCellRangeAdress
);
263 XPropertySet fieldPropSet
= null;
265 Object oDataPilotField
= DPDsc
.getDataPilotFields().getByIndex(0);
267 UnoRuntime
.queryInterface(XPropertySet
.class, oDataPilotField
);
268 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
270 throw new StatusException("Couldn't create a test environment", e
);
271 } catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
273 throw new StatusException("Couldn't create a test environment", e
);
277 fieldPropSet
.setPropertyValue("Function",
278 com
.sun
.star
.sheet
.GeneralFunction
.SUM
);
279 fieldPropSet
.setPropertyValue("Orientation",
280 com
.sun
.star
.sheet
.DataPilotFieldOrientation
.DATA
);
281 } catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
283 throw new StatusException("Couldn't create a test environment", e
);
284 } catch(com
.sun
.star
.lang
.IllegalArgumentException e
) {
286 throw new StatusException("Couldn't create a test environment", e
);
287 } catch(com
.sun
.star
.beans
.PropertyVetoException e
) {
289 throw new StatusException("Couldn't create a test environment", e
);
290 } catch(com
.sun
.star
.beans
.UnknownPropertyException e
) {
292 throw new StatusException("Couldn't create a test environment", e
);
295 System
.out
.println("Insert the DataPilotTable");
296 if (DPT
.hasByName("DataPilotTable")) {
297 DPT
.removeByName("DataPilotTable");
299 DPT
.insertNewByName("DataPilotTable", sCellAdress
, DPDsc
);
301 mDataPilotTableObject
= (XInterface
) AnyConverter
.toObject(
302 new Type(XInterface
.class),DPT
.getByName(DPT
.getElementNames()[0]));
303 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
305 throw new StatusException("Couldn't create a test environment", e
);
306 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
308 throw new StatusException("Couldn't create a test environment", e
);
309 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
311 throw new StatusException("Couldn't create a test environment", e
);
314 XIndexAccess IA
= DPDsc
.getDataPilotFields();
316 mDataPilotFieldObject
= (XInterface
)AnyConverter
.toObject(
317 new Type(XInterface
.class),IA
.getByIndex(0));
318 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
320 throw new StatusException("Couldn't get data pilot field", e
);
321 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
323 throw new StatusException("Couldn't get data pilot field", e
);
324 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
326 throw new StatusException("Couldn't get data pilot field", e
);
329 // Other parameters required for interface tests
330 param
.put("OUTPUTRANGE", sCellAdress
);
331 param
.put("CELLFORCHANGE", oChangeCell
);
332 param
.put("CELLFORCHECK", oCheckCell
);
333 param
.put("CHANGEVALUE", aChangeValue
);
334 param
.put("FIELDSAMOUNT", Integer
.valueOf(5));
339 * this method closes a calc document and resets the corresponding class variable xSheetDoc
341 protected boolean closeSpreadsheetDocument() {
342 boolean worked
= true;
344 System
.out
.println(" disposing xSheetDoc ");
347 XCloseable oCloser
= UnoRuntime
.queryInterface(
348 XCloseable
.class, xSheetDoc
);
350 } catch (com
.sun
.star
.util
.CloseVetoException e
) {
352 System
.out
.println("Couldn't close document");
353 } catch (com
.sun
.star
.lang
.DisposedException e
) {
355 System
.out
.println("Document already disposed");
356 } catch (NullPointerException e
) {
358 System
.out
.println("Couldn't get XCloseable");
366 @After public void after()
368 closeSpreadsheetDocument();
372 @BeforeClass public static void setUpConnection() throws Exception
{
373 System
.out
.println("setUpConnection()");
377 @AfterClass public static void tearDownConnection()
378 throws InterruptedException
, com
.sun
.star
.uno
.Exception
380 System
.out
.println("tearDownConnection()");
381 connection
.tearDown();
384 private static final OfficeConnection connection
= new OfficeConnection();