xref: /trunk/main/sc/qa/complex/cellRanges/CheckXCellRangesQuery.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
115ab5183SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
315ab5183SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
415ab5183SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
515ab5183SAndrew Rist  * distributed with this work for additional information
615ab5183SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
715ab5183SAndrew Rist  * to you under the Apache License, Version 2.0 (the
815ab5183SAndrew Rist  * "License"); you may not use this file except in compliance
915ab5183SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1115ab5183SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1315ab5183SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1415ab5183SAndrew Rist  * software distributed under the License is distributed on an
1515ab5183SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1615ab5183SAndrew Rist  * KIND, either express or implied.  See the License for the
1715ab5183SAndrew Rist  * specific language governing permissions and limitations
1815ab5183SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2015ab5183SAndrew Rist  *************************************************************/
2115ab5183SAndrew Rist 
2215ab5183SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package complex.cellRanges;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
27cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
28cdf0e10cSrcweir // import com.sun.star.sheet.CellFlags;
29cdf0e10cSrcweir import com.sun.star.sheet.XCellRangesQuery;
30cdf0e10cSrcweir import com.sun.star.sheet.XSheetCellRanges;
31cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
32cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
33cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
34cdf0e10cSrcweir import com.sun.star.table.CellAddress;
35cdf0e10cSrcweir // import com.sun.star.table.XColumnRowRange;
36cdf0e10cSrcweir // import com.sun.star.table.XTableColumns;
37cdf0e10cSrcweir // import com.sun.star.table.XTableRows;
38cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
39cdf0e10cSrcweir import com.sun.star.uno.Type;
40cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
41cdf0e10cSrcweir import com.sun.star.uno.XInterface;
42cdf0e10cSrcweir // import java.io.PrintWriter;
43cdf0e10cSrcweir import com.sun.star.util.XCloseable;
44cdf0e10cSrcweir import util.SOfficeFactory;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir import org.junit.After;
47cdf0e10cSrcweir import org.junit.AfterClass;
48cdf0e10cSrcweir import org.junit.Before;
49cdf0e10cSrcweir import org.junit.BeforeClass;
50cdf0e10cSrcweir import org.junit.Test;
51cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
52cdf0e10cSrcweir import static org.junit.Assert.*;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir /**
55cdf0e10cSrcweir  * Check the XCellRangesQuery interface on the SheetCell service. test was
56cdf0e10cSrcweir  * created for bug i20044.
57cdf0e10cSrcweir  */
585971bc35SDamjan Jovanovic public class CheckXCellRangesQuery {
59cdf0e10cSrcweir     XSpreadsheetDocument m_xSheetDoc = null;
60cdf0e10cSrcweir     XCellRangesQuery m_xCell = null;
61cdf0e10cSrcweir     XSpreadsheet m_xSpreadSheet = null;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     /**
64cdf0e10cSrcweir     * Creates Spreadsheet document and the test object,
65cdf0e10cSrcweir     * before the actual test starts.
66cdf0e10cSrcweir     */
before()67cdf0e10cSrcweir     @Before public void before() {
68cdf0e10cSrcweir         // create a calc document
69cdf0e10cSrcweir         // SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)param.getMSF() );
70cdf0e10cSrcweir         final XMultiServiceFactory xMsf = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
71cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(xMsf);
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         try {
74cdf0e10cSrcweir             System.out.println( "creating a Spreadsheet document" );
75cdf0e10cSrcweir             m_xSheetDoc = SOF.createCalcDoc(null);
76cdf0e10cSrcweir         } catch ( com.sun.star.uno.Exception e ) {
77*e598c4deSJohn Bampton             // Some exception occurred. FAILED
78cdf0e10cSrcweir             e.printStackTrace(  );
79cdf0e10cSrcweir             fail( "Couldn?t create document");
80cdf0e10cSrcweir         }
81cdf0e10cSrcweir         XInterface oObj = null;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         try {
84cdf0e10cSrcweir             System.out.println("Getting spreadsheet") ;
85cdf0e10cSrcweir             XSpreadsheets oSheets = m_xSheetDoc.getSheets() ;
86cdf0e10cSrcweir             XIndexAccess oIndexSheets =
87cdf0e10cSrcweir             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
88cdf0e10cSrcweir             m_xSpreadSheet = (XSpreadsheet) AnyConverter.toObject(
89cdf0e10cSrcweir                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
90cdf0e10cSrcweir 
91cdf0e10cSrcweir             // get the cell
92cdf0e10cSrcweir             System.out.println("Getting a cell from sheet") ;
93cdf0e10cSrcweir             oObj = m_xSpreadSheet.getCellByPosition(2, 3);
94cdf0e10cSrcweir             m_xCell = UnoRuntime.queryInterface(XCellRangesQuery.class, oObj);
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
97cdf0e10cSrcweir             e.printStackTrace();
98cdf0e10cSrcweir             fail("Error getting cell object from spreadsheet document");
99cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
100cdf0e10cSrcweir             e.printStackTrace();
101cdf0e10cSrcweir             fail("Error getting cell object from spreadsheet document");
102cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
103cdf0e10cSrcweir             e.printStackTrace();
104cdf0e10cSrcweir             fail("Error getting cell object from spreadsheet document");
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         // set one value for comparison.
108cdf0e10cSrcweir         try {
109cdf0e10cSrcweir             m_xSpreadSheet.getCellByPosition(1, 1).setValue(15);
110cdf0e10cSrcweir             m_xSpreadSheet.getCellByPosition(1, 3).setValue(5);
111cdf0e10cSrcweir             m_xSpreadSheet.getCellByPosition(2, 1).setFormula("=B2+B4");
112cdf0e10cSrcweir /*            m_xSpreadSheet.getCellByPosition(2, 1).setFormula("=B2+B3");
113cdf0e10cSrcweir             m_xSpreadSheet.getCellByPosition(3, 2).setFormula("");
114cdf0e10cSrcweir             m_xSpreadSheet.getCellByPosition(3, 3).setFormula("");            */
115cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
116cdf0e10cSrcweir             e.printStackTrace();
117cdf0e10cSrcweir             fail("Could not fill cell (1, 1) with a value.");
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir        /*
123cdf0e10cSrcweir      * this method closes a calc document and resets the corresponding class variable xSheetDoc
124cdf0e10cSrcweir      */
closeSpreadsheetDocument()125cdf0e10cSrcweir     protected boolean closeSpreadsheetDocument() {
126cdf0e10cSrcweir         boolean worked = true;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         System.out.println("    disposing xSheetDoc ");
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         try {
131cdf0e10cSrcweir             XCloseable oCloser =  UnoRuntime.queryInterface(
132cdf0e10cSrcweir                                          XCloseable.class, m_xSheetDoc);
133cdf0e10cSrcweir             oCloser.close(true);
134cdf0e10cSrcweir         } catch (com.sun.star.util.CloseVetoException e) {
135cdf0e10cSrcweir             worked = false;
136cdf0e10cSrcweir             System.out.println("Couldn't close document");
137cdf0e10cSrcweir         } catch (com.sun.star.lang.DisposedException e) {
138cdf0e10cSrcweir             worked = false;
139cdf0e10cSrcweir             System.out.println("Document already disposed");
140cdf0e10cSrcweir         } catch (java.lang.NullPointerException e) {
141cdf0e10cSrcweir             worked = false;
142cdf0e10cSrcweir             System.out.println("Couldn't get XCloseable");
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         m_xSheetDoc = null;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         return worked;
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir 
after()150cdf0e10cSrcweir     @After public void after()
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir             closeSpreadsheetDocument();
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     /**
156cdf0e10cSrcweir      * Perform some tests on an empty cell:
157cdf0e10cSrcweir      * <ol>
158cdf0e10cSrcweir      * <li>compare an empty cell with a cell with a value in the same column</li>
159cdf0e10cSrcweir      * <li>compare an empty cell with a cell with a value in the same row</li>
160cdf0e10cSrcweir      * <li>query for empty cells</li>
161cdf0e10cSrcweir      * <ol>
162cdf0e10cSrcweir      */
checkEmptyCell()163cdf0e10cSrcweir     @Test public void checkEmptyCell() {
164cdf0e10cSrcweir         System.out.println("Checking an empty cell...");
165cdf0e10cSrcweir         // compare an empty cell with a cell with a value
166cdf0e10cSrcweir         assertTrue("\tQuery column differences did not return the correct value.", _queryColumnDifferences("Sheet1.C4"));
167cdf0e10cSrcweir         // compare an empty cell with a cell with a value
168cdf0e10cSrcweir         assertTrue("\tQuery column differences did not return the correct value.", _queryRowDifferences("Sheet1.C4"));
169cdf0e10cSrcweir         // try to get this cell
170cdf0e10cSrcweir //         assertTrue("\tQuery empty cells did not return the correct value.", _queryEmptyCells("Sheet1.C4"));
171cdf0e10cSrcweir         System.out.println("...done");
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir     /**
175cdf0e10cSrcweir      * Perform some tests on a filled cell:
176cdf0e10cSrcweir      * <ol>
177cdf0e10cSrcweir      * <li>compare an cell with value 5 with a cell with value 15 in the same column</li>
178cdf0e10cSrcweir      * <li>compare an cell with value 5 with a cell with value 15 in the same row</li>
179cdf0e10cSrcweir      * <li>query for an empty cell.</li>
180cdf0e10cSrcweir      * <ol>
181cdf0e10cSrcweir      */
checkFilledCell()182cdf0e10cSrcweir     @Test public void checkFilledCell() {
183cdf0e10cSrcweir         System.out.println("Checking a filled cell...");
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         // fill the cell with a value
186cdf0e10cSrcweir         try {
187cdf0e10cSrcweir             m_xSpreadSheet.getCellByPosition(2, 3).setValue(15);
188cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
189cdf0e10cSrcweir             e.printStackTrace();
190cdf0e10cSrcweir             fail("Could not fill cell (2, 3) with a value.");
191cdf0e10cSrcweir         }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         // compare an cell with value 5 with a cell with value 15
194cdf0e10cSrcweir         assertTrue("\tQuery column differences did not return the correct value.", _queryColumnDifferences("Sheet1.C4"));
195cdf0e10cSrcweir         // compare an cell with value 5 with a cell with value 15
196cdf0e10cSrcweir         assertTrue("\tQuery column differences did not return the correct value.", _queryRowDifferences("Sheet1.C4"));
197cdf0e10cSrcweir         // try to get nothing
198cdf0e10cSrcweir         assertTrue("\tQuery empty cells did not return the correct value.", _queryEmptyCells(""));
199cdf0e10cSrcweir         System.out.println("...done");
200cdf0e10cSrcweir     }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 
203cdf0e10cSrcweir     /**
204cdf0e10cSrcweir      *  Query column differences between my cell(2,3) and (1,1).
205cdf0e10cSrcweir      *  @param expected The expected outcome value.
206cdf0e10cSrcweir      *  @return True, if the result equals the expected result.
207cdf0e10cSrcweir      */
_queryColumnDifferences(String expected)208cdf0e10cSrcweir     public boolean _queryColumnDifferences(String expected) {
209cdf0e10cSrcweir         System.out.println("\tQuery column differences");
210cdf0e10cSrcweir         XSheetCellRanges ranges = m_xCell.queryColumnDifferences(
211cdf0e10cSrcweir                                           new CellAddress((short) 0, 1, 1));
212cdf0e10cSrcweir         String getting = ranges.getRangeAddressesAsString();
213cdf0e10cSrcweir 
214cdf0e10cSrcweir         if (!getting.equals(expected)) {
215cdf0e10cSrcweir             System.out.println("\tGetting: " + getting);
216cdf0e10cSrcweir             System.out.println("\tShould have been: " + expected);
217cdf0e10cSrcweir             return false;
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir         return true;
220cdf0e10cSrcweir     }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     /**
223cdf0e10cSrcweir      * Query for an empty cell.
224cdf0e10cSrcweir      *  @param expected The expected outcome value.
225cdf0e10cSrcweir      *  @return True, if the result equals the expected result.
226cdf0e10cSrcweir      */
_queryEmptyCells(String expected)227cdf0e10cSrcweir     public boolean _queryEmptyCells(String expected) {
228cdf0e10cSrcweir         System.out.println("\tQuery empty cells");
229cdf0e10cSrcweir         XSheetCellRanges ranges = m_xCell.queryEmptyCells();
230cdf0e10cSrcweir         String getting = ranges.getRangeAddressesAsString();
231cdf0e10cSrcweir 
232cdf0e10cSrcweir         if (!getting.equals(expected)) {
233cdf0e10cSrcweir             System.out.println("\tGetting: " + getting);
234cdf0e10cSrcweir             System.out.println("\tShould have been: " + expected);
235cdf0e10cSrcweir             return false;
236cdf0e10cSrcweir         }
237cdf0e10cSrcweir         return true;
238cdf0e10cSrcweir     }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir     /**
241cdf0e10cSrcweir      *  Query row differences between my cell(2,3) and (1,1).
242cdf0e10cSrcweir      *  @param expected The expected outcome value.
243cdf0e10cSrcweir      *  @return True, if the result equals the expected result.
244cdf0e10cSrcweir      */
_queryRowDifferences(String expected)245cdf0e10cSrcweir     public boolean _queryRowDifferences(String expected) {
246cdf0e10cSrcweir         System.out.println("\tQuery row differences");
247cdf0e10cSrcweir         XSheetCellRanges ranges = m_xCell.queryRowDifferences(
248cdf0e10cSrcweir                                           new CellAddress((short) 0, 1, 1));
249cdf0e10cSrcweir         String getting = ranges.getRangeAddressesAsString();
250cdf0e10cSrcweir 
251cdf0e10cSrcweir         if (!getting.equals(expected)) {
252cdf0e10cSrcweir             System.out.println("\tGetting: " + getting);
253cdf0e10cSrcweir             System.out.println("\tShould have been: " + expected);
254cdf0e10cSrcweir             return false;
255cdf0e10cSrcweir         }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir         return true;
258cdf0e10cSrcweir     }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 
setUpConnection()261cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
262cdf0e10cSrcweir         connection.setUp();
263cdf0e10cSrcweir     }
264cdf0e10cSrcweir 
tearDownConnection()265cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
266cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
267cdf0e10cSrcweir     {
268cdf0e10cSrcweir         connection.tearDown();
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
272cdf0e10cSrcweir 
273cdf0e10cSrcweir }
274