bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDatabaseRangeObj.java
blob5f953ad771291c6b83c91bb5a5f451a783093418
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 .
18 package mod._sc;
20 import com.sun.star.beans.PropertyValue;
21 import com.sun.star.beans.XPropertySet;
22 import com.sun.star.container.XNameAccess;
23 import com.sun.star.container.XNamed;
24 import com.sun.star.lang.XComponent;
25 import com.sun.star.sheet.XCellRangeAddressable;
26 import com.sun.star.sheet.XCellRangeReferrer;
27 import com.sun.star.sheet.XDatabaseRanges;
28 import com.sun.star.sheet.XSpreadsheetDocument;
29 import com.sun.star.sheet.XSpreadsheets;
30 import com.sun.star.table.CellRangeAddress;
31 import com.sun.star.table.XCellRange;
32 import com.sun.star.uno.AnyConverter;
33 import com.sun.star.uno.Type;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.XInterface;
36 import com.sun.star.util.XImportable;
38 import java.io.PrintWriter;
40 import lib.StatusException;
41 import lib.TestCase;
42 import lib.TestEnvironment;
43 import lib.TestParameters;
45 import util.SOfficeFactory;
48 /**
49 * Test for object which is represented by service
50 * <code>com.sun.star.sheet.DatabaseRange</code>. <p>
51 * Object implements the following interfaces :
52 * <ul>
53 * <li> <code>com::sun::star::container::XNamed</code></li>
54 * <li> <code>com::sun::star::sheet::DatabaseRange</code></li>
55 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
56 * <li> <code>com::sun::star::sheet::XDatabaseRange</code></li>
57 * <li> <code>com::sun::star::sheet::XCellRangeReferrer</code></li>
58 * </ul>
59 * @see com.sun.star.sheet.DatabaseRange
60 * @see com.sun.star.container.XNamed
61 * @see com.sun.star.sheet.DatabaseRange
62 * @see com.sun.star.beans.XPropertySet
63 * @see com.sun.star.sheet.XDatabaseRange
64 * @see com.sun.star.sheet.XCellRangeReferrer
65 * @see ifc.container._XNamed
66 * @see ifc.sheet._DatabaseRange
67 * @see ifc.beans._XPropertySet
68 * @see ifc.sheet._XDatabaseRange
69 * @see ifc.sheet._XCellRangeReferrer
71 public class ScDatabaseRangeObj extends TestCase {
72 private XSpreadsheetDocument xSheetDoc = null;
74 /**
75 * Creates Spreadsheet document.
77 @Override
78 protected void initialize(TestParameters tParam, PrintWriter log) {
79 SOfficeFactory SOF = SOfficeFactory.getFactory(
80 tParam.getMSF());
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 occurs.FAILED
87 e.printStackTrace(log);
88 throw new StatusException("Couldn't create document", e);
92 /**
93 * Disposes Spreadsheet document.
95 @Override
96 protected void cleanup(TestParameters tParam, PrintWriter log) {
97 log.println(" disposing xSheetDoc ");
99 XComponent oComp = UnoRuntime.queryInterface(
100 XComponent.class, xSheetDoc);
101 util.DesktopTools.closeDoc(oComp);
105 * Creating a Testenvironment for the interfaces to be tested.
106 * Retrieves the collection of database ranges in the document.
107 * If the database range with name <code>'dbRange'</code> exists
108 * in the collection then removes it from the collection.
109 * Creates new database range and adds it to the collection with the name
110 * <code>'dbRange'</code>.The database range that was added to the collection
111 * is the instance of the service <code>com.sun.star.sheet.DatabaseRange</code>.
112 * Object relations created :
113 * <ul>
114 * <li> <code>'DATAAREA'</code> for
115 * {@link ifc.sheet._XCellRangeReferrer}(of type
116 * <code>CellRangeAddress</code>)</li>
117 * <li> <code>'XCELLRANGE'</code> (of type <code>XCellRange</code>):
118 * cell range of the spreadsheet with database range</li>
119 * </ul>
120 * @see com.sun.star.sheet.DatabaseRange
121 * @see com.sun.star.table.CellRangeAddress
123 @Override
124 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
125 PrintWriter log) {
126 XInterface oObj = null;
129 // creation of testobject here
130 // first we write what we are intend to do to log file
131 log.println("Creating a test environment");
133 log.println("Getting test object ");
135 XPropertySet docProps = UnoRuntime.queryInterface(
136 XPropertySet.class, xSheetDoc);
138 XSpreadsheets sheets = xSheetDoc.getSheets();
139 String[] names = sheets.getElementNames();
140 XDatabaseRanges dbRanges = null;
141 XImportable xImp = null;
143 try {
144 Object sheet = sheets.getByName(names[0]);
145 xImp = UnoRuntime.queryInterface(XImportable.class,
146 sheet);
147 dbRanges = (XDatabaseRanges) AnyConverter.toObject(
148 new Type(XDatabaseRanges.class),
149 docProps.getPropertyValue("DatabaseRanges"));
150 _doImport(xImp);
151 } catch (com.sun.star.lang.WrappedTargetException e) {
152 e.printStackTrace(log);
153 throw new StatusException("Couldn't get a property", e);
154 } catch (com.sun.star.beans.UnknownPropertyException e) {
155 e.printStackTrace(log);
156 throw new StatusException("Couldn't get a property", e);
157 } catch (com.sun.star.lang.IllegalArgumentException e) {
158 e.printStackTrace(log);
159 throw new StatusException("Couldn't get a property", e);
160 } catch (com.sun.star.container.NoSuchElementException e) {
161 e.printStackTrace(log);
162 throw new StatusException(
163 "Error getting test object from spreadsheet document", e);
166 String dbName = "dbRange";
168 if (dbRanges.hasByName(dbName)) {
169 dbRanges.removeByName(dbName);
173 CellRangeAddress aRange = null;
175 XNameAccess dbrNA = UnoRuntime.queryInterface(
176 XNameAccess.class, dbRanges);
177 try {
178 // we need to add it
179 dbRanges.addNewByName(dbName,new CellRangeAddress((short)0, 0, 0, 0, 5));
181 UnoRuntime.queryInterface(XNamed.class,
182 dbrNA.getByName(
183 dbName));
185 XCellRangeReferrer aReferrer = UnoRuntime.queryInterface(
186 XCellRangeReferrer.class,
187 dbrNA.getByName(dbName));
188 XCellRangeAddressable aRangeA = UnoRuntime.queryInterface(
189 XCellRangeAddressable.class,
190 aReferrer.getReferredCells());
191 aRange = aRangeA.getRangeAddress();
192 oObj = (XInterface) AnyConverter.toObject(
193 new Type(XInterface.class),
194 dbrNA.getByName(dbName));
195 } catch (com.sun.star.lang.WrappedTargetException e) {
196 e.printStackTrace(log);
197 throw new StatusException(
198 "Error getting test object from spreadsheet document", e);
199 } catch (com.sun.star.container.NoSuchElementException e) {
200 e.printStackTrace(log);
201 throw new StatusException(
202 "Error getting test object from spreadsheet document", e);
203 } catch (com.sun.star.lang.IllegalArgumentException e) {
204 e.printStackTrace(log);
205 throw new StatusException(
206 "Error getting test object from spreadsheet document", e);
209 TestEnvironment tEnv = new TestEnvironment(oObj);
212 // Other parameters required for interface tests
213 tEnv.addObjRelation("DATAAREA", aRange);
215 XCellRange xCellRange = null;
217 try {
218 Object sheet = sheets.getByName(names[0]);
219 xCellRange = UnoRuntime.queryInterface(
220 XCellRange.class, sheet);
221 } catch (com.sun.star.lang.WrappedTargetException e) {
222 e.printStackTrace(log);
223 throw new StatusException(
224 "Error getting of first spreadsheet from spreadsheet" +
225 " document", e);
226 } catch (com.sun.star.container.NoSuchElementException e) {
227 e.printStackTrace(log);
228 throw new StatusException(
229 "Error getting of first spreadsheet from spreadsheet" +
230 " document", e);
233 tEnv.addObjRelation("XCELLRANGE", xCellRange);
235 return tEnv;
238 public void _doImport(XImportable imp) {
239 PropertyValue[] descriptor = imp.createImportDescriptor(false);
241 log.print("Setting the ImportDescriptor (Bibliograpy, SQL, select Identifier from biblio) -- ");
242 descriptor[0].Value = "Bibliography";
243 descriptor[1].Value = com.sun.star.sheet.DataImportMode.SQL;
244 descriptor[2].Value = "select Identifier from biblio";
245 log.println("done");
247 log.print("Importing data (Bibliograpy, Table, biblio) -- ");
248 imp.doImport(descriptor);
249 log.println("done");