bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScSheetLinkObj.java
blob104a72bf17e44346e38c87d75da4022eca24ef99
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod._sc;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
28 import util.utils;
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.container.XIndexAccess;
32 import com.sun.star.container.XNameAccess;
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.sheet.XSheetLinkable;
35 import com.sun.star.sheet.XSpreadsheet;
36 import com.sun.star.sheet.XSpreadsheetDocument;
37 import com.sun.star.sheet.XSpreadsheets;
38 import com.sun.star.uno.AnyConverter;
39 import com.sun.star.uno.Type;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
43 /**
44 * Test for object which is represented by service
45 * <code>com.sun.star.sheet.SheetLink</code>. <p>
46 * Object implements the following interfaces :
47 * <ul>
48 * <li> <code>com::sun::star::container::XNamed</code></li>
49 * <li> <code>com::sun::star::util::XRefreshable</code></li>
50 * <li> <code>com::sun::star::sheet::SheetLink</code></li>
51 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
52 * </ul>
53 * The following files used by this test :
54 * <ul>
55 * <li><b> ScSheetLinksObj.sdc </b> : for creating link </li>
56 * </ul> <p>
57 * @see com.sun.star.sheet.SheetLink
58 * @see com.sun.star.container.XNamed
59 * @see com.sun.star.util.XRefreshable
60 * @see com.sun.star.sheet.SheetLink
61 * @see com.sun.star.beans.XPropertySet
62 * @see ifc.container._XNamed
63 * @see ifc.util._XRefreshable
64 * @see ifc.sheet._SheetLink
65 * @see ifc.beans._XPropertySet
67 public class ScSheetLinkObj extends TestCase {
68 private XSpreadsheetDocument xSheetDoc = null;
70 /**
71 * Creates Spreadsheet document.
73 @Override
74 protected void initialize( TestParameters tParam, PrintWriter log ) {
75 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
77 try {
78 log.println( "creating a Spreadsheet document" );
79 xSheetDoc = SOF.createCalcDoc(null);
80 } catch ( com.sun.star.uno.Exception e ) {
81 // Some exception occurs.FAILED
82 e.printStackTrace( log );
83 throw new StatusException( "Couldn't create document", e );
87 /**
88 * Disposes Spreadsheet document.
90 @Override
91 protected void cleanup( TestParameters tParam, PrintWriter log ) {
92 log.println( " disposing xSheetDoc " );
93 XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSheetDoc);
94 util.DesktopTools.closeDoc(oComp);
97 /**
98 * Creating a Testenvironment for the interfaces to be tested.
99 * Retrieves a collection of spreadsheets from a document
100 * and takes one of them. Links the sheet to another sheet in another
101 * document using the interface <code>XSheetLinkable</code>. Obtains the
102 * value of the property <code>'SheetLinks'</code> that is the collection of
103 * sheet links. Retrieves from the collection one of the sheet link that
104 * is the instance of the service <code>com.sun.star.sheet.SheetLink</code>.
105 * @see com.sun.star.sheet.SheetLink
106 * @see com.sun.star.sheet.XSheetLinkable
108 @Override
109 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
111 XInterface oObj = null;
113 // creation of testobject here
114 // first we write what we are intend to do to log file
115 log.println( "Creating a test environment" );
116 XSpreadsheet oSheet = null;
118 log.println("Getting test object ") ;
119 XSpreadsheets oSheets = xSheetDoc.getSheets() ;
120 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
121 try {
122 oSheet = (XSpreadsheet) AnyConverter.toObject(
123 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
124 } catch (com.sun.star.lang.WrappedTargetException e) {
125 e.printStackTrace(log);
126 throw new StatusException("Couldn't get a spreadsheet", e);
127 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
128 e.printStackTrace(log);
129 throw new StatusException("Couldn't get a spreadsheet", e);
130 } catch (com.sun.star.lang.IllegalArgumentException e) {
131 e.printStackTrace(log);
132 throw new StatusException("Couldn't get a spreadsheet", e);
135 XSheetLinkable SL = UnoRuntime.queryInterface(XSheetLinkable.class, oSheet);
137 // creating link. Doesn't matter that it refers to unexistant object.
138 // this is for proper work of XAccess tests.
139 String sURL = utils.getFullTestDocName("ScSheetLinksObj.ods");
140 SL.link(sURL, "Sheet1", "", "", com.sun.star.sheet.SheetLinkMode.VALUE);
142 // Getting links.
143 XPropertySet docProps = UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
144 Object oLinks = null;
145 try {
146 oLinks = docProps.getPropertyValue("SheetLinks");
147 } catch(com.sun.star.lang.WrappedTargetException e){
148 e.printStackTrace(log);
149 throw new StatusException("Couldn't get SheetLinks", e);
150 } catch(com.sun.star.beans.UnknownPropertyException e){
151 e.printStackTrace(log);
152 throw new StatusException("Couldn't get SheetLinks", e);
155 XNameAccess links = UnoRuntime.queryInterface(XNameAccess.class, oLinks);
157 String[] names = links.getElementNames();
159 try {
160 oObj = (XInterface)AnyConverter.toObject(
161 new Type(XInterface.class),links.getByName(names[0]));
162 } catch(com.sun.star.lang.WrappedTargetException e){
163 e.printStackTrace(log);
164 throw new StatusException("Couldn't get by name", e);
165 } catch(com.sun.star.container.NoSuchElementException e){
166 e.printStackTrace(log);
167 throw new StatusException("Couldn't get by name", e);
168 } catch(com.sun.star.lang.IllegalArgumentException e){
169 e.printStackTrace(log);
170 throw new StatusException("Couldn't get by name", e);
173 log.println("Creating object - " +
174 ((oObj == null) ? "FAILED" : "OK"));
176 TestEnvironment tEnv = new TestEnvironment( oObj );
178 return tEnv;