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 
28 package mod._sc;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.container.XIndexAccess;
40 import com.sun.star.container.XNameContainer;
41 import com.sun.star.lang.XComponent;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.sheet.XSpreadsheets;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.Type;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 import ifc.sheet._XCellRangesQuery;
51 
52 
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.sheet.SheetCellRanges</code>. <p>
56 * Object implements the following interfaces :
57 * <ul>
58 *  <li> <code>com::sun::star::table::CellProperties</code></li>
59 *  <li> <code>com::sun::star::container::XNameReplace</code></li>
60 *  <li> <code>com::sun::star::container::XNameContainer</code></li>
61 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
62 *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
63 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
64 *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
65 *  <li> <code>com::sun::star::sheet::XSheetCellRangeContainer</code></li>
66 *  <li> <code>com::sun::star::sheet::XSheetOperation</code></li>
67 *  <li> <code>com::sun::star::sheet::XSheetCellRanges</code></li>
68 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
69 * </ul>
70 * @see com.sun.star.sheet.SheetCellRanges
71 * @see com.sun.star.table.CellProperties
72 * @see com.sun.star.container.XNameReplace
73 * @see com.sun.star.container.XNameContainer
74 * @see com.sun.star.beans.XPropertySet
75 * @see com.sun.star.container.XIndexAccess
76 * @see com.sun.star.container.XElementAccess
77 * @see com.sun.star.container.XEnumerationAccess
78 * @see com.sun.star.sheet.XSheetCellRangeContainer
79 * @see com.sun.star.sheet.XSheetOperation
80 * @see com.sun.star.sheet.XSheetCellRanges
81 * @see com.sun.star.container.XNameAccess
82 * @see ifc.table._CellProperties
83 * @see ifc.container._XNameReplace
84 * @see ifc.container._XNameContainer
85 * @see ifc.beans._XPropertySet
86 * @see ifc.container._XIndexAccess
87 * @see ifc.container._XElementAccess
88 * @see ifc.container._XEnumerationAccess
89 * @see ifc.sheet._XSheetCellRangeContainer
90 * @see ifc.sheet._XSheetOperation
91 * @see ifc.sheet._XSheetCellRanges
92 * @see ifc.container._XNameAccess
93 */
94 public class ScCellRangesObj extends TestCase {
95     static XSpreadsheetDocument xSheetDoc = null;
96 
97     /**
98     * Creates Spreadsheet document.
99     */
100     protected void initialize( TestParameters tParam, PrintWriter log ) {
101 
102         // get a soffice factory object
103         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
104 
105         try {
106             log.println( "creating a sheetdocument" );
107             xSheetDoc = SOF.createCalcDoc(null);;
108         } catch (com.sun.star.uno.Exception e) {
109             // Some exception occures.FAILED
110             e.printStackTrace( log );
111             throw new StatusException( "Couldn't create document", e );
112         }
113     }
114 
115     /**
116      * Disposes Spreadsheet document.
117      */
118     protected void cleanup( TestParameters tParam, PrintWriter log ) {
119         log.println( "    disposing xSheetDoc " );
120         XComponent oComp =
121             (XComponent) UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
122         util.DesktopTools.closeDoc(oComp);
123     }
124 
125     /**
126     * Creating a Testenvironment for the interfaces to be tested.
127     * Creates an instance of the service
128     * <code>com.sun.star.sheet.SheetCellRanges</code> and fills some cells.
129     * Object relations created :
130     * <ul>
131     *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> for
132     *      {@link ifc.container._XNameReplace},
133     *      {@link ifc.container._XNameContainer} (type of
134     *      <code>XCellRange</code>)</li>
135     *  <li> <code>'THRCNT'</code> for
136     *      {@link ifc.container._XNameReplace}(the number of the running threads
137     *      that was retrieved from the method parameter <code>Param</code>)</li>
138     *  <li> <code>'NameReplaceIndex'</code> for
139     *      {@link ifc.container._XNameReplace} </li>
140     * </ul>
141     * @see com.sun.star.table.XCellRange
142     */
143     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
144 
145         XInterface oObj = null;
146         Object oRange = null ;
147 
148         // creation of testobject here
149         // first we write what we are intend to do to log file
150         log.println( "Creating a test environment" );
151 
152         // get a soffice factory object
153         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF() );
154 
155         log.println("Getting test object ");
156 
157         XComponent oComp = (XComponent)
158                     UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
159 
160         oObj = (XInterface)
161             SOF.createInstance(oComp, "com.sun.star.sheet.SheetCellRanges");
162 
163         XSpreadsheets oSheets = xSheetDoc.getSheets() ;
164         XIndexAccess oIndSheets = (XIndexAccess)
165             UnoRuntime.queryInterface (XIndexAccess.class, oSheets);
166         XSpreadsheet oSheet = null;
167         try {
168             oSheet = (XSpreadsheet) AnyConverter.toObject(
169                     new Type(XSpreadsheet.class), oIndSheets.getByIndex(0));
170             XNameContainer oRanges = (XNameContainer)
171                 UnoRuntime.queryInterface(XNameContainer.class, oObj);
172 
173             oRange = oSheet.getCellRangeByName("C1:D4");
174             oRanges.insertByName("Range1", oRange);
175             oRange = oSheet.getCellRangeByName("E2:F5");
176             oRanges.insertByName("Range2", oRange);
177             oRange = oSheet.getCellRangeByName("G2:H3");
178             oRanges.insertByName("Range3", oRange);
179             oRange = oSheet.getCellRangeByName("I7:J8");
180             oRanges.insertByName("Range4", oRange);
181         } catch(com.sun.star.lang.WrappedTargetException e) {
182             e.printStackTrace(log);
183             throw new StatusException("Couldn't create test object", e);
184         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
185             e.printStackTrace(log);
186             throw new StatusException("Couldn't create test object", e);
187         } catch(com.sun.star.container.ElementExistException e) {
188             e.printStackTrace(log);
189             throw new StatusException("Couldn't create test object", e);
190         } catch(com.sun.star.lang.IllegalArgumentException e) {
191             e.printStackTrace(log);
192             throw new StatusException("Couldn't create test object", e);
193         }
194 
195         log.println("filling some cells");
196         try {
197             for (int i = 0; i < 10; i++) {
198                 for (int j = 0; j < 5; j++) {
199                     oSheet.getCellByPosition(i, j).setFormula("a");
200                 }
201             }
202             for (int i = 0; i < 10; i++) {
203                 for (int j = 5; j < 10; j++) {
204                     oSheet.getCellByPosition(i, j).setValue(i + j);
205                 }
206             }
207         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
208             e.printStackTrace(log);
209             throw new StatusException (
210                 "Exception occurred while filling cells", e);
211         }
212 
213 
214         TestEnvironment tEnv = new TestEnvironment( oObj );
215 
216         // NameReplaceIndex : _XNameReplace
217         log.println( "adding NameReplaceIndex as mod relation to environment" );
218         tEnv.addObjRelation("NameReplaceIndex", "0");
219 
220         // INSTANCEn : _XNameContainer; _XNameReplace
221         log.println( "adding INSTANCEn as mod relation to environment" );
222 
223         int THRCNT = 1;
224         if ((String)Param.get("THRCNT") != null) {
225             THRCNT= Integer.parseInt((String)Param.get("THRCNT"));
226         }
227         int a = 0;
228         int b = 0;
229         for (int n = 1; n < (THRCNT + 1) ; n++) {
230             a = n * 2;
231             b = a + 1;
232             oRange = oSheet.getCellRangeByName("A" + a + ":B" + b);
233             log.println(
234                 "adding INSTANCE" + n + " as mod relation to environment" );
235 
236             tEnv.addObjRelation("INSTANCE" + n, oRange);
237         }
238 
239         XPropertySet PropSet = (XPropertySet)
240                     UnoRuntime.queryInterface(XPropertySet.class, oObj);
241         tEnv.addObjRelation("PropSet",PropSet);
242         tEnv.addObjRelation("SHEET", oSheet);
243         // add expected results for the XCellRangesQuery interface test
244         String[]expectedResults = new String[7];
245         expectedResults[_XCellRangesQuery.QUERYCOLUMNDIFFERENCES] = "Sheet1.I7:J8";
246         expectedResults[_XCellRangesQuery.QUERYCONTENTCELLS] = "";
247         expectedResults[_XCellRangesQuery.QUERYEMPTYCELLS] = "";
248         expectedResults[_XCellRangesQuery.QUERYFORMULACELLS] = "";
249         expectedResults[_XCellRangesQuery.QUERYINTERSECTION] = "Sheet1.D4";
250         expectedResults[_XCellRangesQuery.QUERYROWDIFFERENCES] = "Sheet1.I7:J8";
251         expectedResults[_XCellRangesQuery.QUERYVISIBLECELLS] = "Sheet1.C2:D4"; // first range, first line invisible
252         tEnv.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS", expectedResults);
253 
254         // for XSearchable and XReplaceable interface test
255         tEnv.addObjRelation("SEARCHSTRING", "15");
256 
257         // for XFormulaQuery interface test
258         tEnv.addObjRelation("EXPECTEDDEPENDENTVALUES", new int[]{4,5,1,4});
259         tEnv.addObjRelation("EXPECTEDPRECEDENTVALUES", new int[]{4,5,1,4});
260         return tEnv ;
261     }
262 
263 }    // finish class ScCellRangesObj
264 
265