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