1ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5ef39d40dSAndrew Rist * distributed with this work for additional information 6ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10ef39d40dSAndrew Rist * 11ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12ef39d40dSAndrew Rist * 13ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17ef39d40dSAndrew Rist * specific language governing permissions and limitations 18ef39d40dSAndrew Rist * under the License. 19ef39d40dSAndrew Rist * 20ef39d40dSAndrew Rist *************************************************************/ 21ef39d40dSAndrew Rist 22ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package mod._sc; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.io.PrintWriter; 27cdf0e10cSrcweir 28cdf0e10cSrcweir import lib.StatusException; 29cdf0e10cSrcweir import lib.TestCase; 30cdf0e10cSrcweir import lib.TestEnvironment; 31cdf0e10cSrcweir import lib.TestParameters; 32cdf0e10cSrcweir import util.SOfficeFactory; 33cdf0e10cSrcweir 34cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 35cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 36cdf0e10cSrcweir import com.sun.star.container.XNameContainer; 37cdf0e10cSrcweir import com.sun.star.lang.XComponent; 38cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 39cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet; 40cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 41cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets; 42cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 43cdf0e10cSrcweir import com.sun.star.uno.Type; 44cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 45cdf0e10cSrcweir import com.sun.star.uno.XInterface; 46cdf0e10cSrcweir import ifc.sheet._XCellRangesQuery; 47cdf0e10cSrcweir 48cdf0e10cSrcweir 49cdf0e10cSrcweir /** 50cdf0e10cSrcweir * Test for object which is represented by service 51cdf0e10cSrcweir * <code>com.sun.star.sheet.SheetCellRanges</code>. <p> 52cdf0e10cSrcweir * Object implements the following interfaces : 53cdf0e10cSrcweir * <ul> 54cdf0e10cSrcweir * <li> <code>com::sun::star::table::CellProperties</code></li> 55cdf0e10cSrcweir * <li> <code>com::sun::star::container::XNameReplace</code></li> 56cdf0e10cSrcweir * <li> <code>com::sun::star::container::XNameContainer</code></li> 57cdf0e10cSrcweir * <li> <code>com::sun::star::beans::XPropertySet</code></li> 58cdf0e10cSrcweir * <li> <code>com::sun::star::container::XIndexAccess</code></li> 59cdf0e10cSrcweir * <li> <code>com::sun::star::container::XElementAccess</code></li> 60cdf0e10cSrcweir * <li> <code>com::sun::star::container::XEnumerationAccess</code></li> 61cdf0e10cSrcweir * <li> <code>com::sun::star::sheet::XSheetCellRangeContainer</code></li> 62cdf0e10cSrcweir * <li> <code>com::sun::star::sheet::XSheetOperation</code></li> 63cdf0e10cSrcweir * <li> <code>com::sun::star::sheet::XSheetCellRanges</code></li> 64cdf0e10cSrcweir * <li> <code>com::sun::star::container::XNameAccess</code></li> 65cdf0e10cSrcweir * </ul> 66cdf0e10cSrcweir * @see com.sun.star.sheet.SheetCellRanges 67cdf0e10cSrcweir * @see com.sun.star.table.CellProperties 68cdf0e10cSrcweir * @see com.sun.star.container.XNameReplace 69cdf0e10cSrcweir * @see com.sun.star.container.XNameContainer 70cdf0e10cSrcweir * @see com.sun.star.beans.XPropertySet 71cdf0e10cSrcweir * @see com.sun.star.container.XIndexAccess 72cdf0e10cSrcweir * @see com.sun.star.container.XElementAccess 73cdf0e10cSrcweir * @see com.sun.star.container.XEnumerationAccess 74cdf0e10cSrcweir * @see com.sun.star.sheet.XSheetCellRangeContainer 75cdf0e10cSrcweir * @see com.sun.star.sheet.XSheetOperation 76cdf0e10cSrcweir * @see com.sun.star.sheet.XSheetCellRanges 77cdf0e10cSrcweir * @see com.sun.star.container.XNameAccess 78cdf0e10cSrcweir * @see ifc.table._CellProperties 79cdf0e10cSrcweir * @see ifc.container._XNameReplace 80cdf0e10cSrcweir * @see ifc.container._XNameContainer 81cdf0e10cSrcweir * @see ifc.beans._XPropertySet 82cdf0e10cSrcweir * @see ifc.container._XIndexAccess 83cdf0e10cSrcweir * @see ifc.container._XElementAccess 84cdf0e10cSrcweir * @see ifc.container._XEnumerationAccess 85cdf0e10cSrcweir * @see ifc.sheet._XSheetCellRangeContainer 86cdf0e10cSrcweir * @see ifc.sheet._XSheetOperation 87cdf0e10cSrcweir * @see ifc.sheet._XSheetCellRanges 88cdf0e10cSrcweir * @see ifc.container._XNameAccess 89cdf0e10cSrcweir */ 90cdf0e10cSrcweir public class ScCellRangesObj extends TestCase { 91cdf0e10cSrcweir static XSpreadsheetDocument xSheetDoc = null; 92cdf0e10cSrcweir 93cdf0e10cSrcweir /** 94cdf0e10cSrcweir * Creates Spreadsheet document. 95cdf0e10cSrcweir */ 96cdf0e10cSrcweir protected void initialize( TestParameters tParam, PrintWriter log ) { 97cdf0e10cSrcweir 98cdf0e10cSrcweir // get a soffice factory object 99cdf0e10cSrcweir SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF()); 100cdf0e10cSrcweir 101cdf0e10cSrcweir try { 102cdf0e10cSrcweir log.println( "creating a sheetdocument" ); 103*170fb961SPedro Giffuni xSheetDoc = SOF.createCalcDoc(null); 104cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 105cdf0e10cSrcweir // Some exception occures.FAILED 106cdf0e10cSrcweir e.printStackTrace( log ); 107cdf0e10cSrcweir throw new StatusException( "Couldn't create document", e ); 108cdf0e10cSrcweir } 109cdf0e10cSrcweir } 110cdf0e10cSrcweir 111cdf0e10cSrcweir /** 112cdf0e10cSrcweir * Disposes Spreadsheet document. 113cdf0e10cSrcweir */ 114cdf0e10cSrcweir protected void cleanup( TestParameters tParam, PrintWriter log ) { 115cdf0e10cSrcweir log.println( " disposing xSheetDoc " ); 116cdf0e10cSrcweir XComponent oComp = 117cdf0e10cSrcweir (XComponent) UnoRuntime.queryInterface (XComponent.class, xSheetDoc); 118cdf0e10cSrcweir util.DesktopTools.closeDoc(oComp); 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir /** 122cdf0e10cSrcweir * Creating a Testenvironment for the interfaces to be tested. 123cdf0e10cSrcweir * Creates an instance of the service 124cdf0e10cSrcweir * <code>com.sun.star.sheet.SheetCellRanges</code> and fills some cells. 125cdf0e10cSrcweir * Object relations created : 126cdf0e10cSrcweir * <ul> 127cdf0e10cSrcweir * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> for 128cdf0e10cSrcweir * {@link ifc.container._XNameReplace}, 129cdf0e10cSrcweir * {@link ifc.container._XNameContainer} (type of 130cdf0e10cSrcweir * <code>XCellRange</code>)</li> 131cdf0e10cSrcweir * <li> <code>'THRCNT'</code> for 132cdf0e10cSrcweir * {@link ifc.container._XNameReplace}(the number of the running threads 133cdf0e10cSrcweir * that was retrieved from the method parameter <code>Param</code>)</li> 134cdf0e10cSrcweir * <li> <code>'NameReplaceIndex'</code> for 135cdf0e10cSrcweir * {@link ifc.container._XNameReplace} </li> 136cdf0e10cSrcweir * </ul> 137cdf0e10cSrcweir * @see com.sun.star.table.XCellRange 138cdf0e10cSrcweir */ 139cdf0e10cSrcweir protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) { 140cdf0e10cSrcweir 141cdf0e10cSrcweir XInterface oObj = null; 142cdf0e10cSrcweir Object oRange = null ; 143cdf0e10cSrcweir 144cdf0e10cSrcweir // creation of testobject here 145cdf0e10cSrcweir // first we write what we are intend to do to log file 146cdf0e10cSrcweir log.println( "Creating a test environment" ); 147cdf0e10cSrcweir 148cdf0e10cSrcweir // get a soffice factory object 149cdf0e10cSrcweir SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF() ); 150cdf0e10cSrcweir 151cdf0e10cSrcweir log.println("Getting test object "); 152cdf0e10cSrcweir 153cdf0e10cSrcweir XComponent oComp = (XComponent) 154cdf0e10cSrcweir UnoRuntime.queryInterface (XComponent.class, xSheetDoc); 155cdf0e10cSrcweir 156cdf0e10cSrcweir oObj = (XInterface) 157cdf0e10cSrcweir SOF.createInstance(oComp, "com.sun.star.sheet.SheetCellRanges"); 158cdf0e10cSrcweir 159cdf0e10cSrcweir XSpreadsheets oSheets = xSheetDoc.getSheets() ; 160cdf0e10cSrcweir XIndexAccess oIndSheets = (XIndexAccess) 161cdf0e10cSrcweir UnoRuntime.queryInterface (XIndexAccess.class, oSheets); 162cdf0e10cSrcweir XSpreadsheet oSheet = null; 163cdf0e10cSrcweir try { 164cdf0e10cSrcweir oSheet = (XSpreadsheet) AnyConverter.toObject( 165cdf0e10cSrcweir new Type(XSpreadsheet.class), oIndSheets.getByIndex(0)); 166cdf0e10cSrcweir XNameContainer oRanges = (XNameContainer) 167cdf0e10cSrcweir UnoRuntime.queryInterface(XNameContainer.class, oObj); 168cdf0e10cSrcweir 169cdf0e10cSrcweir oRange = oSheet.getCellRangeByName("C1:D4"); 170cdf0e10cSrcweir oRanges.insertByName("Range1", oRange); 171cdf0e10cSrcweir oRange = oSheet.getCellRangeByName("E2:F5"); 172cdf0e10cSrcweir oRanges.insertByName("Range2", oRange); 173cdf0e10cSrcweir oRange = oSheet.getCellRangeByName("G2:H3"); 174cdf0e10cSrcweir oRanges.insertByName("Range3", oRange); 175cdf0e10cSrcweir oRange = oSheet.getCellRangeByName("I7:J8"); 176cdf0e10cSrcweir oRanges.insertByName("Range4", oRange); 177cdf0e10cSrcweir } catch(com.sun.star.lang.WrappedTargetException e) { 178cdf0e10cSrcweir e.printStackTrace(log); 179cdf0e10cSrcweir throw new StatusException("Couldn't create test object", e); 180cdf0e10cSrcweir } catch(com.sun.star.lang.IndexOutOfBoundsException e) { 181cdf0e10cSrcweir e.printStackTrace(log); 182cdf0e10cSrcweir throw new StatusException("Couldn't create test object", e); 183cdf0e10cSrcweir } catch(com.sun.star.container.ElementExistException e) { 184cdf0e10cSrcweir e.printStackTrace(log); 185cdf0e10cSrcweir throw new StatusException("Couldn't create test object", e); 186cdf0e10cSrcweir } catch(com.sun.star.lang.IllegalArgumentException e) { 187cdf0e10cSrcweir e.printStackTrace(log); 188cdf0e10cSrcweir throw new StatusException("Couldn't create test object", e); 189cdf0e10cSrcweir } 190cdf0e10cSrcweir 191cdf0e10cSrcweir log.println("filling some cells"); 192cdf0e10cSrcweir try { 193cdf0e10cSrcweir for (int i = 0; i < 10; i++) { 194cdf0e10cSrcweir for (int j = 0; j < 5; j++) { 195cdf0e10cSrcweir oSheet.getCellByPosition(i, j).setFormula("a"); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir } 198cdf0e10cSrcweir for (int i = 0; i < 10; i++) { 199cdf0e10cSrcweir for (int j = 5; j < 10; j++) { 200cdf0e10cSrcweir oSheet.getCellByPosition(i, j).setValue(i + j); 201cdf0e10cSrcweir } 202cdf0e10cSrcweir } 203cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 204cdf0e10cSrcweir e.printStackTrace(log); 205cdf0e10cSrcweir throw new StatusException ( 206cdf0e10cSrcweir "Exception occurred while filling cells", e); 207cdf0e10cSrcweir } 208cdf0e10cSrcweir 209cdf0e10cSrcweir 210cdf0e10cSrcweir TestEnvironment tEnv = new TestEnvironment( oObj ); 211cdf0e10cSrcweir 212cdf0e10cSrcweir // NameReplaceIndex : _XNameReplace 213cdf0e10cSrcweir log.println( "adding NameReplaceIndex as mod relation to environment" ); 214cdf0e10cSrcweir tEnv.addObjRelation("NameReplaceIndex", "0"); 215cdf0e10cSrcweir 216cdf0e10cSrcweir // INSTANCEn : _XNameContainer; _XNameReplace 217cdf0e10cSrcweir log.println( "adding INSTANCEn as mod relation to environment" ); 218cdf0e10cSrcweir 219cdf0e10cSrcweir int THRCNT = 1; 220cdf0e10cSrcweir if ((String)Param.get("THRCNT") != null) { 221cdf0e10cSrcweir THRCNT= Integer.parseInt((String)Param.get("THRCNT")); 222cdf0e10cSrcweir } 223cdf0e10cSrcweir int a = 0; 224cdf0e10cSrcweir int b = 0; 225cdf0e10cSrcweir for (int n = 1; n < (THRCNT + 1) ; n++) { 226cdf0e10cSrcweir a = n * 2; 227cdf0e10cSrcweir b = a + 1; 228cdf0e10cSrcweir oRange = oSheet.getCellRangeByName("A" + a + ":B" + b); 229cdf0e10cSrcweir log.println( 230cdf0e10cSrcweir "adding INSTANCE" + n + " as mod relation to environment" ); 231cdf0e10cSrcweir 232cdf0e10cSrcweir tEnv.addObjRelation("INSTANCE" + n, oRange); 233cdf0e10cSrcweir } 234cdf0e10cSrcweir 235cdf0e10cSrcweir XPropertySet PropSet = (XPropertySet) 236cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, oObj); 237cdf0e10cSrcweir tEnv.addObjRelation("PropSet",PropSet); 238cdf0e10cSrcweir tEnv.addObjRelation("SHEET", oSheet); 239cdf0e10cSrcweir // add expected results for the XCellRangesQuery interface test 240cdf0e10cSrcweir String[]expectedResults = new String[7]; 241cdf0e10cSrcweir expectedResults[_XCellRangesQuery.QUERYCOLUMNDIFFERENCES] = "Sheet1.I7:J8"; 242cdf0e10cSrcweir expectedResults[_XCellRangesQuery.QUERYCONTENTCELLS] = ""; 243cdf0e10cSrcweir expectedResults[_XCellRangesQuery.QUERYEMPTYCELLS] = ""; 244cdf0e10cSrcweir expectedResults[_XCellRangesQuery.QUERYFORMULACELLS] = ""; 245cdf0e10cSrcweir expectedResults[_XCellRangesQuery.QUERYINTERSECTION] = "Sheet1.D4"; 246cdf0e10cSrcweir expectedResults[_XCellRangesQuery.QUERYROWDIFFERENCES] = "Sheet1.I7:J8"; 247cdf0e10cSrcweir expectedResults[_XCellRangesQuery.QUERYVISIBLECELLS] = "Sheet1.C2:D4"; // first range, first line invisible 248cdf0e10cSrcweir tEnv.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS", expectedResults); 249cdf0e10cSrcweir 250cdf0e10cSrcweir // for XSearchable and XReplaceable interface test 251cdf0e10cSrcweir tEnv.addObjRelation("SEARCHSTRING", "15"); 252cdf0e10cSrcweir 253cdf0e10cSrcweir // for XFormulaQuery interface test 254cdf0e10cSrcweir tEnv.addObjRelation("EXPECTEDDEPENDENTVALUES", new int[]{4,5,1,4}); 255cdf0e10cSrcweir tEnv.addObjRelation("EXPECTEDPRECEDENTVALUES", new int[]{4,5,1,4}); 256cdf0e10cSrcweir return tEnv ; 257cdf0e10cSrcweir } 258cdf0e10cSrcweir 259cdf0e10cSrcweir } // finish class ScCellRangesObj 260cdf0e10cSrcweir 261