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.GeneralFunction;
38 import com.sun.star.sheet.SubTotalColumn;
39 import com.sun.star.sheet.XSpreadsheet;
40 import com.sun.star.sheet.XSpreadsheetDocument;
41 import com.sun.star.sheet.XSpreadsheets;
42 import com.sun.star.sheet.XSubTotalCalculatable;
43 import com.sun.star.sheet.XSubTotalDescriptor;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
48 
49 
50 /**
51 * Test for object which is represented by service
52 * <code>com.sun.star.sheet.SubTotalDescriptor</code>. <p>
53 * Object implements the following interfaces :
54 * <ul>
55 *  <li> <code>com::sun::star::sheet::XSubTotalDescriptor</code></li>
56 *  <li> <code>com::sun::star::sheet::SubTotalDescriptor</code></li>
57 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
58 * </ul>
59 * @see com.sun.star.sheet.XSubTotalDescriptor
60 * @see com.sun.star.sheet.SubTotalDescriptor
61 * @see com.sun.star.beans.XPropertySet
62 * @see ifc.sheet._XSubTotalDescriptor
63 * @see ifc.sheet._SubTotalDescriptor
64 * @see ifc.beans._XPropertySet
65 */
66 public class ScSubTotalDescriptorBase extends TestCase {
67     public static XSpreadsheetDocument xSpreadsheetDoc;
68 
69     /**
70     * Creates Spreadsheet document.
71     */
initialize( TestParameters Param, PrintWriter log )72     public void initialize( TestParameters Param, PrintWriter log ) {
73         // creation of the testobject here
74         // first we write what we are intend to do to log file
75         log.println("creating a test environment");
76 
77         // get a soffice factory object
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
79 
80         try {
81             log.println("creating a spreadsheetdocument");
82             xSpreadsheetDoc = SOF.createCalcDoc(null);
83         } catch (com.sun.star.uno.Exception e) {
84             e.printStackTrace( log );
85             throw new StatusException( "Couldn't create document ", e );
86         }
87     }
88 
89     /**
90     * Disposes Spreadsheet document.
91     */
cleanup( TestParameters tParam, PrintWriter log )92     protected void cleanup( TestParameters tParam, PrintWriter log ) {
93         log.println( "    disposing xSheetDoc " );
94         XComponent oComp = (XComponent)
95             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
96         util.DesktopTools.closeDoc(oComp);
97     }
98 
99     /**
100     * Creating a Testenvironment for the interfaces to be tested.
101     * Retrieves a collection of spreadsheets from the document and takes one of
102     * them. Creates a subtotal descriptor using the interface
103     * <code>XSubTotalCalculatable</code>. This descriptor is the instance of the
104     * service <code>com.sun.star.sheet.SubTotalDescriptor</code>.
105     * @see com.sun.star.sheet.XSubTotalCalculatable
106     * @see com.sun.star.sheet.SubTotalDescriptor
107     */
createTestEnvironment(TestParameters Param, PrintWriter log)108     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
109 
110         log.println("getting sheets");
111         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
112 
113         log.println("getting a sheet");
114         XSpreadsheet oSheet = null;
115         XIndexAccess oIndexAccess = (XIndexAccess)
116             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
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         XSubTotalCalculatable xSTC = (XSubTotalCalculatable)
132             UnoRuntime.queryInterface(XSubTotalCalculatable.class, oSheet);
133 
134         SubTotalColumn[] columns = new SubTotalColumn[1];
135         SubTotalColumn column = new SubTotalColumn();
136         column.Column = 3;
137         column.Function = GeneralFunction.SUM;
138         columns[0] = column;
139 
140         XSubTotalDescriptor desc = xSTC.createSubTotalDescriptor(true);
141         desc.addNew(columns, 1);
142 
143         XInterface oObj = desc;
144 
145         TestEnvironment tEnv = new TestEnvironment(oObj);
146         return tEnv;
147 
148     } // finish method getTestEnvironment
149 
150 }    // finish class ScSubTotalDescriptorBase
151