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 package mod._sc;
24 
25 import java.io.PrintWriter;
26 
27 import lib.StatusException;
28 import lib.TestCase;
29 import lib.TestEnvironment;
30 import lib.TestParameters;
31 import util.SOfficeFactory;
32 
33 import com.sun.star.container.XEnumerationAccess;
34 import com.sun.star.container.XIndexAccess;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sheet.XCellRangesQuery;
38 import com.sun.star.sheet.XSheetCellRanges;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.sheet.XSpreadsheets;
41 import com.sun.star.table.XCell;
42 import com.sun.star.table.XCellRange;
43 import com.sun.star.text.XTextRange;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
48 
49 /**
50 * Test for object which is represented by service
51 * <code>com.sun.star.sheet.CellsEnumeration</code>. <p>
52 * Object implements the following interfaces :
53 * <ul>
54 *  <li> <code>com::sun::star::container::XEnumeration</code></li>
55 * </ul>
56 * @see com.sun.star.sheet.CellsEnumeration
57 * @see com.sun.star.container.XEnumeration
58 * @see ifc.container._XEnumeration
59 */
60 public class ScCellsEnumeration extends TestCase {
61     static XSpreadsheetDocument xSheetDoc = null;
62 
63     /**
64     * Creates Spreadsheet document.
65     */
initialize( TestParameters tParam, PrintWriter log )66     protected void initialize( TestParameters tParam, PrintWriter log ) {
67 
68         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
69 
70         try {
71             log.println( "creating a Spreadsheet document" );
72             xSheetDoc = SOF.createCalcDoc(null);
73         } catch ( com.sun.star.uno.Exception e ) {
74             // Some exception occures.FAILED
75             e.printStackTrace( log );
76             throw new StatusException( "Couldn't create document", e );
77         }
78     }
79 
80     /**
81     * Disposes Spreadsheet document.
82     */
cleanup( TestParameters tParam, PrintWriter log )83     protected void cleanup( TestParameters tParam, PrintWriter log ) {
84         log.println( "    disposing xSheetDoc " );
85         XComponent oComp = (XComponent)
86             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
87         util.DesktopTools.closeDoc(oComp);
88     }
89 
90     /**
91     * Creating a Testenvironment for the interfaces to be tested.
92     * Retrieves a collection of spreadsheets from a document
93     * and takes one of them. Replaces text of some cells.
94     * Retrives a cell range of the visible cells using the interface
95     * <code>XCellRangesQuery</code>. Retrieves a collection of cells from
96     * this cell range and creates it's enumeration using the interface
97     * <code>XEnumerationAccess</code>.The created enumeration is the instance
98     * of the service <code>com.sun.star.sheet.CellsEnumeration</code>.
99     * Object relations created :
100     * <ul>
101     *  <li> <code>'ENUM'</code> for
102     *      {@link ifc.container._XEnumeration} (type of
103     *      <code>XEnumerationAccess</code> that was retrieved from the
104     *      collection of visible cells)</li>
105     * </ul>
106     * @see com.sun.star.sheet.XCellRangesQuery
107     * @see com.sun.star.container.XEnumerationAccess
108     */
createTestEnvironment(TestParameters Param, PrintWriter log)109     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
110 
111         XInterface oObj = null;
112         Object cellArr[] = new Object[3] ;
113         XEnumerationAccess oEnum = null;
114 
115         // creation of testobject here
116         XSpreadsheets oSheets = (XSpreadsheets)xSheetDoc.getSheets();
117         XIndexAccess oIndexAccess = (XIndexAccess)
118             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
119         XCellRange oSheet = null;
120         try {
121             oSheet = (XCellRange) AnyConverter.toObject(
122                     new Type(XCellRange.class),oIndexAccess.getByIndex(0));
123 
124             XCell oCell_1 = (XCell)oSheet.getCellByPosition(0, 0);
125             XTextRange oTextRange = (XTextRange)
126                 UnoRuntime.queryInterface(XTextRange.class, oCell_1);
127             oTextRange.setString("Test string 1");
128 
129             XCell oCell_2 = (XCell)oSheet.getCellByPosition(5, 1);
130             oCell_2.setValue(15);
131 
132             XCell oCell_3 = (XCell)oSheet.getCellByPosition(3, 9);
133             oTextRange = (XTextRange)
134                 UnoRuntime.queryInterface(XTextRange.class, oCell_3);
135             oTextRange.setString("test 2");
136             cellArr[0] = oCell_1;
137             cellArr[1] = oCell_2;
138             cellArr[2] = oCell_3;
139         } catch(com.sun.star.lang.WrappedTargetException e) {
140             log.println ("Exception occured while creating test Object.");
141             e.printStackTrace(log);
142             throw new StatusException("Couldn't create test object", e);
143         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
144             log.println ("Exception occured while creating test Object.");
145             e.printStackTrace(log);
146             throw new StatusException("Couldn't create test object", e);
147         } catch(com.sun.star.lang.IllegalArgumentException e) {
148             log.println ("Exception occured while creating test Object.");
149             e.printStackTrace(log);
150             throw new StatusException("Couldn't create test object", e);
151         }
152 
153         XCellRangesQuery oCellRangesQuery = (XCellRangesQuery)
154             UnoRuntime.queryInterface(XCellRangesQuery.class, oSheet);
155         XSheetCellRanges oSheetCellRanges = oCellRangesQuery.queryVisibleCells();
156         oEnum = (XEnumerationAccess) oSheetCellRanges.getCells();
157         oObj = oSheetCellRanges.getCells().createEnumeration();
158 
159         TestEnvironment tEnv = new TestEnvironment(oObj);
160 
161         // ENUM: XEnumeration
162         tEnv.addObjRelation("ENUM", oEnum);
163 
164         return tEnv;
165     }
166 
167 }    // finish class ScCellsEnumeration
168 
169