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 ifc.sheet;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import lib.StatusException;
29 
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.sheet.SubTotalColumn;
32 import com.sun.star.sheet.XDatabaseRange;
33 import com.sun.star.sheet.XSheetFilterDescriptor;
34 import com.sun.star.sheet.XSubTotalDescriptor;
35 import com.sun.star.table.CellRangeAddress;
36 import com.sun.star.table.XCell;
37 import com.sun.star.table.XCellRange;
38 
39 /**
40 * Testing <code>com.sun.star.sheet.XDatabaseRange</code>
41 * interface methods :
42 * <ul>
43 *  <li><code> getDataArea()</code></li>
44 *  <li><code> setDataArea()</code></li>
45 *  <li><code> getSortDescriptor()</code></li>
46 *  <li><code> getFilterDescriptor()</code></li>
47 *  <li><code> getSubTotalDescriptor()</code></li>
48 *  <li><code> getImportDescriptor()</code></li>
49 *  <li><code> refresh()</code></li>
50 * </ul> <p>
51 * This test needs the following object relations :
52 * <ul>
53 *  <li> <code>'DATAAREA'</code> (of type <code>CellRangeAddress</code>):
54 *   to have cell range address for test of method <code>getDataArea()</code></li>
55 *   <li> <code>'XCELLRANGE'</code> (of type <code>XCellRange</code>):
56 *   cell range of the spreadsheet with database range,
57 *   to get values of cell</li>
58 * <ul> <p>
59 * @see com.sun.star.sheet.XDatabaseRange
60 * @see com.sun.star.table.CellRangeAddress
61 */
62 public class _XDatabaseRange extends MultiMethodTest {
63 
64     public XDatabaseRange oObj = null;
65     CellRangeAddress oldCRA = null;
66     XCellRange xCellRange = null;
67 
68     /**
69     * Retrieves object relations.
70     * @throws StatusException If one of relations not found.
71     */
before()72     protected void before() {
73         oldCRA = (CellRangeAddress)tEnv.getObjRelation("DATAAREA");
74         if (oldCRA == null) {
75             throw new StatusException(Status.failed
76                 ("Relation 'DATAAREA' not found"));
77         }
78         xCellRange = (XCellRange)tEnv.getObjRelation("XCELLRANGE");
79         if (xCellRange == null) {
80             throw new StatusException(Status.failed
81                 ("Relation 'XCELLRANGE' not found"));
82         }
83     }
84 
85     /**
86     * Test calls the method and compares returned cell range address with
87     * cell range address obtained by object relation <code>'DATAAREA'</code>.<p>
88     * Has <b> OK </b> status if all fields of cell range addresses are equal. <p>
89     */
_getDataArea()90     public void _getDataArea() {
91         boolean bResult = true;
92         CellRangeAddress objCRA = oObj.getDataArea();
93         bResult &= objCRA.EndColumn   == oldCRA.EndColumn;
94         bResult &= objCRA.EndRow       == oldCRA.EndRow;
95         bResult &= objCRA.Sheet       == oldCRA.Sheet;
96         bResult &= objCRA.StartColumn == oldCRA.StartColumn;
97         bResult &= objCRA.StartRow       == oldCRA.StartRow;
98         tRes.tested("getDataArea()", bResult);
99     }
100 
101     /**
102     * Test calls the method and checks returned value. <p>
103     * Has <b> OK </b> status if returned value isn't null. <p>
104     */
_getFilterDescriptor()105     public void _getFilterDescriptor() {
106         XSheetFilterDescriptor FD = oObj.getFilterDescriptor();
107         tRes.tested("getFilterDescriptor()", FD != null);
108     }
109 
110     /**
111     * Test calls the method and checks returned value. <p>
112     * Has <b> OK </b> status if returned value isn't null. <p>
113     */
_getImportDescriptor()114     public void _getImportDescriptor() {
115         PropertyValue[] pva = oObj.getImportDescriptor();
116         tRes.tested("getImportDescriptor()", pva != null);
117     }
118 
119     /**
120     * Test calls the method and checks returned value. <p>
121     * Has <b> OK </b> status if returned value isn't null. <p>
122     */
_getSortDescriptor()123     public void _getSortDescriptor() {
124         PropertyValue[] pva = oObj.getSortDescriptor();
125         tRes.tested("getSortDescriptor()", pva != null);
126     }
127 
128     /**
129     * Test calls the method and checks returned value. <p>
130     * Has <b> OK </b> status if returned value isn't null. <p>
131     */
_getSubTotalDescriptor()132     public void _getSubTotalDescriptor() {
133         STD = oObj.getSubTotalDescriptor();
134         tRes.tested("getSubTotalDescriptor()", STD != null);
135     }
136 
137     XSubTotalDescriptor STD = null;
138 
139     /**
140     * Test adds new SubTotalDescriptor and checks value of cell with
141     * subtotal sum after refresh() call. <p>
142     * Has <b> OK </b> if value of cell with subtotal sum was changed
143     * after refresh() call.<p>
144     */
_refresh()145     public void _refresh() {
146         requiredMethod("getSubTotalDescriptor()");
147         requiredMethod("setDataArea()");
148 
149         for(int i = STARTROW; i < ENDROW+1; i++) {
150             try {
151                 XCell cell = xCellRange.getCellByPosition(COL, i);
152                 cell.setValue(i);
153             } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
154                 log.println("Unexpected exception");
155                 e.printStackTrace(log);
156                 tRes.tested("refresh()", false);
157             }
158         }
159 
160         SubTotalColumn[] STC = new SubTotalColumn[1];
161         STC[0] = new SubTotalColumn();
162         STC[0].Column = COL;
163         STC[0].Function = com.sun.star.sheet.GeneralFunction.SUM;
164 
165         double oldVal = 0;
166         try {
167             XCell checkCell = xCellRange.getCellByPosition(COL, ENDROW);
168             oldVal = checkCell.getValue();
169         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
170             log.println("Unexpected exception");
171             e.printStackTrace(log);
172             tRes.tested("refresh()", false);
173         }
174         log.println("Value of the cell (" + COL + ", " + ENDROW +
175             ") : " + oldVal );
176 
177         log.println("Set new SubTotal descriptor...");
178 
179         STD.clear();
180         STD.addNew(STC, 1);
181 
182         double valBeforeRefresh = 0;
183         try {
184             XCell checkCell = xCellRange.getCellByPosition(COL, ENDROW);
185             valBeforeRefresh = checkCell.getValue();
186         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
187             log.println("Unexpected exception");
188             e.printStackTrace(log);
189             tRes.tested("refresh()", false);
190         }
191         log.println("Value of the cell (" + COL + ", " + ENDROW +
192             ") : " + valBeforeRefresh );
193 
194         log.println("Now call refresh()...");
195         oObj.refresh();
196 
197         double valAfterRefresh = 0;
198         try {
199             XCell checkCell = xCellRange.getCellByPosition(COL, ENDROW);
200             valAfterRefresh = checkCell.getValue();
201         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
202             log.println("Unexpected exception");
203             e.printStackTrace(log);
204             tRes.tested("refresh()", false);
205         }
206         log.println("Value of the cell (" + COL + ", " + ENDROW +
207             ") : " + valAfterRefresh );
208 
209         tRes.tested("refresh()", oldVal != valAfterRefresh &&
210             oldVal == valBeforeRefresh);
211     }
212 
213     final short COL = 0;
214     final short STARTROW = 0;
215     final short ENDROW = 5;
216 
217     /**
218     * Test creates new cell range address and calls the method. <p>
219     * Has <b> OK </b> status if the method successfully returns. <p>
220     */
_setDataArea()221     public void _setDataArea() {
222         executeMethod("getDataArea()");
223         CellRangeAddress newCRA = new CellRangeAddress();
224         newCRA.Sheet = oldCRA.Sheet;
225         newCRA.StartColumn = COL;
226         newCRA.EndColumn = COL;
227         newCRA.StartRow = STARTROW;
228         newCRA.EndRow = ENDROW;
229 
230         oObj.setDataArea(newCRA);
231 
232         tRes.tested("setDataArea()", true);
233     }
234 
after()235     protected void after() {
236         disposeEnvironment();
237     }
238 }
239 
240 
241