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 
24 package mod._sc;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.TableFilterField;
39 import com.sun.star.sheet.XDataPilotDescriptor;
40 import com.sun.star.sheet.XDataPilotTables;
41 import com.sun.star.sheet.XDataPilotTablesSupplier;
42 import com.sun.star.sheet.XSheetFilterDescriptor;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.sheet.XSpreadsheets;
46 import com.sun.star.table.CellAddress;
47 import com.sun.star.table.CellRangeAddress;
48 import com.sun.star.uno.AnyConverter;
49 import com.sun.star.uno.Type;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XInterface;
52 
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.sheet.DataPilotTable</code>. <p>
56 * Object implements the following interfaces :
57 * <ul>
58 *  <li> <code>com::sun::star::container::XNamed</code></li>
59 *  <li> <code>com::sun::star::sheet::XDataPilotTable</code></li>
60 *  <li> <code>com::sun::star::sheet::XDataPilotDescriptor</code></li>
61 * </ul>
62 * @see com.sun.star.sheet.DataPilotTable
63 * @see com.sun.star.container.XNamed
64 * @see com.sun.star.sheet.XDataPilotTable
65 * @see com.sun.star.sheet.XDataPilotDescriptor
66 * @see ifc.container._XNamed
67 * @see ifc.sheet._XDataPilotTable
68 * @see ifc.sheet._XDataPilotDescriptor
69 */
70 public class ScDataPilotTableObj extends TestCase {
71     static XSpreadsheetDocument xSheetDoc = null;
72 
73     /**
74     * Creates Spreadsheet document.
75     */
initialize( TestParameters tParam, PrintWriter log )76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
78 
79         try {
80             log.println( "creating a Spreadsheet document" );
81             xSheetDoc = SOF.createCalcDoc(null);
82         } catch ( com.sun.star.uno.Exception e ) {
83             // Some exception occured.FAILED
84             e.printStackTrace( log );
85             throw new StatusException( "Couldn't create document", e );
86         }
87 
88     }
89 
90     /**
91     * Disposes Spreadsheet document.
92     */
cleanup( TestParameters tParam, PrintWriter log )93     protected void cleanup( TestParameters tParam, PrintWriter log ) {
94         log.println( "    disposing xSheetDoc " );
95         XComponent oComp = (XComponent)
96             UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ;
97         util.DesktopTools.closeDoc(oComp);
98     }
99 
100     /**
101     * Creating a Testenvironment for the interfaces to be tested.
102     * Retrieves a collection of spreadsheets from a document
103     * and takes one of them. Fills some table in the spreadsheet.
104     * Obtains the collection of data pilot tables using the interface
105     * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
106     * for the filled table. Obtains the collection of all the data pilot fields
107     * using the interface <code>XDataPilotDescriptor</code>. Sets field
108     * orientation for every of the data pilot fields. Sets the definitions of
109     * the filter fields for the created description. Inserts new data pilot
110     * table with this descriptor to the collection.
111     * This new data pilot table is the instance of the service
112     * <code>com.sun.star.sheet.DataPilotTable</code>.
113     * Object relations created :
114     * <ul>
115     *  <li> <code>'OUTPUTRANGE'</code> for
116     *      {@link ifc.sheet._XDataPilotTable}(the cell range address of the
117     *      created data pilot table) </li>
118     * <li> <code>'FIELDSAMOUNT'</code> for
119     *      {@link ifc.sheet._XDataPilotDescriptor}(the number of filled fields
120     *      which descriptor was created for) </li>
121     *  <li> <code>'CELLFORCHANGE'</code> for
122     *      {@link ifc.sheet._XDataPilotTable}(value of this cell will be changed)</li>
123     *  <li> <code>'CELLFORCHECK'</code> for
124     *      {@link ifc.sheet._XDataPilotTable}(value of this cell must be changed
125     *      after refresh call)</li>
126     * </ul>
127     * @see com.sun.star.sheet.DataPilotTable
128     * @see com.sun.star.sheet.XDataPilotTablesSupplier
129     * @see com.sun.star.sheet.XDataPilotDescriptor
130     */
createTestEnvironment(TestParameters Param, PrintWriter log)131     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
132 
133         XInterface oObj = null;
134 
135         CellAddress sCellAddress = new CellAddress();
136         sCellAddress.Sheet = 0;
137         sCellAddress.Column = 7;
138         sCellAddress.Row = 8;
139 
140         // creation of testobject here
141         // first we write what we are intend to do to log file
142         log.println( "Creating a test environment" );
143         log.println("getting sheets");
144         XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
145         XIndexAccess oIndexAccess = (XIndexAccess)
146             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
147         XSpreadsheet oSheet = null;
148         Object oChangeCell = null;
149         Object oCheckCell = null;
150         try {
151             oSheet = (XSpreadsheet) AnyConverter.toObject(
152                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
153             oChangeCell = oSheet.getCellByPosition(1, 5);
154             oCheckCell = oSheet.getCellByPosition(
155                 sCellAddress.Column, sCellAddress.Row + 3);
156         } catch (com.sun.star.lang.WrappedTargetException e) {
157             e.printStackTrace(log);
158             throw new StatusException( "Couldn't get a spreadsheet", e);
159         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
160             e.printStackTrace(log);
161             throw new StatusException( "Couldn't get a spreadsheet", e);
162         } catch (com.sun.star.lang.IllegalArgumentException e) {
163             e.printStackTrace(log);
164             throw new StatusException( "Couldn't get a spreadsheet", e);
165         }
166         log.println("Getting sheet - " + (oSheet == null ? "FAILED" : "OK"));
167         try {
168             log.println("Filing a table");
169             for (int i = 1; i < 6; i++) {
170                 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
171                 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
172             }
173 
174             for (int i = 1; i < 6; i++)
175                 for (int j = 1; j < 6; j++) {
176                     oSheet.getCellByPosition(i, j).setValue(2.5 * j + i);
177                 }
178         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
179             e.printStackTrace(log);
180             throw new StatusException("Couldn't fill some cells", e);
181         }
182 
183         CellRangeAddress sCellRangeAddress = new CellRangeAddress();
184         sCellRangeAddress.Sheet = 0;
185         sCellRangeAddress.StartColumn = 1;
186         sCellRangeAddress.StartRow = 0;
187         sCellRangeAddress.EndColumn = 5;
188         sCellRangeAddress.EndRow = 5;
189 
190         TableFilterField[] filterFields = new TableFilterField[2];
191         filterFields[0] = new TableFilterField();
192         filterFields[0].Connection = com.sun.star.sheet.FilterConnection.AND;
193         filterFields[0].Field = 1;
194         filterFields[0].IsNumeric = true;
195         filterFields[0].NumericValue = 4;
196         filterFields[0].Operator = com.sun.star.sheet.FilterOperator.GREATER;
197         filterFields[1] = new TableFilterField();
198         filterFields[1].Connection = com.sun.star.sheet.FilterConnection.AND;
199         filterFields[1].Field = 1;
200         filterFields[1].IsNumeric = true;
201         filterFields[1].NumericValue = 12;
202         filterFields[1].Operator = com.sun.star.sheet.FilterOperator.LESS_EQUAL;
203 
204         XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier)
205             UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
206         log.println("Getting test object");
207         XDataPilotTables DPT = DPTS.getDataPilotTables();
208         XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
209         DPDsc.setSourceRange(sCellRangeAddress);
210 
211         XSheetFilterDescriptor SFD = DPDsc.getFilterDescriptor();
212         SFD.setFilterFields(filterFields);
213 
214         XPropertySet fieldPropSet = null;
215         try {
216             Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
217             fieldPropSet = (XPropertySet)
218                 UnoRuntime.queryInterface(XPropertySet.class, oDataPilotField);
219         } catch (com.sun.star.lang.WrappedTargetException e) {
220             e.printStackTrace(log);
221             throw new StatusException("Couldn't create a test environment", e);
222         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
223             e.printStackTrace(log);
224             throw new StatusException("Couldn't create a test environment", e);
225         }
226 
227         try {
228             fieldPropSet.setPropertyValue("Function",
229                 com.sun.star.sheet.GeneralFunction.SUM);
230             fieldPropSet.setPropertyValue("Orientation",
231                 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
232         } catch(com.sun.star.lang.WrappedTargetException e) {
233             e.printStackTrace(log);
234             throw new StatusException("Couldn't create a test environment", e);
235         } catch(com.sun.star.lang.IllegalArgumentException e) {
236             e.printStackTrace(log);
237             throw new StatusException("Couldn't create a test environment", e);
238         } catch(com.sun.star.beans.PropertyVetoException e) {
239             e.printStackTrace(log);
240             throw new StatusException("Couldn't create a test environment", e);
241         } catch(com.sun.star.beans.UnknownPropertyException e) {
242             e.printStackTrace(log);
243             throw new StatusException("Couldn't create a test environment", e);
244         }
245 
246         if (DPT.hasByName("DataPilotTable")) {
247             DPT.removeByName("DataPilotTable");
248         }
249         DPT.insertNewByName("DataPilotTable", sCellAddress, DPDsc);
250         try {
251             oObj = (XInterface) AnyConverter.toObject(
252                 new Type(XInterface.class),DPT.getByName(DPT.getElementNames()[0]));
253         } catch (com.sun.star.lang.WrappedTargetException e) {
254             e.printStackTrace(log);
255             throw new StatusException("Couldn't create a test environment", e);
256         } catch (com.sun.star.container.NoSuchElementException e) {
257             e.printStackTrace(log);
258             throw new StatusException("Couldn't create a test environment", e);
259         } catch (com.sun.star.lang.IllegalArgumentException e) {
260             e.printStackTrace(log);
261             throw new StatusException("Couldn't create a test environment", e);
262         }
263 
264         log.println("Creating object - " +
265                                     ((oObj == null) ? "FAILED" : "OK"));
266 
267         TestEnvironment tEnv = new TestEnvironment( oObj );
268 
269         // Other parameters required for interface tests
270         tEnv.addObjRelation("OUTPUTRANGE", sCellAddress);
271         tEnv.addObjRelation("CELLFORCHANGE", oChangeCell);
272         tEnv.addObjRelation("CELLFORCHECK", oCheckCell);
273         tEnv.addObjRelation("FIELDSAMOUNT", new Integer(5));
274         tEnv.addObjRelation("SHEETDOCUMENT", xSheetDoc);
275 
276         createTable2(oSheet, sCellRangeAddress, tEnv);
277 
278         return tEnv;
279     }
280 
281     /**
282      * Create a new DataPilot table output for use with testing XDataPilotTable2
283      * interface.
284      *
285      * @param oSheet current sheet instance
286      * @param srcRange source range
287      * @param tEnv test environment instance
288      */
createTable2(XSpreadsheet oSheet, CellRangeAddress srcRange, TestEnvironment tEnv)289     private void createTable2(XSpreadsheet oSheet, CellRangeAddress srcRange, TestEnvironment tEnv)
290     {
291         XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier)
292             UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
293         log.println("Creating test table object");
294         XDataPilotTables DPT = DPTS.getDataPilotTables();
295         XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
296         DPDsc.setSourceRange(srcRange);
297 
298         XIndexAccess xIA = DPDsc.getDataPilotFields();
299         int fieldCount = xIA.getCount() - 1; // skip the last field because it's always hidden.
300         try
301         {
302             for (int i = 0; i < fieldCount; ++i)
303             {
304                 Object o = xIA.getByIndex(i);
305                 XPropertySet fieldPropSet = (XPropertySet)UnoRuntime.queryInterface(
306                     XPropertySet.class, o);
307 
308                 if (i == fieldCount - 1)
309                 {
310                     // last field
311                     fieldPropSet.setPropertyValue(
312                         "Function", com.sun.star.sheet.GeneralFunction.SUM);
313                     fieldPropSet.setPropertyValue(
314                         "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.DATA);
315                 }
316                 else if (i%2 == 0)
317                 {
318                     // even number fields
319                     fieldPropSet.setPropertyValue(
320                         "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.COLUMN);
321                 }
322                 else if (i%2 == 1)
323                 {
324                     // odd number fields
325                     fieldPropSet.setPropertyValue(
326                         "Orientation", com.sun.star.sheet.DataPilotFieldOrientation.ROW);
327                 }
328             }
329 
330             if (DPT.hasByName("DataPilotTable2"))
331                 DPT.removeByName("DataPilotTable2");
332 
333             CellAddress destAddr = new CellAddress();
334             destAddr.Sheet = 0;
335             destAddr.Column = 0;
336             destAddr.Row = 14;
337             DPT.insertNewByName("DataPilotTable2", destAddr, DPDsc);
338 
339             Object o = DPT.getByName("DataPilotTable2");
340             tEnv.addObjRelation("DATAPILOTTABLE2", o);
341         }
342         catch (com.sun.star.uno.Exception e)
343         {
344             e.printStackTrace(log);
345             throw new StatusException("Couldn't create a test environment", e);
346         }
347     }
348 
349 }
350 
351 
352