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