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.container.XEnumerationAccess;
39 import com.sun.star.container.XIndexAccess;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.sheet.XCellFormatRangesSupplier;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.sheet.XSpreadsheets;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.Type;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 
51 /**
52 * Test for object which is represented by service
53 * <code>com.sun.star.sheet.CellFormatRangesEnumeration</code>. <p>
54 * Object implements the following interfaces :
55 * <ul>
56 *  <li> <code>com::sun::star::container::XEnumeration</code></li>
57 * </ul>
58 * @see com.sun.star.sheet.CellFormatRangesEnumeration
59 * @see com.sun.star.container.XEnumeration
60 * @see ifc.container._XEnumeration
61 */
62 public class ScCellFormatsEnumeration extends TestCase {
63     static XSpreadsheetDocument xSheetDoc = null;
64 
65     /**
66     * Creates Spreadsheet document.
67     */
68     protected void initialize( TestParameters tParam, PrintWriter log ) {
69         // get a soffice factory object
70         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
71 
72         try {
73             log.println( "creating a sheetdocument" );
74             xSheetDoc = SOF.createCalcDoc(null);;
75         } catch (com.sun.star.uno.Exception e) {
76             // Some exception occures.FAILED
77             e.printStackTrace( log );
78             throw new StatusException( "Couldn't create document", e);
79         }
80     }
81 
82     /**
83     * Disposes Spreadsheet document.
84     */
85     protected void cleanup( TestParameters tParam, PrintWriter log ) {
86         log.println( "    disposing xSheetDoc " );
87         XComponent oComp = (XComponent)
88             UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
89         util.DesktopTools.closeDoc(oComp);
90     }
91 
92     /**
93     * Creating a Testenvironment for the interfaces to be tested.
94     * Retrieves a collection of spreadsheets from a document,
95     * and takes one of them. Then retrieves a collection of cell format range
96     * using the interface <code>XCellFormatRangesSupplier</code>, creates the
97     * enumeration of this collection using interface <code>XEnumerationAccess</code>.
98     * This enumeration is the instance of the service
99     * <code>com.sun.star.sheet.CellFormatRangesEnumeration</code>.
100     * Object relations created :
101     * <ul>
102     *  <li> <code>'ENUM'</code> for
103     *      {@link ifc.container._XEnumeration} (type of
104     *      <code>XEnumerationAccess</code> that was queried from the collection
105     *      of cell format range)</li>
106     * </ul>
107     * @see com.sun.star.sheet.CellFormatRangesEnumeration
108     * @see com.sun.star.sheet.XCellFormatRangesSupplier
109     * @see com.sun.star.container.XEnumerationAccess
110     */
111     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
112 
113         XInterface oObj = null;
114 
115         log.println("getting sheets");
116         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
117 
118         log.println("getting a sheet");
119         XSpreadsheet oSheet = null;
120         XIndexAccess oIndexAccess = (XIndexAccess)
121             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
122 
123         try {
124             oSheet = (XSpreadsheet) AnyConverter.toObject(
125                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
126         } catch (com.sun.star.lang.WrappedTargetException e) {
127             e.printStackTrace(log);
128             throw new StatusException( "Couldn't get a spreadsheet", e);
129         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
130             e.printStackTrace(log);
131             throw new StatusException( "Couldn't get a spreadsheet", e);
132         } catch (com.sun.star.lang.IllegalArgumentException e) {
133             e.printStackTrace(log);
134             throw new StatusException( "Couldn't get a spreadsheet", e);
135         }
136 
137         log.println("getting CellFormats");
138 
139         XCellFormatRangesSupplier xCFRS = (XCellFormatRangesSupplier)
140             UnoRuntime.queryInterface(XCellFormatRangesSupplier.class,oSheet);
141         XIndexAccess formats = xCFRS.getCellFormatRanges();
142 
143         log.println("getting Enumeration");
144         XEnumerationAccess oEnum = (XEnumerationAccess)
145             UnoRuntime.queryInterface(XEnumerationAccess.class,formats);
146         oObj = oEnum.createEnumeration();
147 
148         log.println("creating a new environment for object");
149         TestEnvironment tEnv = new TestEnvironment(oObj);
150 
151         tEnv.addObjRelation("ENUM", oEnum);
152 
153         return tEnv;
154     }
155 
156 }    // finish class ScCellFormatsEnumeration
157 
158