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