1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package mod._sc; 29 30 import java.io.PrintWriter; 31 32 import lib.StatusException; 33 import lib.TestCase; 34 import lib.TestEnvironment; 35 import lib.TestParameters; 36 import util.SOfficeFactory; 37 38 import com.sun.star.beans.XPropertySet; 39 import com.sun.star.lang.XComponent; 40 import com.sun.star.lang.XMultiServiceFactory; 41 import com.sun.star.sheet.XNamedRanges; 42 import com.sun.star.sheet.XSpreadsheetDocument; 43 import com.sun.star.table.CellAddress; 44 import com.sun.star.table.CellRangeAddress; 45 import com.sun.star.uno.AnyConverter; 46 import com.sun.star.uno.Type; 47 import com.sun.star.uno.UnoRuntime; 48 import com.sun.star.uno.XInterface; 49 50 /** 51 * Test for object which is represented by service 52 * <code>com.sun.star.sheet.NamedRange</code>. <p> 53 * Object implements the following interfaces : 54 * <ul> 55 * <li> <code>com::sun::star::container::XNamed</code></li> 56 * <li> <code>com::sun::star::sheet::XNamedRange</code></li> 57 * <li> <code>com::sun::star::sheet::XCellRangeReferrer</code></li> 58 * </ul> 59 * @see com.sun.star.sheet.NamedRange 60 * @see com.sun.star.container.XNamed 61 * @see com.sun.star.sheet.XNamedRange 62 * @see com.sun.star.sheet.XCellRangeReferrer 63 * @see ifc.container._XNamed 64 * @see ifc.sheet._XNamedRange 65 * @see ifc.sheet._XCellRangeReferrer 66 */ 67 public class ScNamedRangeObj extends TestCase { 68 static XSpreadsheetDocument xSheetDoc = null; 69 70 /** 71 * Creates Spreadsheet document. 72 */ 73 protected void initialize( TestParameters tParam, PrintWriter log ) { 74 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() ); 75 76 try { 77 log.println( "creating a Spreadsheet document" ); 78 xSheetDoc = SOF.createCalcDoc(null); 79 } catch ( com.sun.star.uno.Exception e ) { 80 // Some exception occures.FAILED 81 e.printStackTrace( log ); 82 throw new StatusException( "Couldn't create document", e ); 83 } 84 85 } 86 87 /** 88 * Disposes Spreadsheet document. 89 */ 90 protected void cleanup( TestParameters tParam, PrintWriter log ) { 91 log.println( " disposing xSheetDoc " ); 92 XComponent oComp = (XComponent) 93 UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ; 94 util.DesktopTools.closeDoc(oComp); 95 } 96 97 /** 98 * Creating a Testenvironment for the interfaces to be tested. 99 * Retrieves a collection of spreadsheets from a document 100 * and takes one of them. Obtains the value of the property 101 * <code>'NamedRanges'</code> that is the collection of named ranges. 102 * Creates and adds new range to the collection. This new range is the instance of the 103 * service <code>com.sun.star.sheet.NamedRange</code>. 104 * Object relations created : 105 * <ul> 106 * <li> <code>'DATAAREA'</code> for 107 * {@link ifc.sheet._XCellRangeReferrer} (the cell range address of the 108 * created range) </li> 109 * </ul> 110 */ 111 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) { 112 113 XInterface oObj = null; 114 115 // creation of testobject here 116 // first we write what we are intend to do to log file 117 log.println( "Creating a test environment" ); 118 119 log.println("Getting test object ") ; 120 121 // Getting named ranges. 122 XPropertySet docProps = (XPropertySet) 123 UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc); 124 125 Object ranges = null; 126 try { 127 ranges = docProps.getPropertyValue("NamedRanges"); 128 } catch(com.sun.star.lang.WrappedTargetException e){ 129 e.printStackTrace(log); 130 throw new StatusException("Couldn't get NamedRanges", e); 131 } catch(com.sun.star.beans.UnknownPropertyException e){ 132 e.printStackTrace(log); 133 throw new StatusException("Couldn't get NamedRanges", e); 134 } 135 136 XNamedRanges xNamedRanges = (XNamedRanges) 137 UnoRuntime.queryInterface(XNamedRanges.class, ranges); 138 139 CellRangeAddress DataArea = new CellRangeAddress((short)0, 0, 0, 1, 1); 140 CellAddress base = new CellAddress(DataArea.Sheet, 141 DataArea.StartColumn, 142 DataArea.StartRow); 143 144 if (xNamedRanges.hasByName("ANamedRange")) { 145 xNamedRanges.removeByName("ANamedRange"); 146 } 147 148 xNamedRanges.addNewByName("ANamedRange", "A1:B2", base, 0); 149 150 CellAddress listOutputPosition = new CellAddress((short)0, 1, 1); 151 xNamedRanges.outputList(listOutputPosition); 152 153 try { 154 oObj = (XInterface) AnyConverter.toObject( 155 new Type(XInterface.class),xNamedRanges.getByName("ANamedRange")); 156 } catch(com.sun.star.lang.WrappedTargetException e){ 157 e.printStackTrace(log); 158 throw new StatusException("Couldn't get by name", e); 159 } catch(com.sun.star.container.NoSuchElementException e){ 160 e.printStackTrace(log); 161 throw new StatusException("Couldn't get by name", e); 162 } catch(com.sun.star.lang.IllegalArgumentException e){ 163 e.printStackTrace(log); 164 throw new StatusException("Couldn't get by name", e); 165 } 166 167 TestEnvironment tEnv = new TestEnvironment( oObj ); 168 169 // Other parameters required for interface tests 170 tEnv.addObjRelation("DATAAREA", DataArea); 171 172 return tEnv; 173 } 174 175 } 176 177 178