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.TableFilterField;
26 import com
.sun
.star
.sheet
.XDataPilotDescriptor
;
27 import com
.sun
.star
.sheet
.XDataPilotTable
;
28 import com
.sun
.star
.sheet
.XDataPilotTables
;
29 import com
.sun
.star
.sheet
.XDataPilotTablesSupplier
;
30 // import com.sun.star.sheet.XSheetFilterDescriptor;
31 import com
.sun
.star
.sheet
.XSpreadsheet
;
32 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
33 import com
.sun
.star
.sheet
.XSpreadsheets
;
34 import com
.sun
.star
.table
.CellAddress
;
35 import com
.sun
.star
.table
.CellRangeAddress
;
36 import com
.sun
.star
.uno
.AnyConverter
;
37 import com
.sun
.star
.uno
.Type
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
39 import com
.sun
.star
.uno
.XInterface
;
40 import com
.sun
.star
.util
.XCloseable
;
41 import complex
.dataPilot
._XPropertySet
;
42 import complex
.dataPilot
._XNamed
;
43 import complex
.dataPilot
._XDataPilotDescriptor
;
44 import complex
.dataPilot
._XDataPilotTable
;
45 // import complexlib.ComplexTestCase;
46 import lib
.StatusException
;
47 import lib
.TestParameters
;
48 import util
.SOfficeFactory
;
50 import org
.junit
.After
;
51 import org
.junit
.AfterClass
;
52 import org
.junit
.Before
;
53 import org
.junit
.BeforeClass
;
54 import org
.junit
.Test
;
55 import org
.openoffice
.test
.OfficeConnection
;
56 import static org
.junit
.Assert
.*;
60 * check the DataPilot of Calc.
62 public class CheckDataPilot
{
63 /** The data pilot field object **/
64 private XInterface mDataPilotFieldObject
= null;
65 /** The data pilot table object **/
66 private XInterface mDataPilotTableObject
= null;
69 private XSpreadsheetDocument xSheetDoc
= null;
72 * A field is filled some values. This integer determines the size of the
73 * field in x and y direction.
75 private int mMaxFieldIndex
= 6;
80 private static TestParameters param
= null;
83 * Get all test methods
84 * @return The test methods
86 public String
[] getTestMethodNames() {
87 return new String
[]{"testDataPilotTableObject",
88 "testDataPilotFieldObject"};
92 * Test the data pilot field object:
93 * simply execute the interface tests in a row
95 @Test public void testDataPilotFieldObject() {
96 System
.out
.println("Starting 'testDataPilotFieldObject'");
98 XNamed xNamed
= UnoRuntime
.queryInterface(
99 XNamed
.class, mDataPilotFieldObject
);
101 _XNamed _xNamed
= new _XNamed(xNamed
/*, log*/, param
);
102 assertTrue("_getName failed.",_xNamed
._getName());
103 assertTrue("_setName failed.",_xNamed
._setName());
106 XPropertySet xProp
= UnoRuntime
.queryInterface(
107 XPropertySet
.class, mDataPilotFieldObject
);
108 _XPropertySet _xProp
= new _XPropertySet(xProp
/*, log*/, param
);
109 assertTrue("_getPropertySetInfo failed.",_xProp
._getPropertySetInfo());
110 assertTrue("_addPropertyChangeListener failed.",_xProp
._addPropertyChangeListener());
111 assertTrue("_addVetoableChangeListener failed.",_xProp
._addVetoableChangeListener());
112 assertTrue("_setPropertyValue failed.",_xProp
._setPropertyValue());
113 assertTrue("_getPropertyValue failed.",_xProp
._getPropertyValue());
114 assertTrue("_removePropertyChangeListener failed.",_xProp
._removePropertyChangeListener());
115 assertTrue("_removeVetoableChangeListener failed.",_xProp
._removeVetoableChangeListener());
120 * Test the data pilot table object:
121 * simply execute the interface tests in a row
123 @Test public void testDataPilotTableObject() {
124 System
.out
.println("Starting 'testDataPilotTableObject'");
126 XNamed xNamed
= UnoRuntime
.queryInterface(
127 XNamed
.class, mDataPilotTableObject
);
128 _XNamed _xNamed
= new _XNamed(xNamed
/*, log*/, param
);
129 assertTrue("_getName failed.",_xNamed
._getName());
130 assertTrue("_setName failed.",_xNamed
._setName());
133 XDataPilotTable xDataPilotTable
=
134 UnoRuntime
.queryInterface(XDataPilotTable
.class,
135 mDataPilotTableObject
);
136 _XDataPilotTable _xDataPilotTable
=
137 new _XDataPilotTable(xDataPilotTable
/*, log*/, param
);
138 assertTrue("before failed.", _xDataPilotTable
.before());
139 assertTrue("_getOutputRange failed.", _xDataPilotTable
._getOutputRange()) ;
140 // assertTrue("_refresh failed.", _xDataPilotTable._refresh()) ;
142 // _XDataPilotDescriptor
143 XDataPilotDescriptor xDataPilotDescriptor
=
144 UnoRuntime
.queryInterface(XDataPilotDescriptor
.class,
145 mDataPilotTableObject
);
146 _XDataPilotDescriptor _xDataPilotDescriptor
=
147 new _XDataPilotDescriptor(xDataPilotDescriptor
/*, log*/, param
);
148 assertTrue("before failed.", _xDataPilotDescriptor
.before());
149 assertTrue("_setTag failed.", _xDataPilotDescriptor
._setTag()) ;
150 assertTrue("_getTag failed.", _xDataPilotDescriptor
._getTag()) ;
151 assertTrue("_getFilterDescriptor failed.", _xDataPilotDescriptor
._getFilterDescriptor()) ;
152 assertTrue("_getDataPilotFields failed.", _xDataPilotDescriptor
._getDataPilotFields()) ;
153 assertTrue("_getColumnFields failed.", _xDataPilotDescriptor
._getColumnFields()) ;
154 assertTrue("_getRowFields failed.", _xDataPilotDescriptor
._getRowFields()) ;
155 assertTrue("_getDataFields failed.", _xDataPilotDescriptor
._getDataFields()) ;
156 assertTrue("_getHiddenFields failed.", _xDataPilotDescriptor
._getHiddenFields()) ;
157 assertTrue("_getPageFields failed.", _xDataPilotDescriptor
._getPageFields()) ;
158 assertTrue("_setSourceRange failed.", _xDataPilotDescriptor
._setSourceRange()) ;
159 assertTrue("_getSourceRange failed.", _xDataPilotDescriptor
._getSourceRange()) ;
163 * create an environment for the test
165 @Before public void before() {
166 // Object oInterface = null;
168 // SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)param.getMSF() );
169 final XMultiServiceFactory xMsf
= UnoRuntime
.queryInterface(XMultiServiceFactory
.class, connection
.getComponentContext().getServiceManager());
170 SOfficeFactory SOF
= SOfficeFactory
.getFactory(xMsf
);
172 param
= new TestParameters();
173 param
.put("ServiceFactory", xMsf
);
176 CellRangeAddress sCellRangeAdress
= new CellRangeAddress();
177 sCellRangeAdress
.Sheet
= 0;
178 sCellRangeAdress
.StartColumn
= 1;
179 sCellRangeAdress
.StartRow
= 0;
180 sCellRangeAdress
.EndColumn
= mMaxFieldIndex
-1;
181 sCellRangeAdress
.EndRow
= mMaxFieldIndex
- 1;
183 // position of the data pilot table
184 CellAddress sCellAdress
= new CellAddress();
185 sCellAdress
.Sheet
= 0;
186 sCellAdress
.Column
= 7;
190 System
.out
.println( "Creating a Spreadsheet document" );
191 xSheetDoc
= SOF
.createCalcDoc(null);
192 } catch (com
.sun
.star
.uno
.Exception e
) {
193 // Some exception occurs.FAILED
195 throw new StatusException( "Couldn't create document", e
);
198 System
.out
.println("Getting a sheet");
199 XSpreadsheets xSpreadsheets
= xSheetDoc
.getSheets();
200 XSpreadsheet oSheet
= null;
201 XSpreadsheet oSheet2
= null;
202 XIndexAccess oIndexAccess
=
203 UnoRuntime
.queryInterface(XIndexAccess
.class, xSpreadsheets
);
205 // Per default there's now just one sheet, make sure we have at least two, then
206 xSpreadsheets
.insertNewByName("Some Sheet", (short)0);
209 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
210 new Type(XSpreadsheet
.class),oIndexAccess
.getByIndex(0));
211 oSheet2
= (XSpreadsheet
) AnyConverter
.toObject(
212 new Type(XSpreadsheet
.class),oIndexAccess
.getByIndex(1));
213 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
215 throw new StatusException( "Couldn't get a spreadsheet", e
);
216 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
218 throw new StatusException( "Couldn't get a spreadsheet", e
);
219 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
221 throw new StatusException( "Couldn't get a spreadsheet", e
);
225 System
.out
.println("Filling a table");
226 for (int i
= 1; i
< mMaxFieldIndex
; i
++) {
227 oSheet
.getCellByPosition(i
, 0).setFormula("Col" + i
);
228 oSheet
.getCellByPosition(0, i
).setFormula("Row" + i
);
229 oSheet2
.getCellByPosition(i
, 0).setFormula("Col" + i
);
230 oSheet2
.getCellByPosition(0, i
).setFormula("Row" + i
);
233 for (int i
= 1; i
< mMaxFieldIndex
; i
++)
235 for (int j
= 1; j
< mMaxFieldIndex
; j
++)
237 oSheet
.getCellByPosition(i
, j
).setValue(i
* (j
+ 1));
238 oSheet2
.getCellByPosition(i
, j
).setValue(i
* (j
+ 2));
241 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
243 throw new StatusException("Couldn't fill some cells", e
);
246 // change a value of a cell and check the change in the data pilot
247 // (for the XDataPilotTable.refresh() test)
248 Object oChangeCell
= null;
249 Object oCheckCell
= null;
250 Integer aChangeValue
= null;
253 oChangeCell
= oSheet
.getCellByPosition(1, 5);
254 int x
= sCellAdress
.Column
;
255 int y
= sCellAdress
.Row
+ 3;
256 // cell of the data pilot output
257 oCheckCell
= oSheet
.getCellByPosition(x
, y
);
258 aChangeValue
= new Integer(27);
259 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
261 throw new StatusException( "Couldn't get cells for changing.", e
);
265 // create the test objects
266 System
.out
.println("Getting test objects") ;
267 XDataPilotTablesSupplier DPTS
=
268 UnoRuntime
.queryInterface(XDataPilotTablesSupplier
.class, oSheet
);
269 XDataPilotTables DPT
= DPTS
.getDataPilotTables();
270 XDataPilotDescriptor DPDsc
= DPT
.createDataPilotDescriptor();
271 DPDsc
.setSourceRange(sCellRangeAdress
);
273 XPropertySet fieldPropSet
= null;
275 Object oDataPilotField
= DPDsc
.getDataPilotFields().getByIndex(0);
277 UnoRuntime
.queryInterface(XPropertySet
.class, oDataPilotField
);
278 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
280 throw new StatusException("Couldn't create a test environment", e
);
281 } catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
283 throw new StatusException("Couldn't create a test environment", e
);
287 fieldPropSet
.setPropertyValue("Function",
288 com
.sun
.star
.sheet
.GeneralFunction
.SUM
);
289 fieldPropSet
.setPropertyValue("Orientation",
290 com
.sun
.star
.sheet
.DataPilotFieldOrientation
.DATA
);
291 } catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
293 throw new StatusException("Couldn't create a test environment", e
);
294 } catch(com
.sun
.star
.lang
.IllegalArgumentException e
) {
296 throw new StatusException("Couldn't create a test environment", e
);
297 } catch(com
.sun
.star
.beans
.PropertyVetoException e
) {
299 throw new StatusException("Couldn't create a test environment", e
);
300 } catch(com
.sun
.star
.beans
.UnknownPropertyException e
) {
302 throw new StatusException("Couldn't create a test environment", e
);
305 System
.out
.println("Insert the DataPilotTable");
306 if (DPT
.hasByName("DataPilotTable")) {
307 DPT
.removeByName("DataPilotTable");
309 DPT
.insertNewByName("DataPilotTable", sCellAdress
, DPDsc
);
311 mDataPilotTableObject
= (XInterface
) AnyConverter
.toObject(
312 new Type(XInterface
.class),DPT
.getByName(DPT
.getElementNames()[0]));
313 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
315 throw new StatusException("Couldn't create a test environment", e
);
316 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
318 throw new StatusException("Couldn't create a test environment", e
);
319 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
321 throw new StatusException("Couldn't create a test environment", e
);
324 XIndexAccess IA
= DPDsc
.getDataPilotFields();
326 mDataPilotFieldObject
= (XInterface
)AnyConverter
.toObject(
327 new Type(XInterface
.class),IA
.getByIndex(0));
328 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
330 throw new StatusException("Couldn't get data pilot field", e
);
331 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
333 throw new StatusException("Couldn't get data pilot field", e
);
334 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
336 throw new StatusException("Couldn't get data pilot field", e
);
339 // Other parameters required for interface tests
340 param
.put("OUTPUTRANGE", sCellAdress
);
341 param
.put("CELLFORCHANGE", oChangeCell
);
342 param
.put("CELLFORCHECK", oCheckCell
);
343 param
.put("CHANGEVALUE", aChangeValue
);
344 param
.put("FIELDSAMOUNT", new Integer(5));
349 * this method closes a calc document and resets the corresponding class variable xSheetDoc
351 protected boolean closeSpreadsheetDocument() {
352 boolean worked
= true;
354 System
.out
.println(" disposing xSheetDoc ");
357 XCloseable oCloser
= UnoRuntime
.queryInterface(
358 XCloseable
.class, xSheetDoc
);
360 } catch (com
.sun
.star
.util
.CloseVetoException e
) {
362 System
.out
.println("Couldn't close document");
363 } catch (com
.sun
.star
.lang
.DisposedException e
) {
365 System
.out
.println("Document already disposed");
366 } catch (java
.lang
.NullPointerException e
) {
368 System
.out
.println("Couldn't get XCloseable");
376 @After public void after()
378 closeSpreadsheetDocument();
382 @BeforeClass public static void setUpConnection() throws Exception
{
383 System
.out
.println("setUpConnection()");
387 @AfterClass public static void tearDownConnection()
388 throws InterruptedException
, com
.sun
.star
.uno
.Exception
390 System
.out
.println("tearDownConnection()");
391 connection
.tearDown();
394 private static final OfficeConnection connection
= new OfficeConnection();