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 mod._sc;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.PrintWriter;
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir import lib.StatusException;
33*cdf0e10cSrcweir import lib.TestCase;
34*cdf0e10cSrcweir import lib.TestEnvironment;
35*cdf0e10cSrcweir import lib.TestParameters;
36*cdf0e10cSrcweir import util.SOfficeFactory;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
39*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
40*cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
41*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
42*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
43*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
44*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
45*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
46*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
47*cdf0e10cSrcweir import com.sun.star.uno.Type;
48*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
49*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
50*cdf0e10cSrcweir import ifc.sheet._XCellRangesQuery;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir /**
54*cdf0e10cSrcweir * Test for object which is represented by service
55*cdf0e10cSrcweir * <code>com.sun.star.sheet.SheetCellRanges</code>. <p>
56*cdf0e10cSrcweir * Object implements the following interfaces :
57*cdf0e10cSrcweir * <ul>
58*cdf0e10cSrcweir *  <li> <code>com::sun::star::table::CellProperties</code></li>
59*cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XNameReplace</code></li>
60*cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XNameContainer</code></li>
61*cdf0e10cSrcweir *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
62*cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
63*cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XElementAccess</code></li>
64*cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
65*cdf0e10cSrcweir *  <li> <code>com::sun::star::sheet::XSheetCellRangeContainer</code></li>
66*cdf0e10cSrcweir *  <li> <code>com::sun::star::sheet::XSheetOperation</code></li>
67*cdf0e10cSrcweir *  <li> <code>com::sun::star::sheet::XSheetCellRanges</code></li>
68*cdf0e10cSrcweir *  <li> <code>com::sun::star::container::XNameAccess</code></li>
69*cdf0e10cSrcweir * </ul>
70*cdf0e10cSrcweir * @see com.sun.star.sheet.SheetCellRanges
71*cdf0e10cSrcweir * @see com.sun.star.table.CellProperties
72*cdf0e10cSrcweir * @see com.sun.star.container.XNameReplace
73*cdf0e10cSrcweir * @see com.sun.star.container.XNameContainer
74*cdf0e10cSrcweir * @see com.sun.star.beans.XPropertySet
75*cdf0e10cSrcweir * @see com.sun.star.container.XIndexAccess
76*cdf0e10cSrcweir * @see com.sun.star.container.XElementAccess
77*cdf0e10cSrcweir * @see com.sun.star.container.XEnumerationAccess
78*cdf0e10cSrcweir * @see com.sun.star.sheet.XSheetCellRangeContainer
79*cdf0e10cSrcweir * @see com.sun.star.sheet.XSheetOperation
80*cdf0e10cSrcweir * @see com.sun.star.sheet.XSheetCellRanges
81*cdf0e10cSrcweir * @see com.sun.star.container.XNameAccess
82*cdf0e10cSrcweir * @see ifc.table._CellProperties
83*cdf0e10cSrcweir * @see ifc.container._XNameReplace
84*cdf0e10cSrcweir * @see ifc.container._XNameContainer
85*cdf0e10cSrcweir * @see ifc.beans._XPropertySet
86*cdf0e10cSrcweir * @see ifc.container._XIndexAccess
87*cdf0e10cSrcweir * @see ifc.container._XElementAccess
88*cdf0e10cSrcweir * @see ifc.container._XEnumerationAccess
89*cdf0e10cSrcweir * @see ifc.sheet._XSheetCellRangeContainer
90*cdf0e10cSrcweir * @see ifc.sheet._XSheetOperation
91*cdf0e10cSrcweir * @see ifc.sheet._XSheetCellRanges
92*cdf0e10cSrcweir * @see ifc.container._XNameAccess
93*cdf0e10cSrcweir */
94*cdf0e10cSrcweir public class ScCellRangesObj extends TestCase {
95*cdf0e10cSrcweir     static XSpreadsheetDocument xSheetDoc = null;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     /**
98*cdf0e10cSrcweir     * Creates Spreadsheet document.
99*cdf0e10cSrcweir     */
100*cdf0e10cSrcweir     protected void initialize( TestParameters tParam, PrintWriter log ) {
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir         // get a soffice factory object
103*cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         try {
106*cdf0e10cSrcweir             log.println( "creating a sheetdocument" );
107*cdf0e10cSrcweir             xSheetDoc = SOF.createCalcDoc(null);;
108*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
109*cdf0e10cSrcweir             // Some exception occures.FAILED
110*cdf0e10cSrcweir             e.printStackTrace( log );
111*cdf0e10cSrcweir             throw new StatusException( "Couldn't create document", e );
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     /**
116*cdf0e10cSrcweir      * Disposes Spreadsheet document.
117*cdf0e10cSrcweir      */
118*cdf0e10cSrcweir     protected void cleanup( TestParameters tParam, PrintWriter log ) {
119*cdf0e10cSrcweir         log.println( "    disposing xSheetDoc " );
120*cdf0e10cSrcweir         XComponent oComp =
121*cdf0e10cSrcweir             (XComponent) UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
122*cdf0e10cSrcweir         util.DesktopTools.closeDoc(oComp);
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     /**
126*cdf0e10cSrcweir     * Creating a Testenvironment for the interfaces to be tested.
127*cdf0e10cSrcweir     * Creates an instance of the service
128*cdf0e10cSrcweir     * <code>com.sun.star.sheet.SheetCellRanges</code> and fills some cells.
129*cdf0e10cSrcweir     * Object relations created :
130*cdf0e10cSrcweir     * <ul>
131*cdf0e10cSrcweir     *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> for
132*cdf0e10cSrcweir     *      {@link ifc.container._XNameReplace},
133*cdf0e10cSrcweir     *      {@link ifc.container._XNameContainer} (type of
134*cdf0e10cSrcweir     *      <code>XCellRange</code>)</li>
135*cdf0e10cSrcweir     *  <li> <code>'THRCNT'</code> for
136*cdf0e10cSrcweir     *      {@link ifc.container._XNameReplace}(the number of the running threads
137*cdf0e10cSrcweir     *      that was retrieved from the method parameter <code>Param</code>)</li>
138*cdf0e10cSrcweir     *  <li> <code>'NameReplaceIndex'</code> for
139*cdf0e10cSrcweir     *      {@link ifc.container._XNameReplace} </li>
140*cdf0e10cSrcweir     * </ul>
141*cdf0e10cSrcweir     * @see com.sun.star.table.XCellRange
142*cdf0e10cSrcweir     */
143*cdf0e10cSrcweir     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         XInterface oObj = null;
146*cdf0e10cSrcweir         Object oRange = null ;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir         // creation of testobject here
149*cdf0e10cSrcweir         // first we write what we are intend to do to log file
150*cdf0e10cSrcweir         log.println( "Creating a test environment" );
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         // get a soffice factory object
153*cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF() );
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         log.println("Getting test object ");
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir         XComponent oComp = (XComponent)
158*cdf0e10cSrcweir                     UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         oObj = (XInterface)
161*cdf0e10cSrcweir             SOF.createInstance(oComp, "com.sun.star.sheet.SheetCellRanges");
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         XSpreadsheets oSheets = xSheetDoc.getSheets() ;
164*cdf0e10cSrcweir         XIndexAccess oIndSheets = (XIndexAccess)
165*cdf0e10cSrcweir             UnoRuntime.queryInterface (XIndexAccess.class, oSheets);
166*cdf0e10cSrcweir         XSpreadsheet oSheet = null;
167*cdf0e10cSrcweir         try {
168*cdf0e10cSrcweir             oSheet = (XSpreadsheet) AnyConverter.toObject(
169*cdf0e10cSrcweir                     new Type(XSpreadsheet.class), oIndSheets.getByIndex(0));
170*cdf0e10cSrcweir             XNameContainer oRanges = (XNameContainer)
171*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XNameContainer.class, oObj);
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir             oRange = oSheet.getCellRangeByName("C1:D4");
174*cdf0e10cSrcweir             oRanges.insertByName("Range1", oRange);
175*cdf0e10cSrcweir             oRange = oSheet.getCellRangeByName("E2:F5");
176*cdf0e10cSrcweir             oRanges.insertByName("Range2", oRange);
177*cdf0e10cSrcweir             oRange = oSheet.getCellRangeByName("G2:H3");
178*cdf0e10cSrcweir             oRanges.insertByName("Range3", oRange);
179*cdf0e10cSrcweir             oRange = oSheet.getCellRangeByName("I7:J8");
180*cdf0e10cSrcweir             oRanges.insertByName("Range4", oRange);
181*cdf0e10cSrcweir         } catch(com.sun.star.lang.WrappedTargetException e) {
182*cdf0e10cSrcweir             e.printStackTrace(log);
183*cdf0e10cSrcweir             throw new StatusException("Couldn't create test object", e);
184*cdf0e10cSrcweir         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
185*cdf0e10cSrcweir             e.printStackTrace(log);
186*cdf0e10cSrcweir             throw new StatusException("Couldn't create test object", e);
187*cdf0e10cSrcweir         } catch(com.sun.star.container.ElementExistException e) {
188*cdf0e10cSrcweir             e.printStackTrace(log);
189*cdf0e10cSrcweir             throw new StatusException("Couldn't create test object", e);
190*cdf0e10cSrcweir         } catch(com.sun.star.lang.IllegalArgumentException e) {
191*cdf0e10cSrcweir             e.printStackTrace(log);
192*cdf0e10cSrcweir             throw new StatusException("Couldn't create test object", e);
193*cdf0e10cSrcweir         }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir         log.println("filling some cells");
196*cdf0e10cSrcweir         try {
197*cdf0e10cSrcweir             for (int i = 0; i < 10; i++) {
198*cdf0e10cSrcweir                 for (int j = 0; j < 5; j++) {
199*cdf0e10cSrcweir                     oSheet.getCellByPosition(i, j).setFormula("a");
200*cdf0e10cSrcweir                 }
201*cdf0e10cSrcweir             }
202*cdf0e10cSrcweir             for (int i = 0; i < 10; i++) {
203*cdf0e10cSrcweir                 for (int j = 5; j < 10; j++) {
204*cdf0e10cSrcweir                     oSheet.getCellByPosition(i, j).setValue(i + j);
205*cdf0e10cSrcweir                 }
206*cdf0e10cSrcweir             }
207*cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
208*cdf0e10cSrcweir             e.printStackTrace(log);
209*cdf0e10cSrcweir             throw new StatusException (
210*cdf0e10cSrcweir                 "Exception occurred while filling cells", e);
211*cdf0e10cSrcweir         }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment( oObj );
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         // NameReplaceIndex : _XNameReplace
217*cdf0e10cSrcweir         log.println( "adding NameReplaceIndex as mod relation to environment" );
218*cdf0e10cSrcweir         tEnv.addObjRelation("NameReplaceIndex", "0");
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir         // INSTANCEn : _XNameContainer; _XNameReplace
221*cdf0e10cSrcweir         log.println( "adding INSTANCEn as mod relation to environment" );
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir         int THRCNT = 1;
224*cdf0e10cSrcweir         if ((String)Param.get("THRCNT") != null) {
225*cdf0e10cSrcweir             THRCNT= Integer.parseInt((String)Param.get("THRCNT"));
226*cdf0e10cSrcweir         }
227*cdf0e10cSrcweir         int a = 0;
228*cdf0e10cSrcweir         int b = 0;
229*cdf0e10cSrcweir         for (int n = 1; n < (THRCNT + 1) ; n++) {
230*cdf0e10cSrcweir             a = n * 2;
231*cdf0e10cSrcweir             b = a + 1;
232*cdf0e10cSrcweir             oRange = oSheet.getCellRangeByName("A" + a + ":B" + b);
233*cdf0e10cSrcweir             log.println(
234*cdf0e10cSrcweir                 "adding INSTANCE" + n + " as mod relation to environment" );
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir             tEnv.addObjRelation("INSTANCE" + n, oRange);
237*cdf0e10cSrcweir         }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir         XPropertySet PropSet = (XPropertySet)
240*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XPropertySet.class, oObj);
241*cdf0e10cSrcweir         tEnv.addObjRelation("PropSet",PropSet);
242*cdf0e10cSrcweir         tEnv.addObjRelation("SHEET", oSheet);
243*cdf0e10cSrcweir         // add expected results for the XCellRangesQuery interface test
244*cdf0e10cSrcweir         String[]expectedResults = new String[7];
245*cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYCOLUMNDIFFERENCES] = "Sheet1.I7:J8";
246*cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYCONTENTCELLS] = "";
247*cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYEMPTYCELLS] = "";
248*cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYFORMULACELLS] = "";
249*cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYINTERSECTION] = "Sheet1.D4";
250*cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYROWDIFFERENCES] = "Sheet1.I7:J8";
251*cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYVISIBLECELLS] = "Sheet1.C2:D4"; // first range, first line invisible
252*cdf0e10cSrcweir         tEnv.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS", expectedResults);
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir         // for XSearchable and XReplaceable interface test
255*cdf0e10cSrcweir         tEnv.addObjRelation("SEARCHSTRING", "15");
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir         // for XFormulaQuery interface test
258*cdf0e10cSrcweir         tEnv.addObjRelation("EXPECTEDDEPENDENTVALUES", new int[]{4,5,1,4});
259*cdf0e10cSrcweir         tEnv.addObjRelation("EXPECTEDPRECEDENTVALUES", new int[]{4,5,1,4});
260*cdf0e10cSrcweir         return tEnv ;
261*cdf0e10cSrcweir     }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir }    // finish class ScCellRangesObj
264*cdf0e10cSrcweir 
265