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