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