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 complex
.cellRanges
;
21 import com
.sun
.star
.container
.XIndexAccess
;
22 import com
.sun
.star
.lang
.XMultiServiceFactory
;
23 import com
.sun
.star
.sheet
.XCellRangesQuery
;
24 import com
.sun
.star
.sheet
.XSheetCellRanges
;
25 import com
.sun
.star
.sheet
.XSpreadsheet
;
26 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
27 import com
.sun
.star
.sheet
.XSpreadsheets
;
28 import com
.sun
.star
.table
.CellAddress
;
29 import com
.sun
.star
.uno
.AnyConverter
;
30 import com
.sun
.star
.uno
.Type
;
31 import com
.sun
.star
.uno
.UnoRuntime
;
32 import com
.sun
.star
.uno
.XInterface
;
33 import com
.sun
.star
.util
.XCloseable
;
34 import util
.SOfficeFactory
;
36 import org
.junit
.After
;
37 import org
.junit
.AfterClass
;
38 import org
.junit
.Before
;
39 import org
.junit
.BeforeClass
;
40 import org
.junit
.Test
;
41 import org
.openoffice
.test
.OfficeConnection
;
42 import static org
.junit
.Assert
.*;
44 import com
.sun
.star
.container
.XNamed
;
47 * Check the XCellRangesQuery interface on the SheetCell service. test was
48 * created for bug i20044.
50 public class CheckXCellRangesQuery
{
51 XSpreadsheetDocument m_xSheetDoc
= null;
52 XCellRangesQuery m_xCell
= null;
53 XSpreadsheet m_xSpreadSheet
= null;
54 String sSheetName
= "";
57 * Creates Spreadsheet document and the test object,
58 * before the actual test starts.
60 @Before public void before() {
61 // create a calc document
62 final XMultiServiceFactory xMsf
= UnoRuntime
.queryInterface(XMultiServiceFactory
.class, connection
.getComponentContext().getServiceManager());
63 SOfficeFactory SOF
= SOfficeFactory
.getFactory(xMsf
);
66 System
.out
.println( "creating a Spreadsheet document" );
67 m_xSheetDoc
= SOF
.createCalcDoc(null);
68 } catch ( com
.sun
.star
.uno
.Exception e
) {
69 // Some exception occurs.FAILED
71 fail( "Couldn?t create document");
73 XInterface oObj
= null;
76 System
.out
.println("Getting spreadsheet") ;
77 XSpreadsheets oSheets
= m_xSheetDoc
.getSheets() ;
78 XIndexAccess oIndexSheets
=
79 UnoRuntime
.queryInterface(XIndexAccess
.class, oSheets
);
80 m_xSpreadSheet
= (XSpreadsheet
) AnyConverter
.toObject(
81 new Type(XSpreadsheet
.class),oIndexSheets
.getByIndex(0));
83 // get the first sheet name
84 XNamed xNamed
= (XNamed
) AnyConverter
.toObject(new Type(XNamed
.class),m_xSpreadSheet
);
85 sSheetName
= xNamed
.getName();
88 System
.out
.println("Getting a cell from sheet") ;
89 oObj
= m_xSpreadSheet
.getCellByPosition(2, 3);
90 m_xCell
= UnoRuntime
.queryInterface(XCellRangesQuery
.class, oObj
);
92 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
94 fail("Error getting cell object from spreadsheet document");
95 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
97 fail("Error getting cell object from spreadsheet document");
98 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
100 fail("Error getting cell object from spreadsheet document");
103 // set one value for comparison.
105 m_xSpreadSheet
.getCellByPosition(1, 1).setValue(15);
106 m_xSpreadSheet
.getCellByPosition(1, 3).setValue(5);
107 m_xSpreadSheet
.getCellByPosition(2, 1).setFormula("=B2+B4");
108 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
110 fail("Could not fill cell (1, 1) with a value.");
116 * this method closes a calc document and resets the corresponding class variable xSheetDoc
118 protected boolean closeSpreadsheetDocument() {
119 boolean worked
= true;
121 System
.out
.println(" disposing xSheetDoc ");
124 XCloseable oCloser
= UnoRuntime
.queryInterface(
125 XCloseable
.class, m_xSheetDoc
);
127 } catch (com
.sun
.star
.util
.CloseVetoException e
) {
129 System
.out
.println("Couldn't close document");
130 } catch (com
.sun
.star
.lang
.DisposedException e
) {
132 System
.out
.println("Document already disposed");
133 } catch (NullPointerException e
) {
135 System
.out
.println("Couldn't get XCloseable");
143 @After public void after()
145 closeSpreadsheetDocument();
149 * Perform some tests on an empty cell:
151 * <li>compare an empty cell with a cell with a value in the same column</li>
152 * <li>compare an empty cell with a cell with a value in the same row</li>
153 * <li>query for empty cells</li>
156 @Test public void checkEmptyCell() {
157 System
.out
.println("Checking an empty cell...");
158 // compare an empty cell with a cell with a value
159 assertTrue("\tQuery column differences did not return the correct value.", _queryColumnDifferences(sSheetName
+".C4"));
160 // compare an empty cell with a cell with a value
161 assertTrue("\tQuery column differences did not return the correct value.", _queryRowDifferences(sSheetName
+".C4"));
162 System
.out
.println("...done");
166 * Perform some tests on a filled cell:
168 * <li>compare an cell with value 5 with a cell with value 15 in the same column</li>
169 * <li>compare an cell with value 5 with a cell with value 15 in the same row</li>
170 * <li>query for an empty cell.</li>
173 @Test public void checkFilledCell() {
174 System
.out
.println("Checking a filled cell...");
176 // fill the cell with a value
178 m_xSpreadSheet
.getCellByPosition(2, 3).setValue(15);
179 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
181 fail("Could not fill cell (2, 3) with a value.");
184 // compare an cell with value 5 with a cell with value 15
185 assertTrue("\tQuery column differences did not return the correct value.", _queryColumnDifferences(sSheetName
+ ".C4"));
186 // compare an cell with value 5 with a cell with value 15
187 assertTrue("\tQuery column differences did not return the correct value.", _queryRowDifferences(sSheetName
+".C4"));
188 // try to get nothing
189 assertTrue("\tQuery empty cells did not return the correct value.", _queryEmptyCells(""));
190 System
.out
.println("...done");
195 * Query column differences between my cell(2,3) and (1,1).
196 * @param expected The expected outcome value.
197 * @return True, if the result equals the expected result.
199 public boolean _queryColumnDifferences(String expected
) {
200 System
.out
.println("\tQuery column differences");
201 XSheetCellRanges ranges
= m_xCell
.queryColumnDifferences(
202 new CellAddress((short) 0, 1, 1));
203 String getting
= ranges
.getRangeAddressesAsString();
205 if (!getting
.equals(expected
)) {
206 System
.out
.println("\tGetting: " + getting
);
207 System
.out
.println("\tShould have been: " + expected
);
214 * Query for an empty cell.
215 * @param expected The expected outcome value.
216 * @return True, if the result equals the expected result.
218 public boolean _queryEmptyCells(String expected
) {
219 System
.out
.println("\tQuery empty cells");
220 XSheetCellRanges ranges
= m_xCell
.queryEmptyCells();
221 String getting
= ranges
.getRangeAddressesAsString();
223 if (!getting
.equals(expected
)) {
224 System
.out
.println("\tGetting: " + getting
);
225 System
.out
.println("\tShould have been: " + expected
);
232 * Query row differences between my cell(2,3) and (1,1).
233 * @param expected The expected outcome value.
234 * @return True, if the result equals the expected result.
236 public boolean _queryRowDifferences(String expected
) {
237 System
.out
.println("\tQuery row differences");
238 XSheetCellRanges ranges
= m_xCell
.queryRowDifferences(
239 new CellAddress((short) 0, 1, 1));
240 String getting
= ranges
.getRangeAddressesAsString();
242 if (!getting
.equals(expected
)) {
243 System
.out
.println("\tGetting: " + getting
);
244 System
.out
.println("\tShould have been: " + expected
);
252 @BeforeClass public static void setUpConnection() throws Exception
{
256 @AfterClass public static void tearDownConnection()
257 throws InterruptedException
, com
.sun
.star
.uno
.Exception
259 connection
.tearDown();
262 private static final OfficeConnection connection
= new OfficeConnection();