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