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