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 
24 package mod._sc;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 
34 import com.sun.star.container.XEnumerationAccess;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.container.XNameContainer;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.sheet.XSpreadsheet;
40 import com.sun.star.sheet.XSpreadsheetDocument;
41 import com.sun.star.sheet.XSpreadsheets;
42 import com.sun.star.uno.AnyConverter;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
46 
47 public class ScIndexEnumeration_SheetCellRangesEnumeration extends TestCase {
48     static XSpreadsheetDocument xSheetDoc = null;
49 
50     /**
51     * Creates Spreadsheet document.
52     */
initialize( TestParameters tParam, PrintWriter log )53     protected void initialize( TestParameters tParam, PrintWriter log ) {
54 
55         // get a soffice factory object
56         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
57 
58         try {
59             log.println( "creating a sheetdocument" );
60             xSheetDoc = SOF.createCalcDoc(null);
61         } catch (com.sun.star.uno.Exception e) {
62             // Some exception occured.FAILED
63             e.printStackTrace( log );
64             throw new StatusException( "Couldn't create document", e );
65         }
66     }
67 
68     /**
69     * Disposes Spreadsheet document.
70     */
cleanup( TestParameters tParam, PrintWriter log )71     protected void cleanup( TestParameters tParam, PrintWriter log ) {
72         log.println( "    disposing xSheetDoc " );
73         XComponent oComp =
74             (XComponent) UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
75         util.DesktopTools.closeDoc(oComp);
76     }
77 
createTestEnvironment(TestParameters Param, PrintWriter log)78     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
79 
80         XInterface oObj = null;
81         Object oRange = null ;
82 
83         // creation of testobject here
84         // first we write what we are intend to do to log file
85         log.println( "Creating a test environment" );
86 
87         // get a soffice factory object
88         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF() );
89 
90         log.println("Getting test object ");
91 
92         XComponent oComp = (XComponent)
93                     UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
94 
95         oObj = (XInterface)
96             SOF.createInstance(oComp, "com.sun.star.sheet.SheetCellRanges");
97 
98         XSpreadsheets oSheets = xSheetDoc.getSheets() ;
99         XIndexAccess oIndSheets = (XIndexAccess)
100             UnoRuntime.queryInterface (XIndexAccess.class, oSheets);
101         XSpreadsheet oSheet = null;
102         try {
103             oSheet = (XSpreadsheet) AnyConverter.toObject(
104                     new Type(XSpreadsheet.class),oIndSheets.getByIndex(0));
105             XNameContainer oRanges = (XNameContainer)
106                 UnoRuntime.queryInterface(XNameContainer.class, oObj);
107 
108             oRange = oSheet.getCellRangeByName("C1:D4");
109             oRanges.insertByName("Range1", oRange);
110             oRange = oSheet.getCellRangeByName("E2:F5");
111             oRanges.insertByName("Range2", oRange);
112             oRange = oSheet.getCellRangeByName("G2:H3");
113             oRanges.insertByName("Range3", oRange);
114             oRange = oSheet.getCellRangeByName("I7:J8");
115             oRanges.insertByName("Range4", oRange);
116         } catch(com.sun.star.lang.WrappedTargetException e) {
117             e.printStackTrace(log);
118             throw new StatusException("Couldn't create test object", e);
119         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
120             e.printStackTrace(log);
121             throw new StatusException("Couldn't create test object", e);
122         } catch(com.sun.star.container.ElementExistException e) {
123             e.printStackTrace(log);
124             throw new StatusException("Couldn't create test object", e);
125         } catch(com.sun.star.lang.IllegalArgumentException e) {
126             e.printStackTrace(log);
127             throw new StatusException("Couldn't create test object", e);
128         }
129 
130         log.println("filling some cells");
131         try {
132             for (int i = 0; i < 10; i++) {
133                 for (int j = 0; j < 5; j++) {
134                     oSheet.getCellByPosition(i, j).setFormula("a");
135                 }
136             }
137             for (int i = 0; i < 10; i++) {
138                 for (int j = 5; j < 10; j++) {
139                     oSheet.getCellByPosition(i, j).setValue(i + j);
140                 }
141             }
142         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
143             e.printStackTrace(log);
144             throw new StatusException (
145                 "Exception occurred while filling cells", e);
146         }
147 
148         XEnumerationAccess ea = (XEnumerationAccess)
149                     UnoRuntime.queryInterface(XEnumerationAccess.class,oObj);
150 
151         oObj = ea.createEnumeration();
152 
153         log.println("ImplementationName: "+util.utils.getImplName(oObj));
154         // creating test environment
155         TestEnvironment tEnv = new TestEnvironment( oObj );
156 
157         tEnv.addObjRelation("ENUM",ea);
158 
159         return tEnv ;
160     }
161 
162 }
163 
164