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