1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScCellRangeObj.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 import java
.io
.PrintWriter
;
33 import lib
.StatusException
;
35 import lib
.TestEnvironment
;
36 import lib
.TestParameters
;
37 import util
.SOfficeFactory
;
38 import util
.ValueComparer
;
40 import com
.sun
.star
.beans
.XPropertySet
;
41 import com
.sun
.star
.container
.XNameAccess
;
42 import com
.sun
.star
.lang
.XComponent
;
43 import com
.sun
.star
.lang
.XMultiServiceFactory
;
44 import com
.sun
.star
.sheet
.XSpreadsheet
;
45 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
46 import com
.sun
.star
.sheet
.XSpreadsheets
;
47 import com
.sun
.star
.table
.XCell
;
48 import com
.sun
.star
.table
.XCellRange
;
49 import com
.sun
.star
.uno
.AnyConverter
;
50 import com
.sun
.star
.uno
.Type
;
51 import com
.sun
.star
.uno
.UnoRuntime
;
52 import com
.sun
.star
.uno
.XInterface
;
53 import ifc
.sheet
._XCellRangesQuery
;
57 * Test for object which is represented by service
58 * <code>com.sun.star.sheet.SheetCellRange</code>. <p>
59 * Object implements the following interfaces :
61 * <li> <code>com::sun::star::table::CellProperties</code></li>
62 * <li> <code>com::sun::star::util::XMergeable</code></li>
63 * <li> <code>com::sun::star::sheet::XCellRangeAddressable</code></li>
64 * <li> <code>com::sun::star::sheet::XArrayFormulaRange</code></li>
65 * <li> <code>com::sun::star::chart::XChartData</code></li>
66 * <li> <code>com::sun::star::table::XColumnRowRange</code></li>
67 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
68 * <li> <code>com::sun::star::style::CharacterProperties</code></li>
69 * <li> <code>com::sun::star::sheet::XSheetOperation</code></li>
70 * <li> <code>com::sun::star::table::XCellRange</code></li>
71 * <li> <code>com::sun::star::style::ParagraphProperties</code></li>
72 * <li> <code>com::sun::star::sheet::SheetCellRange</code></li>
74 * @see com.sun.star.sheet.SheetCellRange
75 * @see com.sun.star.table.CellProperties
76 * @see com.sun.star.util.XMergeable
77 * @see com.sun.star.sheet.XCellRangeAddressable
78 * @see com.sun.star.sheet.XArrayFormulaRange
79 * @see com.sun.star.chart.XChartData
80 * @see com.sun.star.table.XColumnRowRange
81 * @see com.sun.star.beans.XPropertySet
82 * @see com.sun.star.style.CharacterProperties
83 * @see com.sun.star.sheet.XSheetOperation
84 * @see com.sun.star.table.XCellRange
85 * @see com.sun.star.style.ParagraphProperties
86 * @see com.sun.star.sheet.SheetCellRange
87 * @see ifc.table._CellProperties
88 * @see ifc.util._XMergeable
89 * @see ifc.sheet._XCellRangeAddressable
90 * @see ifc.sheet._XArrayFormulaRange
91 * @see ifc.chart._XChartData
92 * @see ifc.table._XColumnRowRange
93 * @see ifc.beans._XPropertySet
94 * @see ifc.style._CharacterProperties
95 * @see ifc.sheet._XSheetOperation
96 * @see ifc.table._XCellRange
97 * @see ifc.style._ParagraphProperties
98 * @see ifc.sheet._SheetCellRange
100 public class ScCellRangeObj
extends TestCase
{
101 XSpreadsheetDocument xSheetDoc
= null;
104 * Creates Spreadsheet document.
106 protected void initialize(TestParameters tParam
, PrintWriter log
) {
107 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
) tParam
.getMSF());
110 log
.println("creating a Spreadsheet document");
111 xSheetDoc
= SOF
.createCalcDoc(null);
112 } catch (com
.sun
.star
.uno
.Exception e
) {
113 // Some exception occures.FAILED
114 e
.printStackTrace(log
);
115 throw new StatusException("Couldn't create document", e
);
120 * Disposes Spreadsheet document.
122 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
123 log
.println(" disposing xSheetDoc ");
125 XComponent oComp
= (XComponent
) UnoRuntime
.queryInterface(
126 XComponent
.class, xSheetDoc
);
127 util
.DesktopTools
.closeDoc(oComp
);
131 * Creating a Testenvironment for the interfaces to be tested.
132 * Retrieves a collection of spreadsheets from a document,
133 * and takes one of them. Retrieves some cell range from the spreadsheet.
134 * The retrieved cell range is instance of the service
135 * <code>com.sun.star.sheet.SheetCellRange</code>.
136 * Object relations created :
138 * <li> <code>'SHEET'</code> for
139 * {@link ifc.sheet._XArrayFormulaRange} (the spreadsheet which the cell
140 * range was retrieved from)</li>
142 * @see com.sun.star.sheet.XSpreadsheet
144 protected TestEnvironment
createTestEnvironment(TestParameters Param
,
146 XInterface oObj
= null;
147 XCellRange testRange
;
150 // creation of testobject here
151 // first we write what we are intend to do to log file
152 log
.println("Creating a test environment");
154 XSpreadsheets oSpreadsheets
= ((XSpreadsheetDocument
) UnoRuntime
.queryInterface(
155 XSpreadsheetDocument
.class,
156 xSheetDoc
)).getSheets();
157 XNameAccess oNames
= (XNameAccess
) UnoRuntime
.queryInterface(
158 XNameAccess
.class, oSpreadsheets
);
160 XSpreadsheet oSheet
= null;
163 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
164 new Type(XSpreadsheet
.class),
165 oNames
.getByName(oNames
.getElementNames()[0]));
167 oObj
= oSheet
.getCellRangeByPosition(0, 0, 3, 4);
168 testRange
= (XCellRange
) UnoRuntime
.queryInterface(
169 XCellRange
.class, oObj
);
170 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
171 e
.printStackTrace(log
);
172 throw new StatusException(
173 "Error getting cell object from spreadsheet document", e
);
174 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
175 e
.printStackTrace(log
);
176 throw new StatusException(
177 "Error getting cell object from spreadsheet document", e
);
178 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
179 e
.printStackTrace(log
);
180 throw new StatusException(
181 "Error getting cell object from spreadsheet document", e
);
182 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
183 e
.printStackTrace(log
);
184 throw new StatusException(
185 "Error getting cell object from spreadsheet document", e
);
189 TestEnvironment tEnv
= new TestEnvironment(oObj
);
191 tEnv
.addObjRelation("SHEET", oSheet
);
192 // add expected results for the XCellRangesQuery interface test
193 String
[]expectedResults
= new String
[7];
194 expectedResults
[_XCellRangesQuery
.QUERYCOLUMNDIFFERENCES
] = "Sheet1.B1:C1;Sheet1.B3";
195 expectedResults
[_XCellRangesQuery
.QUERYCONTENTCELLS
] = "Sheet1.B2:B3";
196 expectedResults
[_XCellRangesQuery
.QUERYEMPTYCELLS
] = "Sheet1.A1 ... Sheet1.B1 ... Sheet1.B5 ... Sheet1.C3 ... Sheet1.D1";
197 expectedResults
[_XCellRangesQuery
.QUERYFORMULACELLS
] = "Sheet1.C2";
198 expectedResults
[_XCellRangesQuery
.QUERYINTERSECTION
] = "Sheet1.D4";
199 expectedResults
[_XCellRangesQuery
.QUERYROWDIFFERENCES
] = "Sheet1.A2:A4;Sheet1.C2:D4";
200 expectedResults
[_XCellRangesQuery
.QUERYVISIBLECELLS
] = "Sheet1.A2";
201 tEnv
.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS", expectedResults
);
202 tEnv
.addObjRelation("XCellRangesQuery.CREATEENTRIES", Boolean
.TRUE
);
204 XPropertySet PropSet
= (XPropertySet
) UnoRuntime
.queryInterface(
205 XPropertySet
.class, oObj
);
206 tEnv
.addObjRelation("PropSet", PropSet
);
208 // XSearchable: Add a cell to make a seacrchable entry
210 tEnv
.addObjRelation("XSearchable.MAKEENTRYINCELL", new XCell
[] {
211 testRange
.getCellByPosition(0,0), testRange
.getCellByPosition(0,1)});
213 catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
){
214 e
.printStackTrace((PrintWriter
)log
);
215 log
.println("Cannot make required object relation 'XSearchable.MAKEENTRYINCELL'.");
219 /* Object[][] newData = new Object[5][4];
220 for (int i=0; i<newData.length; i++) {
221 for (int j=0; j<newData[i].length; j++) {
222 newData[i][j] = new Double(i*10+j);
225 tEnv.addObjRelation("NewData", newData); */
227 //Adding relation for util.XSortable
228 final PrintWriter finalLog
= log
;
229 final XCellRange oTable
= testRange
;
230 tEnv
.addObjRelation("SORTCHECKER",
231 new ifc
.util
._XSortable
.XSortChecker() {
232 PrintWriter out
= finalLog
;
234 public void setPrintWriter(PrintWriter log
) {
238 public void prepareToSort() {
240 oTable
.getCellByPosition(0, 0).setValue(4);
241 oTable
.getCellByPosition(0, 1).setFormula("b");
242 oTable
.getCellByPosition(0, 2).setValue(3);
243 oTable
.getCellByPosition(0, 3).setValue(23);
244 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
245 out
.println("Exception while checking sort");
249 public boolean checkSort(boolean isSortNumbering
,
250 boolean isSortAscending
) {
251 out
.println("Sort checking...");
254 String
[] value
= new String
[4];
256 for (int i
= 0; i
< 4; i
++) {
258 XCell cell
= oTable
.getCellByPosition(0, i
);
259 value
[i
] = cell
.getFormula();
260 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
261 out
.println("Exception while checking sort");
265 if (isSortNumbering
) {
266 if (isSortAscending
) {
267 out
.println("Sorting ascending");
269 String
[] rightVal
= { "3", "4", "23", "b" };
270 String
[] vals
= { value
[0], value
[1], value
[2], value
[3] };
271 res
= ValueComparer
.equalValue(vals
, rightVal
);
272 out
.println("Expected 3, 4, 23, b");
273 out
.println("getting: " + value
[0] + ", " +
274 value
[1] + ", " + value
[2] + ", " +
277 String
[] rightVal
= { "b", "23", "4", "3" };
278 String
[] vals
= { value
[0], value
[1], value
[2], value
[3] };
279 res
= ValueComparer
.equalValue(vals
, rightVal
);
280 out
.println("Expected b, 23, 4, 3");
281 out
.println("getting: " + value
[0] + ", " +
282 value
[1] + ", " + value
[2] + ", " +
286 if (isSortAscending
) {
287 String
[] rightVal
= { "3", "4", "23", "b" };
288 res
= ValueComparer
.equalValue(value
, rightVal
);
289 out
.println("Expected 3, 4, 23, b");
290 out
.println("getting: " + value
[0] + ", " +
291 value
[1] + ", " + value
[2] + ", " +
294 String
[] rightVal
= { "b", "23", "4", "3" };
295 res
= ValueComparer
.equalValue(value
, rightVal
);
296 out
.println("Expected b, 23, 4, 3");
297 out
.println("getting: " + value
[0] + ", " +
298 value
[1] + ", " + value
[2] + ", " +
304 out
.println("Sorted correctly");
306 out
.println("Sorted uncorrectly");
315 } // finish class ScCellRangeObj