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.sheet.XSubTotalField;
45 import com.sun.star.uno.AnyConverter;
46 import com.sun.star.uno.Type;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 
50 /**
51 * Test for object which is represented by service
52 * <code>com.sun.star.sheet.SubTotalField</code>. <p>
53 * Object implements the following interfaces :
54 * <ul>
55 *  <li> <code>com::sun::star::sheet::XSubTotalField</code></li>
56 * </ul>
57 * @see com.sun.star.sheet.SubTotalField
58 * @see com.sun.star.sheet.XSubTotalField
59 * @see ifc.sheet._XSubTotalField
60 */
61 public class ScSubTotalFieldObj extends TestCase {
62     public static XSpreadsheetDocument xSpreadsheetDoc;
63 
64     /**
65     * Creates Spreadsheet document.
66     */
initialize( TestParameters Param, PrintWriter log )67     public void initialize( TestParameters Param, PrintWriter log ) {
68         // creation of the testobject here
69         // first we write what we are intend to do to log file
70         log.println("creating a test environment");
71 
72         // get a soffice factory object
73         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
74 
75         try {
76             log.println("creating a spreadsheetdocument");
77             xSpreadsheetDoc = SOF.createCalcDoc(null);
78         } catch (com.sun.star.uno.Exception e) {
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, xSpreadsheetDoc) ;
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 the document and takes one of
97     * them. Creates a subtotal descriptor using the interface
98     * <code>XSubTotalCalculatable</code>. Adds a subtotal field definition to
99     * the descriptor. Obtains the subtotal field with index 0 from the
100     * collection. The obtained subtotal field is the instance of the service
101     * <code>com.sun.star.sheet.SubTotalField</code>.
102     * @see com.sun.star.sheet.XSubTotalCalculatable
103     * @see com.sun.star.sheet.SubTotalField
104     */
createTestEnvironment(TestParameters Param, PrintWriter log)105     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
106 
107         log.println("getting sheets");
108         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
109 
110         log.println("getting a sheet");
111         XSpreadsheet oSheet = null;
112         XIndexAccess oIndexAccess = (XIndexAccess)
113             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
114         try {
115             oSheet = (XSpreadsheet) AnyConverter.toObject(
116                     new Type (XSpreadsheet.class),oIndexAccess.getByIndex(0));
117         } catch (com.sun.star.lang.WrappedTargetException e) {
118             e.printStackTrace(log);
119             throw new StatusException( "Couldn't get a spreadsheet", e);
120         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
121             e.printStackTrace(log);
122             throw new StatusException( "Couldn't get a spreadsheet", e);
123         } catch (com.sun.star.lang.IllegalArgumentException e) {
124             e.printStackTrace(log);
125             throw new StatusException( "Couldn't get a spreadsheet", e);
126         }
127 
128         XSubTotalCalculatable xSTC = (XSubTotalCalculatable)
129             UnoRuntime.queryInterface(XSubTotalCalculatable.class, oSheet);
130 
131         XSubTotalDescriptor xSTD = xSTC.createSubTotalDescriptor(true);
132 
133         SubTotalColumn[] columns = new SubTotalColumn[1];
134         SubTotalColumn column = new SubTotalColumn();
135         column.Column = 5;
136         column.Function = GeneralFunction.SUM;
137         columns[0] = column;
138         xSTD.addNew(columns, 1);
139 
140         XIndexAccess oDescIndex = (XIndexAccess)
141             UnoRuntime.queryInterface(XIndexAccess.class, xSTD);
142 
143         XInterface oObj = null;
144 
145         try {
146             oObj = ( XSubTotalField ) AnyConverter.toObject(
147                     new Type(XSubTotalField.class),oDescIndex.getByIndex(0));
148         } catch (com.sun.star.lang.WrappedTargetException e) {
149             e.printStackTrace(log);
150             throw new StatusException("Couldn't get XSubTotalField", e);
151         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
152             e.printStackTrace(log);
153             throw new StatusException("Couldn't get XSubTotalField", e);
154         } catch (com.sun.star.lang.IllegalArgumentException e) {
155             e.printStackTrace(log);
156             throw new StatusException("Couldn't get XSubTotalField", e);
157         }
158 
159         TestEnvironment tEnv = new TestEnvironment(oObj);
160         return tEnv;
161 
162     } // finish method getTestEnvironment
163 
164 }    // finish class ScSubTotalFieldObj
165 
166