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