bump product version to 4.1.6.2
[LibreOffice.git] / sc / qa / complex / cellRanges / CheckXCellRangesQuery.java
blob8ec02b0feed5db0816c1d5708eb6bdce6eccccdf
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 complex.cellRanges;
21 import com.sun.star.container.XIndexAccess;
22 // import complexlib.ComplexTestCase;
23 import com.sun.star.lang.XMultiServiceFactory;
24 // import com.sun.star.sheet.CellFlags;
25 import com.sun.star.sheet.XCellRangesQuery;
26 import com.sun.star.sheet.XSheetCellRanges;
27 import com.sun.star.sheet.XSpreadsheet;
28 import com.sun.star.sheet.XSpreadsheetDocument;
29 import com.sun.star.sheet.XSpreadsheets;
30 import com.sun.star.table.CellAddress;
31 // import com.sun.star.table.XColumnRowRange;
32 // import com.sun.star.table.XTableColumns;
33 // import com.sun.star.table.XTableRows;
34 import com.sun.star.uno.AnyConverter;
35 import com.sun.star.uno.Type;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XInterface;
38 // import java.io.PrintWriter;
39 import com.sun.star.util.XCloseable;
40 import util.SOfficeFactory;
42 import org.junit.After;
43 import org.junit.AfterClass;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.openoffice.test.OfficeConnection;
48 import static org.junit.Assert.*;
50 /**
51 * Check the XCellRangesQuery interface on the SheetCell service. test was
52 * created for bug i20044.
54 public class CheckXCellRangesQuery /* extends ComplexTestCase */ {
55 XSpreadsheetDocument m_xSheetDoc = null;
56 XCellRangesQuery m_xCell = null;
57 XSpreadsheet m_xSpreadSheet = null;
59 /**
60 * Get all test methods.
61 * @return The test methods.
63 // public String[] getTestMethodNames() {
64 // return new String[] {"checkEmptyCell", "checkFilledCell"};
65 // }
67 /**
68 * Creates Spreadsheet document and the test object,
69 * before the actual test starts.
71 @Before public void before() {
72 // create a calc document
73 // SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)param.getMSF() );
74 final XMultiServiceFactory xMsf = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
75 SOfficeFactory SOF = SOfficeFactory.getFactory(xMsf);
77 try {
78 System.out.println( "creating a Spreadsheet document" );
79 m_xSheetDoc = SOF.createCalcDoc(null);
80 } catch ( com.sun.star.uno.Exception e ) {
81 // Some exception occures.FAILED
82 e.printStackTrace( );
83 fail( "Couldn?t create document");
85 XInterface oObj = null;
87 try {
88 System.out.println("Getting spreadsheet") ;
89 XSpreadsheets oSheets = m_xSheetDoc.getSheets() ;
90 XIndexAccess oIndexSheets =
91 UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
92 m_xSpreadSheet = (XSpreadsheet) AnyConverter.toObject(
93 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
95 // get the cell
96 System.out.println("Getting a cell from sheet") ;
97 oObj = m_xSpreadSheet.getCellByPosition(2, 3);
98 m_xCell = UnoRuntime.queryInterface(XCellRangesQuery.class, oObj);
100 } catch (com.sun.star.lang.WrappedTargetException e) {
101 e.printStackTrace();
102 fail("Error getting cell object from spreadsheet document");
103 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
104 e.printStackTrace();
105 fail("Error getting cell object from spreadsheet document");
106 } catch (com.sun.star.lang.IllegalArgumentException e) {
107 e.printStackTrace();
108 fail("Error getting cell object from spreadsheet document");
111 // set one value for comparison.
112 try {
113 m_xSpreadSheet.getCellByPosition(1, 1).setValue(15);
114 m_xSpreadSheet.getCellByPosition(1, 3).setValue(5);
115 m_xSpreadSheet.getCellByPosition(2, 1).setFormula("=B2+B4");
116 /* m_xSpreadSheet.getCellByPosition(2, 1).setFormula("=B2+B3");
117 m_xSpreadSheet.getCellByPosition(3, 2).setFormula("");
118 m_xSpreadSheet.getCellByPosition(3, 3).setFormula(""); */
119 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
120 e.printStackTrace();
121 fail("Could not fill cell (1, 1) with a value.");
127 * this method closes a calc document and resets the corresponding class variable xSheetDoc
129 protected boolean closeSpreadsheetDocument() {
130 boolean worked = true;
132 System.out.println(" disposing xSheetDoc ");
134 try {
135 XCloseable oCloser = UnoRuntime.queryInterface(
136 XCloseable.class, m_xSheetDoc);
137 oCloser.close(true);
138 } catch (com.sun.star.util.CloseVetoException e) {
139 worked = false;
140 System.out.println("Couldn't close document");
141 } catch (com.sun.star.lang.DisposedException e) {
142 worked = false;
143 System.out.println("Document already disposed");
144 } catch (java.lang.NullPointerException e) {
145 worked = false;
146 System.out.println("Couldn't get XCloseable");
149 m_xSheetDoc = null;
151 return worked;
154 @After public void after()
156 closeSpreadsheetDocument();
160 * Perform some tests on an empty cell:
161 * <ol>
162 * <li>compare an empty cell with a cell with a value in the same column</li>
163 * <li>compare an empty cell with a cell with a value in the same row</li>
164 * <li>query for empty cells</li>
165 * <ol>
167 @Test public void checkEmptyCell() {
168 System.out.println("Checking an empty cell...");
169 // compare an empty cell with a cell with a value
170 assertTrue("\tQuery column differences did not return the correct value.", _queryColumnDifferences("Sheet1.C4"));
171 // compare an empty cell with a cell with a value
172 assertTrue("\tQuery column differences did not return the correct value.", _queryRowDifferences("Sheet1.C4"));
173 // try to get this cell
174 // assertTrue("\tQuery empty cells did not return the correct value.", _queryEmptyCells("Sheet1.C4"));
175 System.out.println("...done");
179 * Perform some tests on a filled cell:
180 * <ol>
181 * <li>compare an cell with value 5 with a cell with value 15 in the same column</li>
182 * <li>compare an cell with value 5 with a cell with value 15 in the same row</li>
183 * <li>query for an empty cell.</li>
184 * <ol>
186 @Test public void checkFilledCell() {
187 System.out.println("Checking a filled cell...");
189 // fill the cell with a value
190 try {
191 m_xSpreadSheet.getCellByPosition(2, 3).setValue(15);
192 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
193 e.printStackTrace();
194 fail("Could not fill cell (2, 3) with a value.");
197 // compare an cell with value 5 with a cell with value 15
198 assertTrue("\tQuery column differences did not return the correct value.", _queryColumnDifferences("Sheet1.C4"));
199 // compare an cell with value 5 with a cell with value 15
200 assertTrue("\tQuery column differences did not return the correct value.", _queryRowDifferences("Sheet1.C4"));
201 // try to get nothing
202 assertTrue("\tQuery empty cells did not return the correct value.", _queryEmptyCells(""));
203 System.out.println("...done");
208 * Query column differences between my cell(2,3) and (1,1).
209 * @param expected The expected outcome value.
210 * @return True, if the result equals the expected result.
212 public boolean _queryColumnDifferences(String expected) {
213 System.out.println("\tQuery column differences");
214 XSheetCellRanges ranges = m_xCell.queryColumnDifferences(
215 new CellAddress((short) 0, 1, 1));
216 String getting = ranges.getRangeAddressesAsString();
218 if (!getting.equals(expected)) {
219 System.out.println("\tGetting: " + getting);
220 System.out.println("\tShould have been: " + expected);
221 return false;
223 return true;
227 * Query for an empty cell.
228 * @param expected The expected outcome value.
229 * @return True, if the result equals the expected result.
231 public boolean _queryEmptyCells(String expected) {
232 System.out.println("\tQuery empty cells");
233 XSheetCellRanges ranges = m_xCell.queryEmptyCells();
234 String getting = ranges.getRangeAddressesAsString();
236 if (!getting.equals(expected)) {
237 System.out.println("\tGetting: " + getting);
238 System.out.println("\tShould have been: " + expected);
239 return false;
241 return true;
245 * Query row differences between my cell(2,3) and (1,1).
246 * @param expected The expected outcome value.
247 * @return True, if the result equals the expected result.
249 public boolean _queryRowDifferences(String expected) {
250 System.out.println("\tQuery row differences");
251 XSheetCellRanges ranges = m_xCell.queryRowDifferences(
252 new CellAddress((short) 0, 1, 1));
253 String getting = ranges.getRangeAddressesAsString();
255 if (!getting.equals(expected)) {
256 System.out.println("\tGetting: " + getting);
257 System.out.println("\tShould have been: " + expected);
258 return false;
261 return true;
265 @BeforeClass public static void setUpConnection() throws Exception {
266 connection.setUp();
269 @AfterClass public static void tearDownConnection()
270 throws InterruptedException, com.sun.star.uno.Exception
272 connection.tearDown();
275 private static final OfficeConnection connection = new OfficeConnection();