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.container.XIndexAccess;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sheet.XCellFormatRangesSupplier;
38 import com.sun.star.sheet.XSpreadsheet;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.sheet.XSpreadsheets;
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.CellFormatRanges</code>. <p>
49 * Object implements the following interfaces :
50 * <ul>
51 *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
52 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
53 * </ul>
54 * @see com.sun.star.sheet.CellFormatRanges
55 * @see com.sun.star.container.XEnumerationAccess
56 * @see com.sun.star.container.XElementAccess
57 * @see ifc.container._XEnumerationAccess
58 * @see ifc.container._XElementAccess
59 */
60 public class ScCellFormatsObj extends TestCase {
61     static XSpreadsheetDocument xSheetDoc = null;
62 
63     /**
64     * Creates Spreadsheet document.
65     */
initialize( TestParameters tParam, PrintWriter log )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 occured.FAILED
75             e.printStackTrace( log );
76             throw new StatusException( "Couldn't create document", e );
77         }
78     }
79 
80     /**
81     * Disposes Spreadsheet document.
82     */
cleanup( TestParameters tParam, PrintWriter log )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     * Retrieves a collection of spreadsheets from a document,
93     * and takes one of them. Then retrieves a collection of format cell
94     * range using the interface <code>XCellFormatRangesSupplier</code> that is
95     * instance of service  <code>com.sun.star.sheet.CellFormatRanges</code>.
96     * @see com.sun.star.sheet.CellFormatRanges
97     * @see com.sun.star.sheet.XCellFormatRangesSupplier
98     */
createTestEnvironment(TestParameters Param, PrintWriter log)99     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
100 
101         XInterface oObj = null;
102 
103         log.println("getting sheets");
104         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
105 
106         log.println("getting a sheet");
107         XSpreadsheet oSheet = null;
108         XIndexAccess oIndexAccess = (XIndexAccess)
109             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
110         try {
111             oSheet = (XSpreadsheet) AnyConverter.toObject(
112                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
113         } catch (com.sun.star.lang.WrappedTargetException e) {
114             e.printStackTrace(log);
115             throw new StatusException( "Couldn't get a spreadsheet", e);
116         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
117             e.printStackTrace(log);
118             throw new StatusException( "Couldn't get a spreadsheet", e);
119         } catch (com.sun.star.lang.IllegalArgumentException e) {
120             e.printStackTrace(log);
121             throw new StatusException( "Couldn't get a spreadsheet", e);
122         }
123 
124         log.println("getting CellFormats");
125         XCellFormatRangesSupplier xCFRS = (XCellFormatRangesSupplier)
126             UnoRuntime.queryInterface(XCellFormatRangesSupplier.class, oSheet);
127         XIndexAccess formats = xCFRS.getCellFormatRanges();
128 
129         oObj = formats;
130 
131         log.println("creating a new environment for object");
132         TestEnvironment tEnv = new TestEnvironment(oObj);
133 
134         return tEnv;
135     }
136 }    // finish class ScCellFormatsObj
137 
138