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 
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XSheetLinkable;
39 import com.sun.star.sheet.XSpreadsheet;
40 import com.sun.star.sheet.XSpreadsheetDocument;
41 import com.sun.star.sheet.XSpreadsheets;
42 import com.sun.star.uno.AnyConverter;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
46 
47 /**
48 * Test for object which is represented by service
49 * <code>com.sun.star.sheet.SheetLinks</code>. <p>
50 * Object implements the following interfaces :
51 * <ul>
52 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
53 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
54 * </ul>
55 * @see com.sun.star.sheet.SheetLinks
56 * @see com.sun.star.container.XNameAccess
57 * @see com.sun.star.container.XElementAccess
58 * @see ifc.container._XNameAccess
59 * @see ifc.container._XElementAccess
60 */
61 public class ScSheetLinksObj extends TestCase {
62     static XSpreadsheetDocument xSheetDoc = null;
63 
64     /**
65     * Creates Spreadsheet document.
66     */
67     protected void initialize( TestParameters tParam, PrintWriter log ) {
68         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
69 
70         try {
71             log.println( "creating a Spreadsheet document" );
72             xSheetDoc = SOF.createCalcDoc(null);
73         } catch ( com.sun.star.uno.Exception e ) {
74             // Some exception occures.FAILED
75             e.printStackTrace( log );
76             throw new StatusException( "Couldn't create document", e );
77         }
78     }
79 
80     /**
81     * Disposes Spreadsheet document.
82     */
83     protected void cleanup( TestParameters tParam, PrintWriter log ) {
84         log.println( "    disposing xSheetDoc " );
85         XComponent oComp = (XComponent)
86             UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ;
87         util.DesktopTools.closeDoc(oComp);
88     }
89 
90     /**
91     * Creating a Testenvironment for the interfaces to be tested.
92     * Retrieves a collection of spreadsheets from a document
93     * and takes one of them. Links the sheet to another sheet using the
94     * interface <code>XSheetLinkable</code>. Obtains the
95     * value of the property <code>'SheetLinks'</code> that is the collection of
96     * sheet links. This collection is the instance of the service
97     *  <code>com.sun.star.sheet.SheetLinks</code>.
98     * @see com.sun.star.sheet.SheetLink
99     * @see com.sun.star.sheet.XSheetLinkable
100     */
101     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
102 
103         XInterface oObj = null;
104 
105         // creation of testobject here
106         // first we write what we are intend to do to log file
107         log.println( "Creating a test environment" );
108         XSpreadsheet oSheet = null;
109 
110         log.println("Getting test object ") ;
111         XSpreadsheets oSheets = xSheetDoc.getSheets() ;
112         XIndexAccess oIndexSheets = (XIndexAccess)
113             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
114         try {
115             oSheet = (XSpreadsheet) AnyConverter.toObject(
116                     new Type(XSpreadsheet.class),oIndexSheets.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         XSheetLinkable SL = (XSheetLinkable)
129             UnoRuntime.queryInterface(XSheetLinkable.class, oSheet);
130 
131         // creating link. Doesn't matter that it refers to unexistant object.
132         // this is for proper work of X*Access tests.
133         String aSourceArea = util.utils.getFullTestURL("calcshapes.sxc");
134         SL.link(aSourceArea, "Sheet1", "", "",
135             com.sun.star.sheet.SheetLinkMode.VALUE);
136 
137         // Getting links.
138         XPropertySet docProps = (XPropertySet)
139             UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
140 
141         Object links = null;
142         try {
143             links = docProps.getPropertyValue("SheetLinks");
144         } catch(com.sun.star.lang.WrappedTargetException e){
145             e.printStackTrace(log);
146             throw new StatusException("Couldn't get SheetLinks", e);
147         } catch(com.sun.star.beans.UnknownPropertyException e){
148             e.printStackTrace(log);
149             throw new StatusException("Couldn't get SheetLinks", e);
150         }
151 
152         oObj = (XInterface)UnoRuntime.queryInterface(XInterface.class, links);
153 
154         log.println("Creating object - " +
155                                     ((oObj == null) ? "FAILED" : "OK"));
156 
157         TestEnvironment tEnv = new TestEnvironment( oObj );
158 
159         return tEnv;
160     }
161 
162 }
163 
164