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