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