bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScChartsObj.java
blobc85646d1d2208f498169293ae726db80bc728d03
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._sc;
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.container.XIndexAccess;
31 import com.sun.star.lang.XComponent;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.sheet.XCellRangeAddressable;
34 import com.sun.star.sheet.XSpreadsheet;
35 import com.sun.star.sheet.XSpreadsheetDocument;
36 import com.sun.star.sheet.XSpreadsheets;
37 import com.sun.star.table.CellRangeAddress;
38 import com.sun.star.table.XCell;
39 import com.sun.star.table.XCellRange;
40 import com.sun.star.table.XTableCharts;
41 import com.sun.star.table.XTableChartsSupplier;
42 import com.sun.star.uno.AnyConverter;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
46 /**
47 * Test for object which is represented by service
48 * <code>com.sun.star.table.TableCharts</code>. <p>
49 * Object implements the following interfaces :
50 * <ul>
51 * <li> <code>com::sun::star::container::XNameAccess</code></li>
52 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
53 * <li> <code>com::sun::star::container::XElementAccess</code></li>
54 * <li> <code>com::sun::star::table::XTableCharts</code></li>
55 * </ul>
56 * @see com.sun.star.table.TableCharts
57 * @see com.sun.star.container.XNameAccess
58 * @see com.sun.star.container.XIndexAccess
59 * @see com.sun.star.container.XElementAccess
60 * @see com.sun.star.table.XTableCharts
61 * @see ifc.container._XNameAccess
62 * @see ifc.container._XIndexAccess
63 * @see ifc.container._XElementAccess
64 * @see ifc.table._XTableCharts
66 public class ScChartsObj extends TestCase {
67 private XSpreadsheetDocument xSheetDoc = null;
69 /**
70 * Creates Spreadsheet document.
72 protected void initialize( TestParameters tParam, PrintWriter log ) {
73 // get a soffice factory object
74 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
76 try {
77 log.println( "creating a sheetdocument" );
78 xSheetDoc = SOF.createCalcDoc(null);
79 } catch (com.sun.star.uno.Exception e) {
80 // Some exception occurs.FAILED
81 e.printStackTrace( log );
82 throw new StatusException( "Couldn't create document", e );
86 /**
87 * Disposes Spreadsheet document.
89 protected void cleanup( TestParameters tParam, PrintWriter log ) {
90 log.println( " disposing xSheetDoc " );
91 XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSheetDoc);
92 util.DesktopTools.closeDoc(oComp);
95 /**
96 * Creating a Testenvironment for the interfaces to be tested.
97 * Retrieves a collection of spreadsheets from a document
98 * and takes one of them. Inserts some values into the cells of the some cell
99 * range address. Obtains the collection of the charts using the interface
100 * <code>XTableChartsSupplier</code>. Creates and adds the chart that using
101 * the data from the cells of this cell range address. Collection of
102 * the charts is the instance of the service
103 * <code>com.sun.star.table.TableCharts</code>.
104 * Object relations created :
105 * <ul>
106 * <li> <code>'RECT'</code> for
107 * {@link ifc.table._XTableCharts}(of type <code>Rectangle</code>
108 * the position of the chart)</li>
109 * <li> <code>'ADDR'</code> for
110 * {@link ifc.table._XTableCharts}(of type
111 * <code>com.sun.star.table.CellRangeAddress[]</code> data source ranges
112 * for chart creating)</li>
113 * </ul>
114 * @see com.sun.star.container.XNamed
115 * @see com.sun.star.table.XTableChartsSupplier
117 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
119 XSpreadsheet oSheet=null;
121 try {
122 log.println("Getting spreadsheet") ;
123 XSpreadsheets oSheets = xSheetDoc.getSheets() ;
124 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
125 oSheet = (XSpreadsheet) AnyConverter.toObject(
126 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
127 } catch (com.sun.star.lang.WrappedTargetException e) {
128 log.println("Couldn't get Sheet ");
129 e.printStackTrace(log);
130 throw new StatusException("Couldn't get sheet", e);
131 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
132 log.println("Couldn't get Sheet ");
133 e.printStackTrace(log);
134 throw new StatusException("Couldn't get sheet", e);
135 } catch (com.sun.star.lang.IllegalArgumentException e) {
136 log.println("Couldn't get Sheet ");
137 e.printStackTrace(log);
138 throw new StatusException("Couldn't get sheet", e);
141 log.println("Creating the Header") ;
143 insertIntoCell(1,0,"JAN",oSheet,"");
144 insertIntoCell(2,0,"FEB",oSheet,"");
145 insertIntoCell(3,0,"MAR",oSheet,"");
146 insertIntoCell(4,0,"APR",oSheet,"");
147 insertIntoCell(5,0,"MAI",oSheet,"");
148 insertIntoCell(6,0,"JUN",oSheet,"");
149 insertIntoCell(7,0,"JUL",oSheet,"");
150 insertIntoCell(8,0,"AUG",oSheet,"");
151 insertIntoCell(9,0,"SEP",oSheet,"");
152 insertIntoCell(10,0,"OCT",oSheet,"");
153 insertIntoCell(11,0,"NOV",oSheet,"");
154 insertIntoCell(12,0,"DEC",oSheet,"");
155 insertIntoCell(13,0,"SUM",oSheet,"");
157 log.println("Fill the lines");
159 insertIntoCell(0,1,"Smith",oSheet,"");
160 insertIntoCell(1,1,"42",oSheet,"V");
161 insertIntoCell(2,1,"58.9",oSheet,"V");
162 insertIntoCell(3,1,"-66.5",oSheet,"V");
163 insertIntoCell(4,1,"43.4",oSheet,"V");
164 insertIntoCell(5,1,"44.5",oSheet,"V");
165 insertIntoCell(6,1,"45.3",oSheet,"V");
166 insertIntoCell(7,1,"-67.3",oSheet,"V");
167 insertIntoCell(8,1,"30.5",oSheet,"V");
168 insertIntoCell(9,1,"23.2",oSheet,"V");
169 insertIntoCell(10,1,"-97.3",oSheet,"V");
170 insertIntoCell(11,1,"22.4",oSheet,"V");
171 insertIntoCell(12,1,"23.5",oSheet,"V");
172 insertIntoCell(13,1,"=SUM(B2:M2)",oSheet,"");
174 insertIntoCell(0,2,"Jones",oSheet,"");
175 insertIntoCell(1,2,"21",oSheet,"V");
176 insertIntoCell(2,2,"40.9",oSheet,"V");
177 insertIntoCell(3,2,"-57.5",oSheet,"V");
178 insertIntoCell(4,2,"-23.4",oSheet,"V");
179 insertIntoCell(5,2,"34.5",oSheet,"V");
180 insertIntoCell(6,2,"59.3",oSheet,"V");
181 insertIntoCell(7,2,"27.3",oSheet,"V");
182 insertIntoCell(8,2,"-38.5",oSheet,"V");
183 insertIntoCell(9,2,"43.2",oSheet,"V");
184 insertIntoCell(10,2,"57.3",oSheet,"V");
185 insertIntoCell(11,2,"25.4",oSheet,"V");
186 insertIntoCell(12,2,"28.5",oSheet,"V");
187 insertIntoCell(13,2,"=SUM(B3:M3)",oSheet,"");
189 insertIntoCell(0,3,"Brown",oSheet,"");
190 insertIntoCell(1,3,"31.45",oSheet,"V");
191 insertIntoCell(2,3,"-20.9",oSheet,"V");
192 insertIntoCell(3,3,"-117.5",oSheet,"V");
193 insertIntoCell(4,3,"23.4",oSheet,"V");
194 insertIntoCell(5,3,"-114.5",oSheet,"V");
195 insertIntoCell(6,3,"115.3",oSheet,"V");
196 insertIntoCell(7,3,"-171.3",oSheet,"V");
197 insertIntoCell(8,3,"89.5",oSheet,"V");
198 insertIntoCell(9,3,"41.2",oSheet,"V");
199 insertIntoCell(10,3,"71.3",oSheet,"V");
200 insertIntoCell(11,3,"25.4",oSheet,"V");
201 insertIntoCell(12,3,"38.5",oSheet,"V");
202 insertIntoCell(13,3,"=SUM(A4:L4)",oSheet,"");
204 // insert a chart
205 Rectangle oRect = new Rectangle(500, 3000, 25000, 11000);
207 XCellRange oRange = UnoRuntime.queryInterface(XCellRange.class, oSheet);
208 XCellRange myRange = oRange.getCellRangeByName("A1:N4");
209 XCellRangeAddressable oRangeAddr = UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
210 CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
212 CellRangeAddress[] oAddr = new CellRangeAddress[1];
213 oAddr[0] = myAddr;
214 XTableChartsSupplier oSupp = UnoRuntime.queryInterface(XTableChartsSupplier.class, oSheet);
217 log.println("Insert Chart");
218 XTableCharts oCharts = oSupp.getCharts();
219 oCharts.addNewByName("ScChartObj", oRect, oAddr, true, true);
221 log.println("creating a new environment for object");
222 TestEnvironment tEnv = new TestEnvironment(oCharts);
224 tEnv.addObjRelation("RECT", oRect);
225 tEnv.addObjRelation("ADDR", oAddr);
227 return tEnv;
231 * Inserts a value or a formula in the cell of the spreasheet.
232 * @param CellX is the column index of the cell
233 * @param CellY is the row index of the cell
234 * @param theValue string representation of the value
235 * @param TT1 specify the spreadsheet, the interface
236 * <code>com.sun.star.sheet.XSpreadsheet</code>
237 * @param flag if it's equal to <code>'V'</code> then the method inserts
238 * a double-value in the cell else it inserts a formula in the cell
240 public static void insertIntoCell(
241 int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag) {
243 XCell oCell = null;
245 try {
246 oCell = TT1.getCellByPosition(CellX, CellY);
247 } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
248 System.out.println("Could not get Cell");
251 if (flag.equals("V")) {
252 oCell.setValue(new Float(theValue).floatValue());
253 } else {
254 oCell.setFormula(theValue);
257 } // end of insertIntoCell