merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sc / ScScenariosObj.java
blob09fd79ecbaf19234d4fad674275bc7834d2e73bd
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: ScScenariosObj.java,v $
10 * $Revision: 1.8 $
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.container.XIndexAccess;
42 import com.sun.star.lang.XComponent;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.sheet.XCellRangeAddressable;
45 import com.sun.star.sheet.XScenariosSupplier;
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.CellRangeAddress;
50 import com.sun.star.table.XCellRange;
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.Scenarios</code>. <p>
59 * Object implements the following interfaces :
60 * <ul>
61 * <li> <code>com::sun::star::container::XNameAccess</code></li>
62 * <li> <code>com::sun::star::container::XElementAccess</code></li>
63 * <li> <code>com::sun::star::sheet::XScenarios</code></li>
64 * </ul>
65 * @see com.sun.star.sheet.Scenarios
66 * @see com.sun.star.container.XNameAccess
67 * @see com.sun.star.container.XElementAccess
68 * @see com.sun.star.sheet.XScenarios
69 * @see ifc.container._XNameAccess
70 * @see ifc.container._XElementAccess
71 * @see ifc.sheet._XScenarios
73 public class ScScenariosObj extends TestCase {
74 public static XSpreadsheetDocument xSpreadsheetDoc;
76 /**
77 * Creates Spreadsheet document.
79 public void initialize( TestParameters Param, PrintWriter log ) {
80 // get a soffice factory object
81 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
83 try {
84 log.println("creating a spreadsheetdocument");
85 xSpreadsheetDoc = SOF.createCalcDoc(null);
86 } catch (com.sun.star.uno.Exception e) {
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, xSpreadsheetDoc) ;
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. Fills some cells of the spreadsheet.
106 * Retrieves the collection of all scenarios using the interface
107 * <code>XScenariosSupplier</code>. Creates a new scenario and adds it to the
108 * collection. This collection is the instance of the service
109 * <code>com.sun.star.sheet.Scenarios</code>.
110 * Object relations created :
111 * <ul>
112 * <li> <code>'ADDR'</code> for
113 * {@link ifc.sheet._XScenarios}(the array of cell range addresses
114 * of the created scenario)</li>
115 * </ul>
116 * @see com.sun.star.sheet.Scenarios
117 * @see com.sun.star.sheet.XScenariosSupplier
119 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
121 log.println("getting sheets");
122 XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
123 log.println("getting a sheet");
124 XSpreadsheet oSheet = null;
125 XIndexAccess oIndexAccess = (XIndexAccess)
126 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
127 try {
128 oSheet = (XSpreadsheet)AnyConverter.toObject(
129 new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
130 } catch (com.sun.star.lang.WrappedTargetException e) {
131 e.printStackTrace(log);
132 throw new StatusException( "Couldn't get a spreadsheet", e);
133 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
134 e.printStackTrace(log);
135 throw new StatusException( "Couldn't get a spreadsheet", e);
136 } catch (com.sun.star.lang.IllegalArgumentException e) {
137 e.printStackTrace(log);
138 throw new StatusException( "Couldn't get a spreadsheet", e);
141 log.println("filling some cells");
142 try {
143 oSheet.getCellByPosition(5, 5).setValue(15);
144 oSheet.getCellByPosition(1, 4).setValue(10);
145 oSheet.getCellByPosition(2, 0).setValue(-5.15);
146 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
147 e.printStackTrace(log);
148 throw new StatusException("Couldn't fill some cell", e);
151 XScenariosSupplier xSupp = (XScenariosSupplier)
152 UnoRuntime.queryInterface(XScenariosSupplier.class, oSheet);
153 XCellRange oRange = (XCellRange)
154 UnoRuntime.queryInterface(XCellRange.class, oSheet);
155 XCellRange myRange = oRange.getCellRangeByName("A1:N4");
156 XCellRangeAddressable oRangeAddr = (XCellRangeAddressable)
157 UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
158 CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
160 CellRangeAddress[] oAddr = new CellRangeAddress[1];
161 oAddr[0] = myAddr;
163 xSupp.getScenarios().addNewByName("ScScenarios", oAddr, "Range");
165 XInterface oObj = xSupp.getScenarios();
167 TestEnvironment tEnv = new TestEnvironment(oObj);
169 log.println("adding ObjectRelation for XScenarios");
170 tEnv.addObjRelation("ADDR", oAddr);
172 return tEnv;
176 } // finish class ScScenariosObj