1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package mod._sc;
28 
29 import java.io.PrintWriter;
30 
31 import lib.StatusException;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
35 import util.SOfficeFactory;
36 
37 import com.sun.star.container.XEnumerationAccess;
38 import com.sun.star.container.XIndexAccess;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.sheet.XCellRangesQuery;
42 import com.sun.star.sheet.XSheetCellRanges;
43 import com.sun.star.sheet.XSpreadsheetDocument;
44 import com.sun.star.sheet.XSpreadsheets;
45 import com.sun.star.table.XCell;
46 import com.sun.star.table.XCellRange;
47 import com.sun.star.text.XTextRange;
48 import com.sun.star.uno.AnyConverter;
49 import com.sun.star.uno.Type;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XInterface;
52 
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.sheet.CellsEnumeration</code>. <p>
56 * Object implements the following interfaces :
57 * <ul>
58 *  <li> <code>com::sun::star::container::XEnumeration</code></li>
59 * </ul>
60 * @see com.sun.star.sheet.CellsEnumeration
61 * @see com.sun.star.container.XEnumeration
62 * @see ifc.container._XEnumeration
63 */
64 public class ScCellsEnumeration extends TestCase {
65     static XSpreadsheetDocument xSheetDoc = null;
66 
67     /**
68     * Creates Spreadsheet document.
69     */
70     protected void initialize( TestParameters tParam, PrintWriter log ) {
71 
72         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
73 
74         try {
75             log.println( "creating a Spreadsheet document" );
76             xSheetDoc = SOF.createCalcDoc(null);
77         } catch ( com.sun.star.uno.Exception e ) {
78             // Some exception occures.FAILED
79             e.printStackTrace( log );
80             throw new StatusException( "Couldn't create document", e );
81         }
82     }
83 
84     /**
85     * Disposes Spreadsheet document.
86     */
87     protected void cleanup( TestParameters tParam, PrintWriter log ) {
88         log.println( "    disposing xSheetDoc " );
89         XComponent oComp = (XComponent)
90             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
91         util.DesktopTools.closeDoc(oComp);
92     }
93 
94     /**
95     * Creating a Testenvironment for the interfaces to be tested.
96     * Retrieves a collection of spreadsheets from a document
97     * and takes one of them. Replaces text of some cells.
98     * Retrives a cell range of the visible cells using the interface
99     * <code>XCellRangesQuery</code>. Retrieves a collection of cells from
100     * this cell range and creates it's enumeration using the interface
101     * <code>XEnumerationAccess</code>.The created enumeration is the instance
102     * of the service <code>com.sun.star.sheet.CellsEnumeration</code>.
103     * Object relations created :
104     * <ul>
105     *  <li> <code>'ENUM'</code> for
106     *      {@link ifc.container._XEnumeration} (type of
107     *      <code>XEnumerationAccess</code> that was retrieved from the
108     *      collection of visible cells)</li>
109     * </ul>
110     * @see com.sun.star.sheet.XCellRangesQuery
111     * @see com.sun.star.container.XEnumerationAccess
112     */
113     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
114 
115         XInterface oObj = null;
116         Object cellArr[] = new Object[3] ;
117         XEnumerationAccess oEnum = null;
118 
119         // creation of testobject here
120         XSpreadsheets oSheets = (XSpreadsheets)xSheetDoc.getSheets();
121         XIndexAccess oIndexAccess = (XIndexAccess)
122             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
123         XCellRange oSheet = null;
124         try {
125             oSheet = (XCellRange) AnyConverter.toObject(
126                     new Type(XCellRange.class),oIndexAccess.getByIndex(0));
127 
128             XCell oCell_1 = (XCell)oSheet.getCellByPosition(0, 0);
129             XTextRange oTextRange = (XTextRange)
130                 UnoRuntime.queryInterface(XTextRange.class, oCell_1);
131             oTextRange.setString("Test string 1");
132 
133             XCell oCell_2 = (XCell)oSheet.getCellByPosition(5, 1);
134             oCell_2.setValue(15);
135 
136             XCell oCell_3 = (XCell)oSheet.getCellByPosition(3, 9);
137             oTextRange = (XTextRange)
138                 UnoRuntime.queryInterface(XTextRange.class, oCell_3);
139             oTextRange.setString("test 2");
140             cellArr[0] = oCell_1;
141             cellArr[1] = oCell_2;
142             cellArr[2] = oCell_3;
143         } catch(com.sun.star.lang.WrappedTargetException 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.IndexOutOfBoundsException 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         } catch(com.sun.star.lang.IllegalArgumentException e) {
152             log.println ("Exception occured while creating test Object.");
153             e.printStackTrace(log);
154             throw new StatusException("Couldn't create test object", e);
155         }
156 
157         XCellRangesQuery oCellRangesQuery = (XCellRangesQuery)
158             UnoRuntime.queryInterface(XCellRangesQuery.class, oSheet);
159         XSheetCellRanges oSheetCellRanges = oCellRangesQuery.queryVisibleCells();
160         oEnum = (XEnumerationAccess) oSheetCellRanges.getCells();
161         oObj = oSheetCellRanges.getCells().createEnumeration();
162 
163         TestEnvironment tEnv = new TestEnvironment(oObj);
164 
165         // ENUM: XEnumeration
166         tEnv.addObjRelation("ENUM", oEnum);
167 
168         return tEnv;
169     }
170 
171 }    // finish class ScCellsEnumeration
172 
173