xref: /AOO41X/main/qadevOOo/tests/java/mod/_sc/ScScenariosObj.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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.container.XIndexAccess;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.sheet.XCellRangeAddressable;
42 import com.sun.star.sheet.XScenariosSupplier;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.sheet.XSpreadsheets;
46 import com.sun.star.table.CellRangeAddress;
47 import com.sun.star.table.XCellRange;
48 import com.sun.star.uno.AnyConverter;
49 import com.sun.star.uno.Type;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XInterface;
52 
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.sheet.Scenarios</code>. <p>
56 * Object implements the following interfaces :
57 * <ul>
58 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
59 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
60 *  <li> <code>com::sun::star::sheet::XScenarios</code></li>
61 * </ul>
62 * @see com.sun.star.sheet.Scenarios
63 * @see com.sun.star.container.XNameAccess
64 * @see com.sun.star.container.XElementAccess
65 * @see com.sun.star.sheet.XScenarios
66 * @see ifc.container._XNameAccess
67 * @see ifc.container._XElementAccess
68 * @see ifc.sheet._XScenarios
69 */
70 public class ScScenariosObj extends TestCase {
71     public static XSpreadsheetDocument xSpreadsheetDoc;
72 
73     /**
74     * Creates Spreadsheet document.
75     */
76     public void initialize( TestParameters Param, PrintWriter log ) {
77         // get a soffice factory object
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
79 
80         try {
81             log.println("creating a spreadsheetdocument");
82             xSpreadsheetDoc = SOF.createCalcDoc(null);
83         } catch (com.sun.star.uno.Exception e) {
84             e.printStackTrace( log );
85             throw new StatusException( "Couldn't create document ", e );
86         }
87     }
88 
89     /**
90     * Disposes Spreadsheet document.
91     */
92     protected void cleanup( TestParameters tParam, PrintWriter log ) {
93         log.println( "    disposing xSheetDoc " );
94         XComponent oComp = (XComponent)
95             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
96         util.DesktopTools.closeDoc(oComp);
97     }
98 
99     /**
100     * Creating a Testenvironment for the interfaces to be tested.
101     * Retrieves a collection of spreadsheets from a document
102     * and takes one of them. Fills some cells of the spreadsheet.
103     * Retrieves the collection of all scenarios using the interface
104     * <code>XScenariosSupplier</code>. Creates a new scenario and adds it to the
105     * collection. This collection is the instance of the service
106     * <code>com.sun.star.sheet.Scenarios</code>.
107     * Object relations created :
108     * <ul>
109     *  <li> <code>'ADDR'</code> for
110     *      {@link ifc.sheet._XScenarios}(the array of cell range addresses
111     *      of the created scenario)</li>
112     * </ul>
113     * @see com.sun.star.sheet.Scenarios
114     * @see com.sun.star.sheet.XScenariosSupplier
115     */
116     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
117 
118         log.println("getting sheets");
119         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
120         log.println("getting a sheet");
121         XSpreadsheet oSheet = null;
122         XIndexAccess oIndexAccess = (XIndexAccess)
123             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
124         try {
125             oSheet = (XSpreadsheet)AnyConverter.toObject(
126                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
127         } catch (com.sun.star.lang.WrappedTargetException e) {
128             e.printStackTrace(log);
129             throw new StatusException( "Couldn't get a spreadsheet", e);
130         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
131             e.printStackTrace(log);
132             throw new StatusException( "Couldn't get a spreadsheet", e);
133         } catch (com.sun.star.lang.IllegalArgumentException e) {
134             e.printStackTrace(log);
135             throw new StatusException( "Couldn't get a spreadsheet", e);
136         }
137 
138         log.println("filling some cells");
139         try {
140             oSheet.getCellByPosition(5, 5).setValue(15);
141             oSheet.getCellByPosition(1, 4).setValue(10);
142             oSheet.getCellByPosition(2, 0).setValue(-5.15);
143         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
144             e.printStackTrace(log);
145             throw new StatusException("Couldn't fill some cell", e);
146         }
147 
148         XScenariosSupplier xSupp = (XScenariosSupplier)
149             UnoRuntime.queryInterface(XScenariosSupplier.class, oSheet);
150         XCellRange oRange = (XCellRange)
151             UnoRuntime.queryInterface(XCellRange.class, oSheet);
152         XCellRange myRange = oRange.getCellRangeByName("A1:N4");
153         XCellRangeAddressable oRangeAddr = (XCellRangeAddressable)
154             UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
155         CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
156 
157         CellRangeAddress[] oAddr = new CellRangeAddress[1];
158         oAddr[0] = myAddr;
159 
160         xSupp.getScenarios().addNewByName("ScScenarios", oAddr, "Range");
161 
162         XInterface oObj = xSupp.getScenarios();
163 
164         TestEnvironment tEnv = new TestEnvironment(oObj);
165 
166         log.println("adding ObjectRelation for XScenarios");
167         tEnv.addObjRelation("ADDR", oAddr);
168 
169         return tEnv;
170     }
171 
172 
173 }    // finish class ScScenariosObj
174 
175