1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package mod._sc;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 
34 import com.sun.star.container.XIndexAccess;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sheet.XCellRangeAddressable;
38 import com.sun.star.sheet.XScenariosSupplier;
39 import com.sun.star.sheet.XSpreadsheet;
40 import com.sun.star.sheet.XSpreadsheetDocument;
41 import com.sun.star.sheet.XSpreadsheets;
42 import com.sun.star.table.CellRangeAddress;
43 import com.sun.star.table.XCellRange;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
48 
49 /**
50 * Test for object which is represented by service
51 * <code>com.sun.star.sheet.Scenarios</code>. <p>
52 * Object implements the following interfaces :
53 * <ul>
54 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
55 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
56 *  <li> <code>com::sun::star::sheet::XScenarios</code></li>
57 * </ul>
58 * @see com.sun.star.sheet.Scenarios
59 * @see com.sun.star.container.XNameAccess
60 * @see com.sun.star.container.XElementAccess
61 * @see com.sun.star.sheet.XScenarios
62 * @see ifc.container._XNameAccess
63 * @see ifc.container._XElementAccess
64 * @see ifc.sheet._XScenarios
65 */
66 public class ScScenariosObj extends TestCase {
67     public static XSpreadsheetDocument xSpreadsheetDoc;
68 
69     /**
70     * Creates Spreadsheet document.
71     */
initialize( TestParameters Param, PrintWriter log )72     public void initialize( TestParameters Param, PrintWriter log ) {
73         // get a soffice factory object
74         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
75 
76         try {
77             log.println("creating a spreadsheetdocument");
78             xSpreadsheetDoc = SOF.createCalcDoc(null);
79         } catch (com.sun.star.uno.Exception e) {
80             e.printStackTrace( log );
81             throw new StatusException( "Couldn't create document ", e );
82         }
83     }
84 
85     /**
86     * Disposes Spreadsheet document.
87     */
cleanup( TestParameters tParam, PrintWriter log )88     protected void cleanup( TestParameters tParam, PrintWriter log ) {
89         log.println( "    disposing xSheetDoc " );
90         XComponent oComp = (XComponent)
91             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
92         util.DesktopTools.closeDoc(oComp);
93     }
94 
95     /**
96     * Creating a Testenvironment for the interfaces to be tested.
97     * Retrieves a collection of spreadsheets from a document
98     * and takes one of them. Fills some cells of the spreadsheet.
99     * Retrieves the collection of all scenarios using the interface
100     * <code>XScenariosSupplier</code>. Creates a new scenario and adds it to the
101     * collection. This collection is the instance of the service
102     * <code>com.sun.star.sheet.Scenarios</code>.
103     * Object relations created :
104     * <ul>
105     *  <li> <code>'ADDR'</code> for
106     *      {@link ifc.sheet._XScenarios}(the array of cell range addresses
107     *      of the created scenario)</li>
108     * </ul>
109     * @see com.sun.star.sheet.Scenarios
110     * @see com.sun.star.sheet.XScenariosSupplier
111     */
createTestEnvironment(TestParameters Param, PrintWriter log)112     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
113 
114         log.println("getting sheets");
115         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
116         log.println("getting a sheet");
117         XSpreadsheet oSheet = null;
118         XIndexAccess oIndexAccess = (XIndexAccess)
119             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
120         try {
121             oSheet = (XSpreadsheet)AnyConverter.toObject(
122                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
123         } catch (com.sun.star.lang.WrappedTargetException e) {
124             e.printStackTrace(log);
125             throw new StatusException( "Couldn't get a spreadsheet", e);
126         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
127             e.printStackTrace(log);
128             throw new StatusException( "Couldn't get a spreadsheet", e);
129         } catch (com.sun.star.lang.IllegalArgumentException e) {
130             e.printStackTrace(log);
131             throw new StatusException( "Couldn't get a spreadsheet", e);
132         }
133 
134         log.println("filling some cells");
135         try {
136             oSheet.getCellByPosition(5, 5).setValue(15);
137             oSheet.getCellByPosition(1, 4).setValue(10);
138             oSheet.getCellByPosition(2, 0).setValue(-5.15);
139         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
140             e.printStackTrace(log);
141             throw new StatusException("Couldn't fill some cell", e);
142         }
143 
144         XScenariosSupplier xSupp = (XScenariosSupplier)
145             UnoRuntime.queryInterface(XScenariosSupplier.class, oSheet);
146         XCellRange oRange = (XCellRange)
147             UnoRuntime.queryInterface(XCellRange.class, oSheet);
148         XCellRange myRange = oRange.getCellRangeByName("A1:N4");
149         XCellRangeAddressable oRangeAddr = (XCellRangeAddressable)
150             UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
151         CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
152 
153         CellRangeAddress[] oAddr = new CellRangeAddress[1];
154         oAddr[0] = myAddr;
155 
156         xSupp.getScenarios().addNewByName("ScScenarios", oAddr, "Range");
157 
158         XInterface oObj = xSupp.getScenarios();
159 
160         TestEnvironment tEnv = new TestEnvironment(oObj);
161 
162         log.println("adding ObjectRelation for XScenarios");
163         tEnv.addObjRelation("ADDR", oAddr);
164 
165         return tEnv;
166     }
167 
168 
169 }    // finish class ScScenariosObj
170 
171