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