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 
24 package mod._sc;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 import util.utils;
34 
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.container.XIndexAccess;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.sheet.XSpreadsheet;
40 import com.sun.star.sheet.XSpreadsheetDocument;
41 import com.sun.star.sheet.XSpreadsheets;
42 import com.sun.star.uno.AnyConverter;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
46 
47 /**
48 * Test for object which is represented by service
49 * <code>com.sun.star.sheet.DDELinks</code>. <p>
50 * Object implements the following interfaces :
51 * <ul>
52 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
53 *  <li> <code>com::sun::star::container::XElementAccess</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.DDELinks
60 * @see com.sun.star.container.XNameAccess
61 * @see com.sun.star.container.XElementAccess
62 * @see ifc.container._XNameAccess
63 * @see ifc.container._XElementAccess
64 */
65 public class ScDDELinksObj extends TestCase {
66     static XSpreadsheetDocument xSheetDoc = null;
67     static XComponent oDoc = null;
68 
69     /**
70     * Creates Spreadsheet document.
71     */
initialize( TestParameters tParam, PrintWriter log )72     protected void initialize( TestParameters tParam, PrintWriter log ) {
73         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
74 
75         try {
76             log.println( "creating a Spreadsheet document" );
77             xSheetDoc = SOF.createCalcDoc(null);
78         } catch ( com.sun.star.uno.Exception e ) {
79             // Some exception occures.FAILED
80             e.printStackTrace( log );
81             throw new StatusException( "Couldn't create document", e );
82         }
83 
84     }
85 
86     /**
87     * Disposes Spreadsheet document and testdocument.
88     */
cleanup( TestParameters tParam, PrintWriter log )89     protected void cleanup( TestParameters tParam, PrintWriter log ) {
90         log.println( "    disposing xSheetDoc " );
91         XComponent oComp = (XComponent) UnoRuntime.
92                                 queryInterface(XComponent.class, xSheetDoc) ;
93         util.DesktopTools.closeDoc(oComp);
94         util.DesktopTools.closeDoc(oDoc);
95     }
96 
97     /**
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.
105     * The retrived collection of DDE link is the instance of the service
106     * <code>com.sun.star.sheet.DDELinks</code>.
107     * @see com.sun.star.frame.Desktop
108     * @see com.sun.star.sheet.DDELinks
109     */
createTestEnvironment(TestParameters Param, PrintWriter log)110     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
111 
112         XInterface oObj = null;
113 
114         // creation of testobject here
115         // first we write what we are intend to do to log file
116         log.println( "Creating a test environment" );
117 
118         // create testobject here
119 
120         XMultiServiceFactory oMSF = (XMultiServiceFactory)Param.getMSF();
121 
122         // load the predefined testdocument
123         String testdoc = utils.getFullTestURL("ScDDELinksObj.sdc");
124         try {
125             oDoc = SOfficeFactory.getFactory(oMSF).loadDocument(testdoc);
126         } catch (com.sun.star.lang.IllegalArgumentException e) {
127             e.printStackTrace(log);
128             throw new StatusException("Can't load test document", e);
129         } catch (com.sun.star.io.IOException e) {
130             e.printStackTrace(log);
131             throw new StatusException("Can't load test document", e);
132         } catch (com.sun.star.uno.Exception e) {
133             e.printStackTrace(log);
134             throw new StatusException("Can't load test document", e);
135         }
136 
137         log.println("getting sheets");
138         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
139 
140         log.println("getting a sheet");
141         XSpreadsheet oSheet = null;
142         XIndexAccess oIndexAccess = (XIndexAccess)
143             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
144         try {
145             oSheet = (XSpreadsheet) AnyConverter.toObject(
146                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
147         } catch (com.sun.star.lang.WrappedTargetException e) {
148             e.printStackTrace(log);
149             throw new StatusException("Couldn't get a spreadsheet", e);
150         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
151             e.printStackTrace(log);
152             throw new StatusException("Couldn't get a spreadsheet", e);
153         } catch (com.sun.star.lang.IllegalArgumentException e) {
154             e.printStackTrace(log);
155             throw new StatusException("Couldn't get a spreadsheet", e);
156         }
157 
158         testdoc = utils.getFullTestDocName("ScDDELinksObj.sdc");
159         log.println("filling some cells");
160         try {
161             oSheet.getCellByPosition(5, 5).setFormula(
162                 "=DDE(\"soffice\";\""+testdoc+"\";\"Sheet1.A1\"");
163             oSheet.getCellByPosition(1, 4).setFormula(
164                 "=DDE(\"soffice\";\""+testdoc+"\";\"Sheet1.A1\"");
165             oSheet.getCellByPosition(2, 0).setFormula(
166                 "=DDE(\"soffice\";\""+testdoc+"\";\"Sheet1.A1\"");
167         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
168             e.printStackTrace(log);
169             throw new StatusException(
170                 "Exception occurred while filling cells", e);
171         }
172 
173         try {
174             log.println("Getting test object ") ;
175 
176             // Getting named ranges.
177             XPropertySet docProps = (XPropertySet)
178                 UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
179             oObj = (XInterface)AnyConverter.toObject(
180                 new Type(XInterface.class),docProps.getPropertyValue("DDELinks"));
181             log.println("Creating object - " +
182                                         ((oObj == null) ? "FAILED" : "OK"));
183         } catch (com.sun.star.lang.WrappedTargetException e) {
184             e.printStackTrace(log) ;
185             throw new StatusException(
186                 "Error getting test object from spreadsheet document", e) ;
187         } catch (com.sun.star.beans.UnknownPropertyException e) {
188             e.printStackTrace(log) ;
189             throw new StatusException(
190                 "Error getting test object from spreadsheet document", e) ;
191         } catch (com.sun.star.lang.IllegalArgumentException e) {
192             e.printStackTrace(log) ;
193             throw new StatusException(
194                 "Error getting test object from spreadsheet document", e) ;
195         }
196 
197         TestEnvironment tEnv = new TestEnvironment( oObj );
198 
199         return tEnv;
200     }
201 
202 }
203 
204 
205