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.beans.XPropertySet;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XNamedRanges;
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.table.CellAddress;
43 import com.sun.star.table.CellRangeAddress;
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.NamedRanges</code>. <p>
52 * Object implements the following interfaces :
53 * <ul>
54 *  <li> <code>com::sun::star::sheet::XNamedRanges</code></li>
55 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
56 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
57 * </ul>
58 * @see com.sun.star.sheet.NamedRanges
59 * @see com.sun.star.sheet.XNamedRanges
60 * @see com.sun.star.container.XNameAccess
61 * @see com.sun.star.container.XElementAccess
62 * @see ifc.sheet._XNamedRanges
63 * @see ifc.container._XNameAccess
64 * @see ifc.container._XElementAccess
65 */
66 public class ScNamedRangesObj extends TestCase {
67     static XSpreadsheetDocument xSheetDoc = null;
68 
69     /**
70     * Creates Spreadsheet document.
71     */
initialize( TestParameters tParam, PrintWriter log )72     protected void initialize( TestParameters tParam, PrintWriter log ) {
73         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
74 
75         try {
76             log.println( "creating a Spreadsheet document" );
77             xSheetDoc = SOF.createCalcDoc(null);
78         } catch ( com.sun.star.uno.Exception e ) {
79             // Some exception occures.FAILED
80             e.printStackTrace( log );
81             throw new StatusException( "Couldn't create document", e );
82         }
83     }
84 
85     /**
86     * Disposes Spreadsheet document.
87     */
cleanup( TestParameters tParam, PrintWriter log )88     protected void cleanup( TestParameters tParam, PrintWriter log ) {
89         log.println( "    disposing xSheetDoc " );
90         XComponent oComp = (XComponent)
91             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
92         util.DesktopTools.closeDoc(oComp);
93     }
94 
95     /**
96     * Creating a Testenvironment for the interfaces to be tested.
97     * Retrieves a collection of spreadsheets from a document
98     * and takes one of them. Obtains the value of the property
99     * <code>'NamedRanges'</code> that is the collection of named ranges.
100     * This collection is the instance of the service
101     * <code>com.sun.star.sheet.NamedRanges</code>. Creates and adds new range to
102     * the collection.
103     * Object relations created :
104     * <ul>
105     *  <li> <code>'SHEET'</code> for
106     *      {@link ifc.sheet._XNamedRanges} (the retrieved spreadsheet) </li>
107     * </ul>
108     * @see com.sun.star.sheet.NamedRanges
109     */
createTestEnvironment(TestParameters Param, PrintWriter log)110     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
111 
112         XInterface oObj = null;
113 
114         // creation of testobject here
115         // first we write what we are intend to do to log file
116         log.println( "Creating a test environment" );
117         XSpreadsheet oSheet = null;
118 
119         log.println("Getting test object ");
120         XSpreadsheets oSheets = xSheetDoc.getSheets();
121         XIndexAccess oIndexSheets = (XIndexAccess)
122             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
123         try {
124             oSheet = (XSpreadsheet) AnyConverter.toObject(
125                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
126         } catch (com.sun.star.lang.WrappedTargetException e) {
127             e.printStackTrace(log);
128             throw new StatusException( "Couldn't get a spreadsheet", e);
129         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
130             e.printStackTrace(log);
131             throw new StatusException( "Couldn't get a spreadsheet", e);
132         } catch (com.sun.star.lang.IllegalArgumentException e) {
133             e.printStackTrace(log);
134             throw new StatusException( "Couldn't get a spreadsheet", e);
135         }
136 
137         // Getting named ranges.
138         XPropertySet docProps = (XPropertySet)
139             UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
140         Object ranges = null;
141         try {
142             ranges = docProps.getPropertyValue("NamedRanges");
143         } catch(com.sun.star.lang.WrappedTargetException e){
144             e.printStackTrace(log);
145             throw new StatusException("Couldn't get NamedRanges", e);
146         } catch(com.sun.star.beans.UnknownPropertyException e){
147             e.printStackTrace(log);
148             throw new StatusException("Couldn't get NamedRanges", e);
149         }
150 
151         XNamedRanges xNamedRanges = (XNamedRanges)
152             UnoRuntime.queryInterface(XNamedRanges.class, ranges);
153 
154         CellRangeAddress DataArea = new CellRangeAddress((short)0, 0, 0, 2, 2);
155         CellAddress base = new CellAddress(DataArea.Sheet,
156                                            DataArea.StartColumn,
157                                            DataArea.StartRow);
158 
159         xNamedRanges.addNewByName("ANamedRange", "A1:B2", base, 0);
160 
161         CellAddress listOutputPosition = new CellAddress((short)0, 1, 1);
162         xNamedRanges.outputList(listOutputPosition);
163 
164         oObj = xNamedRanges;
165 
166         TestEnvironment tEnv = new TestEnvironment( oObj );
167 
168         // Other parameters required for interface tests
169         tEnv.addObjRelation("SHEET", oSheet);
170 
171         return tEnv;
172     }
173 
174 }
175