1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.sheet;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import lib.MultiMethodTest;
31*cdf0e10cSrcweir import lib.Status;
32*cdf0e10cSrcweir import lib.StatusException;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
35*cdf0e10cSrcweir import com.sun.star.sheet.SubTotalColumn;
36*cdf0e10cSrcweir import com.sun.star.sheet.XDatabaseRange;
37*cdf0e10cSrcweir import com.sun.star.sheet.XSheetFilterDescriptor;
38*cdf0e10cSrcweir import com.sun.star.sheet.XSubTotalDescriptor;
39*cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress;
40*cdf0e10cSrcweir import com.sun.star.table.XCell;
41*cdf0e10cSrcweir import com.sun.star.table.XCellRange;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir /**
44*cdf0e10cSrcweir * Testing <code>com.sun.star.sheet.XDatabaseRange</code>
45*cdf0e10cSrcweir * interface methods :
46*cdf0e10cSrcweir * <ul>
47*cdf0e10cSrcweir *  <li><code> getDataArea()</code></li>
48*cdf0e10cSrcweir *  <li><code> setDataArea()</code></li>
49*cdf0e10cSrcweir *  <li><code> getSortDescriptor()</code></li>
50*cdf0e10cSrcweir *  <li><code> getFilterDescriptor()</code></li>
51*cdf0e10cSrcweir *  <li><code> getSubTotalDescriptor()</code></li>
52*cdf0e10cSrcweir *  <li><code> getImportDescriptor()</code></li>
53*cdf0e10cSrcweir *  <li><code> refresh()</code></li>
54*cdf0e10cSrcweir * </ul> <p>
55*cdf0e10cSrcweir * This test needs the following object relations :
56*cdf0e10cSrcweir * <ul>
57*cdf0e10cSrcweir *  <li> <code>'DATAAREA'</code> (of type <code>CellRangeAddress</code>):
58*cdf0e10cSrcweir *   to have cell range address for test of method <code>getDataArea()</code></li>
59*cdf0e10cSrcweir *   <li> <code>'XCELLRANGE'</code> (of type <code>XCellRange</code>):
60*cdf0e10cSrcweir *   cell range of the spreadsheet with database range,
61*cdf0e10cSrcweir *   to get values of cell</li>
62*cdf0e10cSrcweir * <ul> <p>
63*cdf0e10cSrcweir * @see com.sun.star.sheet.XDatabaseRange
64*cdf0e10cSrcweir * @see com.sun.star.table.CellRangeAddress
65*cdf0e10cSrcweir */
66*cdf0e10cSrcweir public class _XDatabaseRange extends MultiMethodTest {
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     public XDatabaseRange oObj = null;
69*cdf0e10cSrcweir     CellRangeAddress oldCRA = null;
70*cdf0e10cSrcweir     XCellRange xCellRange = null;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     /**
73*cdf0e10cSrcweir     * Retrieves object relations.
74*cdf0e10cSrcweir     * @throws StatusException If one of relations not found.
75*cdf0e10cSrcweir     */
76*cdf0e10cSrcweir     protected void before() {
77*cdf0e10cSrcweir         oldCRA = (CellRangeAddress)tEnv.getObjRelation("DATAAREA");
78*cdf0e10cSrcweir         if (oldCRA == null) {
79*cdf0e10cSrcweir             throw new StatusException(Status.failed
80*cdf0e10cSrcweir                 ("Relation 'DATAAREA' not found"));
81*cdf0e10cSrcweir         }
82*cdf0e10cSrcweir         xCellRange = (XCellRange)tEnv.getObjRelation("XCELLRANGE");
83*cdf0e10cSrcweir         if (xCellRange == null) {
84*cdf0e10cSrcweir             throw new StatusException(Status.failed
85*cdf0e10cSrcweir                 ("Relation 'XCELLRANGE' not found"));
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir     /**
90*cdf0e10cSrcweir     * Test calls the method and compares returned cell range address with
91*cdf0e10cSrcweir     * cell range address obtained by object relation <code>'DATAAREA'</code>.<p>
92*cdf0e10cSrcweir     * Has <b> OK </b> status if all fields of cell range addresses are equal. <p>
93*cdf0e10cSrcweir     */
94*cdf0e10cSrcweir     public void _getDataArea() {
95*cdf0e10cSrcweir         boolean bResult = true;
96*cdf0e10cSrcweir         CellRangeAddress objCRA = oObj.getDataArea();
97*cdf0e10cSrcweir         bResult &= objCRA.EndColumn   == oldCRA.EndColumn;
98*cdf0e10cSrcweir         bResult &= objCRA.EndRow       == oldCRA.EndRow;
99*cdf0e10cSrcweir         bResult &= objCRA.Sheet       == oldCRA.Sheet;
100*cdf0e10cSrcweir         bResult &= objCRA.StartColumn == oldCRA.StartColumn;
101*cdf0e10cSrcweir         bResult &= objCRA.StartRow       == oldCRA.StartRow;
102*cdf0e10cSrcweir         tRes.tested("getDataArea()", bResult);
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     /**
106*cdf0e10cSrcweir     * Test calls the method and checks returned value. <p>
107*cdf0e10cSrcweir     * Has <b> OK </b> status if returned value isn't null. <p>
108*cdf0e10cSrcweir     */
109*cdf0e10cSrcweir     public void _getFilterDescriptor() {
110*cdf0e10cSrcweir         XSheetFilterDescriptor FD = oObj.getFilterDescriptor();
111*cdf0e10cSrcweir         tRes.tested("getFilterDescriptor()", FD != null);
112*cdf0e10cSrcweir     }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     /**
115*cdf0e10cSrcweir     * Test calls the method and checks returned value. <p>
116*cdf0e10cSrcweir     * Has <b> OK </b> status if returned value isn't null. <p>
117*cdf0e10cSrcweir     */
118*cdf0e10cSrcweir     public void _getImportDescriptor() {
119*cdf0e10cSrcweir         PropertyValue[] pva = oObj.getImportDescriptor();
120*cdf0e10cSrcweir         tRes.tested("getImportDescriptor()", pva != null);
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     /**
124*cdf0e10cSrcweir     * Test calls the method and checks returned value. <p>
125*cdf0e10cSrcweir     * Has <b> OK </b> status if returned value isn't null. <p>
126*cdf0e10cSrcweir     */
127*cdf0e10cSrcweir     public void _getSortDescriptor() {
128*cdf0e10cSrcweir         PropertyValue[] pva = oObj.getSortDescriptor();
129*cdf0e10cSrcweir         tRes.tested("getSortDescriptor()", pva != null);
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     /**
133*cdf0e10cSrcweir     * Test calls the method and checks returned value. <p>
134*cdf0e10cSrcweir     * Has <b> OK </b> status if returned value isn't null. <p>
135*cdf0e10cSrcweir     */
136*cdf0e10cSrcweir     public void _getSubTotalDescriptor() {
137*cdf0e10cSrcweir         STD = oObj.getSubTotalDescriptor();
138*cdf0e10cSrcweir         tRes.tested("getSubTotalDescriptor()", STD != null);
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     XSubTotalDescriptor STD = null;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     /**
144*cdf0e10cSrcweir     * Test adds new SubTotalDescriptor and checks value of cell with
145*cdf0e10cSrcweir     * subtotal sum after refresh() call. <p>
146*cdf0e10cSrcweir     * Has <b> OK </b> if value of cell with subtotal sum was changed
147*cdf0e10cSrcweir     * after refresh() call.<p>
148*cdf0e10cSrcweir     */
149*cdf0e10cSrcweir     public void _refresh() {
150*cdf0e10cSrcweir         requiredMethod("getSubTotalDescriptor()");
151*cdf0e10cSrcweir         requiredMethod("setDataArea()");
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         for(int i = STARTROW; i < ENDROW+1; i++) {
154*cdf0e10cSrcweir             try {
155*cdf0e10cSrcweir                 XCell cell = xCellRange.getCellByPosition(COL, i);
156*cdf0e10cSrcweir                 cell.setValue(i);
157*cdf0e10cSrcweir             } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
158*cdf0e10cSrcweir                 log.println("Unexpected exception");
159*cdf0e10cSrcweir                 e.printStackTrace(log);
160*cdf0e10cSrcweir                 tRes.tested("refresh()", false);
161*cdf0e10cSrcweir             }
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir         SubTotalColumn[] STC = new SubTotalColumn[1];
165*cdf0e10cSrcweir         STC[0] = new SubTotalColumn();
166*cdf0e10cSrcweir         STC[0].Column = COL;
167*cdf0e10cSrcweir         STC[0].Function = com.sun.star.sheet.GeneralFunction.SUM;
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         double oldVal = 0;
170*cdf0e10cSrcweir         try {
171*cdf0e10cSrcweir             XCell checkCell = xCellRange.getCellByPosition(COL, ENDROW);
172*cdf0e10cSrcweir             oldVal = checkCell.getValue();
173*cdf0e10cSrcweir         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
174*cdf0e10cSrcweir             log.println("Unexpected exception");
175*cdf0e10cSrcweir             e.printStackTrace(log);
176*cdf0e10cSrcweir             tRes.tested("refresh()", false);
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir         log.println("Value of the cell (" + COL + ", " + ENDROW +
179*cdf0e10cSrcweir             ") : " + oldVal );
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir         log.println("Set new SubTotal descriptor...");
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir         STD.clear();
184*cdf0e10cSrcweir         STD.addNew(STC, 1);
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         double valBeforeRefresh = 0;
187*cdf0e10cSrcweir         try {
188*cdf0e10cSrcweir             XCell checkCell = xCellRange.getCellByPosition(COL, ENDROW);
189*cdf0e10cSrcweir             valBeforeRefresh = checkCell.getValue();
190*cdf0e10cSrcweir         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
191*cdf0e10cSrcweir             log.println("Unexpected exception");
192*cdf0e10cSrcweir             e.printStackTrace(log);
193*cdf0e10cSrcweir             tRes.tested("refresh()", false);
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir         log.println("Value of the cell (" + COL + ", " + ENDROW +
196*cdf0e10cSrcweir             ") : " + valBeforeRefresh );
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir         log.println("Now call refresh()...");
199*cdf0e10cSrcweir         oObj.refresh();
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir         double valAfterRefresh = 0;
202*cdf0e10cSrcweir         try {
203*cdf0e10cSrcweir             XCell checkCell = xCellRange.getCellByPosition(COL, ENDROW);
204*cdf0e10cSrcweir             valAfterRefresh = checkCell.getValue();
205*cdf0e10cSrcweir         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
206*cdf0e10cSrcweir             log.println("Unexpected exception");
207*cdf0e10cSrcweir             e.printStackTrace(log);
208*cdf0e10cSrcweir             tRes.tested("refresh()", false);
209*cdf0e10cSrcweir         }
210*cdf0e10cSrcweir         log.println("Value of the cell (" + COL + ", " + ENDROW +
211*cdf0e10cSrcweir             ") : " + valAfterRefresh );
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir         tRes.tested("refresh()", oldVal != valAfterRefresh &&
214*cdf0e10cSrcweir             oldVal == valBeforeRefresh);
215*cdf0e10cSrcweir     }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir     final short COL = 0;
218*cdf0e10cSrcweir     final short STARTROW = 0;
219*cdf0e10cSrcweir     final short ENDROW = 5;
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     /**
222*cdf0e10cSrcweir     * Test creates new cell range address and calls the method. <p>
223*cdf0e10cSrcweir     * Has <b> OK </b> status if the method successfully returns. <p>
224*cdf0e10cSrcweir     */
225*cdf0e10cSrcweir     public void _setDataArea() {
226*cdf0e10cSrcweir         executeMethod("getDataArea()");
227*cdf0e10cSrcweir         CellRangeAddress newCRA = new CellRangeAddress();
228*cdf0e10cSrcweir         newCRA.Sheet = oldCRA.Sheet;
229*cdf0e10cSrcweir         newCRA.StartColumn = COL;
230*cdf0e10cSrcweir         newCRA.EndColumn = COL;
231*cdf0e10cSrcweir         newCRA.StartRow = STARTROW;
232*cdf0e10cSrcweir         newCRA.EndRow = ENDROW;
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir         oObj.setDataArea(newCRA);
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir         tRes.tested("setDataArea()", true);
237*cdf0e10cSrcweir     }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir     protected void after() {
240*cdf0e10cSrcweir         disposeEnvironment();
241*cdf0e10cSrcweir     }
242*cdf0e10cSrcweir }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 
245