xref: /AOO41X/main/qadevOOo/tests/java/mod/_sc/ScDataPilotFieldsObj.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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.XDataPilotDescriptor;
42 import com.sun.star.sheet.XDataPilotTables;
43 import com.sun.star.sheet.XDataPilotTablesSupplier;
44 import com.sun.star.sheet.XSpreadsheet;
45 import com.sun.star.sheet.XSpreadsheetDocument;
46 import com.sun.star.sheet.XSpreadsheets;
47 import com.sun.star.table.CellAddress;
48 import com.sun.star.table.CellRangeAddress;
49 import com.sun.star.uno.AnyConverter;
50 import com.sun.star.uno.Type;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XInterface;
53 
54 /**
55 * Test for object which is represented by service
56 * <code>com.sun.star.sheet.DataPilotFields</code>. <p>
57 * Object implements the following interfaces :
58 * <ul>
59 *  <li> <code>com::sun::star::container::XNameAccess</code></li>
60 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
61 * </ul>
62 * @see com.sun.star.sheet.DataPilotFields
63 * @see com.sun.star.container.XNameAccess
64 * @see com.sun.star.container.XElementAccess
65 * @see ifc.container._XNameAccess
66 * @see ifc.container._XElementAccess
67 */
68 public class ScDataPilotFieldsObj extends TestCase {
69     static XSpreadsheetDocument xSheetDoc = null;
70 
71     /**
72     * Creates Spreadsheet document.
73     */
74     protected void initialize( TestParameters tParam, PrintWriter log ) {
75         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
76 
77         try {
78             log.println( "creating a Spreadsheet document" );
79             xSheetDoc = SOF.createCalcDoc(null);
80         } catch ( com.sun.star.uno.Exception e ) {
81             // Some exception occures.FAILED
82             e.printStackTrace( log );
83             throw new StatusException( "Couldn't create document", e );
84         }
85 
86     }
87 
88     /**
89     * Disposes Spreadsheet document.
90     */
91     protected void cleanup( TestParameters tParam, PrintWriter log ) {
92         log.println( "    disposing xSheetDoc " );
93         XComponent oComp = (XComponent)
94             UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ;
95         util.DesktopTools.closeDoc(oComp);
96     }
97 
98     /**
99     * Creating a Testenvironment for the interfaces to be tested.
100     * Retrieves a collection of spreadsheets from a document
101     * and takes one of them. Fills some table in the spreadsheet.
102     * Obtains the collection of data pilot tables using the interface
103     * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
104     * for the filled table and inserts new data pilot table with this descriptor
105     * to the collection. Obtains the collection of all the data pilot fields
106     * using the interface <code>XDataPilotDescriptor</code>. This collection
107     * is the instance of the service <code>com.sun.star.sheet.DataPilotFields</code>.
108     * @see com.sun.star.sheet.DataPilotFields
109     * @see com.sun.star.sheet.XDataPilotTablesSupplier
110     * @see com.sun.star.sheet.XDataPilotDescriptor
111     */
112     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
113 
114         XInterface oObj = null;
115 
116         // creation of testobject here
117         // first we write what we are intend to do to log file
118         log.println( "Creating a test environment" );
119 
120         // create testobject here
121 
122         log.println("getting sheets");
123         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
124 
125         log.println("getting a sheet");
126         XSpreadsheet oSheet = null;
127         XIndexAccess oIndexAccess = (XIndexAccess)
128             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
129         try {
130             oSheet = (XSpreadsheet) AnyConverter.toObject(
131                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
132         } catch (com.sun.star.lang.WrappedTargetException e) {
133             e.printStackTrace(log);
134             throw new StatusException( "Couldn't get a spreadsheet", e);
135         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
136             e.printStackTrace(log);
137             throw new StatusException( "Couldn't get a spreadsheet", e);
138         } catch (com.sun.star.lang.IllegalArgumentException e) {
139             e.printStackTrace(log);
140             throw new StatusException( "Couldn't get a spreadsheet", e);
141         }
142 
143         try {
144             log.println("Filing a table");
145             for (int i = 1; i < 4; i++) {
146                 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
147                 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
148             }
149 
150             for (int i = 1; i < 4; i++)
151                 for (int j = 1; j < 4; j++) {
152                     oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
153                 }
154         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
155             e.printStackTrace(log);
156             throw new StatusException("Couldn't fill some cells", e);
157         }
158 
159         XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier)
160             UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
161 
162         log.println("Getting test object ") ;
163 
164         XDataPilotTables DPT = DPTS.getDataPilotTables();
165         XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
166         DPDsc.setSourceRange(new CellRangeAddress((short)0, 0, 0, 4, 4));
167         DPT.insertNewByName(
168             "DataPilotTable",
169             new CellAddress((short)0, 5, 5),
170             DPDsc);
171 
172         oObj = DPDsc.getDataPilotFields();
173         log.println("Creating object - " +
174                                     ((oObj == null) ? "FAILED" : "OK"));
175 
176         TestEnvironment tEnv = new TestEnvironment( oObj );
177 
178         // Other parameters required for interface tests
179 
180         return tEnv;
181     }
182 
183 }
184