xref: /AOO41X/main/qadevOOo/tests/java/mod/_sc/ScIndexEnumeration_DataPilotItemsEnumeration.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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 package mod._sc;
28 
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.container.XEnumerationAccess;
31 import com.sun.star.container.XIndexAccess;
32 import com.sun.star.container.XNamed;
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.sheet.DataPilotFieldOrientation;
36 import com.sun.star.sheet.XDataPilotDescriptor;
37 import com.sun.star.sheet.XDataPilotField;
38 import com.sun.star.sheet.XDataPilotTables;
39 import com.sun.star.sheet.XDataPilotTablesSupplier;
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 
50 import java.io.PrintWriter;
51 
52 import lib.StatusException;
53 import lib.TestCase;
54 import lib.TestEnvironment;
55 import lib.TestParameters;
56 
57 import util.SOfficeFactory;
58 
59 
60 /**
61 * Test for object which is represented by service
62 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
63 * Object implements the following interfaces :
64 * <ul>
65 *  <li> <code>com::sun::star::container::XNamed</code></li>
66 *  <li> <code>com::sun::star::sheet::DataPilotField</code></li>
67 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
68 * </ul>
69 * @see com.sun.star.sheet.DataPilotField
70 * @see com.sun.star.container.XNamed
71 * @see com.sun.star.sheet.DataPilotField
72 * @see com.sun.star.beans.XPropertySet
73 * @see ifc.container._XNamed
74 * @see ifc.sheet._DataPilotField
75 * @see ifc.beans._XPropertySet
76 */
77 public class ScIndexEnumeration_DataPilotItemsEnumeration
78     extends TestCase {
79     static XSpreadsheetDocument xSheetDoc = null;
80 
81     /**
82      * A field is filled some values. This integer determines the size of the
83      * field in x and y direction.
84      */
85     private int mMaxFieldIndex = 6;
86 
87     /**
88     * Creates Spreadsheet document.
89     */
90     protected void initialize(TestParameters tParam, PrintWriter log) {
91         SOfficeFactory SOF = SOfficeFactory.getFactory(
92                                      (XMultiServiceFactory) tParam.getMSF());
93 
94         try {
95             log.println("creating a Spreadsheet document");
96             xSheetDoc = SOF.createCalcDoc(null);
97         } catch (com.sun.star.uno.Exception e) {
98             // Some exception occures.FAILED
99             e.printStackTrace(log);
100             throw new StatusException("Couldn't create document", e);
101         }
102     }
103 
104     /**
105     * Disposes Spreadsheet document.
106     */
107     protected void cleanup(TestParameters tParam, PrintWriter log) {
108         log.println("    disposing xSheetDoc ");
109 
110         XComponent oComp = (XComponent) UnoRuntime.queryInterface(
111                                    XComponent.class, xSheetDoc);
112         util.DesktopTools.closeDoc(oComp);
113     }
114 
115     /**
116     * Creating a Testenvironment for the interfaces to be tested.
117     * Retrieves a collection of spreadsheets from a document
118     * and takes one of them. Fills some table in the spreadsheet.
119     * Obtains the collection of data pilot tables using the interface
120     * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
121     * for the filled table and inserts new data pilot table with this descriptor
122     * to the collection. Obtains the collection of all the data pilot fields
123     * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
124     * the collection the data pilot field with index 0. This data pilot field
125     * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
126     * @see com.sun.star.sheet.DataPilotField
127     * @see com.sun.star.sheet.XDataPilotTablesSupplier
128     * @see com.sun.star.sheet.XDataPilotDescriptor
129     */
130     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
131                                                                  PrintWriter log) {
132         XInterface oObj = null;
133 
134 
135         // creation of testobject here
136         // first we write what we are intend to do to log file
137         log.println("Creating a test environment");
138 
139         // the cell range
140         CellRangeAddress sCellRangeAdress = new CellRangeAddress();
141         sCellRangeAdress.Sheet = 0;
142         sCellRangeAdress.StartColumn = 1;
143         sCellRangeAdress.StartRow = 0;
144         sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
145         sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
146 
147         // position of the data pilot table
148         CellAddress sCellAdress = new CellAddress();
149         sCellAdress.Sheet = 0;
150         sCellAdress.Column = 7;
151         sCellAdress.Row = 8;
152 
153         log.println("Getting a sheet");
154 
155         XSpreadsheets xSpreadsheets = (XSpreadsheets) xSheetDoc.getSheets();
156         XSpreadsheet oSheet = null;
157         XSpreadsheet oSheet2 = null;
158         XIndexAccess oIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
159                                             XIndexAccess.class, xSpreadsheets);
160 
161         try {
162             oSheet = (XSpreadsheet) AnyConverter.toObject(
163                              new Type(XSpreadsheet.class),
164                              oIndexAccess.getByIndex(0));
165             oSheet2 = (XSpreadsheet) AnyConverter.toObject(
166                               new Type(XSpreadsheet.class),
167                               oIndexAccess.getByIndex(1));
168         } catch (com.sun.star.lang.WrappedTargetException e) {
169             e.printStackTrace();
170             throw new StatusException("Couldn't get a spreadsheet", e);
171         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
172             e.printStackTrace();
173             throw new StatusException("Couldn't get a spreadsheet", e);
174         } catch (com.sun.star.lang.IllegalArgumentException e) {
175             e.printStackTrace();
176             throw new StatusException("Couldn't get a spreadsheet", e);
177         }
178 
179         try {
180             log.println("Filling a table");
181 
182             for (int i = 1; i < mMaxFieldIndex; i++) {
183                 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
184                 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
185                 oSheet2.getCellByPosition(i, 0).setFormula("Col" + i);
186                 oSheet2.getCellByPosition(0, i).setFormula("Row" + i);
187             }
188 
189             for (int i = 1; i < mMaxFieldIndex; i++)
190                 for (int j = 1; j < mMaxFieldIndex; j++) {
191                     oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
192                     oSheet2.getCellByPosition(i, j).setValue(i * (j + 2));
193                 }
194         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
195             e.printStackTrace();
196             throw new StatusException("Couldn't fill some cells", e);
197         }
198 
199         // change a value of a cell and check the change in the data pilot
200         // (for the XDataPilotTable.refresh() test)
201         Object oChangeCell = null;
202         Object oCheckCell = null;
203         Integer aChangeValue = null;
204 
205         try {
206             // cell of data
207             oChangeCell = oSheet.getCellByPosition(1, 5);
208 
209             int x = sCellAdress.Column;
210             int y = sCellAdress.Row + 3;
211 
212 
213             // cell of the data pilot output
214             oCheckCell = oSheet.getCellByPosition(x, y);
215             aChangeValue = new Integer(27);
216         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
217             e.printStackTrace();
218             throw new StatusException("Couldn't get cells for changeing.", e);
219         }
220 
221 
222         // create the test objects
223         log.println("Getting test objects");
224 
225         XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier) UnoRuntime.queryInterface(
226                                                 XDataPilotTablesSupplier.class,
227                                                 oSheet);
228         XDataPilotTables DPT = DPTS.getDataPilotTables();
229         XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
230         DPDsc.setSourceRange(sCellRangeAdress);
231 
232         XPropertySet fieldPropSet = null;
233 
234         try {
235             Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
236             fieldPropSet = (XPropertySet) UnoRuntime.queryInterface(
237                                    XPropertySet.class, oDataPilotField);
238         } catch (com.sun.star.lang.WrappedTargetException e) {
239             e.printStackTrace();
240             throw new StatusException("Couldn't create a test environment", e);
241         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
242             e.printStackTrace();
243             throw new StatusException("Couldn't create a test environment", e);
244         }
245 
246         try {
247             fieldPropSet.setPropertyValue("Function",
248                                           com.sun.star.sheet.GeneralFunction.SUM);
249             fieldPropSet.setPropertyValue("Orientation",
250                                           com.sun.star.sheet.DataPilotFieldOrientation.DATA);
251         } catch (com.sun.star.lang.WrappedTargetException e) {
252             e.printStackTrace();
253             throw new StatusException("Couldn't create a test environment", e);
254         } catch (com.sun.star.lang.IllegalArgumentException e) {
255             e.printStackTrace();
256             throw new StatusException("Couldn't create a test environment", e);
257         } catch (com.sun.star.beans.PropertyVetoException e) {
258             e.printStackTrace();
259             throw new StatusException("Couldn't create a test environment", e);
260         } catch (com.sun.star.beans.UnknownPropertyException e) {
261             e.printStackTrace();
262             throw new StatusException("Couldn't create a test environment", e);
263         }
264 
265         log.println("Insert the DataPilotTable");
266 
267         if (DPT.hasByName("DataPilotTable")) {
268             DPT.removeByName("DataPilotTable");
269         }
270 
271         XIndexAccess IA = DPDsc.getDataPilotFields();
272         getSRange(IA);
273 
274         DPT.insertNewByName("DataPilotTable", sCellAdress, DPDsc);
275 
276         try {
277             oObj = (XInterface) AnyConverter.toObject(
278                            new Type(XInterface.class), IA.getByIndex(0));
279         } catch (com.sun.star.lang.WrappedTargetException e) {
280             e.printStackTrace();
281             throw new StatusException("Couldn't get data pilot field", e);
282         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
283             e.printStackTrace();
284             throw new StatusException("Couldn't get data pilot field", e);
285         } catch (com.sun.star.lang.IllegalArgumentException e) {
286             e.printStackTrace();
287             throw new StatusException("Couldn't get data pilot field", e);
288         }
289 
290         log.println("Creating object - " +
291                     ((oObj == null) ? "FAILED" : "OK"));
292 
293         XDataPilotField xDataPilotField = (XDataPilotField) UnoRuntime.queryInterface(
294                                                   XDataPilotField.class, oObj);
295 
296         XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime.queryInterface(
297                                                         XEnumerationAccess.class,
298                                                         xDataPilotField.getItems());
299 
300         oObj = xEnumerationAccess.createEnumeration();
301 
302         TestEnvironment tEnv = new TestEnvironment(oObj);
303 
304         log.println("Implementationname: " + util.utils.getImplName(oObj));
305 
306         // Other parameters required for interface tests
307         return tEnv;
308     }
309 
310     private void getSRange(XIndexAccess IA) {
311         int fieldsAmount = IA.getCount() + 1;
312 
313         String[] fieldsNames = new String[fieldsAmount];
314 
315         int i = -1;
316         int cnt = 0;
317 
318         while ((++i) < fieldsAmount) {
319             Object field;
320 
321             try {
322                 field = IA.getByIndex(i);
323             } catch (com.sun.star.lang.WrappedTargetException e) {
324                 e.printStackTrace(log);
325 
326                 return;
327             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
328                 e.printStackTrace(log);
329 
330                 return;
331             }
332 
333             XNamed named = (XNamed) UnoRuntime.queryInterface(XNamed.class,
334                                                               field);
335             String name = named.getName();
336 
337             log.println("**Field : '" + name + "' ... ");
338 
339             if (!name.equals("Data")) {
340                 fieldsNames[cnt] = name;
341 
342                 XPropertySet props = (XPropertySet) UnoRuntime.queryInterface(
343                                              XPropertySet.class, field);
344 
345                 try {
346                     switch (cnt % 5) {
347                     case 0:
348                         props.setPropertyValue("Orientation",
349                                                DataPilotFieldOrientation.COLUMN);
350                         log.println("  Column");
351 
352                         break;
353 
354                     case 1:
355                         props.setPropertyValue("Orientation",
356                                                DataPilotFieldOrientation.ROW);
357                         log.println("  Row");
358 
359                         break;
360 
361                     case 2:
362                         props.setPropertyValue("Orientation",
363                                                DataPilotFieldOrientation.DATA);
364                         log.println("  Data");
365 
366                         break;
367 
368                     case 3:
369                         props.setPropertyValue("Orientation",
370                                                DataPilotFieldOrientation.HIDDEN);
371                         log.println("  Hidden");
372 
373                         break;
374 
375                     case 4:
376                         props.setPropertyValue("Orientation",
377                                                DataPilotFieldOrientation.PAGE);
378                         log.println("  Page");
379 
380                         break;
381                     }
382                 } catch (com.sun.star.lang.WrappedTargetException e) {
383                     e.printStackTrace(log);
384 
385                     return;
386                 } catch (com.sun.star.lang.IllegalArgumentException e) {
387                     e.printStackTrace(log);
388 
389                     return;
390                 } catch (com.sun.star.beans.PropertyVetoException e) {
391                     e.printStackTrace(log);
392 
393                     return;
394                 } catch (com.sun.star.beans.UnknownPropertyException e) {
395                     e.printStackTrace(log);
396 
397                     return;
398                 }
399 
400                 if ((++cnt) > 4) {
401                     break;
402                 }
403             } else {
404                 return;
405             }
406         }
407     }
408 }
409