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