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 ScDataPilotItemObj 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         XIndexAccess xIA = xDataPilotField.getItems();
295 
296         try {
297             oObj = (XInterface) UnoRuntime.queryInterface(XInterface.class,
298                                                           xIA.getByIndex(0));
299         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
300             log.println("Couldn't get DataPilotItemObj");
301             e.printStackTrace(log);
302         } catch (com.sun.star.lang.WrappedTargetException e) {
303             log.println("Couldn't get DataPilotItemObj");
304             e.printStackTrace(log);
305         }
306 
307         TestEnvironment tEnv = new TestEnvironment(oObj);
308 
309         log.println("Implementationname: " + util.utils.getImplName(oObj));
310 
311         // Other parameters required for interface tests
312         tEnv.addObjRelation("NoSetName",  "ScDataPilotItemObj");
313         return tEnv;
314     }
315 
316     private void getSRange(XIndexAccess IA) {
317         int fieldsAmount = IA.getCount() + 1;
318 
319         String[] fieldsNames = new String[fieldsAmount];
320 
321         int i = -1;
322         int cnt = 0;
323 
324         while ((++i) < fieldsAmount) {
325             Object field;
326 
327             try {
328                 field = IA.getByIndex(i);
329             } catch (com.sun.star.lang.WrappedTargetException e) {
330                 e.printStackTrace(log);
331 
332                 return;
333             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
334                 e.printStackTrace(log);
335 
336                 return;
337             }
338 
339             XNamed named = (XNamed) UnoRuntime.queryInterface(XNamed.class,
340                                                               field);
341             String name = named.getName();
342 
343             log.println("**Field : '" + name + "' ... ");
344 
345             if (!name.equals("Data")) {
346                 fieldsNames[cnt] = name;
347 
348                 XPropertySet props = (XPropertySet) UnoRuntime.queryInterface(
349                                              XPropertySet.class, field);
350 
351                 try {
352                     switch (cnt % 5) {
353                     case 0:
354                         props.setPropertyValue("Orientation",
355                                                DataPilotFieldOrientation.COLUMN);
356                         log.println("  Column");
357 
358                         break;
359 
360                     case 1:
361                         props.setPropertyValue("Orientation",
362                                                DataPilotFieldOrientation.ROW);
363                         log.println("  Row");
364 
365                         break;
366 
367                     case 2:
368                         props.setPropertyValue("Orientation",
369                                                DataPilotFieldOrientation.DATA);
370                         log.println("  Data");
371 
372                         break;
373 
374                     case 3:
375                         props.setPropertyValue("Orientation",
376                                                DataPilotFieldOrientation.HIDDEN);
377                         log.println("  Hidden");
378 
379                         break;
380 
381                     case 4:
382                         props.setPropertyValue("Orientation",
383                                                DataPilotFieldOrientation.PAGE);
384                         log.println("  Page");
385 
386                         break;
387                     }
388                 } catch (com.sun.star.lang.WrappedTargetException e) {
389                     e.printStackTrace(log);
390 
391                     return;
392                 } catch (com.sun.star.lang.IllegalArgumentException e) {
393                     e.printStackTrace(log);
394 
395                     return;
396                 } catch (com.sun.star.beans.PropertyVetoException e) {
397                     e.printStackTrace(log);
398 
399                     return;
400                 } catch (com.sun.star.beans.UnknownPropertyException e) {
401                     e.printStackTrace(log);
402 
403                     return;
404                 }
405 
406                 if ((++cnt) > 4) {
407                     break;
408                 }
409             } else {
410                 return;
411             }
412         }
413     }
414 }
415