1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package complex.dataPilot;
25 
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.container.XIndexAccess;
28 import com.sun.star.container.XNamed;
29 import com.sun.star.lang.XMultiServiceFactory;
30 // import com.sun.star.sheet.TableFilterField;
31 import com.sun.star.sheet.XDataPilotDescriptor;
32 import com.sun.star.sheet.XDataPilotTable;
33 import com.sun.star.sheet.XDataPilotTables;
34 import com.sun.star.sheet.XDataPilotTablesSupplier;
35 // import com.sun.star.sheet.XSheetFilterDescriptor;
36 import com.sun.star.sheet.XSpreadsheet;
37 import com.sun.star.sheet.XSpreadsheetDocument;
38 import com.sun.star.sheet.XSpreadsheets;
39 import com.sun.star.table.CellAddress;
40 import com.sun.star.table.CellRangeAddress;
41 import com.sun.star.uno.AnyConverter;
42 import com.sun.star.uno.Type;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 import com.sun.star.util.XCloseable;
46 import complex.dataPilot._XPropertySet;
47 import complex.dataPilot._XNamed;
48 import complex.dataPilot._XDataPilotDescriptor;
49 import complex.dataPilot._XDataPilotTable;
50 import lib.StatusException;
51 import lib.TestParameters;
52 import util.SOfficeFactory;
53 
54 import org.junit.After;
55 import org.junit.AfterClass;
56 import org.junit.Before;
57 import org.junit.BeforeClass;
58 import org.junit.Test;
59 import org.openoffice.test.OfficeConnection;
60 import static org.junit.Assert.*;
61 
62 
63 /**
64  * check the DataPilot of Calc.
65  */
66 public class CheckDataPilot {
67     /** The data pilot field object **/
68     private XInterface mDataPilotFieldObject = null;
69     /** The data pilot table object **/
70     private XInterface mDataPilotTableObject = null;
71 
72 
73     private XSpreadsheetDocument xSheetDoc = null;
74 
75     /**
76      * A field is filled some values. This integer determines the size of the
77      * field in x and y direction.
78      */
79     private int mMaxFieldIndex = 6;
80 
81     /**
82      * The test parameters
83      */
84     private static TestParameters param = null;
85 
86     /**
87      * Test the data pilot field object:
88      * simply execute the interface tests in a row
89      */
testDataPilotFieldObject()90     @Test public void testDataPilotFieldObject() {
91         System.out.println("Starting 'testDataPilotFieldObject'");
92         // _XNamed
93         XNamed xNamed = UnoRuntime.queryInterface(
94                                     XNamed.class, mDataPilotFieldObject);
95 
96         _XNamed _xNamed = new _XNamed(xNamed/*, log*/, param);
97         assertTrue("_getName failed.",_xNamed._getName());
98         assertTrue("_setName failed.",_xNamed._setName());
99 
100         // _XPropertySet
101         XPropertySet xProp = UnoRuntime.queryInterface(
102                                     XPropertySet.class, mDataPilotFieldObject);
103         _XPropertySet _xProp = new _XPropertySet(xProp/*, log*/, param);
104         assertTrue("_getPropertySetInfo failed.",_xProp._getPropertySetInfo());
105         assertTrue("_addPropertyChangeListener failed.",_xProp._addPropertyChangeListener());
106         assertTrue("_addVetoableChangeListener failed.",_xProp._addVetoableChangeListener());
107         assertTrue("_setPropertyValue failed.",_xProp._setPropertyValue());
108         assertTrue("_getPropertyValue failed.",_xProp._getPropertyValue());
109         assertTrue("_removePropertyChangeListener failed.",_xProp._removePropertyChangeListener());
110         assertTrue("_removeVetoableChangeListener failed.",_xProp._removeVetoableChangeListener());
111 
112     }
113 
114     /**
115      * Test the data pilot table object:
116      * simply execute the interface tests in a row
117      */
testDataPilotTableObject()118     @Test public void testDataPilotTableObject() {
119         System.out.println("Starting 'testDataPilotTableObject'");
120         // _XNamed
121         XNamed xNamed = UnoRuntime.queryInterface(
122                                     XNamed.class, mDataPilotTableObject);
123         _XNamed _xNamed = new _XNamed(xNamed/*, log*/, param);
124         assertTrue("_getName failed.",_xNamed._getName());
125         assertTrue("_setName failed.",_xNamed._setName());
126 
127         // _XDataPilotTable
128         XDataPilotTable xDataPilotTable =
129                     UnoRuntime.queryInterface(XDataPilotTable.class,
130                     mDataPilotTableObject);
131         _XDataPilotTable _xDataPilotTable =
132                     new _XDataPilotTable(xDataPilotTable/*, log*/, param);
133         assertTrue("before failed.", _xDataPilotTable.before());
134         assertTrue("_getOutputRange failed.", _xDataPilotTable._getOutputRange()) ;
135 //        assertTrue("_refresh failed.", _xDataPilotTable._refresh()) ;
136 
137         // _XDataPilotDescriptor
138         XDataPilotDescriptor xDataPilotDescriptor =
139                     UnoRuntime.queryInterface(XDataPilotDescriptor.class,
140                     mDataPilotTableObject);
141         _XDataPilotDescriptor _xDataPilotDescriptor =
142                     new _XDataPilotDescriptor(xDataPilotDescriptor/*, log*/, param);
143         assertTrue("before failed.", _xDataPilotDescriptor.before());
144         assertTrue("_setTag failed.", _xDataPilotDescriptor._setTag()) ;
145         assertTrue("_getTag failed.", _xDataPilotDescriptor._getTag()) ;
146         assertTrue("_getFilterDescriptor failed.", _xDataPilotDescriptor._getFilterDescriptor()) ;
147         assertTrue("_getDataPilotFields failed.", _xDataPilotDescriptor._getDataPilotFields()) ;
148         assertTrue("_getColumnFields failed.", _xDataPilotDescriptor._getColumnFields()) ;
149         assertTrue("_getRowFields failed.", _xDataPilotDescriptor._getRowFields()) ;
150         assertTrue("_getDataFields failed.", _xDataPilotDescriptor._getDataFields()) ;
151         assertTrue("_getHiddenFields failed.", _xDataPilotDescriptor._getHiddenFields()) ;
152         assertTrue("_getPageFields failed.", _xDataPilotDescriptor._getPageFields()) ;
153         assertTrue("_setSourceRange failed.", _xDataPilotDescriptor._setSourceRange()) ;
154         assertTrue("_getSourceRange failed.", _xDataPilotDescriptor._getSourceRange()) ;
155     }
156 
157     /**
158      * create an environment for the test
159      */
before()160     @Before public void before() {
161 //        Object oInterface = null;
162 
163         final XMultiServiceFactory xMsf = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
164         SOfficeFactory SOF = SOfficeFactory.getFactory(xMsf);
165 
166         param = new TestParameters();
167         param.put("ServiceFactory", xMsf);
168 
169         // the cell range
170         CellRangeAddress sCellRangeAdress = new CellRangeAddress();
171         sCellRangeAdress.Sheet = 0;
172         sCellRangeAdress.StartColumn = 1;
173         sCellRangeAdress.StartRow = 0;
174         sCellRangeAdress.EndColumn = mMaxFieldIndex-1;
175         sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
176 
177         // position of the data pilot table
178         CellAddress sCellAdress = new CellAddress();
179         sCellAdress.Sheet = 0;
180         sCellAdress.Column = 7;
181         sCellAdress.Row = 8;
182 
183         try {
184             System.out.println( "Creating a Spreadsheet document" );
185             xSheetDoc = SOF.createCalcDoc(null);
186         } catch (com.sun.star.uno.Exception e) {
187             // Some exception occured.FAILED
188             e.printStackTrace();
189             throw new StatusException( "Couldn't create document", e );
190         }
191 
192         System.out.println("Getting a sheet");
193         XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
194         XSpreadsheet oSheet = null;
195         XSpreadsheet oSheet2 = null;
196         XIndexAccess oIndexAccess =
197             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
198 
199         try {
200             oSheet = (XSpreadsheet) AnyConverter.toObject(
201                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
202             oSheet2 = (XSpreadsheet) AnyConverter.toObject(
203                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(1));
204         } catch (com.sun.star.lang.WrappedTargetException e) {
205             e.printStackTrace();
206             throw new StatusException( "Couldn't get a spreadsheet", e);
207         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
208             e.printStackTrace();
209             throw new StatusException( "Couldn't get a spreadsheet", e);
210         } catch (com.sun.star.lang.IllegalArgumentException e) {
211             e.printStackTrace();
212             throw new StatusException( "Couldn't get a spreadsheet", e);
213         }
214 
215         try {
216             System.out.println("Filling a table");
217             for (int i = 1; i < mMaxFieldIndex; i++) {
218                 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
219                 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
220                 oSheet2.getCellByPosition(i, 0).setFormula("Col" + i);
221                 oSheet2.getCellByPosition(0, i).setFormula("Row" + i);
222             }
223 
224             for (int i = 1; i < mMaxFieldIndex; i++)
225             {
226                 for (int j = 1; j < mMaxFieldIndex; j++)
227                 {
228                     oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
229                     oSheet2.getCellByPosition(i, j).setValue(i * (j + 2));
230                 }
231             }
232         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
233             e.printStackTrace();
234             throw new StatusException("Couldn't fill some cells", e);
235         }
236 
237         // change a value of a cell and check the change in the data pilot
238         // (for the XDataPilotTable.refresh() test)
239         Object oChangeCell = null;
240         Object oCheckCell = null;
241         Integer aChangeValue = null;
242         try {
243             // cell of data
244             oChangeCell = oSheet.getCellByPosition(1, 5);
245             int x = sCellAdress.Column;
246             int y = sCellAdress.Row + 3;
247             // cell of the data pilot output
248             oCheckCell = oSheet.getCellByPosition(x, y);
249             aChangeValue = new Integer(27);
250         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
251             e.printStackTrace();
252             throw new StatusException( "Couldn't get cells for changeing.", e);
253         }
254 
255 
256         // create the test objects
257         System.out.println("Getting test objects") ;
258         XDataPilotTablesSupplier DPTS =
259             UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
260         XDataPilotTables DPT = DPTS.getDataPilotTables();
261         XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
262         DPDsc.setSourceRange(sCellRangeAdress);
263 
264         XPropertySet fieldPropSet = null;
265         try {
266             Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
267             fieldPropSet =
268                 UnoRuntime.queryInterface(XPropertySet.class, oDataPilotField);
269         } catch (com.sun.star.lang.WrappedTargetException e) {
270             e.printStackTrace();
271             throw new StatusException("Couldn't create a test environment", e);
272         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
273             e.printStackTrace();
274             throw new StatusException("Couldn't create a test environment", e);
275         }
276 
277         try {
278             fieldPropSet.setPropertyValue("Function",
279                 com.sun.star.sheet.GeneralFunction.SUM);
280             fieldPropSet.setPropertyValue("Orientation",
281                 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
282         } catch(com.sun.star.lang.WrappedTargetException e) {
283             e.printStackTrace();
284             throw new StatusException("Couldn't create a test environment", e);
285         } catch(com.sun.star.lang.IllegalArgumentException e) {
286             e.printStackTrace();
287             throw new StatusException("Couldn't create a test environment", e);
288         } catch(com.sun.star.beans.PropertyVetoException e) {
289             e.printStackTrace();
290             throw new StatusException("Couldn't create a test environment", e);
291         } catch(com.sun.star.beans.UnknownPropertyException e) {
292             e.printStackTrace();
293             throw new StatusException("Couldn't create a test environment", e);
294         }
295 
296         System.out.println("Insert the DataPilotTable");
297         if (DPT.hasByName("DataPilotTable")) {
298             DPT.removeByName("DataPilotTable");
299         }
300         DPT.insertNewByName("DataPilotTable", sCellAdress, DPDsc);
301         try {
302             mDataPilotTableObject = (XInterface) AnyConverter.toObject(
303                 new Type(XInterface.class),DPT.getByName(DPT.getElementNames()[0]));
304         } catch (com.sun.star.lang.WrappedTargetException e) {
305             e.printStackTrace();
306             throw new StatusException("Couldn't create a test environment", e);
307         } catch (com.sun.star.container.NoSuchElementException e) {
308             e.printStackTrace();
309             throw new StatusException("Couldn't create a test environment", e);
310         } catch (com.sun.star.lang.IllegalArgumentException e) {
311             e.printStackTrace();
312             throw new StatusException("Couldn't create a test environment", e);
313         }
314 
315         XIndexAccess IA = DPDsc.getDataPilotFields();
316         try {
317             mDataPilotFieldObject = (XInterface)AnyConverter.toObject(
318                                 new Type(XInterface.class),IA.getByIndex(0));
319         } catch (com.sun.star.lang.WrappedTargetException e) {
320             e.printStackTrace();
321             throw new StatusException("Couldn't get data pilot field", e);
322         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
323             e.printStackTrace();
324             throw new StatusException("Couldn't get data pilot field", e);
325         } catch (com.sun.star.lang.IllegalArgumentException e) {
326             e.printStackTrace();
327             throw new StatusException("Couldn't get data pilot field", e);
328         }
329 
330         // Other parameters required for interface tests
331         param.put("OUTPUTRANGE", sCellAdress);
332         param.put("CELLFORCHANGE", oChangeCell);
333         param.put("CELLFORCHECK", oCheckCell);
334         param.put("CHANGEVALUE", aChangeValue);
335         param.put("FIELDSAMOUNT", new Integer(5));
336 
337     }
338 
339         /*
340      * this method closes a calc document and resets the corresponding class variable xSheetDoc
341      */
closeSpreadsheetDocument()342     protected boolean closeSpreadsheetDocument() {
343         boolean worked = true;
344 
345         System.out.println("    disposing xSheetDoc ");
346 
347         try {
348             XCloseable oCloser =  UnoRuntime.queryInterface(
349                                          XCloseable.class, xSheetDoc);
350             oCloser.close(true);
351         } catch (com.sun.star.util.CloseVetoException e) {
352             worked = false;
353             System.out.println("Couldn't close document");
354         } catch (com.sun.star.lang.DisposedException e) {
355             worked = false;
356             System.out.println("Document already disposed");
357         } catch (java.lang.NullPointerException e) {
358             worked = false;
359             System.out.println("Couldn't get XCloseable");
360         }
361 
362         xSheetDoc = null;
363 
364         return worked;
365     }
366 
after()367     @After public void after()
368         {
369             closeSpreadsheetDocument();
370         }
371 
372 
setUpConnection()373     @BeforeClass public static void setUpConnection() throws Exception {
374         System.out.println("setUpConnection()");
375         connection.setUp();
376     }
377 
tearDownConnection()378     @AfterClass public static void tearDownConnection()
379         throws InterruptedException, com.sun.star.uno.Exception
380     {
381         System.out.println("tearDownConnection()");
382         connection.tearDown();
383     }
384 
385     private static final OfficeConnection connection = new OfficeConnection();
386 
387 
388 }
389