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