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