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 .
20 import java
.io
.PrintWriter
;
22 import lib
.StatusException
;
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
;
42 * Test for object which is represented by service
43 * <code>com.sun.star.sheet.DDELink</code>. <p>
44 * Object implements the following interfaces :
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>
50 * The following files used by this test :
52 * <li><b> ScDDELinksObj.ods </b> : the predefined testdocument </li>
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;
67 * Creates Spreadsheet document.
70 protected void initialize( TestParameters tParam
, PrintWriter log
) {
71 SOfficeFactory SOF
= SOfficeFactory
.getFactory( tParam
.getMSF() );
74 log
.println( "creating a Spreadsheet document" );
75 xSheetDoc
= SOF
.createCalcDoc(null);
76 } catch ( com
.sun
.star
.uno
.Exception e
) {
77 // Some exception occurs.FAILED
78 e
.printStackTrace( log
);
79 throw new StatusException( "Couldn't create document", e
);
85 * Disposes Spreadsheet document and testdocument if it was loaded already.
88 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
89 log
.println( " disposing xSheetDoc " );
90 XComponent oComp
= UnoRuntime
.queryInterface(XComponent
.class, xSheetDoc
) ;
91 util
.DesktopTools
.closeDoc(oComp
);
93 util
.DesktopTools
.closeDoc(oDoc
);
98 * Creating a Testenvironment for the interfaces to be tested.
99 * Disposes the testdocument if it was loaded already.
100 * Creates an instance of the <code>com.sun.star.frame.Desktop</code>
101 * and loads the predefined testdocument. Retrieves a collection of
102 * spreadsheets from a document and takes one of them. Sets specific formula
103 * to some cells in the spreadsheet(the formula specify DDE links to the
104 * testdocument). Retrieves the collection of DDE links in the document and
105 * retrieves first DDE link from the collection.
106 * The retrieved DDE link is the instance of the service
107 * <code>com.sun.star.sheet.DDELink</code>.
108 * Object relations created :
110 * <li> <code>'setName'</code> for
111 * {@link ifc.container._XNamed}(specify that the method
112 * <code>setName</code> must not be tested)</li>
113 * <li> <code>'APPLICATION'</code> for
114 * {@link ifc.sheet._XDDELink}(the name of the current application)</li>
115 * <li> <code>'ITEM'</code> for
116 * {@link ifc.sheet._XDDELink}(the DDE item that was set in the formula)</li>
117 * <li> <code>'TOPIC'</code> for
118 * {@link ifc.sheet._XDDELink}(the full testdocument name)</li>
120 * @see com.sun.star.frame.Desktop
121 * @see com.sun.star.sheet.DDELink
124 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
126 XInterface oObj
= null;
129 util
.DesktopTools
.closeDoc(oDoc
);
132 // creation of testobject here
133 // first we write what we are intend to do to log file
134 log
.println( "Creating a test environment" );
136 // create testobject here
138 XMultiServiceFactory oMSF
= Param
.getMSF();
140 // load the predefined testdocument
141 String testdoc
= util
.utils
.getFullTestURL("ScDDELinksObj.ods");
143 oDoc
= SOfficeFactory
.getFactory(oMSF
).loadDocument(testdoc
);
144 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
145 e
.printStackTrace(log
);
146 throw new StatusException("Can't load test document", e
);
147 } catch (com
.sun
.star
.io
.IOException e
) {
148 e
.printStackTrace(log
);
149 throw new StatusException("Can't load test document", e
);
150 } catch (com
.sun
.star
.uno
.Exception e
) {
151 e
.printStackTrace(log
);
152 throw new StatusException("Can't load test document", e
);
155 log
.println("getting sheets");
156 XSpreadsheets xSpreadsheets
= xSheetDoc
.getSheets();
158 log
.println("getting a sheet");
159 XSpreadsheet oSheet
= null;
160 XIndexAccess oIndexAccess
= UnoRuntime
.queryInterface(XIndexAccess
.class, xSpreadsheets
);
162 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
163 new Type(XSpreadsheet
.class),oIndexAccess
.getByIndex(0));
164 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
165 e
.printStackTrace(log
);
166 throw new StatusException("Couldn't get a spreadsheet", e
);
167 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
168 e
.printStackTrace(log
);
169 throw new StatusException("Couldn't get a spreadsheet", e
);
170 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
171 e
.printStackTrace(log
);
172 throw new StatusException("Couldn't get a spreadsheet", e
);
175 log
.println("filling some cells");
176 String sAppl
= "soffice";
177 String sItem
= "Sheet1.A1";
178 testdoc
= util
.utils
.getFullTestDocName("ScDDELinksObj.ods");
180 oSheet
.getCellByPosition(5, 5).setFormula(
181 "=DDE(\""+ sAppl
+"\";\""+testdoc
+"\";\""+ sItem
+"\"");
182 oSheet
.getCellByPosition(1, 4).setFormula(
183 "=DDE(\""+ sAppl
+"\";\""+testdoc
+"\";\""+ sItem
+"\"");
184 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
185 e
.printStackTrace(log
);
186 throw new StatusException(
187 "Exception occurred while filling cells", e
);
191 log
.println("Getting test object ") ;
193 // Getting named ranges.
194 XPropertySet docProps
= UnoRuntime
.queryInterface(XPropertySet
.class, xSheetDoc
);
195 XNameAccess links
= (XNameAccess
) AnyConverter
.toObject(
196 new Type(XNameAccess
.class), UnoRuntime
.queryInterface(
197 XNameAccess
.class, docProps
.getPropertyValue("DDELinks")));
199 String
[] linkNames
= links
.getElementNames();
201 oObj
= (XInterface
) AnyConverter
.toObject(
202 new Type(XInterface
.class),links
.getByName(linkNames
[0]));
203 log
.println("Creating object - " +
204 ((oObj
== null) ?
"FAILED" : "OK"));
205 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
206 e
.printStackTrace(log
) ;
207 throw new StatusException(
208 "Error getting test object from spreadsheet document", e
) ;
209 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
210 e
.printStackTrace(log
) ;
211 throw new StatusException(
212 "Error getting test object from spreadsheet document", e
) ;
213 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
214 e
.printStackTrace(log
) ;
215 throw new StatusException(
216 "Error getting test object from spreadsheet document", e
) ;
217 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
218 e
.printStackTrace(log
) ;
219 throw new StatusException(
220 "Error getting test object from spreadsheet document", e
) ;
223 TestEnvironment tEnv
= new TestEnvironment( oObj
);
225 // Other parameters required for interface tests
226 tEnv
.addObjRelation("APPLICATION", sAppl
);
227 tEnv
.addObjRelation("ITEM", sItem
);
228 tEnv
.addObjRelation("TOPIC", testdoc
);
229 tEnv
.addObjRelation("setName", Boolean
.TRUE
);