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