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