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