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