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