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