bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotFieldGroupsObj.java
blob3560f61c915fb1728464fff257c0dd083287947b
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package mod._sc;
20 import com.sun.star.beans.XPropertySet;
21 import com.sun.star.container.XIndexAccess;
22 import com.sun.star.container.XNamed;
23 import com.sun.star.lang.XComponent;
24 import com.sun.star.lang.XMultiServiceFactory;
25 import com.sun.star.sheet.DataPilotFieldGroupInfo;
26 import com.sun.star.sheet.XDataPilotDescriptor;
27 import com.sun.star.sheet.XDataPilotFieldGrouping;
28 import com.sun.star.sheet.XDataPilotTables;
29 import com.sun.star.sheet.XDataPilotTablesSupplier;
30 import com.sun.star.sheet.XSpreadsheet;
31 import com.sun.star.sheet.XSpreadsheetDocument;
32 import com.sun.star.sheet.XSpreadsheets;
33 import com.sun.star.table.CellAddress;
34 import com.sun.star.table.CellRangeAddress;
35 import com.sun.star.uno.AnyConverter;
36 import com.sun.star.uno.Type;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
40 import java.io.PrintWriter;
42 import lib.StatusException;
43 import lib.TestCase;
44 import lib.TestEnvironment;
45 import lib.TestParameters;
47 import util.SOfficeFactory;
51 public class ScDataPilotFieldGroupsObj extends TestCase
53 private XSpreadsheetDocument xSheetDoc = null;
55 /**
56 * A field is filled some values. This integer determines the size of the
57 * field in x and y direction.
59 private int mMaxFieldIndex = 6;
61 /**
62 * Creates Spreadsheet document.
64 protected void initialize (TestParameters tParam, PrintWriter log)
66 SOfficeFactory SOF = SOfficeFactory.getFactory (
67 (XMultiServiceFactory) tParam.getMSF ());
69 try
71 log.println ("creating a Spreadsheet document");
72 xSheetDoc = SOF.createCalcDoc (null);
74 catch (com.sun.star.uno.Exception e)
76 // Some exception occurs.FAILED
77 e.printStackTrace (log);
78 throw new StatusException ("Couldn't create document", e);
82 /**
83 * Disposes Spreadsheet document.
85 protected void cleanup (TestParameters tParam, PrintWriter log)
87 log.println (" disposing xSheetDoc ");
89 XComponent oComp = UnoRuntime.queryInterface (
90 XComponent.class, xSheetDoc);
91 util.DesktopTools.closeDoc (oComp);
94 protected synchronized TestEnvironment createTestEnvironment (TestParameters Param,
95 PrintWriter log)
97 XInterface oObj = null;
98 XInterface datapilotfield = null;
101 // creation of testobject here
102 // first we write what we are intend to do to log file
103 log.println ("Creating a test environment");
105 // the cell range
106 CellRangeAddress sCellRangeAdress = new CellRangeAddress ();
107 sCellRangeAdress.Sheet = 0;
108 sCellRangeAdress.StartColumn = 1;
109 sCellRangeAdress.StartRow = 0;
110 sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
111 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
113 // position of the data pilot table
114 CellAddress sCellAdress = new CellAddress ();
115 sCellAdress.Sheet = 0;
116 sCellAdress.Column = 7;
117 sCellAdress.Row = 8;
119 log.println ("Getting a sheet");
121 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets ();
122 XSpreadsheet oSheet = null;
123 XSpreadsheet oSheet2 = null;
124 XIndexAccess oIndexAccess = UnoRuntime.queryInterface (
125 XIndexAccess.class, xSpreadsheets);
126 // Make sure there are at least two sheets
127 xSpreadsheets.insertNewByName("Some Sheet", (short)0);
131 oSheet = (XSpreadsheet) AnyConverter.toObject (
132 new Type (XSpreadsheet.class),
133 oIndexAccess.getByIndex (0));
134 oSheet2 = (XSpreadsheet) AnyConverter.toObject (
135 new Type (XSpreadsheet.class),
136 oIndexAccess.getByIndex (1));
138 catch (com.sun.star.lang.WrappedTargetException e)
140 e.printStackTrace ();
141 throw new StatusException ("Couldn't get a spreadsheet", e);
143 catch (com.sun.star.lang.IndexOutOfBoundsException e)
145 e.printStackTrace ();
146 throw new StatusException ("Couldn't get a spreadsheet", e);
148 catch (com.sun.star.lang.IllegalArgumentException e)
150 e.printStackTrace ();
151 throw new StatusException ("Couldn't get a spreadsheet", e);
156 log.println ("Filling a table");
158 for (int i = 1; i < mMaxFieldIndex; i++)
160 oSheet.getCellByPosition (i, 0).setFormula ("Col" + i);
161 oSheet.getCellByPosition (0, i).setFormula ("Row" + i);
162 oSheet2.getCellByPosition (i, 0).setFormula ("Col" + i);
163 oSheet2.getCellByPosition (0, i).setFormula ("Row" + i);
166 for (int i = 1; i < mMaxFieldIndex; i++)
168 for (int j = 1; j < mMaxFieldIndex; j++)
170 oSheet.getCellByPosition (i, j).setValue (i * (j + 1));
171 oSheet2.getCellByPosition (i, j).setValue (i * (j + 2));
175 oSheet.getCellByPosition (1, 1).setFormula ("aName");
176 oSheet.getCellByPosition (1, 2).setFormula ("otherName");
177 oSheet.getCellByPosition (1, 3).setFormula ("una");
178 oSheet.getCellByPosition (1, 4).setFormula ("otherName");
179 oSheet.getCellByPosition (1, 5).setFormula ("somethingelse");
182 catch (com.sun.star.lang.IndexOutOfBoundsException e)
184 e.printStackTrace ();
185 throw new StatusException ("Couldn't fill some cells", e);
190 oSheet.getCellByPosition (1, 5);
192 int x = sCellAdress.Column;
193 int y = sCellAdress.Row + 3;
196 oSheet.getCellByPosition (x, y);
197 new Integer (27);
199 catch (com.sun.star.lang.IndexOutOfBoundsException e)
201 e.printStackTrace ();
202 throw new StatusException ("Couldn't get cells for changing.", e);
206 // create the test objects
207 log.println ("Getting test objects");
209 XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface (
210 XDataPilotTablesSupplier.class,
211 oSheet);
212 XDataPilotTables DPT = DPTS.getDataPilotTables ();
213 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor ();
214 DPDsc.setSourceRange (sCellRangeAdress);
216 XPropertySet fieldPropSet = null;
220 Object oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (0);
221 fieldPropSet = UnoRuntime.queryInterface (
222 XPropertySet.class, oDataPilotField);
223 fieldPropSet.setPropertyValue ("Orientation",
224 com.sun.star.sheet.DataPilotFieldOrientation.ROW);
225 oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (1);
226 fieldPropSet = UnoRuntime.queryInterface (
227 XPropertySet.class, oDataPilotField);
228 fieldPropSet.setPropertyValue ("Function",
229 com.sun.star.sheet.GeneralFunction.SUM);
230 fieldPropSet.setPropertyValue ("Orientation",
231 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
232 oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (2);
233 fieldPropSet = UnoRuntime.queryInterface (
234 XPropertySet.class, oDataPilotField);
235 fieldPropSet.setPropertyValue ("Orientation",
236 com.sun.star.sheet.DataPilotFieldOrientation.COLUMN);
238 catch (com.sun.star.lang.WrappedTargetException e)
240 e.printStackTrace ();
241 throw new StatusException ("Couldn't create a test environment", e);
243 catch (com.sun.star.lang.IllegalArgumentException e)
245 e.printStackTrace ();
246 throw new StatusException ("Couldn't create a test environment", e);
248 catch (com.sun.star.beans.PropertyVetoException e)
250 e.printStackTrace ();
251 throw new StatusException ("Couldn't create a test environment", e);
253 catch (com.sun.star.beans.UnknownPropertyException e)
255 e.printStackTrace ();
256 throw new StatusException ("Couldn't create a test environment", e);
258 catch (com.sun.star.lang.IndexOutOfBoundsException e)
260 e.printStackTrace ();
261 throw new StatusException ("Couldn't create a test environment", e);
264 log.println ("Insert the DataPilotTable");
266 if (DPT.hasByName ("DataPilotTable"))
268 DPT.removeByName ("DataPilotTable");
271 DPT.insertNewByName ("DataPilotTable", sCellAdress, DPDsc);
272 XIndexAccess xIA = UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ());
273 XIndexAccess IA = null;
276 XDataPilotDescriptor xDPT = UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0));
277 IA = xDPT.getRowFields ();
278 //getSRange(IA);
279 System.out.println ("COUNT: "+IA.getCount ());
280 datapilotfield = (XInterface) AnyConverter.toObject (
281 new Type (XInterface.class), IA.getByIndex (0));
283 catch (com.sun.star.lang.WrappedTargetException e)
285 e.printStackTrace ();
286 throw new StatusException ("Couldn't get data pilot field", e);
288 catch (com.sun.star.lang.IndexOutOfBoundsException e)
290 e.printStackTrace ();
291 throw new StatusException ("Couldn't get data pilot field", e);
293 catch (com.sun.star.lang.IllegalArgumentException e)
295 e.printStackTrace ();
296 throw new StatusException ("Couldn't get data pilot field", e);
301 XDataPilotFieldGrouping dpfg = UnoRuntime.queryInterface (XDataPilotFieldGrouping.class, datapilotfield);
302 String[] elements = new String[]{"aName","otherName"};
303 dpfg.createNameGroup (elements);
304 DataPilotFieldGroupInfo dpgi=null;
305 xIA = UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ());
308 XDataPilotDescriptor xDPT = UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0));
309 IA = xDPT.getRowFields ();
311 catch (com.sun.star.lang.WrappedTargetException e)
313 e.printStackTrace ();
314 throw new StatusException ("Couldn't get data pilot field", e);
316 catch (com.sun.star.lang.IndexOutOfBoundsException e)
318 e.printStackTrace ();
319 throw new StatusException ("Couldn't get data pilot field", e);
321 for (int i=0;i<IA.getCount ();i++)
323 datapilotfield = (XInterface) AnyConverter.toObject (
324 new Type (XInterface.class), IA.getByIndex (i));
325 XPropertySet xPropertySet = UnoRuntime.queryInterface (XPropertySet.class, IA.getByIndex (i));
326 if (((Boolean)xPropertySet.getPropertyValue ("IsGroupField")).booleanValue ())
328 xPropertySet = UnoRuntime.queryInterface (XPropertySet.class, datapilotfield);
329 XNamed xNamed = UnoRuntime.queryInterface (XNamed.class, IA.getByIndex (i));
330 System.out.println ("name: "+xNamed.getName ());
331 dpgi = (DataPilotFieldGroupInfo) xPropertySet.getPropertyValue ("GroupInfo");
334 oObj = dpgi.Groups;
336 catch (Exception e)
338 e.printStackTrace ();
341 log.println ("Creating object - " +
342 ((oObj == null) ? "FAILED" : "OK"));
344 TestEnvironment tEnv = new TestEnvironment (oObj);
346 log.println ("Implementationname: " + util.utils.getImplName (oObj));
348 // Other parameters required for interface tests
349 return tEnv;