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