bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDDELinkObj.java
blobf669f387af8a857f8cc22fcb19c8f87eecae3eb9
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 .
18 package mod._sc;
20 import java.io.PrintWriter;
22 import lib.StatusException;
23 import lib.TestCase;
24 import lib.TestEnvironment;
25 import lib.TestParameters;
26 import util.SOfficeFactory;
28 import com.sun.star.beans.XPropertySet;
29 import com.sun.star.container.XIndexAccess;
30 import com.sun.star.container.XNameAccess;
31 import com.sun.star.lang.XComponent;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.sheet.XSpreadsheet;
34 import com.sun.star.sheet.XSpreadsheetDocument;
35 import com.sun.star.sheet.XSpreadsheets;
36 import com.sun.star.uno.AnyConverter;
37 import com.sun.star.uno.Type;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
41 /**
42 * Test for object which is represented by service
43 * <code>com.sun.star.sheet.DDELink</code>. <p>
44 * Object implements the following interfaces :
45 * <ul>
46 * <li> <code>com::sun::star::container::XNamed</code></li>
47 * <li> <code>com::sun::star::util::XRefreshable</code></li>
48 * <li> <code>com::sun::star::sheet::XDDELink</code></li>
49 * </ul>
50 * The following files used by this test :
51 * <ul>
52 * <li><b> ScDDELinksObj.ods </b> : the predefined testdocument </li>
53 * </ul> <p>
54 * @see com.sun.star.sheet.DDELink
55 * @see com.sun.star.container.XNamed
56 * @see com.sun.star.util.XRefreshable
57 * @see com.sun.star.sheet.XDDELink
58 * @see ifc.container._XNamed
59 * @see ifc.util._XRefreshable
60 * @see ifc.sheet._XDDELink
62 public class ScDDELinkObj extends TestCase {
63 private XSpreadsheetDocument xSheetDoc = null;
64 static XComponent oDoc = null;
66 /**
67 * Creates Spreadsheet document.
69 protected void initialize( TestParameters tParam, PrintWriter log ) {
70 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
72 try {
73 log.println( "creating a Spreadsheet document" );
74 xSheetDoc = SOF.createCalcDoc(null);
75 } catch ( com.sun.star.uno.Exception e ) {
76 // Some exception occurs.FAILED
77 e.printStackTrace( log );
78 throw new StatusException( "Couldn't create document", e );
83 /**
84 * Disposes Spreadsheet document and testdocument if it was loaded already.
86 protected void cleanup( TestParameters tParam, PrintWriter log ) {
87 log.println( " disposing xSheetDoc " );
88 XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ;
89 util.DesktopTools.closeDoc(oComp);
90 if (oDoc != null) {
91 util.DesktopTools.closeDoc(oDoc);
95 /**
96 * Creating a Testenvironment for the interfaces to be tested.
97 * Disposes the testdocument if it was loaded already.
98 * Creates an instance of the <code>com.sun.star.frame.Desktop</code>
99 * and loads the predefined testdocument. Retrieves a collection of
100 * spreadsheets from a document and takes one of them. Sets specific formula
101 * to some cells in the spreadsheet(the formula specify DDE links to the
102 * testdocument). Retrieves the collection of DDE links in the document and
103 * retrives first DDE link from the collection.
104 * The retrived DDE link is the instance of the service
105 * <code>com.sun.star.sheet.DDELink</code>.
106 * Object relations created :
107 * <ul>
108 * <li> <code>'setName'</code> for
109 * {@link ifc.container._XNamed}(specify that the method
110 * <code>setName</code> must not be tested)</li>
111 * <li> <code>'APPLICATION'</code> for
112 * {@link ifc.sheet._XDDELink}(the name of the current application)</li>
113 * <li> <code>'ITEM'</code> for
114 * {@link ifc.sheet._XDDELink}(the DDE item that was set in the formula)</li>
115 * <li> <code>'TOPIC'</code> for
116 * {@link ifc.sheet._XDDELink}(the full testdocument name)</li>
117 * </ul>
118 * @see com.sun.star.frame.Desktop
119 * @see com.sun.star.sheet.DDELink
121 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
123 XInterface oObj = null;
125 if (oDoc != null) {
126 util.DesktopTools.closeDoc(oDoc);
129 // creation of testobject here
130 // first we write what we are intend to do to log file
131 log.println( "Creating a test environment" );
133 // create testobject here
135 XMultiServiceFactory oMSF = (XMultiServiceFactory)Param.getMSF();
137 // load the predefined testdocument
138 String testdoc = util.utils.getFullTestURL("ScDDELinksObj.ods");
139 try {
140 oDoc = SOfficeFactory.getFactory(oMSF).loadDocument(testdoc);
141 } catch (com.sun.star.lang.IllegalArgumentException e) {
142 e.printStackTrace(log);
143 throw new StatusException("Can't load test document", e);
144 } catch (com.sun.star.io.IOException e) {
145 e.printStackTrace(log);
146 throw new StatusException("Can't load test document", e);
147 } catch (com.sun.star.uno.Exception e) {
148 e.printStackTrace(log);
149 throw new StatusException("Can't load test document", e);
152 log.println("getting sheets");
153 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
155 log.println("getting a sheet");
156 XSpreadsheet oSheet = null;
157 XIndexAccess oIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
158 try {
159 oSheet = (XSpreadsheet) AnyConverter.toObject(
160 new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
161 } catch (com.sun.star.lang.WrappedTargetException e) {
162 e.printStackTrace(log);
163 throw new StatusException("Couldn't get a spreadsheet", e);
164 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
165 e.printStackTrace(log);
166 throw new StatusException("Couldn't get a spreadsheet", e);
167 } catch (com.sun.star.lang.IllegalArgumentException e) {
168 e.printStackTrace(log);
169 throw new StatusException("Couldn't get a spreadsheet", e);
172 log.println("filling some cells");
173 String sAppl = "soffice";
174 String sItem = "Sheet1.A1";
175 testdoc = util.utils.getFullTestDocName("ScDDELinksObj.ods");
176 try {
177 oSheet.getCellByPosition(5, 5).setFormula(
178 "=DDE(\""+ sAppl +"\";\""+testdoc+"\";\""+ sItem +"\"");
179 oSheet.getCellByPosition(1, 4).setFormula(
180 "=DDE(\""+ sAppl +"\";\""+testdoc+"\";\""+ sItem +"\"");
181 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
182 e.printStackTrace(log);
183 throw new StatusException(
184 "Exception occurred while filling cells", e);
187 try {
188 log.println("Getting test object ") ;
190 // Getting named ranges.
191 XPropertySet docProps = UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
192 XNameAccess links = (XNameAccess) AnyConverter.toObject(
193 new Type(XNameAccess.class), UnoRuntime.queryInterface(
194 XNameAccess.class, docProps.getPropertyValue("DDELinks")));
196 String[] linkNames = links.getElementNames();
198 oObj = (XInterface) AnyConverter.toObject(
199 new Type(XInterface.class),links.getByName(linkNames[0]));
200 log.println("Creating object - " +
201 ((oObj == null) ? "FAILED" : "OK"));
202 } catch (com.sun.star.lang.WrappedTargetException e) {
203 e.printStackTrace(log) ;
204 throw new StatusException(
205 "Error getting test object from spreadsheet document", e) ;
206 } catch (com.sun.star.beans.UnknownPropertyException e) {
207 e.printStackTrace(log) ;
208 throw new StatusException(
209 "Error getting test object from spreadsheet document", e) ;
210 } catch (com.sun.star.container.NoSuchElementException e) {
211 e.printStackTrace(log) ;
212 throw new StatusException(
213 "Error getting test object from spreadsheet document", e) ;
214 } catch (com.sun.star.lang.IllegalArgumentException e) {
215 e.printStackTrace(log) ;
216 throw new StatusException(
217 "Error getting test object from spreadsheet document", e) ;
220 TestEnvironment tEnv = new TestEnvironment( oObj );
222 // Other parameters required for interface tests
223 tEnv.addObjRelation("APPLICATION", sAppl);
224 tEnv.addObjRelation("ITEM", sItem);
225 tEnv.addObjRelation("TOPIC", testdoc);
226 tEnv.addObjRelation("setName", new Boolean(true));
228 return tEnv;