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