1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScDDELinkObj.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 import java
.io
.PrintWriter
;
34 import lib
.StatusException
;
36 import lib
.TestEnvironment
;
37 import lib
.TestParameters
;
38 import util
.SOfficeFactory
;
40 import com
.sun
.star
.beans
.XPropertySet
;
41 import com
.sun
.star
.container
.XIndexAccess
;
42 import com
.sun
.star
.container
.XNameAccess
;
43 import com
.sun
.star
.lang
.XComponent
;
44 import com
.sun
.star
.lang
.XMultiServiceFactory
;
45 import com
.sun
.star
.sheet
.XSpreadsheet
;
46 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
47 import com
.sun
.star
.sheet
.XSpreadsheets
;
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
;
54 * Test for object which is represented by service
55 * <code>com.sun.star.sheet.DDELink</code>. <p>
56 * Object implements the following interfaces :
58 * <li> <code>com::sun::star::container::XNamed</code></li>
59 * <li> <code>com::sun::star::util::XRefreshable</code></li>
60 * <li> <code>com::sun::star::sheet::XDDELink</code></li>
62 * The following files used by this test :
64 * <li><b> ScDDELinksObj.sdc </b> : the predefined testdocument </li>
66 * @see com.sun.star.sheet.DDELink
67 * @see com.sun.star.container.XNamed
68 * @see com.sun.star.util.XRefreshable
69 * @see com.sun.star.sheet.XDDELink
70 * @see ifc.container._XNamed
71 * @see ifc.util._XRefreshable
72 * @see ifc.sheet._XDDELink
74 public class ScDDELinkObj
extends TestCase
{
75 static XSpreadsheetDocument xSheetDoc
= null;
76 static XComponent oDoc
= null;
79 * Creates Spreadsheet document.
81 protected void initialize( TestParameters tParam
, PrintWriter log
) {
82 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF() );
85 log
.println( "creating a Spreadsheet document" );
86 xSheetDoc
= SOF
.createCalcDoc(null);
87 } catch ( com
.sun
.star
.uno
.Exception e
) {
88 // Some exception occures.FAILED
89 e
.printStackTrace( log
);
90 throw new StatusException( "Couldn't create document", e
);
96 * Disposes Spreadsheet document and testdocument if it was loaded already.
98 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
99 log
.println( " disposing xSheetDoc " );
100 XComponent oComp
= (XComponent
)
101 UnoRuntime
.queryInterface(XComponent
.class, xSheetDoc
) ;
102 util
.DesktopTools
.closeDoc(oComp
);
104 util
.DesktopTools
.closeDoc(oDoc
);
109 * Creating a Testenvironment for the interfaces to be tested.
110 * Disposes the testdocument if it was loaded already.
111 * Creates an instance of the <code>com.sun.star.frame.Desktop</code>
112 * and loads the predefined testdocument. Retrieves a collection of
113 * spreadsheets from a document and takes one of them. Sets specific formula
114 * to some cells in the spreadsheet(the formula specify DDE links to the
115 * testdocument). Retrieves the collection of DDE links in the document and
116 * retrives first DDE link from the collection.
117 * The retrived DDE link is the instance of the service
118 * <code>com.sun.star.sheet.DDELink</code>.
119 * Object relations created :
121 * <li> <code>'setName'</code> for
122 * {@link ifc.container._XNamed}(specify that the method
123 * <code>setName</code> must not be tested)</li>
124 * <li> <code>'APPLICATION'</code> for
125 * {@link ifc.sheet._XDDELink}(the name of the current application)</li>
126 * <li> <code>'ITEM'</code> for
127 * {@link ifc.sheet._XDDELink}(the DDE item that was set in the formula)</li>
128 * <li> <code>'TOPIC'</code> for
129 * {@link ifc.sheet._XDDELink}(the full testdocument name)</li>
131 * @see com.sun.star.frame.Desktop
132 * @see com.sun.star.sheet.DDELink
134 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
136 XInterface oObj
= null;
139 util
.DesktopTools
.closeDoc(oDoc
);
142 // creation of testobject here
143 // first we write what we are intend to do to log file
144 log
.println( "Creating a test environment" );
146 // create testobject here
148 XMultiServiceFactory oMSF
= (XMultiServiceFactory
)Param
.getMSF();
150 // load the predefined testdocument
151 String testdoc
= util
.utils
.getFullTestURL("ScDDELinksObj.sdc");
153 oDoc
= SOfficeFactory
.getFactory(oMSF
).loadDocument(testdoc
);
154 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
155 e
.printStackTrace(log
);
156 throw new StatusException("Can't load test document", e
);
157 } catch (com
.sun
.star
.io
.IOException e
) {
158 e
.printStackTrace(log
);
159 throw new StatusException("Can't load test document", e
);
160 } catch (com
.sun
.star
.uno
.Exception e
) {
161 e
.printStackTrace(log
);
162 throw new StatusException("Can't load test document", e
);
165 log
.println("getting sheets");
166 XSpreadsheets xSpreadsheets
= (XSpreadsheets
)xSheetDoc
.getSheets();
168 log
.println("getting a sheet");
169 XSpreadsheet oSheet
= null;
170 XIndexAccess oIndexAccess
= (XIndexAccess
)
171 UnoRuntime
.queryInterface(XIndexAccess
.class, xSpreadsheets
);
173 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
174 new Type(XSpreadsheet
.class),oIndexAccess
.getByIndex(0));
175 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
176 e
.printStackTrace(log
);
177 throw new StatusException("Couldn't get a spreadsheet", e
);
178 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
179 e
.printStackTrace(log
);
180 throw new StatusException("Couldn't get a spreadsheet", e
);
181 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
182 e
.printStackTrace(log
);
183 throw new StatusException("Couldn't get a spreadsheet", e
);
186 log
.println("filling some cells");
187 String sAppl
= "soffice";
188 String sItem
= "Sheet1.A1";
189 testdoc
= util
.utils
.getFullTestDocName("ScDDELinksObj.sdc");
191 oSheet
.getCellByPosition(5, 5).setFormula(
192 "=DDE(\""+ sAppl
+"\";\""+testdoc
+"\";\""+ sItem
+"\"");
193 oSheet
.getCellByPosition(1, 4).setFormula(
194 "=DDE(\""+ sAppl
+"\";\""+testdoc
+"\";\""+ sItem
+"\"");
195 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
196 e
.printStackTrace(log
);
197 throw new StatusException(
198 "Exception occurred while filling cells", e
);
202 log
.println("Getting test object ") ;
204 // Getting named ranges.
205 XPropertySet docProps
= (XPropertySet
)
206 UnoRuntime
.queryInterface(XPropertySet
.class, xSheetDoc
);
207 XNameAccess links
= (XNameAccess
) AnyConverter
.toObject(
208 new Type(XNameAccess
.class), UnoRuntime
.queryInterface(
209 XNameAccess
.class, docProps
.getPropertyValue("DDELinks")));
211 String
[] linkNames
= links
.getElementNames();
213 oObj
= (XInterface
) AnyConverter
.toObject(
214 new Type(XInterface
.class),links
.getByName(linkNames
[0]));
215 log
.println("Creating object - " +
216 ((oObj
== null) ?
"FAILED" : "OK"));
217 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
218 e
.printStackTrace(log
) ;
219 throw new StatusException(
220 "Error getting test object from spreadsheet document", e
) ;
221 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
222 e
.printStackTrace(log
) ;
223 throw new StatusException(
224 "Error getting test object from spreadsheet document", e
) ;
225 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
226 e
.printStackTrace(log
) ;
227 throw new StatusException(
228 "Error getting test object from spreadsheet document", e
) ;
229 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
230 e
.printStackTrace(log
) ;
231 throw new StatusException(
232 "Error getting test object from spreadsheet document", e
) ;
235 TestEnvironment tEnv
= new TestEnvironment( oObj
);
237 // Other parameters required for interface tests
238 tEnv
.addObjRelation("APPLICATION", sAppl
);
239 tEnv
.addObjRelation("ITEM", sItem
);
240 tEnv
.addObjRelation("TOPIC", testdoc
);
241 tEnv
.addObjRelation("setName", new Boolean(true));