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.XEnumerationAccess;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XDataPilotDescriptor;
39 import com.sun.star.sheet.XDataPilotTables;
40 import com.sun.star.sheet.XDataPilotTablesSupplier;
41 import com.sun.star.sheet.XSpreadsheet;
42 import com.sun.star.sheet.XSpreadsheetDocument;
43 import com.sun.star.sheet.XSpreadsheets;
44 import com.sun.star.table.CellAddress;
45 import com.sun.star.table.CellRangeAddress;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.Type;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 
51 public class ScIndexEnumeration_DataPilotFieldsEnumeration extends TestCase {
52     static XSpreadsheetDocument xSheetDoc = null;
53 
54     /**
55     * Creates Spreadsheet document.
56     */
initialize( TestParameters tParam, PrintWriter log )57     protected void initialize( TestParameters tParam, PrintWriter log ) {
58         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
59 
60         try {
61             log.println( "creating a Spreadsheet document" );
62             xSheetDoc = SOF.createCalcDoc(null);
63         } catch ( com.sun.star.uno.Exception e ) {
64             // Some exception occures.FAILED
65             e.printStackTrace( log );
66             throw new StatusException( "Couldn't create document", e );
67         }
68 
69     }
70 
71     /**
72     * Disposes Spreadsheet document.
73     */
cleanup( TestParameters tParam, PrintWriter log )74     protected void cleanup( TestParameters tParam, PrintWriter log ) {
75         log.println( "    disposing xSheetDoc " );
76         XComponent oComp = (XComponent)
77             UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ;
78         util.DesktopTools.closeDoc(oComp);
79     }
80 
createTestEnvironment(TestParameters Param, PrintWriter log)81     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
82 
83         XInterface oObj = null;
84 
85         // creation of testobject here
86         // first we write what we are intend to do to log file
87         log.println( "Creating a test environment" );
88 
89         // create testobject here
90 
91         log.println("getting sheets");
92         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
93 
94         log.println("getting a sheet");
95         XSpreadsheet oSheet = null;
96         XIndexAccess oIndexAccess = (XIndexAccess)
97             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
98         try {
99             oSheet = (XSpreadsheet) AnyConverter.toObject(
100                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
101         } catch (com.sun.star.lang.WrappedTargetException e) {
102             e.printStackTrace(log);
103             throw new StatusException( "Couldn't get a spreadsheet", e);
104         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
105             e.printStackTrace(log);
106             throw new StatusException( "Couldn't get a spreadsheet", e);
107         } catch (com.sun.star.lang.IllegalArgumentException e) {
108             e.printStackTrace(log);
109             throw new StatusException( "Couldn't get a spreadsheet", e);
110         }
111 
112         try {
113             log.println("Filing a table");
114             for (int i = 1; i < 4; i++) {
115                 oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
116                 oSheet.getCellByPosition(0, i).setFormula("Row" + i);
117             }
118 
119             for (int i = 1; i < 4; i++)
120                 for (int j = 1; j < 4; j++) {
121                     oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
122                 }
123         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
124             e.printStackTrace(log);
125             throw new StatusException("Couldn't fill some cells", e);
126         }
127 
128         XDataPilotTablesSupplier DPTS = (XDataPilotTablesSupplier)
129             UnoRuntime.queryInterface(XDataPilotTablesSupplier.class, oSheet);
130 
131         log.println("Getting test object ") ;
132 
133         XDataPilotTables DPT = DPTS.getDataPilotTables();
134         XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
135         DPDsc.setSourceRange(new CellRangeAddress((short)0, 0, 0, 4, 4));
136         DPT.insertNewByName(
137             "DataPilotTable",
138             new CellAddress((short)0, 5, 5),
139             DPDsc);
140 
141         oObj = DPDsc.getDataPilotFields();
142         log.println("Creating object - " +
143                                     ((oObj == null) ? "FAILED" : "OK"));
144 
145         XEnumerationAccess ea = (XEnumerationAccess)
146                     UnoRuntime.queryInterface(XEnumerationAccess.class,oObj);
147 
148         oObj = ea.createEnumeration();
149 
150         log.println("ImplementationName: "+util.utils.getImplName(oObj));
151         // creating test environment
152         TestEnvironment tEnv = new TestEnvironment( oObj );
153 
154         tEnv.addObjRelation("ENUM",ea);
155 
156         return tEnv;
157     }
158 
159 }
160 
161 
162