bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sch / ChXDiagram.java
blobb74ec9944dac200c546e0ade59fac3c970fc01c4
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 .
19 package mod._sch;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
29 import com.sun.star.awt.Rectangle;
30 import com.sun.star.chart.XChartDataArray;
31 import com.sun.star.chart.XChartDocument;
32 import com.sun.star.chart.XDiagram;
33 import com.sun.star.container.XIndexAccess;
34 import com.sun.star.container.XNameAccess;
35 import com.sun.star.document.XEmbeddedObjectSupplier;
36 import com.sun.star.sheet.XCellRangeAddressable;
37 import com.sun.star.sheet.XSpreadsheet;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.sheet.XSpreadsheets;
40 import com.sun.star.table.CellRangeAddress;
41 import com.sun.star.table.XCell;
42 import com.sun.star.table.XCellRange;
43 import com.sun.star.table.XTableChart;
44 import com.sun.star.table.XTableCharts;
45 import com.sun.star.table.XTableChartsSupplier;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.Type;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
51 /**
52 * Test for object which is represented by the following services:
53 * <ul>
54 * <li> <code>com.sun.star.chart.Dim3DDiagram</code> </li>
55 * <li> <code>com.sun.star.chart.StockDiagram</code> </li>
56 * <li> <code>com.sun.star.chart.LineDiagram</code> </li>
57 * <li> <code>com.sun.star.chart.BarDiagram</code> </li>
58 * <li> <code>com.sun.star.chart.StackableDiagram</code> </li>
59 * </ul>
60 * <p>
61 * Object implements the following interfaces :
62 * <ul>
63 * <li> <code>com::sun::star::chart::XDiagram</code></li>
64 * <li> <code>com::sun::star::chart::ChartAxisXSupplier</code></li>
65 * <li> <code>com::sun::star::chart::Dim3DDiagram</code></li>
66 * <li> <code>com::sun::star::chart::StockDiagram</code></li>
67 * <li> <code>com::sun::star::chart::ChartAxisZSupplier</code></li>
68 * <li> <code>com::sun::star::chart::XTwoAxisXSupplier</code></li>
69 * <li> <code>com::sun::star::chart::LineDiagram</code></li>
70 * <li> <code>com::sun::star::chart::BarDiagram</code></li>
71 * <li> <code>com::sun::star::chart::XAxisYSupplier</code></li>
72 * <li> <code>com::sun::star::chart::Diagram</code></li>
73 * <li> <code>com::sun::star::chart::X3DDisplay</code></li>
74 * <li> <code>com::sun::star::chart::ChartTwoAxisYSupplier</code></li>
75 * <li> <code>com::sun::star::chart::StackableDiagram</code></li>
76 * <li> <code>com::sun::star::chart::ChartAxisYSupplier</code></li>
77 * <li> <code>com::sun::star::chart::XAxisXSupplier</code></li>
78 * <li> <code>com::sun::star::chart::ChartTwoAxisXSupplier</code></li>
79 * <li> <code>com::sun::star::drawing::XShape</code></li>
80 * <li> <code>com::sun::star::chart::XTwoAxisYSupplier</code></li>
81 * <li> <code>com::sun::star::chart::ChartStatistics</code></li>
82 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
83 * <li> <code>com::sun::star::drawing::XShapeDescriptor</code></li>
84 * <li> <code>com::sun::star::chart::XAxisZSupplier</code></li>
85 * <li> <code>com::sun::star::chart::XStatisticDisplay</code></li>
86 * </ul>
87 * @see com.sun.star.chart.XDiagram
88 * @see com.sun.star.chart.ChartAxisXSupplier
89 * @see com.sun.star.chart.Dim3DDiagram
90 * @see com.sun.star.chart.StockDiagram
91 * @see com.sun.star.chart.ChartAxisZSupplier
92 * @see com.sun.star.chart.XTwoAxisXSupplier
93 * @see com.sun.star.chart.LineDiagram
94 * @see com.sun.star.chart.BarDiagram
95 * @see com.sun.star.chart.XAxisYSupplier
96 * @see com.sun.star.chart.Diagram
97 * @see com.sun.star.chart.X3DDisplay
98 * @see com.sun.star.chart.ChartTwoAxisYSupplier
99 * @see com.sun.star.chart.StackableDiagram
100 * @see com.sun.star.chart.ChartAxisYSupplier
101 * @see com.sun.star.chart.XAxisXSupplier
102 * @see com.sun.star.chart.ChartTwoAxisXSupplier
103 * @see com.sun.star.drawing.XShape
104 * @see com.sun.star.chart.XTwoAxisYSupplier
105 * @see com.sun.star.chart.ChartStatistics
106 * @see com.sun.star.beans.XPropertySet
107 * @see com.sun.star.drawing.XShapeDescriptor
108 * @see com.sun.star.chart.XAxisZSupplier
109 * @see com.sun.star.chart.XStatisticDisplay
110 * @see ifc.chart._XDiagram
111 * @see ifc.chart._ChartAxisXSupplier
112 * @see ifc.chart._Dim3DDiagram
113 * @see ifc.chart._StockDiagram
114 * @see ifc.chart._ChartAxisZSupplier
115 * @see ifc.chart._XTwoAxisXSupplier
116 * @see ifc.chart._LineDiagram
117 * @see ifc.chart._BarDiagram
118 * @see ifc.chart._XAxisYSupplier
119 * @see ifc.chart._Diagram
120 * @see ifc.chart._X3DDisplay
121 * @see ifc.chart._ChartTwoAxisYSupplier
122 * @see ifc.chart._StackableDiagram
123 * @see ifc.chart._ChartAxisYSupplier
124 * @see ifc.chart._XAxisXSupplier
125 * @see ifc.chart._ChartTwoAxisXSupplier
126 * @see ifc.drawing._XShape
127 * @see ifc.chart._XTwoAxisYSupplier
128 * @see ifc.chart._ChartStatistics
129 * @see ifc.beans._XPropertySet
130 * @see ifc.drawing._XShapeDescriptor
131 * @see ifc.chart._XAxisZSupplier
132 * @see ifc.chart._XStatisticDisplay
134 public class ChXDiagram extends TestCase {
135 XSpreadsheetDocument xSheetDoc = null;
138 * Creates Spreadsheet document.
140 @Override
141 protected void initialize( TestParameters tParam, PrintWriter log ) {
142 // get a soffice factory object
143 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF());
145 try {
146 log.println( "creating a sheetdocument" );
147 xSheetDoc = SOF.createCalcDoc(null);
148 } catch (com.sun.star.uno.Exception e) {
149 // Some exception occurs.FAILED
150 e.printStackTrace( log );
151 throw new StatusException( "Couldn't create document", e );
156 * Disposes Spreadsheet document.
158 @Override
159 protected void cleanup( TestParameters tParam, PrintWriter log ) {
160 log.println( " closing xSheetDoc " );
161 util.DesktopTools.closeDoc(xSheetDoc);
165 * Creating a Testenvironment for the interfaces to be tested.
166 * Retrieves a collection of spreadsheets from a document
167 * and takes one of them. Inserts some values into the cells of the some cell
168 * range address. Adds and retrieves the chart that using the data from
169 * the cells of this cell range address. Obtains the chart document which is
170 * embedded into the retrieved chart using the interface
171 * <code>XEmbeddedObjectSupplier</code>. Retrieves the diagram from
172 * the obtained chart document. The retrieved diagram is the instance of
173 * the service <code>com.sun.star.chart.Diagram</code>.
174 * Obtains the data source of the chart from the chart document.
175 * Creates a stock-diagram, a bar-diagram, a XY-diagram and line-diagram
176 * that are the instances of the following services:
177 * <ul>
178 * <li> <code>com.sun.star.chart.StockDiagram</code> </li>
179 * <li> <code>com.sun.star.chart.BarDiagram</code> </li>
180 * <li> <code>com.sun.star.chart.LineDiagram</code> </li>
181 * <li> <code>com.sun.star.chart.StackableDiagram</code> </li>
182 * </ul>.
183 * Object relations created :
184 * <ul>
185 * <li> <code>'CHARTDOC'</code> for
186 * {@link ifc.chart._Dim3DDiagram}, {@link ifc.chart._StockDiagram},
187 * {@link ifc.chart._ChartAxisZSupplier}, {@link ifc.chart._LineDiagram},
188 * {@link ifc.chart._BarDiagram}, {@link ifc.chart._Diagram},
189 * {@link ifc.chart._ChartTwoAxisYSupplier},
190 * {@link ifc.chart._StackableDiagram}, {@link ifc.chart._Diagram},
191 * {@link ifc.chart._ChartAxisYSupplier},
192 * {@link ifc.chart._ChartTwoAxisXSupplier},
193 * {@link ifc.chart._ChartStatistics} (the obtained chart document)</li>
194 * <li> <code>'ROWAMOUNT', 'COLAMOUNT'</code> for
195 * {@link ifc.chart._XDiagram}(the number of chart columns and
196 * the number of chart rows) </li>
197 * <li> <code>'STOCK'</code> for
198 * {@link ifc.chart._StockDiagram}(the created stock-diagram) </li>
199 * <li> <code>'BAR'</code> for
200 * {@link ifc.chart._BarDiagram}, {@link ifc.chart._ChartAxisZSupplier},
201 * {@link ifc.chart._ChartTwoAxisXSupplier},
202 * {@link ifc.chart._ChartTwoAxisYSupplier}(the created bar-diagram)</li>
203 * <li> <code>'LINE'</code> for
204 * {@link ifc.chart._LineDiagram}(the created XY-diagram) </li>
205 * <li> <code>'STACK'</code> for
206 * {@link ifc.chart._StackableDiagram}(the created Line-diagram) </li>
207 * </ul>
208 * @see com.sun.star.document.XEmbeddedObjectSupplier
209 * @see com.sun.star.chart.Diagram
210 * @see com.sun.star.chart.StockDiagram
212 @Override
213 protected synchronized TestEnvironment createTestEnvironment
214 (TestParameters Param, PrintWriter log) {
216 XSpreadsheet oSheet=null;
217 XChartDocument xChartDoc=null;
218 XDiagram oObj = null;
220 System.out.println("Getting spreadsheet") ;
221 XSpreadsheets oSheets = xSheetDoc.getSheets() ;
222 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
223 try {
224 oSheet = (XSpreadsheet) AnyConverter.toObject(
225 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
226 } catch(com.sun.star.lang.WrappedTargetException e) {
227 e.printStackTrace(log);
228 throw new StatusException("Couldn't get sheet", e);
229 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
230 e.printStackTrace(log);
231 throw new StatusException("Couldn't get sheet", e);
232 } catch(com.sun.star.lang.IllegalArgumentException e) {
233 e.printStackTrace(log);
234 throw new StatusException("Couldn't get sheet", e);
237 log.println("Creating the Header") ;
239 insertIntoCell(1,0,"JAN",oSheet,"");
240 insertIntoCell(2,0,"FEB",oSheet,"");
241 insertIntoCell(3,0,"MAR",oSheet,"");
242 insertIntoCell(4,0,"APR",oSheet,"");
243 insertIntoCell(5,0,"MAI",oSheet,"");
244 insertIntoCell(6,0,"JUN",oSheet,"");
245 insertIntoCell(7,0,"JUL",oSheet,"");
246 insertIntoCell(8,0,"AUG",oSheet,"");
247 insertIntoCell(9,0,"SEP",oSheet,"");
248 insertIntoCell(10,0,"OCT",oSheet,"");
249 insertIntoCell(11,0,"NOV",oSheet,"");
250 insertIntoCell(12,0,"DEC",oSheet,"");
251 insertIntoCell(13,0,"SUM",oSheet,"");
253 log.println("Fill the lines");
255 insertIntoCell(0,1,"Smith",oSheet,"");
256 insertIntoCell(1,1,"42",oSheet,"V");
257 insertIntoCell(2,1,"58.9",oSheet,"V");
258 insertIntoCell(3,1,"-66.5",oSheet,"V");
259 insertIntoCell(4,1,"43.4",oSheet,"V");
260 insertIntoCell(5,1,"44.5",oSheet,"V");
261 insertIntoCell(6,1,"45.3",oSheet,"V");
262 insertIntoCell(7,1,"-67.3",oSheet,"V");
263 insertIntoCell(8,1,"30.5",oSheet,"V");
264 insertIntoCell(9,1,"23.2",oSheet,"V");
265 insertIntoCell(10,1,"-97.3",oSheet,"V");
266 insertIntoCell(11,1,"22.4",oSheet,"V");
267 insertIntoCell(12,1,"23.5",oSheet,"V");
268 insertIntoCell(13,1,"=SUM(B2:M2)",oSheet,"");
270 insertIntoCell(0,2,"Jones",oSheet,"");
271 insertIntoCell(1,2,"21",oSheet,"V");
272 insertIntoCell(2,2,"40.9",oSheet,"V");
273 insertIntoCell(3,2,"-57.5",oSheet,"V");
274 insertIntoCell(4,2,"-23.4",oSheet,"V");
275 insertIntoCell(5,2,"34.5",oSheet,"V");
276 insertIntoCell(6,2,"59.3",oSheet,"V");
277 insertIntoCell(7,2,"27.3",oSheet,"V");
278 insertIntoCell(8,2,"-38.5",oSheet,"V");
279 insertIntoCell(9,2,"43.2",oSheet,"V");
280 insertIntoCell(10,2,"57.3",oSheet,"V");
281 insertIntoCell(11,2,"25.4",oSheet,"V");
282 insertIntoCell(12,2,"28.5",oSheet,"V");
283 insertIntoCell(13,2,"=SUM(B3:M3)",oSheet,"");
285 insertIntoCell(0,3,"Brown",oSheet,"");
286 insertIntoCell(1,3,"31.45",oSheet,"V");
287 insertIntoCell(2,3,"-20.9",oSheet,"V");
288 insertIntoCell(3,3,"-117.5",oSheet,"V");
289 insertIntoCell(4,3,"23.4",oSheet,"V");
290 insertIntoCell(5,3,"-114.5",oSheet,"V");
291 insertIntoCell(6,3,"115.3",oSheet,"V");
292 insertIntoCell(7,3,"-171.3",oSheet,"V");
293 insertIntoCell(8,3,"89.5",oSheet,"V");
294 insertIntoCell(9,3,"41.2",oSheet,"V");
295 insertIntoCell(10,3,"71.3",oSheet,"V");
296 insertIntoCell(11,3,"25.4",oSheet,"V");
297 insertIntoCell(12,3,"38.5",oSheet,"V");
298 insertIntoCell(13,3,"=SUM(A4:L4)",oSheet,"");
300 // insert a chart
301 Rectangle oRect = new Rectangle(500, 3000, 25000, 11000);
303 XCellRange oRange = UnoRuntime.queryInterface(XCellRange.class, oSheet);
304 XCellRange myRange = oRange.getCellRangeByName("A1:N4");
305 XCellRangeAddressable oRangeAddr = UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
306 CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
308 CellRangeAddress[] oAddr = new CellRangeAddress[1];
309 oAddr[0] = myAddr;
310 XTableChartsSupplier oSupp = UnoRuntime.queryInterface(XTableChartsSupplier.class, oSheet);
312 log.println("Insert Chart");
313 XTableCharts oCharts = oSupp.getCharts();
316 if (!oCharts.hasByName("ChXDiagram")) {
317 oCharts.addNewByName("ChXDiagram", oRect, oAddr, true, true);
320 // get the TableChart
321 XTableChart oChart = null;
322 try {
323 oChart = (XTableChart) AnyConverter.toObject(
324 new Type(XTableChart.class),UnoRuntime.queryInterface(
325 XNameAccess.class, oCharts).getByName("ChXDiagram"));
326 } catch (com.sun.star.lang.WrappedTargetException e) {
327 e.printStackTrace(log);
328 throw new StatusException("Couldn't get TableChart", e);
329 } catch (com.sun.star.container.NoSuchElementException e) {
330 e.printStackTrace(log);
331 throw new StatusException("Couldn't get TableChart", e);
332 } catch (com.sun.star.lang.IllegalArgumentException e) {
333 e.printStackTrace(log);
334 throw new StatusException("Couldn't get TableChart", e);
337 XEmbeddedObjectSupplier oEOS = UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, oChart);
338 XInterface oInt = oEOS.getEmbeddedObject();
339 xChartDoc = UnoRuntime.queryInterface(XChartDocument.class,oInt);
340 oObj = xChartDoc.getDiagram();
342 log.println( "creating a new environment for chartdocument object" );
343 TestEnvironment tEnv = new TestEnvironment( oObj );
345 log.println( "adding ChartDocument as mod relation to environment" );
346 tEnv.addObjRelation("CHARTDOC", xChartDoc);
348 XChartDataArray da = UnoRuntime.queryInterface(XChartDataArray.class, xChartDoc.getData());
349 int cols = da.getColumnDescriptions().length;
350 int rows = da.getRowDescriptions().length;
352 tEnv.addObjRelation("ROWAMOUNT", Integer.valueOf(rows));
353 tEnv.addObjRelation("COLAMOUNT", Integer.valueOf(cols));
355 SOfficeFactory SOF = SOfficeFactory.getFactory( Param.getMSF());
356 Object stock = SOF.createDiagram(xChartDoc,"StockDiagram");
357 tEnv.addObjRelation("STOCK",stock);
359 Object bar = SOF.createDiagram(xChartDoc,"BarDiagram");
360 tEnv.addObjRelation("BAR",bar);
362 Object line = SOF.createDiagram(xChartDoc,"XYDiagram");
363 tEnv.addObjRelation("LINE",line);
365 Object stack = SOF.createDiagram(xChartDoc,"LineDiagram");
366 tEnv.addObjRelation("STACK",stack);
368 return tEnv;
369 } // finish method getTestEnvironment
372 * Inserts a value or a formula in the cell of the spreasheet.
373 * @param CellX is the column index of the cell
374 * @param CellY is the row index of the cell
375 * @param theValue string representation of the value
376 * @param TT1 specify the spreadsheet, the interface
377 * <code>com.sun.star.sheet.XSpreadsheet</code>
378 * @param flag if it's equal to <code>'V'</code> then the method inserts
379 * a double-value in the cell else it inserts a formula in the cell
381 public static void insertIntoCell(
382 int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag) {
384 XCell oCell = null;
386 try {
387 oCell = TT1.getCellByPosition(CellX, CellY);
388 } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
389 System.out.println("Could not get Cell");
391 if (flag.equals("V")) {
392 oCell.setValue(Float.parseFloat(theValue));
394 else {
395 oCell.setFormula(theValue);
398 } // end of insertIntoCell
401 } // finish class ChXDiagram