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.container.XNameAccess;
42 import com.sun.star.lang.XComponent;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.sheet.XSheetLinkable;
45 import com.sun.star.sheet.XSpreadsheet;
46 import com.sun.star.sheet.XSpreadsheetDocument;
47 import com.sun.star.sheet.XSpreadsheets;
48 import com.sun.star.uno.AnyConverter;
49 import com.sun.star.uno.Type;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XInterface;
52 
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.sheet.SheetLink</code>. <p>
56 * Object implements the following interfaces :
57 * <ul>
58 *  <li> <code>com::sun::star::container::XNamed</code></li>
59 *  <li> <code>com::sun::star::util::XRefreshable</code></li>
60 *  <li> <code>com::sun::star::sheet::SheetLink</code></li>
61 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
62 * </ul>
63 * The following files used by this test :
64 * <ul>
65 *  <li><b> ScSheetLinksObj.sdc </b> : for creating link </li>
66 * </ul> <p>
67 * @see com.sun.star.sheet.SheetLink
68 * @see com.sun.star.container.XNamed
69 * @see com.sun.star.util.XRefreshable
70 * @see com.sun.star.sheet.SheetLink
71 * @see com.sun.star.beans.XPropertySet
72 * @see ifc.container._XNamed
73 * @see ifc.util._XRefreshable
74 * @see ifc.sheet._SheetLink
75 * @see ifc.beans._XPropertySet
76 */
77 public class ScSheetLinkObj extends TestCase {
78     static XSpreadsheetDocument xSheetDoc = null;
79 
80     /**
81     * Creates Spreadsheet document.
82     */
83     protected void initialize( TestParameters tParam, PrintWriter log ) {
84         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
85 
86         try {
87             log.println( "creating a Spreadsheet document" );
88             xSheetDoc = SOF.createCalcDoc(null);
89         } catch ( com.sun.star.uno.Exception e ) {
90             // Some exception occures.FAILED
91             e.printStackTrace( log );
92             throw new StatusException( "Couldn't create document", e );
93         }
94     }
95 
96     /**
97     * Disposes Spreadsheet document.
98     */
99     protected void cleanup( TestParameters tParam, PrintWriter log ) {
100         log.println( "    disposing xSheetDoc " );
101         XComponent oComp = (XComponent)
102             UnoRuntime.queryInterface(XComponent.class, xSheetDoc);
103         util.DesktopTools.closeDoc(oComp);
104     }
105 
106     /**
107     * Creating a Testenvironment for the interfaces to be tested.
108     * Retrieves a collection of spreadsheets from a document
109     * and takes one of them. Links the sheet to another sheet in another
110     * document using the interface <code>XSheetLinkable</code>. Obtains the
111     * value of the property <code>'SheetLinks'</code> that is the collection of
112     * sheet links. Retrieves from the collection one of the sheet link that
113     * is the instance of the service <code>com.sun.star.sheet.SheetLink</code>.
114     * @see com.sun.star.sheet.SheetLink
115     * @see com.sun.star.sheet.XSheetLinkable
116     */
117     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
118 
119         XInterface oObj = null;
120 
121         // creation of testobject here
122         // first we write what we are intend to do to log file
123         log.println( "Creating a test environment" );
124         XSpreadsheet oSheet = null;
125 
126         log.println("Getting test object ") ;
127         XSpreadsheets oSheets = xSheetDoc.getSheets() ;
128         XIndexAccess oIndexSheets = (XIndexAccess)
129             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
130         try {
131             oSheet = (XSpreadsheet) AnyConverter.toObject(
132                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
133         } catch (com.sun.star.lang.WrappedTargetException e) {
134             e.printStackTrace(log);
135             throw new StatusException("Couldn't get a spreadsheet", e);
136         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
137             e.printStackTrace(log);
138             throw new StatusException("Couldn't get a spreadsheet", e);
139         } catch (com.sun.star.lang.IllegalArgumentException e) {
140             e.printStackTrace(log);
141             throw new StatusException("Couldn't get a spreadsheet", e);
142         }
143 
144         XSheetLinkable SL = (XSheetLinkable)
145             UnoRuntime.queryInterface(XSheetLinkable.class, oSheet);
146 
147         // creating link. Doesn't matter that it refers to unexistant object.
148         // this is for proper work of XAccess tests.
149         String sURL = utils.getFullTestDocName("ScSheetLinksObj.sdc");
150         SL.link(sURL, "Sheet1", "", "", com.sun.star.sheet.SheetLinkMode.VALUE);
151 
152         // Getting links.
153         XPropertySet docProps = (XPropertySet)
154             UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
155         Object oLinks = null;
156         try {
157             oLinks = docProps.getPropertyValue("SheetLinks");
158         } catch(com.sun.star.lang.WrappedTargetException e){
159             e.printStackTrace(log);
160             throw new StatusException("Couldn't get SheetLinks", e);
161         } catch(com.sun.star.beans.UnknownPropertyException e){
162             e.printStackTrace(log);
163             throw new StatusException("Couldn't get SheetLinks", e);
164         }
165 
166         XNameAccess links = (XNameAccess)
167             UnoRuntime.queryInterface(XNameAccess.class, oLinks);
168 
169         String[] names = links.getElementNames();
170 
171         try {
172             oObj = (XInterface)AnyConverter.toObject(
173                         new Type(XInterface.class),links.getByName(names[0]));
174         } catch(com.sun.star.lang.WrappedTargetException e){
175             e.printStackTrace(log);
176             throw new StatusException("Couldn't get by name", e);
177         } catch(com.sun.star.container.NoSuchElementException e){
178             e.printStackTrace(log);
179             throw new StatusException("Couldn't get by name", e);
180         } catch(com.sun.star.lang.IllegalArgumentException e){
181             e.printStackTrace(log);
182             throw new StatusException("Couldn't get by name", e);
183         }
184 
185         log.println("Creating object - " +
186             ((oObj == null) ? "FAILED" : "OK"));
187 
188         TestEnvironment tEnv = new TestEnvironment( oObj );
189 
190         return tEnv;
191     }
192 
193 }
194