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: CheckXCellRangesQuery.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 package complex
.cellRanges
;
33 import com
.sun
.star
.container
.XIndexAccess
;
34 import complexlib
.ComplexTestCase
;
35 import com
.sun
.star
.lang
.XMultiServiceFactory
;
36 import com
.sun
.star
.sheet
.CellFlags
;
37 import com
.sun
.star
.sheet
.XCellRangesQuery
;
38 import com
.sun
.star
.sheet
.XSheetCellRanges
;
39 import com
.sun
.star
.sheet
.XSpreadsheet
;
40 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
41 import com
.sun
.star
.sheet
.XSpreadsheets
;
42 import com
.sun
.star
.table
.CellAddress
;
43 import com
.sun
.star
.table
.XColumnRowRange
;
44 import com
.sun
.star
.table
.XTableColumns
;
45 import com
.sun
.star
.table
.XTableRows
;
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
;
50 import java
.io
.PrintWriter
;
51 import util
.SOfficeFactory
;
54 * Check the XCellRangesQuery interface on the SheetCell service. test was
55 * created for bug i20044.
57 public class CheckXCellRangesQuery
extends ComplexTestCase
{
58 XSpreadsheetDocument m_xSheetDoc
= null;
59 XCellRangesQuery m_xCell
= null;
60 XSpreadsheet m_xSpreadSheet
= null;
63 * Get all test methods.
64 * @return The test methods.
66 public String
[] getTestMethodNames() {
67 return new String
[] {"checkEmptyCell", "checkFilledCell"};
71 * Creates Spreadsheet document and the test object,
72 * before the actual test starts.
74 public void before() {
75 // create a calc document
76 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)param
.getMSF() );
79 log
.println( "creating a Spreadsheet document" );
80 m_xSheetDoc
= SOF
.createCalcDoc(null);
81 } catch ( com
.sun
.star
.uno
.Exception e
) {
82 // Some exception occures.FAILED
83 e
.printStackTrace( (PrintWriter
)log
);
84 failed( "Couldn?t create document");
86 XInterface oObj
= null;
89 log
.println("Getting spreadsheet") ;
90 XSpreadsheets oSheets
= m_xSheetDoc
.getSheets() ;
91 XIndexAccess oIndexSheets
= (XIndexAccess
)
92 UnoRuntime
.queryInterface(XIndexAccess
.class, oSheets
);
93 m_xSpreadSheet
= (XSpreadsheet
) AnyConverter
.toObject(
94 new Type(XSpreadsheet
.class),oIndexSheets
.getByIndex(0));
97 log
.println("Getting a cell from sheet") ;
98 oObj
= m_xSpreadSheet
.getCellByPosition(2, 3);
99 m_xCell
= (XCellRangesQuery
)UnoRuntime
.queryInterface(XCellRangesQuery
.class, oObj
);
101 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
102 e
.printStackTrace((PrintWriter
)log
);
103 failed("Error getting cell object from spreadsheet document");
104 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
105 e
.printStackTrace((PrintWriter
)log
);
106 failed("Error getting cell object from spreadsheet document");
107 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
108 e
.printStackTrace((PrintWriter
)log
);
109 failed("Error getting cell object from spreadsheet document");
112 // set one value for comparison.
114 m_xSpreadSheet
.getCellByPosition(1, 1).setValue(15);
115 m_xSpreadSheet
.getCellByPosition(1, 3).setValue(5);
116 m_xSpreadSheet
.getCellByPosition(2, 1).setFormula("=B2+B4");
117 /* m_xSpreadSheet.getCellByPosition(2, 1).setFormula("=B2+B3");
118 m_xSpreadSheet.getCellByPosition(3, 2).setFormula("");
119 m_xSpreadSheet.getCellByPosition(3, 3).setFormula(""); */
120 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
121 e
.printStackTrace((PrintWriter
)log
);
122 failed("Could not fill cell (1, 1) with a value.");
128 * Perform some tests on an empty cell:
130 * <li>compare an empty cell with a cell with a value in the same column</li>
131 * <li>compare an empty cell with a cell with a value in the same row</li>
132 * <li>query for empty cells</li>
135 public void checkEmptyCell() {
136 log
.println("Checking an empty cell...");
137 // compare an empty cell with a cell with a value
138 assure("\tQuery column differences did not return the correct value.", _queryColumnDifferences("Sheet1.C4"), true);
139 // compare an empty cell with a cell with a value
140 assure("\tQuery column differences did not return the correct value.", _queryRowDifferences("Sheet1.C4"), true);
141 // try to get this cell
142 assure("\tQuery empty cells did not return the correct value.", _queryEmptyCells("Sheet1.C4"), true);
143 log
.println("...done");
147 * Perform some tests on a filled cell:
149 * <li>compare an cell with value 5 with a cell with value 15 in the same column</li>
150 * <li>compare an cell with value 5 with a cell with value 15 in the same row</li>
151 * <li>query for an empty cell.</li>
154 public void checkFilledCell() {
155 log
.println("Checking a filled cell...");
157 // fill the cell with a value
159 m_xSpreadSheet
.getCellByPosition(2, 3).setValue(15);
160 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
161 e
.printStackTrace((PrintWriter
)log
);
162 failed("Could not fill cell (2, 3) with a value.");
165 // compare an cell with value 5 with a cell with value 15
166 assure("\tQuery column differences did not return the correct value.", _queryColumnDifferences("Sheet1.C4"), true);
167 // compare an cell with value 5 with a cell with value 15
168 assure("\tQuery column differences did not return the correct value.", _queryRowDifferences("Sheet1.C4"), true);
169 // try to get nothing
170 assure("\tQuery empty cells did not return the correct value.", _queryEmptyCells(""), true);
171 log
.println("...done");
176 * Query column differences between my cell(2,3) and (1,1).
177 * @param expected The expected outcome value.
178 * @return True, if the result equals the expected result.
180 public boolean _queryColumnDifferences(String expected
) {
181 log
.println("\tQuery column differences");
182 XSheetCellRanges ranges
= m_xCell
.queryColumnDifferences(
183 new CellAddress((short) 0, 1, 1));
184 String getting
= ranges
.getRangeAddressesAsString();
186 if (!getting
.equals(expected
)) {
187 log
.println("\tGetting: " + getting
);
188 log
.println("\tShould have been: " + expected
);
195 * Query for an empty cell.
196 * @param expected The expected outcome value.
197 * @return True, if the result equals the expected result.
199 public boolean _queryEmptyCells(String expected
) {
200 log
.println("\tQuery empty cells");
201 XSheetCellRanges ranges
= m_xCell
.queryEmptyCells();
202 String getting
= ranges
.getRangeAddressesAsString();
204 if (!getting
.equals(expected
)) {
205 log
.println("\tGetting: " + getting
);
206 log
.println("\tShould have been: " + expected
);
213 * Query row differences between my cell(2,3) and (1,1).
214 * @param expected The expected outcome value.
215 * @return True, if the result equals the expected result.
217 public boolean _queryRowDifferences(String expected
) {
218 log
.println("\tQuery row differences");
219 XSheetCellRanges ranges
= m_xCell
.queryRowDifferences(
220 new CellAddress((short) 0, 1, 1));
221 String getting
= ranges
.getRangeAddressesAsString();
223 if (!getting
.equals(expected
)) {
224 log
.println("\tGetting: " + getting
);
225 log
.println("\tShould have been: " + expected
);