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