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 import java.io.PrintWriter;
25 
26 import lib.StatusException;
27 import lib.TestCase;
28 import lib.TestEnvironment;
29 import lib.TestParameters;
30 import util.SOfficeFactory;
31 import util.ValueComparer;
32 
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.container.XNameAccess;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sheet.XSpreadsheet;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.sheet.XSpreadsheets;
40 import com.sun.star.table.XCell;
41 import com.sun.star.table.XCellRange;
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.SheetCellRange</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::util::XMergeable</code></li>
56 *  <li> <code>com::sun::star::sheet::XCellRangeAddressable</code></li>
57 *  <li> <code>com::sun::star::sheet::XArrayFormulaRange</code></li>
58 *  <li> <code>com::sun::star::chart::XChartData</code></li>
59 *  <li> <code>com::sun::star::table::XColumnRowRange</code></li>
60 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
61 *  <li> <code>com::sun::star::style::CharacterProperties</code></li>
62 *  <li> <code>com::sun::star::sheet::XSheetOperation</code></li>
63 *  <li> <code>com::sun::star::table::XCellRange</code></li>
64 *  <li> <code>com::sun::star::style::ParagraphProperties</code></li>
65 *  <li> <code>com::sun::star::sheet::SheetCellRange</code></li>
66 * </ul>
67 * @see com.sun.star.sheet.SheetCellRange
68 * @see com.sun.star.table.CellProperties
69 * @see com.sun.star.util.XMergeable
70 * @see com.sun.star.sheet.XCellRangeAddressable
71 * @see com.sun.star.sheet.XArrayFormulaRange
72 * @see com.sun.star.chart.XChartData
73 * @see com.sun.star.table.XColumnRowRange
74 * @see com.sun.star.beans.XPropertySet
75 * @see com.sun.star.style.CharacterProperties
76 * @see com.sun.star.sheet.XSheetOperation
77 * @see com.sun.star.table.XCellRange
78 * @see com.sun.star.style.ParagraphProperties
79 * @see com.sun.star.sheet.SheetCellRange
80 * @see ifc.table._CellProperties
81 * @see ifc.util._XMergeable
82 * @see ifc.sheet._XCellRangeAddressable
83 * @see ifc.sheet._XArrayFormulaRange
84 * @see ifc.chart._XChartData
85 * @see ifc.table._XColumnRowRange
86 * @see ifc.beans._XPropertySet
87 * @see ifc.style._CharacterProperties
88 * @see ifc.sheet._XSheetOperation
89 * @see ifc.table._XCellRange
90 * @see ifc.style._ParagraphProperties
91 * @see ifc.sheet._SheetCellRange
92 */
93 public class ScCellRangeObj extends TestCase {
94     XSpreadsheetDocument xSheetDoc = null;
95 
96     /**
97     * Creates Spreadsheet document.
98     */
initialize(TestParameters tParam, PrintWriter log)99     protected void initialize(TestParameters tParam, PrintWriter log) {
100         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) tParam.getMSF());
101 
102         try {
103             log.println("creating a Spreadsheet document");
104             xSheetDoc = SOF.createCalcDoc(null);
105         } catch (com.sun.star.uno.Exception e) {
106             // Some exception occures.FAILED
107             e.printStackTrace(log);
108             throw new StatusException("Couldn't create document", e);
109         }
110     }
111 
112     /**
113     * Disposes Spreadsheet document.
114     */
cleanup(TestParameters tParam, PrintWriter log)115     protected void cleanup(TestParameters tParam, PrintWriter log) {
116         log.println("    disposing xSheetDoc ");
117 
118         XComponent oComp = (XComponent) UnoRuntime.queryInterface(
119                                    XComponent.class, xSheetDoc);
120         util.DesktopTools.closeDoc(oComp);
121     }
122 
123     /**
124     * Creating a Testenvironment for the interfaces to be tested.
125     * Retrieves a collection of spreadsheets from a document,
126     * and takes one of them. Retrieves some cell range from the spreadsheet.
127     * The retrieved cell range is instance of the service
128     * <code>com.sun.star.sheet.SheetCellRange</code>.
129     * Object relations created :
130     * <ul>
131     *  <li> <code>'SHEET'</code> for
132     *      {@link ifc.sheet._XArrayFormulaRange} (the spreadsheet which the cell
133     *      range was retrieved from)</li>
134     * </ul>
135     * @see com.sun.star.sheet.XSpreadsheet
136     */
createTestEnvironment(TestParameters Param, PrintWriter log)137     protected TestEnvironment createTestEnvironment(TestParameters Param,
138                                                     PrintWriter log) {
139         XInterface oObj = null;
140         XCellRange testRange;
141 
142 
143         // creation of testobject here
144         // first we write what we are intend to do to log file
145         log.println("Creating a test environment");
146 
147         XSpreadsheets oSpreadsheets = ((XSpreadsheetDocument) UnoRuntime.queryInterface(
148                                                XSpreadsheetDocument.class,
149                                                xSheetDoc)).getSheets();
150         XNameAccess oNames = (XNameAccess) UnoRuntime.queryInterface(
151                                      XNameAccess.class, oSpreadsheets);
152 
153         XSpreadsheet oSheet = null;
154 
155         try {
156             oSheet = (XSpreadsheet) AnyConverter.toObject(
157                              new Type(XSpreadsheet.class),
158                              oNames.getByName(oNames.getElementNames()[0]));
159 
160             oObj = oSheet.getCellRangeByPosition(0, 0, 3, 4);
161             testRange = (XCellRange) UnoRuntime.queryInterface(
162                                 XCellRange.class, oObj);
163         } catch (com.sun.star.lang.WrappedTargetException e) {
164             e.printStackTrace(log);
165             throw new StatusException(
166                     "Error getting cell object from spreadsheet document", e);
167         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
168             e.printStackTrace(log);
169             throw new StatusException(
170                     "Error getting cell object from spreadsheet document", e);
171         } catch (com.sun.star.container.NoSuchElementException e) {
172             e.printStackTrace(log);
173             throw new StatusException(
174                     "Error getting cell object from spreadsheet document", e);
175         } catch (com.sun.star.lang.IllegalArgumentException e) {
176             e.printStackTrace(log);
177             throw new StatusException(
178                     "Error getting cell object from spreadsheet document", e);
179         }
180 
181 
182         TestEnvironment tEnv = new TestEnvironment(oObj);
183 
184         tEnv.addObjRelation("SHEET", oSheet);
185         // add expected results for the XCellRangesQuery interface test
186         String[]expectedResults = new String[7];
187         expectedResults[_XCellRangesQuery.QUERYCOLUMNDIFFERENCES] = "Sheet1.B1:C1;Sheet1.B3";
188         expectedResults[_XCellRangesQuery.QUERYCONTENTCELLS] = "Sheet1.B2:B3";
189         expectedResults[_XCellRangesQuery.QUERYEMPTYCELLS] = "Sheet1.A1 ... Sheet1.B1 ... Sheet1.B5 ... Sheet1.C3 ... Sheet1.D1";
190         expectedResults[_XCellRangesQuery.QUERYFORMULACELLS] = "Sheet1.C2";
191         expectedResults[_XCellRangesQuery.QUERYINTERSECTION] = "Sheet1.D4";
192         expectedResults[_XCellRangesQuery.QUERYROWDIFFERENCES] = "Sheet1.A2:A4;Sheet1.C2:D4";
193         expectedResults[_XCellRangesQuery.QUERYVISIBLECELLS] = "Sheet1.A2";
194         tEnv.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS", expectedResults);
195         tEnv.addObjRelation("XCellRangesQuery.CREATEENTRIES", Boolean.TRUE);
196 
197         XPropertySet PropSet = (XPropertySet) UnoRuntime.queryInterface(
198                                        XPropertySet.class, oObj);
199         tEnv.addObjRelation("PropSet", PropSet);
200 
201         // XSearchable: Add a cell to make a seacrchable entry
202         try {
203             tEnv.addObjRelation("XSearchable.MAKEENTRYINCELL", new XCell[] {
204                         testRange.getCellByPosition(0,0), testRange.getCellByPosition(0,1)});
205         }
206         catch(com.sun.star.lang.IndexOutOfBoundsException e){
207             e.printStackTrace((PrintWriter)log);
208             log.println("Cannot make required object relation 'XSearchable.MAKEENTRYINCELL'.");
209         }
210 
211         // XCellRangeData
212 /*        Object[][] newData = new Object[5][4];
213         for (int i=0; i<newData.length; i++) {
214             for (int j=0; j<newData[i].length; j++) {
215                 newData[i][j] = new Double(i*10+j);
216             }
217         }
218         tEnv.addObjRelation("NewData", newData); */
219 
220         //Adding relation for util.XSortable
221         final PrintWriter finalLog = log;
222         final XCellRange oTable = testRange;
223         tEnv.addObjRelation("SORTCHECKER",
224                             new ifc.util._XSortable.XSortChecker() {
225             PrintWriter out = finalLog;
226 
227             public void setPrintWriter(PrintWriter log) {
228                 out = log;
229             }
230 
231             public void prepareToSort() {
232                 try {
233                     oTable.getCellByPosition(0, 0).setValue(4);
234                     oTable.getCellByPosition(0, 1).setFormula("b");
235                     oTable.getCellByPosition(0, 2).setValue(3);
236                     oTable.getCellByPosition(0, 3).setValue(23);
237                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
238                     out.println("Exception while checking sort");
239                 }
240             }
241 
242             public boolean checkSort(boolean isSortNumbering,
243                                      boolean isSortAscending) {
244                 out.println("Sort checking...");
245 
246                 boolean res = false;
247                 String[] value = new String[4];
248 
249                 for (int i = 0; i < 4; i++) {
250                     try {
251                         XCell cell = oTable.getCellByPosition(0, i);
252                         value[i] = cell.getFormula();
253                     } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
254                         out.println("Exception while checking sort");
255                     }
256                 }
257 
258                 if (isSortNumbering) {
259                     if (isSortAscending) {
260                         out.println("Sorting ascending");
261 
262                         String[] rightVal = { "3", "4", "23", "b" };
263                         String[] vals = { value[0], value[1], value[2], value[3] };
264                         res = ValueComparer.equalValue(vals, rightVal);
265                         out.println("Expected 3, 4, 23, b");
266                         out.println("getting: " + value[0] + ", " +
267                                         value[1] + ", " + value[2] + ", " +
268                                         value[3]);
269                     } else {
270                         String[] rightVal = { "b", "23", "4", "3" };
271                         String[] vals = { value[0], value[1], value[2], value[3] };
272                         res = ValueComparer.equalValue(vals, rightVal);
273                         out.println("Expected b, 23, 4, 3");
274                         out.println("getting: " + value[0] + ", " +
275                                         value[1] + ", " + value[2] + ", " +
276                                         value[3]);
277                     }
278                 } else {
279                     if (isSortAscending) {
280                         String[] rightVal = { "3", "4", "23", "b" };
281                         res = ValueComparer.equalValue(value, rightVal);
282                         out.println("Expected 3, 4, 23, b");
283                         out.println("getting: " + value[0] + ", " +
284                                         value[1] + ", " + value[2] + ", " +
285                                         value[3]);
286                     } else {
287                         String[] rightVal = { "b", "23", "4", "3" };
288                         res = ValueComparer.equalValue(value, rightVal);
289                         out.println("Expected b, 23, 4, 3");
290                         out.println("getting: " + value[0] + ", " +
291                                         value[1] + ", " + value[2] + ", " +
292                                         value[3]);
293                     }
294                 }
295 
296                 if (res) {
297                     out.println("Sorted correctly");
298                 } else {
299                     out.println("Sorted uncorrectly");
300                 }
301 
302                 return res;
303             }
304         });
305 
306         return tEnv;
307     }
308 } // finish class ScCellRangeObj
309