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