2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.io
.PrintWriter
;
23 import lib
.StatusException
;
25 import lib
.TestEnvironment
;
26 import lib
.TestParameters
;
27 import util
.SOfficeFactory
;
29 import com
.sun
.star
.beans
.XPropertySet
;
30 import com
.sun
.star
.container
.XIndexAccess
;
31 import com
.sun
.star
.container
.XNameContainer
;
32 import com
.sun
.star
.lang
.XComponent
;
33 import com
.sun
.star
.lang
.XMultiServiceFactory
;
34 import com
.sun
.star
.sheet
.XSpreadsheet
;
35 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
36 import com
.sun
.star
.sheet
.XSpreadsheets
;
37 import com
.sun
.star
.uno
.AnyConverter
;
38 import com
.sun
.star
.uno
.Type
;
39 import com
.sun
.star
.uno
.UnoRuntime
;
40 import com
.sun
.star
.uno
.XInterface
;
41 import ifc
.sheet
._XCellRangesQuery
;
45 * Test for object which is represented by service
46 * <code>com.sun.star.sheet.SheetCellRanges</code>. <p>
47 * Object implements the following interfaces :
49 * <li> <code>com::sun::star::table::CellProperties</code></li>
50 * <li> <code>com::sun::star::container::XNameReplace</code></li>
51 * <li> <code>com::sun::star::container::XNameContainer</code></li>
52 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
53 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
54 * <li> <code>com::sun::star::container::XElementAccess</code></li>
55 * <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
56 * <li> <code>com::sun::star::sheet::XSheetCellRangeContainer</code></li>
57 * <li> <code>com::sun::star::sheet::XSheetOperation</code></li>
58 * <li> <code>com::sun::star::sheet::XSheetCellRanges</code></li>
59 * <li> <code>com::sun::star::container::XNameAccess</code></li>
61 * @see com.sun.star.sheet.SheetCellRanges
62 * @see com.sun.star.table.CellProperties
63 * @see com.sun.star.container.XNameReplace
64 * @see com.sun.star.container.XNameContainer
65 * @see com.sun.star.beans.XPropertySet
66 * @see com.sun.star.container.XIndexAccess
67 * @see com.sun.star.container.XElementAccess
68 * @see com.sun.star.container.XEnumerationAccess
69 * @see com.sun.star.sheet.XSheetCellRangeContainer
70 * @see com.sun.star.sheet.XSheetOperation
71 * @see com.sun.star.sheet.XSheetCellRanges
72 * @see com.sun.star.container.XNameAccess
73 * @see ifc.table._CellProperties
74 * @see ifc.container._XNameReplace
75 * @see ifc.container._XNameContainer
76 * @see ifc.beans._XPropertySet
77 * @see ifc.container._XIndexAccess
78 * @see ifc.container._XElementAccess
79 * @see ifc.container._XEnumerationAccess
80 * @see ifc.sheet._XSheetCellRangeContainer
81 * @see ifc.sheet._XSheetOperation
82 * @see ifc.sheet._XSheetCellRanges
83 * @see ifc.container._XNameAccess
85 public class ScCellRangesObj
extends TestCase
{
86 private XSpreadsheetDocument xSheetDoc
= null;
89 * Creates Spreadsheet document.
91 protected void initialize( TestParameters tParam
, PrintWriter log
) {
93 // get a soffice factory object
94 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF());
97 log
.println( "creating a sheetdocument" );
98 xSheetDoc
= SOF
.createCalcDoc(null);
99 } catch (com
.sun
.star
.uno
.Exception e
) {
100 // Some exception occurs.FAILED
101 e
.printStackTrace( log
);
102 throw new StatusException( "Couldn't create document", e
);
107 * Disposes Spreadsheet document.
109 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
110 log
.println( " disposing xSheetDoc " );
112 UnoRuntime
.queryInterface (XComponent
.class, xSheetDoc
);
113 util
.DesktopTools
.closeDoc(oComp
);
117 * Creating a Testenvironment for the interfaces to be tested.
118 * Creates an instance of the service
119 * <code>com.sun.star.sheet.SheetCellRanges</code> and fills some cells.
120 * Object relations created :
122 * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> for
123 * {@link ifc.container._XNameReplace},
124 * {@link ifc.container._XNameContainer} (type of
125 * <code>XCellRange</code>)</li>
126 * <li> <code>'THRCNT'</code> for
127 * {@link ifc.container._XNameReplace}(the number of the running threads
128 * that was retrieved from the method parameter <code>Param</code>)</li>
129 * <li> <code>'NameReplaceIndex'</code> for
130 * {@link ifc.container._XNameReplace} </li>
132 * @see com.sun.star.table.XCellRange
134 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
136 XInterface oObj
= null;
137 Object oRange
= null ;
139 // creation of testobject here
140 // first we write what we are intend to do to log file
141 log
.println( "Creating a test environment" );
143 // get a soffice factory object
144 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)Param
.getMSF() );
146 log
.println("Getting test object ");
148 XComponent oComp
= UnoRuntime
.queryInterface (XComponent
.class, xSheetDoc
);
151 SOF
.createInstance(oComp
, "com.sun.star.sheet.SheetCellRanges");
153 XSpreadsheets oSheets
= xSheetDoc
.getSheets() ;
154 XIndexAccess oIndSheets
= UnoRuntime
.queryInterface (XIndexAccess
.class, oSheets
);
155 XSpreadsheet oSheet
= null;
157 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
158 new Type(XSpreadsheet
.class), oIndSheets
.getByIndex(0));
159 XNameContainer oRanges
= UnoRuntime
.queryInterface(XNameContainer
.class, oObj
);
161 oRange
= oSheet
.getCellRangeByName("C1:D4");
162 oRanges
.insertByName("Range1", oRange
);
163 oRange
= oSheet
.getCellRangeByName("E2:F5");
164 oRanges
.insertByName("Range2", oRange
);
165 oRange
= oSheet
.getCellRangeByName("G2:H3");
166 oRanges
.insertByName("Range3", oRange
);
167 oRange
= oSheet
.getCellRangeByName("I7:J8");
168 oRanges
.insertByName("Range4", oRange
);
169 } catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
170 e
.printStackTrace(log
);
171 throw new StatusException("Couldn't create test object", e
);
172 } catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
173 e
.printStackTrace(log
);
174 throw new StatusException("Couldn't create test object", e
);
175 } catch(com
.sun
.star
.container
.ElementExistException e
) {
176 e
.printStackTrace(log
);
177 throw new StatusException("Couldn't create test object", e
);
178 } catch(com
.sun
.star
.lang
.IllegalArgumentException e
) {
179 e
.printStackTrace(log
);
180 throw new StatusException("Couldn't create test object", e
);
183 log
.println("filling some cells");
185 for (int i
= 0; i
< 10; i
++) {
186 for (int j
= 0; j
< 5; j
++) {
187 oSheet
.getCellByPosition(i
, j
).setFormula("a");
190 for (int i
= 0; i
< 10; i
++) {
191 for (int j
= 5; j
< 10; j
++) {
192 oSheet
.getCellByPosition(i
, j
).setValue(i
+ j
);
195 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
196 e
.printStackTrace(log
);
197 throw new StatusException (
198 "Exception occurred while filling cells", e
);
202 TestEnvironment tEnv
= new TestEnvironment( oObj
);
204 // NameReplaceIndex : _XNameReplace
205 log
.println( "adding NameReplaceIndex as mod relation to environment" );
206 tEnv
.addObjRelation("NameReplaceIndex", "0");
208 // INSTANCEn : _XNameContainer; _XNameReplace
209 log
.println( "adding INSTANCEn as mod relation to environment" );
212 if ((String
)Param
.get("THRCNT") != null) {
213 THRCNT
= Integer
.parseInt((String
)Param
.get("THRCNT"));
217 for (int n
= 1; n
< (THRCNT
+ 1) ; n
++) {
220 oRange
= oSheet
.getCellRangeByName("A" + a
+ ":B" + b
);
222 "adding INSTANCE" + n
+ " as mod relation to environment" );
224 tEnv
.addObjRelation("INSTANCE" + n
, oRange
);
227 XPropertySet PropSet
= UnoRuntime
.queryInterface(XPropertySet
.class, oObj
);
228 tEnv
.addObjRelation("PropSet",PropSet
);
229 tEnv
.addObjRelation("SHEET", oSheet
);
230 // add expected results for the XCellRangesQuery interface test
231 String
[]expectedResults
= new String
[7];
232 expectedResults
[_XCellRangesQuery
.QUERYCOLUMNDIFFERENCES
] = "Sheet1.I7:J8";
233 expectedResults
[_XCellRangesQuery
.QUERYCONTENTCELLS
] = "";
234 expectedResults
[_XCellRangesQuery
.QUERYEMPTYCELLS
] = "";
235 expectedResults
[_XCellRangesQuery
.QUERYFORMULACELLS
] = "";
236 expectedResults
[_XCellRangesQuery
.QUERYINTERSECTION
] = "Sheet1.D4";
237 expectedResults
[_XCellRangesQuery
.QUERYROWDIFFERENCES
] = "Sheet1.I7:J8";
238 expectedResults
[_XCellRangesQuery
.QUERYVISIBLECELLS
] = "Sheet1.C2:D4"; // first range, first line invisible
239 tEnv
.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS", expectedResults
);
241 // for XSearchable and XReplaceable interface test
242 tEnv
.addObjRelation("SEARCHSTRING", "15");
244 // for XFormulaQuery interface test
245 tEnv
.addObjRelation("EXPECTEDDEPENDENTVALUES", new int[]{4,5,1,4});
246 tEnv
.addObjRelation("EXPECTEDPRECEDENTVALUES", new int[]{4,5,1,4});
250 } // finish class ScCellRangesObj