bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScDatabaseRangeObj.java
blob69e113b18509af36877fa8d50823fcf3c89e477e
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.lang.XMultiServiceFactory;
26 import com.sun.star.sheet.XCellRangeAddressable;
27 import com.sun.star.sheet.XCellRangeReferrer;
28 import com.sun.star.sheet.XDatabaseRanges;
29 import com.sun.star.sheet.XSpreadsheetDocument;
30 import com.sun.star.sheet.XSpreadsheets;
31 import com.sun.star.table.CellRangeAddress;
32 import com.sun.star.table.XCellRange;
33 import com.sun.star.uno.AnyConverter;
34 import com.sun.star.uno.Type;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 import com.sun.star.util.XImportable;
39 import java.io.PrintWriter;
41 import lib.StatusException;
42 import lib.TestCase;
43 import lib.TestEnvironment;
44 import lib.TestParameters;
46 import util.SOfficeFactory;
49 /**
50 * Test for object which is represented by service
51 * <code>com.sun.star.sheet.DatabaseRange</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::sheet::DatabaseRange</code></li>
56 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
57 * <li> <code>com::sun::star::sheet::XDatabaseRange</code></li>
58 * <li> <code>com::sun::star::sheet::XCellRangeReferrer</code></li>
59 * </ul>
60 * @see com.sun.star.sheet.DatabaseRange
61 * @see com.sun.star.container.XNamed
62 * @see com.sun.star.sheet.DatabaseRange
63 * @see com.sun.star.beans.XPropertySet
64 * @see com.sun.star.sheet.XDatabaseRange
65 * @see com.sun.star.sheet.XCellRangeReferrer
66 * @see ifc.container._XNamed
67 * @see ifc.sheet._DatabaseRange
68 * @see ifc.beans._XPropertySet
69 * @see ifc.sheet._XDatabaseRange
70 * @see ifc.sheet._XCellRangeReferrer
72 public class ScDatabaseRangeObj extends TestCase {
73 private XSpreadsheetDocument xSheetDoc = null;
75 /**
76 * Creates Spreadsheet document.
78 protected void initialize(TestParameters tParam, PrintWriter log) {
79 SOfficeFactory SOF = SOfficeFactory.getFactory(
80 (XMultiServiceFactory) 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 protected void cleanup(TestParameters tParam, PrintWriter log) {
96 log.println(" disposing xSheetDoc ");
98 XComponent oComp = UnoRuntime.queryInterface(
99 XComponent.class, xSheetDoc);
100 util.DesktopTools.closeDoc(oComp);
104 * Creating a Testenvironment for the interfaces to be tested.
105 * Retrieves the collection of database ranges in the document.
106 * If the database range with name <code>'dbRange'</code> exists
107 * in the collection then removes it from the collection.
108 * Creates new database range and adds it to the collection with the name
109 * <code>'dbRange'</code>.The database range that was added to the collection
110 * is the instance of the service <code>com.sun.star.sheet.DatabaseRange</code>.
111 * Object relations created :
112 * <ul>
113 * <li> <code>'DATAAREA'</code> for
114 * {@link ifc.sheet._XCellRangeReferrer}(of type
115 * <code>CellRangeAddress</code>)</li>
116 * <li> <code>'XCELLRANGE'</code> (of type <code>XCellRange</code>):
117 * cell range of the spreadsheet with database range</li>
118 * </ul>
119 * @see com.sun.star.sheet.DatabaseRange
120 * @see com.sun.star.table.CellRangeAddress
122 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
123 PrintWriter log) {
124 XInterface oObj = null;
127 // creation of testobject here
128 // first we write what we are intend to do to log file
129 log.println("Creating a test environment");
131 log.println("Getting test object ");
133 XPropertySet docProps = UnoRuntime.queryInterface(
134 XPropertySet.class, xSheetDoc);
136 XSpreadsheets sheets = xSheetDoc.getSheets();
137 String[] names = sheets.getElementNames();
138 XDatabaseRanges dbRanges = null;
139 XImportable xImp = null;
141 try {
142 Object sheet = sheets.getByName(names[0]);
143 xImp = UnoRuntime.queryInterface(XImportable.class,
144 sheet);
145 dbRanges = (XDatabaseRanges) AnyConverter.toObject(
146 new Type(XDatabaseRanges.class),
147 docProps.getPropertyValue("DatabaseRanges"));
148 _doImport(xImp);
149 } catch (com.sun.star.lang.WrappedTargetException e) {
150 e.printStackTrace(log);
151 throw new StatusException("Couldn't get a property", e);
152 } catch (com.sun.star.beans.UnknownPropertyException e) {
153 e.printStackTrace(log);
154 throw new StatusException("Couldn't get a property", e);
155 } catch (com.sun.star.lang.IllegalArgumentException e) {
156 e.printStackTrace(log);
157 throw new StatusException("Couldn't get a property", e);
158 } catch (com.sun.star.container.NoSuchElementException e) {
159 e.printStackTrace(log);
160 throw new StatusException(
161 "Error getting test object from spreadsheet document", e);
164 String dbName = "dbRange";
166 if (dbRanges.hasByName(dbName)) {
167 dbRanges.removeByName(dbName);
171 //CellRangeAddress aRange = new CellRangeAddress((short)0, 0, 0, 0, 13);
172 CellRangeAddress aRange = null;
174 //dbRanges.addNewByName("dbRange", aRange);
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");