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