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.XEnumerationAccess;
37 import com.sun.star.container.XIndexAccess;
38 import com.sun.star.lang.XComponent;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.sheet.XSpreadsheet;
41 import com.sun.star.sheet.XSpreadsheetDocument;
42 import com.sun.star.sheet.XSpreadsheets;
43 import com.sun.star.uno.AnyConverter;
44 import com.sun.star.uno.Type;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 public class ScIndexEnumeration_DDELinksEnumeration extends TestCase {
49     static XSpreadsheetDocument xSheetDoc = null;
50     static XComponent oDoc = null;
51 
52     /**
53     * Creates Spreadsheet document.
54     */
55     protected void initialize( TestParameters tParam, PrintWriter log ) {
56         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
57 
58         try {
59             log.println( "creating a Spreadsheet document" );
60             xSheetDoc = SOF.createCalcDoc(null);
61         } catch ( com.sun.star.uno.Exception e ) {
62             // Some exception occures.FAILED
63             e.printStackTrace( log );
64             throw new StatusException( "Couldn't create document", e );
65         }
66 
67     }
68 
69     /**
70     * Disposes Spreadsheet document and testdocument.
71     */
72     protected void cleanup( TestParameters tParam, PrintWriter log ) {
73         log.println( "    disposing xSheetDoc " );
74         XComponent oComp = (XComponent) UnoRuntime.
75                                 queryInterface(XComponent.class, xSheetDoc) ;
76         util.DesktopTools.closeDoc(oComp);
77         util.DesktopTools.closeDoc(oDoc);
78     }
79 
80     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
81 
82         XInterface oObj = null;
83 
84         // creation of testobject here
85         // first we write what we are intend to do to log file
86         log.println( "Creating a test environment" );
87 
88         // create testobject here
89 
90         XMultiServiceFactory oMSF = (XMultiServiceFactory)Param.getMSF();
91 
92         // load the predefined testdocument
93         String testdoc = utils.getFullTestURL("ScDDELinksObj.sdc");
94         try {
95             oDoc = SOfficeFactory.getFactory(oMSF).loadDocument(testdoc);
96         } catch (com.sun.star.lang.IllegalArgumentException e) {
97             e.printStackTrace(log);
98             throw new StatusException("Can't load test document", e);
99         } catch (com.sun.star.io.IOException e) {
100             e.printStackTrace(log);
101             throw new StatusException("Can't load test document", e);
102         } catch (com.sun.star.uno.Exception e) {
103             e.printStackTrace(log);
104             throw new StatusException("Can't load test document", e);
105         }
106 
107         log.println("getting sheets");
108         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
109 
110         log.println("getting a sheet");
111         XSpreadsheet oSheet = null;
112         XIndexAccess oIndexAccess = (XIndexAccess)
113             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
114         try {
115             oSheet = (XSpreadsheet) AnyConverter.toObject(
116                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
117         } catch (com.sun.star.lang.WrappedTargetException e) {
118             e.printStackTrace(log);
119             throw new StatusException( "Couldn't get a spreadsheet", e);
120         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
121             e.printStackTrace(log);
122             throw new StatusException( "Couldn't get a spreadsheet", e);
123         } catch (com.sun.star.lang.IllegalArgumentException e) {
124             e.printStackTrace(log);
125             throw new StatusException( "Couldn't get a spreadsheet", e);
126         }
127 
128         testdoc = utils.getFullTestDocName("ScDDELinksObj.sdc");
129         log.println("filling some cells");
130         try {
131             oSheet.getCellByPosition(5, 5).setFormula(
132                 "=DDE(\"soffice\";\""+testdoc+"\";\"Sheet1.A1\"");
133             oSheet.getCellByPosition(1, 4).setFormula(
134                 "=DDE(\"soffice\";\""+testdoc+"\";\"Sheet1.A1\"");
135             oSheet.getCellByPosition(2, 0).setFormula(
136                 "=DDE(\"soffice\";\""+testdoc+"\";\"Sheet1.A1\"");
137         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
138             e.printStackTrace(log);
139             throw new StatusException(
140                 "Exception occurred while filling cells", e);
141         }
142 
143         try {
144             log.println("Getting test object ") ;
145 
146             // Getting named ranges.
147             XPropertySet docProps = (XPropertySet)
148                 UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
149             oObj = (XInterface)AnyConverter.toObject(
150                 new Type(XInterface.class),docProps.getPropertyValue("DDELinks"));
151             log.println("Creating object - " +
152                                         ((oObj == null) ? "FAILED" : "OK"));
153         } catch (com.sun.star.lang.WrappedTargetException e) {
154             e.printStackTrace(log) ;
155             throw new StatusException(
156                 "Error getting test object from spreadsheet document", e) ;
157         } catch (com.sun.star.beans.UnknownPropertyException e) {
158             e.printStackTrace(log) ;
159             throw new StatusException(
160                 "Error getting test object from spreadsheet document", e) ;
161         } catch (com.sun.star.lang.IllegalArgumentException e) {
162             e.printStackTrace(log) ;
163             throw new StatusException(
164                 "Error getting test object from spreadsheet document", e) ;
165         }
166 
167         XEnumerationAccess ea = (XEnumerationAccess)
168                     UnoRuntime.queryInterface(XEnumerationAccess.class,oObj);
169 
170         oObj = ea.createEnumeration();
171 
172         log.println("ImplementationName: "+util.utils.getImplName(oObj));
173         // creating test environment
174         TestEnvironment tEnv = new TestEnvironment( oObj );
175 
176         tEnv.addObjRelation("ENUM",ea);
177 
178         return tEnv;
179     }
180 
181 }
182 
183 
184