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