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