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