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