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: ScDDELinksObj.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 ************************************************************************/
33 import java
.io
.PrintWriter
;
35 import lib
.StatusException
;
37 import lib
.TestEnvironment
;
38 import lib
.TestParameters
;
39 import util
.SOfficeFactory
;
42 import com
.sun
.star
.beans
.XPropertySet
;
43 import com
.sun
.star
.container
.XIndexAccess
;
44 import com
.sun
.star
.lang
.XComponent
;
45 import com
.sun
.star
.lang
.XMultiServiceFactory
;
46 import com
.sun
.star
.sheet
.XSpreadsheet
;
47 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
48 import com
.sun
.star
.sheet
.XSpreadsheets
;
49 import com
.sun
.star
.uno
.AnyConverter
;
50 import com
.sun
.star
.uno
.Type
;
51 import com
.sun
.star
.uno
.UnoRuntime
;
52 import com
.sun
.star
.uno
.XInterface
;
55 * Test for object which is represented by service
56 * <code>com.sun.star.sheet.DDELinks</code>. <p>
57 * Object implements the following interfaces :
59 * <li> <code>com::sun::star::container::XNameAccess</code></li>
60 * <li> <code>com::sun::star::container::XElementAccess</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.DDELinks
67 * @see com.sun.star.container.XNameAccess
68 * @see com.sun.star.container.XElementAccess
69 * @see ifc.container._XNameAccess
70 * @see ifc.container._XElementAccess
72 public class ScDDELinksObj
extends TestCase
{
73 static XSpreadsheetDocument xSheetDoc
= null;
74 static XComponent oDoc
= null;
77 * Creates Spreadsheet document.
79 protected void initialize( TestParameters tParam
, PrintWriter log
) {
80 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF() );
83 log
.println( "creating a Spreadsheet document" );
84 xSheetDoc
= SOF
.createCalcDoc(null);
85 } catch ( com
.sun
.star
.uno
.Exception e
) {
86 // Some exception occures.FAILED
87 e
.printStackTrace( log
);
88 throw new StatusException( "Couldn't create document", e
);
94 * Disposes Spreadsheet document and testdocument.
96 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
97 log
.println( " disposing xSheetDoc " );
98 XComponent oComp
= (XComponent
) UnoRuntime
.
99 queryInterface(XComponent
.class, xSheetDoc
) ;
100 util
.DesktopTools
.closeDoc(oComp
);
101 util
.DesktopTools
.closeDoc(oDoc
);
105 * Creating a Testenvironment for the interfaces to be tested.
106 * Disposes the testdocument if it was loaded already.
107 * Creates an instance of the <code>com.sun.star.frame.Desktop</code>
108 * and loads the predefined testdocument. Retrieves a collection of
109 * spreadsheets from a document and takes one of them. Sets specific formula
110 * to some cells in the spreadsheet(the formula specify DDE links to the
111 * testdocument). Retrieves the collection of DDE links in the document.
112 * The retrived collection of DDE link is the instance of the service
113 * <code>com.sun.star.sheet.DDELinks</code>.
114 * @see com.sun.star.frame.Desktop
115 * @see com.sun.star.sheet.DDELinks
117 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
119 XInterface oObj
= null;
121 // creation of testobject here
122 // first we write what we are intend to do to log file
123 log
.println( "Creating a test environment" );
125 // create testobject here
127 XMultiServiceFactory oMSF
= (XMultiServiceFactory
)Param
.getMSF();
129 // load the predefined testdocument
130 String testdoc
= utils
.getFullTestURL("ScDDELinksObj.sdc");
132 oDoc
= SOfficeFactory
.getFactory(oMSF
).loadDocument(testdoc
);
133 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
134 e
.printStackTrace(log
);
135 throw new StatusException("Can't load test document", e
);
136 } catch (com
.sun
.star
.io
.IOException e
) {
137 e
.printStackTrace(log
);
138 throw new StatusException("Can't load test document", e
);
139 } catch (com
.sun
.star
.uno
.Exception e
) {
140 e
.printStackTrace(log
);
141 throw new StatusException("Can't load test document", e
);
144 log
.println("getting sheets");
145 XSpreadsheets xSpreadsheets
= (XSpreadsheets
)xSheetDoc
.getSheets();
147 log
.println("getting a sheet");
148 XSpreadsheet oSheet
= null;
149 XIndexAccess oIndexAccess
= (XIndexAccess
)
150 UnoRuntime
.queryInterface(XIndexAccess
.class, xSpreadsheets
);
152 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
153 new Type(XSpreadsheet
.class),oIndexAccess
.getByIndex(0));
154 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
155 e
.printStackTrace(log
);
156 throw new StatusException("Couldn't get a spreadsheet", e
);
157 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
158 e
.printStackTrace(log
);
159 throw new StatusException("Couldn't get a spreadsheet", e
);
160 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
161 e
.printStackTrace(log
);
162 throw new StatusException("Couldn't get a spreadsheet", e
);
165 testdoc
= utils
.getFullTestDocName("ScDDELinksObj.sdc");
166 log
.println("filling some cells");
168 oSheet
.getCellByPosition(5, 5).setFormula(
169 "=DDE(\"soffice\";\""+testdoc
+"\";\"Sheet1.A1\"");
170 oSheet
.getCellByPosition(1, 4).setFormula(
171 "=DDE(\"soffice\";\""+testdoc
+"\";\"Sheet1.A1\"");
172 oSheet
.getCellByPosition(2, 0).setFormula(
173 "=DDE(\"soffice\";\""+testdoc
+"\";\"Sheet1.A1\"");
174 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
175 e
.printStackTrace(log
);
176 throw new StatusException(
177 "Exception occurred while filling cells", e
);
181 log
.println("Getting test object ") ;
183 // Getting named ranges.
184 XPropertySet docProps
= (XPropertySet
)
185 UnoRuntime
.queryInterface(XPropertySet
.class, xSheetDoc
);
186 oObj
= (XInterface
)AnyConverter
.toObject(
187 new Type(XInterface
.class),docProps
.getPropertyValue("DDELinks"));
188 log
.println("Creating object - " +
189 ((oObj
== null) ?
"FAILED" : "OK"));
190 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
191 e
.printStackTrace(log
) ;
192 throw new StatusException(
193 "Error getting test object from spreadsheet document", e
) ;
194 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
195 e
.printStackTrace(log
) ;
196 throw new StatusException(
197 "Error getting test object from spreadsheet document", e
) ;
198 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
199 e
.printStackTrace(log
) ;
200 throw new StatusException(
201 "Error getting test object from spreadsheet document", e
) ;
204 TestEnvironment tEnv
= new TestEnvironment( oObj
);