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