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.XLabelRange;
38 import com.sun.star.sheet.XLabelRanges;
39 import com.sun.star.sheet.XSpreadsheetDocument;
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.LabelRange</code>. <p>
49 * Object implements the following interfaces :
50 * <ul>
51 *  <li> <code>com::sun::star::sheet::XLabelRange</code></li>
52 * </ul>
53 * @see com.sun.star.sheet.LabelRange
54 * @see com.sun.star.sheet.XLabelRange
55 * @see ifc.sheet._XLabelRange
56 */
57 public class ScLabelRangeObj extends TestCase {
58     static XSpreadsheetDocument xSheetDoc = null;
59 
60     /**
61     * Creates Spreadsheet document.
62     */
initialize( TestParameters tParam, PrintWriter log )63     protected void initialize( TestParameters tParam, PrintWriter log ) {
64         // get a soffice factory object
65         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
66 
67         try {
68             log.println( "creating a sheetdocument" );
69             xSheetDoc = SOF.createCalcDoc(null);
70         } catch (com.sun.star.uno.Exception e) {
71             // Some exception occures.FAILED
72             e.printStackTrace( log );
73             throw new StatusException( "Couldn't create document", e );
74         }
75     }
76 
77     /**
78     * Disposes Spreadsheet document.
79     */
cleanup( TestParameters tParam, PrintWriter log )80     protected void cleanup( TestParameters tParam, PrintWriter log ) {
81         log.println( "    disposing xSheetDoc " );
82         XComponent oComp = (XComponent)
83             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
84         util.DesktopTools.closeDoc(oComp);
85     }
86 
87     /**
88     * Creating a Testenvironment for the interfaces to be tested.
89     * Obtains the value of the property <code>'ColumnLabelRanges'</code>
90     * from the document. The property value is the collection of label ranges.
91     * Adds new label range to the collection using the interface
92     * <code>XLabelRanges</code> that was queried from the property value.
93     * Retrieved from the collection the label range with index 0.
94     * The retrieved label range is the instance of the service
95     * <code>com.sun.star.sheet.LabelRange</code>.
96     * @see com.sun.star.sheet.LabelRange
97     * @see com.sun.star.sheet.XLabelRanges
98     */
createTestEnvironment(TestParameters Param, PrintWriter log)99     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
100 
101         XInterface oObj = null;
102 
103         // creation of testobject here
104         // first we write what we are intend to do to log file
105         log.println( "Creating a test environment" );
106 
107         try {
108             log.println("Getting test object ") ;
109             XPropertySet docProps = (XPropertySet)
110                 UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
111             Object ranges = docProps.getPropertyValue("ColumnLabelRanges");
112             XLabelRanges lRanges = (XLabelRanges)
113                 UnoRuntime.queryInterface(XLabelRanges.class, ranges);
114 
115             log.println("Adding at least one element for ElementAccess interface");
116             CellRangeAddress aRange2 = new CellRangeAddress((short)0, 0, 1, 0, 6);
117             CellRangeAddress aRange1 = new CellRangeAddress((short)0, 0, 0, 0, 1);
118             lRanges.addNew(aRange1, aRange2);
119 
120             oObj = (XLabelRange) AnyConverter.toObject(
121                         new Type(XLabelRange.class),lRanges.getByIndex(0));
122         } catch (com.sun.star.lang.WrappedTargetException e) {
123             e.printStackTrace(log) ;
124             throw new StatusException(
125                 "Error getting test object from spreadsheet document",e) ;
126         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
127             e.printStackTrace(log) ;
128             throw new StatusException(
129                 "Error getting test object from spreadsheet document",e) ;
130         } catch (com.sun.star.beans.UnknownPropertyException e) {
131             e.printStackTrace(log) ;
132             throw new StatusException(
133                 "Error getting test object from spreadsheet document",e) ;
134         } catch (com.sun.star.lang.IllegalArgumentException e) {
135             e.printStackTrace(log) ;
136             throw new StatusException(
137                 "Error getting test object from spreadsheet document",e) ;
138         }
139 
140         log.println("creating a new environment for object");
141         TestEnvironment tEnv = new TestEnvironment(oObj);
142 
143         log.println("testing...");
144 
145         return tEnv;
146 
147     } // finish method getTestEnvironment
148 
149 }    // finish class ScLabelRangeObj
150 
151