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.DataPilotFieldGroupInfo;
31 import com.sun.star.sheet.XDataPilotDescriptor;
32 import com.sun.star.sheet.XDataPilotFieldGrouping;
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 public class ScDataPilotFieldGroupObj extends TestCase
57 {
58     static XSpreadsheetDocument xSheetDoc = null;
59 
60     /**
61      * A field is filled some values. This integer determines the size of the
62      * field in x and y direction.
63      */
64     private int mMaxFieldIndex = 6;
65 
66     /**
67      * Creates Spreadsheet document.
68      */
initialize(TestParameters tParam, PrintWriter log)69     protected void initialize (TestParameters tParam, PrintWriter log)
70     {
71         SOfficeFactory SOF = SOfficeFactory.getFactory (
72             (XMultiServiceFactory) tParam.getMSF ());
73 
74         try
75         {
76             log.println ("creating a Spreadsheet document");
77             xSheetDoc = SOF.createCalcDoc (null);
78         }
79         catch (com.sun.star.uno.Exception e)
80         {
81             // Some exception occures.FAILED
82             e.printStackTrace (log);
83             throw new StatusException ("Couldn't create document", e);
84         }
85     }
86 
87     /**
88      * Disposes Spreadsheet document.
89      */
cleanup(TestParameters tParam, PrintWriter log)90     protected void cleanup (TestParameters tParam, PrintWriter log)
91     {
92         log.println ("    disposing xSheetDoc ");
93 
94         XComponent oComp = (XComponent) UnoRuntime.queryInterface (
95             XComponent.class, xSheetDoc);
96         util.DesktopTools.closeDoc (oComp);
97     }
98 
createTestEnvironment(TestParameters Param, PrintWriter log)99     protected synchronized TestEnvironment createTestEnvironment (TestParameters Param,
100         PrintWriter log)
101     {
102         XInterface oObj = null;
103         XInterface datapilotfield = null;
104         XInterface groups = null;
105 
106         // creation of testobject here
107         // first we write what we are intend to do to log file
108         log.println ("Creating a test environment");
109 
110         // the cell range
111         CellRangeAddress sCellRangeAdress = new CellRangeAddress ();
112         sCellRangeAdress.Sheet = 0;
113         sCellRangeAdress.StartColumn = 1;
114         sCellRangeAdress.StartRow = 0;
115         sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
116         sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
117 
118         // position of the data pilot table
119         CellAddress sCellAdress = new CellAddress ();
120         sCellAdress.Sheet = 0;
121         sCellAdress.Column = 7;
122         sCellAdress.Row = 8;
123 
124         log.println ("Getting a sheet");
125 
126         XSpreadsheets xSpreadsheets = (XSpreadsheets) xSheetDoc.getSheets ();
127         XSpreadsheet oSheet = null;
128         XSpreadsheet oSheet2 = null;
129         XIndexAccess oIndexAccess = (XIndexAccess) UnoRuntime.queryInterface (
130             XIndexAccess.class, xSpreadsheets);
131 
132         try
133         {
134             oSheet = (XSpreadsheet) AnyConverter.toObject (
135                 new Type (XSpreadsheet.class),
136                 oIndexAccess.getByIndex (0));
137             oSheet2 = (XSpreadsheet) AnyConverter.toObject (
138                 new Type (XSpreadsheet.class),
139                 oIndexAccess.getByIndex (1));
140         }
141         catch (com.sun.star.lang.WrappedTargetException e)
142         {
143             e.printStackTrace ();
144             throw new StatusException ("Couldn't get a spreadsheet", e);
145         }
146         catch (com.sun.star.lang.IndexOutOfBoundsException e)
147         {
148             e.printStackTrace ();
149             throw new StatusException ("Couldn't get a spreadsheet", e);
150         }
151         catch (com.sun.star.lang.IllegalArgumentException e)
152         {
153             e.printStackTrace ();
154             throw new StatusException ("Couldn't get a spreadsheet", e);
155         }
156 
157         try
158         {
159             log.println ("Filling a table");
160 
161             for (int i = 1; i < mMaxFieldIndex; i++)
162             {
163                 oSheet.getCellByPosition (i, 0).setFormula ("Col" + i);
164                 oSheet.getCellByPosition (0, i).setFormula ("Row" + i);
165                 oSheet2.getCellByPosition (i, 0).setFormula ("Col" + i);
166                 oSheet2.getCellByPosition (0, i).setFormula ("Row" + i);
167             }
168 
169             for (int i = 1; i < mMaxFieldIndex; i++)
170             {
171                 for (int j = 1; j < mMaxFieldIndex; j++)
172                 {
173                     oSheet.getCellByPosition (i, j).setValue (i * (j + 1));
174                     oSheet2.getCellByPosition (i, j).setValue (i * (j + 2));
175                 }
176             }
177 
178             oSheet.getCellByPosition (1, 1).setFormula ("aName");
179             oSheet.getCellByPosition (1, 2).setFormula ("otherName");
180             oSheet.getCellByPosition (1, 3).setFormula ("una");
181             oSheet.getCellByPosition (1, 4).setFormula ("otherName");
182             oSheet.getCellByPosition (1, 5).setFormula ("somethingelse");
183 
184         }
185         catch (com.sun.star.lang.IndexOutOfBoundsException e)
186         {
187             e.printStackTrace ();
188             throw new StatusException ("Couldn't fill some cells", e);
189         }
190 
191         // change a value of a cell and check the change in the data pilot
192         // (for the XDataPilotTable.refresh() test)
193         Object oChangeCell = null;
194         Object oCheckCell = null;
195         Integer aChangeValue = null;
196 
197         try
198         {
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         }
210         catch (com.sun.star.lang.IndexOutOfBoundsException e)
211         {
212             e.printStackTrace ();
213             throw new StatusException ("Couldn't get cells for changeing.", e);
214         }
215 
216 
217         // create the test objects
218         log.println ("Getting test objects");
219 
220         XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier) UnoRuntime.queryInterface (
221             XDataPilotTablesSupplier.class,
222             oSheet);
223         XDataPilotTables DPT = DPTS.getDataPilotTables ();
224         XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor ();
225         DPDsc.setSourceRange (sCellRangeAdress);
226 
227         XPropertySet fieldPropSet = null;
228 
229         try
230         {
231             Object oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (0);
232             fieldPropSet = (XPropertySet) UnoRuntime.queryInterface (
233                 XPropertySet.class, oDataPilotField);
234             fieldPropSet.setPropertyValue ("Orientation",
235                 com.sun.star.sheet.DataPilotFieldOrientation.ROW);
236             oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (1);
237             fieldPropSet = (XPropertySet) UnoRuntime.queryInterface (
238                 XPropertySet.class, oDataPilotField);
239             fieldPropSet.setPropertyValue ("Function",
240                 com.sun.star.sheet.GeneralFunction.SUM);
241             fieldPropSet.setPropertyValue ("Orientation",
242                 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
243             oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (2);
244             fieldPropSet = (XPropertySet) UnoRuntime.queryInterface (
245                 XPropertySet.class, oDataPilotField);
246             fieldPropSet.setPropertyValue ("Orientation",
247                 com.sun.star.sheet.DataPilotFieldOrientation.COLUMN);
248         }
249         catch (com.sun.star.lang.WrappedTargetException e)
250         {
251             e.printStackTrace ();
252             throw new StatusException ("Couldn't create a test environment", e);
253         }
254         catch (com.sun.star.lang.IllegalArgumentException e)
255         {
256             e.printStackTrace ();
257             throw new StatusException ("Couldn't create a test environment", e);
258         }
259         catch (com.sun.star.beans.PropertyVetoException e)
260         {
261             e.printStackTrace ();
262             throw new StatusException ("Couldn't create a test environment", e);
263         }
264         catch (com.sun.star.beans.UnknownPropertyException e)
265         {
266             e.printStackTrace ();
267             throw new StatusException ("Couldn't create a test environment", e);
268         }
269         catch (com.sun.star.lang.IndexOutOfBoundsException e)
270         {
271             e.printStackTrace ();
272             throw new StatusException ("Couldn't create a test environment", e);
273         }
274 
275         log.println ("Insert the DataPilotTable");
276 
277         if (DPT.hasByName ("DataPilotTable"))
278         {
279             DPT.removeByName ("DataPilotTable");
280         }
281 
282         DPT.insertNewByName ("DataPilotTable", sCellAdress, DPDsc);
283         XIndexAccess xIA = (XIndexAccess) UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ());
284         XIndexAccess IA = null;
285         try
286         {
287             XDataPilotDescriptor xDPT = (XDataPilotDescriptor) UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0));
288             IA = xDPT.getRowFields ();
289             //getSRange(IA);
290             System.out.println ("COUNT: "+IA.getCount ());
291             datapilotfield = (XInterface) AnyConverter.toObject (
292                 new Type (XInterface.class), IA.getByIndex (0));
293         }
294         catch (com.sun.star.lang.WrappedTargetException e)
295         {
296             e.printStackTrace ();
297             throw new StatusException ("Couldn't get data pilot field", e);
298         }
299         catch (com.sun.star.lang.IndexOutOfBoundsException e)
300         {
301             e.printStackTrace ();
302             throw new StatusException ("Couldn't get data pilot field", e);
303         }
304         catch (com.sun.star.lang.IllegalArgumentException e)
305         {
306             e.printStackTrace ();
307             throw new StatusException ("Couldn't get data pilot field", e);
308         }
309 
310         try
311         {
312             XDataPilotFieldGrouping  dpfg = (XDataPilotFieldGrouping) UnoRuntime.queryInterface (XDataPilotFieldGrouping.class, datapilotfield);
313             String[] elements = new String[]{"aName","otherName"};
314             dpfg.createNameGroup (elements);
315             DataPilotFieldGroupInfo dpgi=null;
316             xIA = (XIndexAccess) UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ());
317         try
318         {
319             XDataPilotDescriptor xDPT = (XDataPilotDescriptor) UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0));
320             IA = xDPT.getRowFields ();
321         }
322         catch (com.sun.star.lang.WrappedTargetException e)
323         {
324             e.printStackTrace ();
325             throw new StatusException ("Couldn't get data pilot field", e);
326         }
327         catch (com.sun.star.lang.IndexOutOfBoundsException e)
328         {
329             e.printStackTrace ();
330             throw new StatusException ("Couldn't get data pilot field", e);
331         }
332             for (int i=0;i<IA.getCount ();i++)
333             {
334                 datapilotfield = (XInterface) AnyConverter.toObject (
335                     new Type (XInterface.class), IA.getByIndex (i));
336                 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface (XPropertySet.class, IA.getByIndex (i));
337                 if (((Boolean)xPropertySet.getPropertyValue ("IsGroupField")).booleanValue ())
338                 {
339                     xPropertySet = (XPropertySet) UnoRuntime.queryInterface (XPropertySet.class, datapilotfield);
340                     XNamed xNamed = (XNamed) UnoRuntime.queryInterface (XNamed.class, IA.getByIndex (i));
341                     System.out.println ("name: "+xNamed.getName ());
342                     dpgi = (DataPilotFieldGroupInfo) xPropertySet.getPropertyValue ("GroupInfo");
343                 }
344             }
345             groups = dpgi.Groups;
346             XIndexAccess groupAccess = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, groups);
347             oObj = (XInterface) UnoRuntime.queryInterface(XInterface.class, groupAccess.getByIndex(0));
348         }
349         catch (Exception e)
350         {
351             e.printStackTrace ();
352         }
353 
354         log.println ("Creating object - " +
355             ((oObj == null) ? "FAILED" : "OK"));
356 
357         TestEnvironment tEnv = new TestEnvironment (oObj);
358 
359         log.println ("Implementationname: " + util.utils.getImplName (oObj));
360 
361         // Other parameters required for interface tests
362         return tEnv;
363     }
364 
365 }
366