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