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.XDatabaseRanges;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.table.CellRangeAddress;
40 import com.sun.star.uno.AnyConverter;
41 import com.sun.star.uno.Type;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 
45 /**
46 * Test for object which is represented by service
47 * <code>com.sun.star.sheet.DatabaseRanges</code>. <p>
48 * Object implements the following interfaces :
49 * <ul>
50 *  <li> <code>com::sun::star::sheet::XDatabaseRanges</code></li>
51 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
52 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
53 * </ul>
54 * @see com.sun.star.sheet.DatabaseRanges
55 * @see com.sun.star.sheet.XDatabaseRanges
56 * @see com.sun.star.container.XNameAccess
57 * @see com.sun.star.container.XElementAccess
58 * @see ifc.sheet._XDatabaseRanges
59 * @see ifc.container._XNameAccess
60 * @see ifc.container._XElementAccess
61 */
62 public class ScDatabaseRangesObj extends TestCase {
63     static XSpreadsheetDocument xSheetDoc = null;
64 
65     /**
66     * Creates Spreadsheet document.
67     */
68     protected void initialize( TestParameters tParam, PrintWriter log ) {
69         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
70 
71         try {
72             log.println( "creating a Spreadsheet document" );
73             xSheetDoc = SOF.createCalcDoc(null);
74         } catch ( com.sun.star.uno.Exception e ) {
75             // Some exception occures.FAILED
76             e.printStackTrace( log );
77             throw new StatusException( "Couldn't create document", e );
78         }
79 
80     }
81 
82     /**
83     * Disposes Spreadsheet document.
84     */
85     protected void cleanup( TestParameters tParam, PrintWriter log ) {
86         log.println( "    disposing xSheetDoc " );
87         XComponent oComp = (XComponent)
88             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
89         util.DesktopTools.closeDoc(oComp);
90     }
91 
92     /**
93     * Creating a Testenvironment for the interfaces to be tested.
94     * Retrieves the collection of database ranges in the document.
95     * If the database range with name <code>'dbRange'</code> doesn't exist
96     * in the collection then creates new database range and adds it to the
97     * collection with the name <code>'dbRange'</code> to have one element
98     * for the test of the interface <code>ElementAccess</code> at least.
99     * The collection of database ranges is the instance of the service
100     * <code>com.sun.star.sheet.DatabaseRanges</code>.
101     * @see com.sun.star.sheet.DatabaseRanges
102     * @see com.sun.star.container.XElementAccess
103     */
104     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
105 
106         XInterface oObj = null;
107 
108         // creation of testobject here
109         // first we write what we are intend to do to log file
110         log.println( "Creating a test environment" );
111 
112         log.println("Getting test object ") ;
113         XPropertySet docProps = (XPropertySet)
114             UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
115 
116         XDatabaseRanges dbRanges = null;
117         try {
118             dbRanges = (XDatabaseRanges) AnyConverter.toObject(
119                 new Type(XDatabaseRanges.class),
120                     docProps.getPropertyValue("DatabaseRanges"));
121         } catch (com.sun.star.lang.WrappedTargetException e) {
122             e.printStackTrace(log) ;
123             throw new StatusException(
124                 "Error getting test object from spreadsheet document",e) ;
125         } catch (com.sun.star.beans.UnknownPropertyException e) {
126             e.printStackTrace(log) ;
127             throw new StatusException(
128                 "Error getting test object from spreadsheet document",e) ;
129         } catch (com.sun.star.lang.IllegalArgumentException e) {
130             e.printStackTrace(log) ;
131             throw new StatusException(
132                 "Error getting test object from spreadsheet document",e) ;
133         }
134 
135         log.println("Adding at least one element for ElementAccess interface");
136         CellRangeAddress aRange = new CellRangeAddress((short)0, 2, 4, 5, 6);
137         if (!dbRanges.hasByName("dbRange")) {
138             dbRanges.addNewByName("dbRange", aRange);
139         }
140 
141         oObj = dbRanges;
142         TestEnvironment tEnv = new TestEnvironment( oObj );
143 
144         // Other parameters required for interface tests
145         return tEnv;
146     }
147 
148 }
149 
150 
151