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.XSheetFilterable;
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.SheetFilterDescriptor</code>. <p>
49 * Object implements the following interfaces :
50 * <ul>
51 *  <li> <code>com::sun::star::sheet::XSheetFilterDescriptor</code></li>
52 *  <li> <code>com::sun::star::sheet::SheetFilterDescriptor</code></li>
53 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
54 * </ul>
55 * @see com.sun.star.sheet.SheetFilterDescriptor
56 * @see com.sun.star.sheet.XSheetFilterDescriptor
57 * @see com.sun.star.sheet.SheetFilterDescriptor
58 * @see com.sun.star.beans.XPropertySet
59 * @see ifc.sheet._XSheetFilterDescriptor
60 * @see ifc.sheet._SheetFilterDescriptor
61 * @see ifc.beans._XPropertySet
62 */
63 public class ScFilterDescriptorBase extends TestCase {
64     static XSpreadsheetDocument xSheetDoc = null;
65 
66     /**
67     * Creates Spreadsheet document.
68     */
initialize( TestParameters tParam, PrintWriter log )69     protected void initialize( TestParameters tParam, PrintWriter log ) {
70 
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     */
cleanup( TestParameters tParam, PrintWriter log )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     * Retrieves a collection of spreadsheets from a document
97     * and takes one of them. Fills some cells in the spreadsheet.
98     * Creates the filter descriptor using the interface
99     * <code>XSheetFilterable</code>. This filter descriptor is the instance
100     * of the service <code>com.sun.star.sheet.SheetFilterDescriptor</code>.
101     * @see com.sun.star.sheet.XSheetFilterable
102     * @see com.sun.star.sheet.SheetFilterDescriptor
103     */
createTestEnvironment(TestParameters Param, PrintWriter log)104     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
105 
106         XInterface oObj = null;
107         XSheetFilterable SF = null;
108 
109         log.println("getting sheets");
110         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
111 
112         log.println("getting a sheet");
113         XSpreadsheet oSheet = null;
114         XIndexAccess oIndexAccess = (XIndexAccess)
115             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
116 
117         try {
118             oSheet = (XSpreadsheet) AnyConverter.toObject(
119                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
120         } catch (com.sun.star.lang.WrappedTargetException e) {
121             e.printStackTrace(log);
122             throw new StatusException("Couldn't get a spreadsheet", e);
123         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
124             e.printStackTrace(log);
125             throw new StatusException("Couldn't get a spreadsheet", e);
126         } catch (com.sun.star.lang.IllegalArgumentException e) {
127             e.printStackTrace(log);
128             throw new StatusException("Couldn't get a spreadsheet", e);
129         }
130 
131         log.println("filling some cells");
132         try {
133             oSheet.getCellByPosition(5, 5).setValue(15);
134             oSheet.getCellByPosition(1, 4).setValue(10);
135             oSheet.getCellByPosition(2, 0).setValue(-5.15);
136         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
137             e.printStackTrace(log);
138             throw new StatusException(
139                 "Exception occurred while filling cells", e);
140         }
141 
142         SF = (XSheetFilterable)
143             UnoRuntime.queryInterface(XSheetFilterable.class, oSheet);
144 
145         oObj = SF.createFilterDescriptor(true);
146 
147         log.println("creating a new environment for object");
148         TestEnvironment tEnv = new TestEnvironment(oObj);
149 
150         return tEnv;
151 
152     } // finish method getTestEnvironment
153 
154 }    // finish class ScFilterDescriptorBase
155 
156