Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScCellRangeObj.java
blobd1e84ab8dce03e1a51bee94991fb6bd813ef2347
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;
19 import java.io.PrintWriter;
21 import lib.StatusException;
22 import lib.TestCase;
23 import lib.TestEnvironment;
24 import lib.TestParameters;
25 import util.SOfficeFactory;
26 import util.ValueComparer;
28 import com.sun.star.beans.XPropertySet;
29 import com.sun.star.container.XNameAccess;
30 import com.sun.star.lang.XComponent;
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.XCell;
35 import com.sun.star.table.XCellRange;
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;
40 import ifc.sheet._XCellRangesQuery;
43 /**
44 * Test for object which is represented by service
45 * <code>com.sun.star.sheet.SheetCellRange</code>. <p>
46 * Object implements the following interfaces :
47 * <ul>
48 * <li> <code>com::sun::star::table::CellProperties</code></li>
49 * <li> <code>com::sun::star::util::XMergeable</code></li>
50 * <li> <code>com::sun::star::sheet::XCellRangeAddressable</code></li>
51 * <li> <code>com::sun::star::sheet::XArrayFormulaRange</code></li>
52 * <li> <code>com::sun::star::chart::XChartData</code></li>
53 * <li> <code>com::sun::star::table::XColumnRowRange</code></li>
54 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
55 * <li> <code>com::sun::star::style::CharacterProperties</code></li>
56 * <li> <code>com::sun::star::sheet::XSheetOperation</code></li>
57 * <li> <code>com::sun::star::table::XCellRange</code></li>
58 * <li> <code>com::sun::star::style::ParagraphProperties</code></li>
59 * <li> <code>com::sun::star::sheet::SheetCellRange</code></li>
60 * </ul>
61 * @see com.sun.star.sheet.SheetCellRange
62 * @see com.sun.star.table.CellProperties
63 * @see com.sun.star.util.XMergeable
64 * @see com.sun.star.sheet.XCellRangeAddressable
65 * @see com.sun.star.sheet.XArrayFormulaRange
66 * @see com.sun.star.chart.XChartData
67 * @see com.sun.star.table.XColumnRowRange
68 * @see com.sun.star.beans.XPropertySet
69 * @see com.sun.star.style.CharacterProperties
70 * @see com.sun.star.sheet.XSheetOperation
71 * @see com.sun.star.table.XCellRange
72 * @see com.sun.star.style.ParagraphProperties
73 * @see com.sun.star.sheet.SheetCellRange
74 * @see ifc.table._CellProperties
75 * @see ifc.util._XMergeable
76 * @see ifc.sheet._XCellRangeAddressable
77 * @see ifc.sheet._XArrayFormulaRange
78 * @see ifc.chart._XChartData
79 * @see ifc.table._XColumnRowRange
80 * @see ifc.beans._XPropertySet
81 * @see ifc.style._CharacterProperties
82 * @see ifc.sheet._XSheetOperation
83 * @see ifc.table._XCellRange
84 * @see ifc.style._ParagraphProperties
85 * @see ifc.sheet._SheetCellRange
87 public class ScCellRangeObj extends TestCase {
88 XSpreadsheetDocument xSheetDoc = null;
90 /**
91 * Creates Spreadsheet document.
93 @Override
94 protected void initialize(TestParameters tParam, PrintWriter log) {
95 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF());
97 try {
98 log.println("creating a Spreadsheet document");
99 xSheetDoc = SOF.createCalcDoc(null);
100 } catch (com.sun.star.uno.Exception e) {
101 // Some exception occurs.FAILED
102 e.printStackTrace(log);
103 throw new StatusException("Couldn't create document", e);
108 * Disposes Spreadsheet document.
110 @Override
111 protected void cleanup(TestParameters tParam, PrintWriter log) {
112 log.println(" disposing xSheetDoc ");
114 XComponent oComp = UnoRuntime.queryInterface(
115 XComponent.class, xSheetDoc);
116 util.DesktopTools.closeDoc(oComp);
120 * Creating a Testenvironment for the interfaces to be tested.
121 * Retrieves a collection of spreadsheets from a document,
122 * and takes one of them. Retrieves some cell range from the spreadsheet.
123 * The retrieved cell range is instance of the service
124 * <code>com.sun.star.sheet.SheetCellRange</code>.
125 * Object relations created :
126 * <ul>
127 * <li> <code>'SHEET'</code> for
128 * {@link ifc.sheet._XArrayFormulaRange} (the spreadsheet which the cell
129 * range was retrieved from)</li>
130 * </ul>
131 * @see com.sun.star.sheet.XSpreadsheet
133 @Override
134 protected TestEnvironment createTestEnvironment(TestParameters Param,
135 PrintWriter log) {
136 XInterface oObj = null;
137 XCellRange testRange;
140 // creation of testobject here
141 // first we write what we are intend to do to log file
142 log.println("Creating a test environment");
144 XSpreadsheets oSpreadsheets = UnoRuntime.queryInterface(
145 XSpreadsheetDocument.class,
146 xSheetDoc).getSheets();
147 XNameAccess oNames = UnoRuntime.queryInterface(
148 XNameAccess.class, oSpreadsheets);
150 XSpreadsheet oSheet = null;
152 try {
153 oSheet = (XSpreadsheet) AnyConverter.toObject(
154 new Type(XSpreadsheet.class),
155 oNames.getByName(oNames.getElementNames()[0]));
157 oObj = oSheet.getCellRangeByPosition(0, 0, 3, 4);
158 testRange = UnoRuntime.queryInterface(
159 XCellRange.class, oObj);
160 } catch (com.sun.star.lang.WrappedTargetException e) {
161 e.printStackTrace(log);
162 throw new StatusException(
163 "Error getting cell object from spreadsheet document", e);
164 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
165 e.printStackTrace(log);
166 throw new StatusException(
167 "Error getting cell object from spreadsheet document", e);
168 } catch (com.sun.star.container.NoSuchElementException e) {
169 e.printStackTrace(log);
170 throw new StatusException(
171 "Error getting cell object from spreadsheet document", e);
172 } catch (com.sun.star.lang.IllegalArgumentException e) {
173 e.printStackTrace(log);
174 throw new StatusException(
175 "Error getting cell object from spreadsheet document", e);
179 TestEnvironment tEnv = new TestEnvironment(oObj);
181 tEnv.addObjRelation("SHEET", oSheet);
182 // add expected results for the XCellRangesQuery interface test
183 String[]expectedResults = new String[7];
184 expectedResults[_XCellRangesQuery.QUERYCOLUMNDIFFERENCES] = "Sheet1.B1:C1;Sheet1.B3";
185 expectedResults[_XCellRangesQuery.QUERYCONTENTCELLS] = "Sheet1.B2:B3";
186 expectedResults[_XCellRangesQuery.QUERYEMPTYCELLS] = "Sheet1.A1 ... Sheet1.B1 ... Sheet1.B5 ... Sheet1.C3 ... Sheet1.D1";
187 expectedResults[_XCellRangesQuery.QUERYFORMULACELLS] = "Sheet1.C2";
188 expectedResults[_XCellRangesQuery.QUERYINTERSECTION] = "Sheet1.D4";
189 expectedResults[_XCellRangesQuery.QUERYROWDIFFERENCES] = "Sheet1.A2:A4;Sheet1.C2:D4";
190 expectedResults[_XCellRangesQuery.QUERYVISIBLECELLS] = "Sheet1.A2";
191 tEnv.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS", expectedResults);
192 tEnv.addObjRelation("XCellRangesQuery.CREATEENTRIES", Boolean.TRUE);
194 XPropertySet PropSet = UnoRuntime.queryInterface(
195 XPropertySet.class, oObj);
196 tEnv.addObjRelation("PropSet", PropSet);
198 // XSearchable: Add a cell to make a seacrchable entry
199 try {
200 tEnv.addObjRelation("XSearchable.MAKEENTRYINCELL", new XCell[] {
201 testRange.getCellByPosition(0,0), testRange.getCellByPosition(0,1)});
203 catch(com.sun.star.lang.IndexOutOfBoundsException e){
204 e.printStackTrace(log);
205 log.println("Cannot make required object relation 'XSearchable.MAKEENTRYINCELL'.");
208 // XCellRangeData
209 /* Object[][] newData = new Object[5][4];
210 for (int i=0; i<newData.length; i++) {
211 for (int j=0; j<newData[i].length; j++) {
212 newData[i][j] = new Double(i*10+j);
215 tEnv.addObjRelation("NewData", newData); */
217 //Adding relation for util.XSortable
218 final PrintWriter finalLog = log;
219 final XCellRange oTable = testRange;
220 tEnv.addObjRelation("SORTCHECKER",
221 new ifc.util._XSortable.XSortChecker() {
222 PrintWriter out = finalLog;
224 public void setPrintWriter(PrintWriter log) {
225 out = log;
228 public void prepareToSort() {
229 try {
230 oTable.getCellByPosition(0, 0).setValue(4);
231 oTable.getCellByPosition(0, 1).setFormula("b");
232 oTable.getCellByPosition(0, 2).setValue(3);
233 oTable.getCellByPosition(0, 3).setValue(23);
234 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
235 out.println("Exception while checking sort");
239 public boolean checkSort(boolean isSortNumbering,
240 boolean isSortAscending) {
241 out.println("Sort checking...");
243 boolean res = false;
244 String[] value = new String[4];
246 for (int i = 0; i < 4; i++) {
247 try {
248 XCell cell = oTable.getCellByPosition(0, i);
249 value[i] = cell.getFormula();
250 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
251 out.println("Exception while checking sort");
255 if (isSortNumbering) {
256 if (isSortAscending) {
257 out.println("Sorting ascending");
259 String[] rightVal = { "3", "4", "23", "b" };
260 String[] vals = { value[0], value[1], value[2], value[3] };
261 res = ValueComparer.equalValue(vals, rightVal);
262 out.println("Expected 3, 4, 23, b");
263 out.println("getting: " + value[0] + ", " +
264 value[1] + ", " + value[2] + ", " +
265 value[3]);
266 } else {
267 String[] rightVal = { "b", "23", "4", "3" };
268 String[] vals = { value[0], value[1], value[2], value[3] };
269 res = ValueComparer.equalValue(vals, rightVal);
270 out.println("Expected b, 23, 4, 3");
271 out.println("getting: " + value[0] + ", " +
272 value[1] + ", " + value[2] + ", " +
273 value[3]);
275 } else {
276 if (isSortAscending) {
277 String[] rightVal = { "3", "4", "23", "b" };
278 res = ValueComparer.equalValue(value, rightVal);
279 out.println("Expected 3, 4, 23, b");
280 out.println("getting: " + value[0] + ", " +
281 value[1] + ", " + value[2] + ", " +
282 value[3]);
283 } else {
284 String[] rightVal = { "b", "23", "4", "3" };
285 res = ValueComparer.equalValue(value, rightVal);
286 out.println("Expected b, 23, 4, 3");
287 out.println("getting: " + value[0] + ", " +
288 value[1] + ", " + value[2] + ", " +
289 value[3]);
293 if (res) {
294 out.println("Sorted correctly");
295 } else {
296 out.println("Sorted uncorrectly");
299 return res;
303 return tEnv;
305 } // finish class ScCellRangeObj