bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDataPilotFieldGroupItemObj.java
blobe18f8990c2298506e9ebfc30cdb652f6a9afef87
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.XNameAccess;
23 import com.sun.star.container.XNamed;
24 import com.sun.star.lang.XComponent;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.sheet.DataPilotFieldGroupInfo;
27 import com.sun.star.sheet.XDataPilotDescriptor;
28 import com.sun.star.sheet.XDataPilotFieldGrouping;
29 import com.sun.star.sheet.XDataPilotTables;
30 import com.sun.star.sheet.XDataPilotTablesSupplier;
31 import com.sun.star.sheet.XSpreadsheet;
32 import com.sun.star.sheet.XSpreadsheetDocument;
33 import com.sun.star.sheet.XSpreadsheets;
34 import com.sun.star.table.CellAddress;
35 import com.sun.star.table.CellRangeAddress;
36 import com.sun.star.uno.AnyConverter;
37 import com.sun.star.uno.Type;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
41 import java.io.PrintWriter;
43 import lib.StatusException;
44 import lib.TestCase;
45 import lib.TestEnvironment;
46 import lib.TestParameters;
48 import util.SOfficeFactory;
52 public class ScDataPilotFieldGroupItemObj extends TestCase
54 private XSpreadsheetDocument xSheetDoc = null;
56 /**
57 * A field is filled some values. This integer determines the size of the
58 * field in x and y direction.
60 private int mMaxFieldIndex = 6;
62 /**
63 * Creates Spreadsheet document.
65 protected void initialize (TestParameters tParam, PrintWriter log)
67 SOfficeFactory SOF = SOfficeFactory.getFactory (
68 (XMultiServiceFactory) tParam.getMSF ());
70 try
72 log.println ("creating a Spreadsheet document");
73 xSheetDoc = SOF.createCalcDoc (null);
75 catch (com.sun.star.uno.Exception e)
77 // Some exception occurs.FAILED
78 e.printStackTrace (log);
79 throw new StatusException ("Couldn't create document", e);
83 /**
84 * Disposes Spreadsheet document.
86 protected void cleanup (TestParameters tParam, PrintWriter log)
88 log.println (" disposing xSheetDoc ");
90 XComponent oComp = UnoRuntime.queryInterface (
91 XComponent.class, xSheetDoc);
92 util.DesktopTools.closeDoc (oComp);
95 protected synchronized TestEnvironment createTestEnvironment (TestParameters Param,
96 PrintWriter log)
98 XInterface oObj = null;
99 XInterface datapilotfield = null;
100 XInterface groups = null;
102 // creation of testobject here
103 // first we write what we are intend to do to log file
104 log.println ("Creating a test environment");
106 // the cell range
107 CellRangeAddress sCellRangeAdress = new CellRangeAddress ();
108 sCellRangeAdress.Sheet = 0;
109 sCellRangeAdress.StartColumn = 1;
110 sCellRangeAdress.StartRow = 0;
111 sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
112 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
114 // position of the data pilot table
115 CellAddress sCellAdress = new CellAddress ();
116 sCellAdress.Sheet = 0;
117 sCellAdress.Column = 7;
118 sCellAdress.Row = 8;
120 log.println ("Getting a sheet");
122 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets ();
123 XSpreadsheet oSheet = null;
124 XSpreadsheet oSheet2 = null;
125 XIndexAccess oIndexAccess = UnoRuntime.queryInterface (
126 XIndexAccess.class, xSpreadsheets);
127 // Make sure there are at least two sheets
128 xSpreadsheets.insertNewByName("Some Sheet", (short)0);
132 oSheet = (XSpreadsheet) AnyConverter.toObject (
133 new Type (XSpreadsheet.class),
134 oIndexAccess.getByIndex (0));
135 oSheet2 = (XSpreadsheet) AnyConverter.toObject (
136 new Type (XSpreadsheet.class),
137 oIndexAccess.getByIndex (1));
139 catch (com.sun.star.lang.WrappedTargetException e)
141 e.printStackTrace ();
142 throw new StatusException ("Couldn't get a spreadsheet", e);
144 catch (com.sun.star.lang.IndexOutOfBoundsException e)
146 e.printStackTrace ();
147 throw new StatusException ("Couldn't get a spreadsheet", e);
149 catch (com.sun.star.lang.IllegalArgumentException e)
151 e.printStackTrace ();
152 throw new StatusException ("Couldn't get a spreadsheet", e);
157 log.println ("Filling a table");
159 for (int i = 1; i < mMaxFieldIndex; i++)
161 oSheet.getCellByPosition (i, 0).setFormula ("Col" + i);
162 oSheet.getCellByPosition (0, i).setFormula ("Row" + i);
163 oSheet2.getCellByPosition (i, 0).setFormula ("Col" + i);
164 oSheet2.getCellByPosition (0, i).setFormula ("Row" + i);
167 for (int i = 1; i < mMaxFieldIndex; i++)
169 for (int j = 1; j < mMaxFieldIndex; j++)
171 oSheet.getCellByPosition (i, j).setValue (i * (j + 1));
172 oSheet2.getCellByPosition (i, j).setValue (i * (j + 2));
176 oSheet.getCellByPosition (1, 1).setFormula ("aName");
177 oSheet.getCellByPosition (1, 2).setFormula ("otherName");
178 oSheet.getCellByPosition (1, 3).setFormula ("una");
179 oSheet.getCellByPosition (1, 4).setFormula ("otherName");
180 oSheet.getCellByPosition (1, 5).setFormula ("somethingelse");
183 catch (com.sun.star.lang.IndexOutOfBoundsException e)
185 e.printStackTrace ();
186 throw new StatusException ("Couldn't fill some cells", e);
191 oSheet.getCellByPosition (1, 5);
193 int x = sCellAdress.Column;
194 int y = sCellAdress.Row + 3;
197 oSheet.getCellByPosition (x, y);
198 new Integer (27);
200 catch (com.sun.star.lang.IndexOutOfBoundsException e)
202 e.printStackTrace ();
203 throw new StatusException ("Couldn't get cells for changing.", e);
207 // create the test objects
208 log.println ("Getting test objects");
210 XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface (
211 XDataPilotTablesSupplier.class,
212 oSheet);
213 XDataPilotTables DPT = DPTS.getDataPilotTables ();
214 XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor ();
215 DPDsc.setSourceRange (sCellRangeAdress);
217 XPropertySet fieldPropSet = null;
221 Object oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (0);
222 fieldPropSet = UnoRuntime.queryInterface (
223 XPropertySet.class, oDataPilotField);
224 fieldPropSet.setPropertyValue ("Orientation",
225 com.sun.star.sheet.DataPilotFieldOrientation.ROW);
226 oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (1);
227 fieldPropSet = UnoRuntime.queryInterface (
228 XPropertySet.class, oDataPilotField);
229 fieldPropSet.setPropertyValue ("Function",
230 com.sun.star.sheet.GeneralFunction.SUM);
231 fieldPropSet.setPropertyValue ("Orientation",
232 com.sun.star.sheet.DataPilotFieldOrientation.DATA);
233 oDataPilotField = DPDsc.getDataPilotFields ().getByIndex (2);
234 fieldPropSet = UnoRuntime.queryInterface (
235 XPropertySet.class, oDataPilotField);
236 fieldPropSet.setPropertyValue ("Orientation",
237 com.sun.star.sheet.DataPilotFieldOrientation.COLUMN);
239 catch (com.sun.star.lang.WrappedTargetException e)
241 e.printStackTrace ();
242 throw new StatusException ("Couldn't create a test environment", e);
244 catch (com.sun.star.lang.IllegalArgumentException e)
246 e.printStackTrace ();
247 throw new StatusException ("Couldn't create a test environment", e);
249 catch (com.sun.star.beans.PropertyVetoException e)
251 e.printStackTrace ();
252 throw new StatusException ("Couldn't create a test environment", e);
254 catch (com.sun.star.beans.UnknownPropertyException e)
256 e.printStackTrace ();
257 throw new StatusException ("Couldn't create a test environment", e);
259 catch (com.sun.star.lang.IndexOutOfBoundsException e)
261 e.printStackTrace ();
262 throw new StatusException ("Couldn't create a test environment", e);
265 log.println ("Insert the DataPilotTable");
267 if (DPT.hasByName ("DataPilotTable"))
269 DPT.removeByName ("DataPilotTable");
272 DPT.insertNewByName ("DataPilotTable", sCellAdress, DPDsc);
273 XIndexAccess xIA = UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ());
274 XIndexAccess IA = null;
277 XDataPilotDescriptor xDPT = UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0));
278 IA = xDPT.getRowFields ();
279 //getSRange(IA);
280 System.out.println ("COUNT: "+IA.getCount ());
281 datapilotfield = (XInterface) AnyConverter.toObject (
282 new Type (XInterface.class), IA.getByIndex (0));
284 catch (com.sun.star.lang.WrappedTargetException e)
286 e.printStackTrace ();
287 throw new StatusException ("Couldn't get data pilot field", e);
289 catch (com.sun.star.lang.IndexOutOfBoundsException e)
291 e.printStackTrace ();
292 throw new StatusException ("Couldn't get data pilot field", e);
294 catch (com.sun.star.lang.IllegalArgumentException e)
296 e.printStackTrace ();
297 throw new StatusException ("Couldn't get data pilot field", e);
302 XDataPilotFieldGrouping dpfg = UnoRuntime.queryInterface (XDataPilotFieldGrouping.class, datapilotfield);
303 String[] elements = new String[]{"aName","otherName"};
304 dpfg.createNameGroup (elements);
305 DataPilotFieldGroupInfo dpgi=null;
306 xIA = UnoRuntime.queryInterface (XIndexAccess.class,DPTS.getDataPilotTables ());
309 XDataPilotDescriptor xDPT = UnoRuntime.queryInterface (XDataPilotDescriptor.class,xIA.getByIndex (0));
310 IA = xDPT.getRowFields ();
312 catch (com.sun.star.lang.WrappedTargetException e)
314 e.printStackTrace ();
315 throw new StatusException ("Couldn't get data pilot field", e);
317 catch (com.sun.star.lang.IndexOutOfBoundsException e)
319 e.printStackTrace ();
320 throw new StatusException ("Couldn't get data pilot field", e);
322 for (int i=0;i<IA.getCount ();i++)
324 datapilotfield = (XInterface) AnyConverter.toObject (
325 new Type (XInterface.class), IA.getByIndex (i));
326 XPropertySet xPropertySet = UnoRuntime.queryInterface (XPropertySet.class, IA.getByIndex (i));
327 if (((Boolean)xPropertySet.getPropertyValue ("IsGroupField")).booleanValue ())
329 xPropertySet = UnoRuntime.queryInterface (XPropertySet.class, datapilotfield);
330 XNamed xNamed = UnoRuntime.queryInterface (XNamed.class, IA.getByIndex (i));
331 System.out.println ("name: "+xNamed.getName ());
332 dpgi = (DataPilotFieldGroupInfo) xPropertySet.getPropertyValue ("GroupInfo");
335 groups = dpgi.Groups;
336 XIndexAccess groupAccess = UnoRuntime.queryInterface(XIndexAccess.class, groups);
337 XNameAccess groupNames = UnoRuntime.queryInterface(XNameAccess.class, groupAccess.getByIndex(0));
338 oObj = UnoRuntime.queryInterface(XInterface.class, groupNames.getByName("aName"));
340 catch (Exception e)
342 e.printStackTrace ();
345 log.println ("Creating object - " +
346 ((oObj == null) ? "FAILED" : "OK"));
348 TestEnvironment tEnv = new TestEnvironment (oObj);
350 log.println ("Implementationname: " + util.utils.getImplName (oObj));
352 // Other parameters required for interface tests
353 return tEnv;