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