bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScScenariosObj.java
blob6c07364ff745644c6ad6cd9b63458f8f67f99b1c
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod._sc;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
29 import com.sun.star.container.XIndexAccess;
30 import com.sun.star.lang.XComponent;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.sheet.XCellRangeAddressable;
33 import com.sun.star.sheet.XScenariosSupplier;
34 import com.sun.star.sheet.XSpreadsheet;
35 import com.sun.star.sheet.XSpreadsheetDocument;
36 import com.sun.star.sheet.XSpreadsheets;
37 import com.sun.star.table.CellRangeAddress;
38 import com.sun.star.table.XCellRange;
39 import com.sun.star.uno.AnyConverter;
40 import com.sun.star.uno.Type;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
44 /**
45 * Test for object which is represented by service
46 * <code>com.sun.star.sheet.Scenarios</code>. <p>
47 * Object implements the following interfaces :
48 * <ul>
49 * <li> <code>com::sun::star::container::XNameAccess</code></li>
50 * <li> <code>com::sun::star::container::XElementAccess</code></li>
51 * <li> <code>com::sun::star::sheet::XScenarios</code></li>
52 * </ul>
53 * @see com.sun.star.sheet.Scenarios
54 * @see com.sun.star.container.XNameAccess
55 * @see com.sun.star.container.XElementAccess
56 * @see com.sun.star.sheet.XScenarios
57 * @see ifc.container._XNameAccess
58 * @see ifc.container._XElementAccess
59 * @see ifc.sheet._XScenarios
61 public class ScScenariosObj extends TestCase {
62 private XSpreadsheetDocument xSpreadsheetDoc;
64 /**
65 * Creates Spreadsheet document.
67 public void initialize( TestParameters Param, PrintWriter log ) {
68 // get a soffice factory object
69 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
71 try {
72 log.println("creating a spreadsheetdocument");
73 xSpreadsheetDoc = SOF.createCalcDoc(null);
74 } catch (com.sun.star.uno.Exception e) {
75 e.printStackTrace( log );
76 throw new StatusException( "Couldn't create document ", e );
80 /**
81 * Disposes Spreadsheet document.
83 protected void cleanup( TestParameters tParam, PrintWriter log ) {
84 log.println( " disposing xSheetDoc " );
85 XComponent oComp = UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
86 util.DesktopTools.closeDoc(oComp);
89 /**
90 * Creating a Testenvironment for the interfaces to be tested.
91 * Retrieves a collection of spreadsheets from a document
92 * and takes one of them. Fills some cells of the spreadsheet.
93 * Retrieves the collection of all scenarios using the interface
94 * <code>XScenariosSupplier</code>. Creates a new scenario and adds it to the
95 * collection. This collection is the instance of the service
96 * <code>com.sun.star.sheet.Scenarios</code>.
97 * Object relations created :
98 * <ul>
99 * <li> <code>'ADDR'</code> for
100 * {@link ifc.sheet._XScenarios}(the array of cell range addresses
101 * of the created scenario)</li>
102 * </ul>
103 * @see com.sun.star.sheet.Scenarios
104 * @see com.sun.star.sheet.XScenariosSupplier
106 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
108 log.println("getting sheets");
109 XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
110 log.println("getting a sheet");
111 XSpreadsheet oSheet = null;
112 XIndexAccess oIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
113 try {
114 oSheet = (XSpreadsheet)AnyConverter.toObject(
115 new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
116 } catch (com.sun.star.lang.WrappedTargetException e) {
117 e.printStackTrace(log);
118 throw new StatusException( "Couldn't get a spreadsheet", e);
119 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
120 e.printStackTrace(log);
121 throw new StatusException( "Couldn't get a spreadsheet", e);
122 } catch (com.sun.star.lang.IllegalArgumentException e) {
123 e.printStackTrace(log);
124 throw new StatusException( "Couldn't get a spreadsheet", e);
127 log.println("filling some cells");
128 try {
129 oSheet.getCellByPosition(5, 5).setValue(15);
130 oSheet.getCellByPosition(1, 4).setValue(10);
131 oSheet.getCellByPosition(2, 0).setValue(-5.15);
132 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
133 e.printStackTrace(log);
134 throw new StatusException("Couldn't fill some cell", e);
137 XScenariosSupplier xSupp = UnoRuntime.queryInterface(XScenariosSupplier.class, oSheet);
138 XCellRange oRange = UnoRuntime.queryInterface(XCellRange.class, oSheet);
139 XCellRange myRange = oRange.getCellRangeByName("A1:N4");
140 XCellRangeAddressable oRangeAddr = UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
141 CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
143 CellRangeAddress[] oAddr = new CellRangeAddress[1];
144 oAddr[0] = myAddr;
146 xSupp.getScenarios().addNewByName("ScScenarios", oAddr, "Range");
148 XInterface oObj = xSupp.getScenarios();
150 TestEnvironment tEnv = new TestEnvironment(oObj);
152 log.println("adding ObjectRelation for XScenarios");
153 tEnv.addObjRelation("ADDR", oAddr);
155 return tEnv;
159 } // finish class ScScenariosObj