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