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