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.awt.Rectangle;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.container.XNameAccess;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.sheet.XCellRangeAddressable;
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.CellRangeAddress;
44 import com.sun.star.table.XCell;
45 import com.sun.star.table.XCellRange;
46 import com.sun.star.table.XTableChart;
47 import com.sun.star.table.XTableCharts;
48 import com.sun.star.table.XTableChartsSupplier;
49 import com.sun.star.uno.AnyConverter;
50 import com.sun.star.uno.Type;
51 import com.sun.star.uno.UnoRuntime;
52 
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.table.TableChart</code>. <p>
56 * Object implements the following interfaces :
57 * <ul>
58 *  <li> <code>com::sun::star::container::XNamed</code></li>
59 *  <li> <code>com::sun::star::table::XTableChart</code></li>
60 *  <li> <code>com::sun::star::document::XEmbeddedObjectSupplier</code></li>
61 * </ul>
62 * @see com.sun.star.table.TableChart
63 * @see com.sun.star.container.XNamed
64 * @see com.sun.star.table.XTableChart
65 * @see com.sun.star.document.XEmbeddedObjectSupplier
66 * @see ifc.container._XNamed
67 * @see ifc.table._XTableChart
68 * @see ifc.document._XEmbeddedObjectSupplier
69 */
70 public class ScChartObj extends TestCase {
71     static XSpreadsheetDocument xSheetDoc = null;
72 
73     /**
74     * Creates Spreadsheet document.
75     */
initialize( TestParameters tParam, PrintWriter log )76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77         // get a soffice factory object
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
79 
80         try {
81             log.println( "creating a sheetdocument" );
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     * Disposes Spreadsheet document.
92     */
cleanup( TestParameters tParam, PrintWriter log )93     protected void cleanup( TestParameters tParam, PrintWriter log ) {
94         log.println( "    disposing xSheetDoc " );
95         XComponent oComp = (XComponent)
96             UnoRuntime.queryInterface(XComponent.class, xSheetDoc);
97         util.DesktopTools.closeDoc(oComp);
98     }
99 
100     /**
101     * Creating a Testenvironment for the interfaces to be tested.
102     * Retrieves a collection of spreadsheets from a document
103     * and takes one of them. Inserts some values into the cells of the some cell
104     * range address. Adds the chart that using the data from the cells of this
105     * cell range address. New chart is the instance of
106     * the service <code>com.sun.star.table.TableChart</code>.
107     * Object relations created :
108     * <ul>
109     *  <li> <code>'setName'</code> for
110     *      {@link ifc.container._XNamed}(the method <code>setName</code> must
111     *      not be tested)</li>
112     * </ul>
113     * @see com.sun.star.container.XNamed
114     */
createTestEnvironment(TestParameters Param, PrintWriter log)115     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
116 
117         XSpreadsheet oSheet=null;
118 
119         try {
120             log.println("Getting spreadsheet") ;
121             XSpreadsheets oSheets = xSheetDoc.getSheets() ;
122             XIndexAccess oIndexSheets = (XIndexAccess)
123                         UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
124             oSheet = (XSpreadsheet) AnyConverter.toObject(
125                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
126         } catch (com.sun.star.lang.WrappedTargetException e) {
127             log.println("Couldn't get Sheet ");
128             e.printStackTrace(log);
129             throw new StatusException("Couldn't get sheet", e);
130         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
131             log.println("Couldn't get Sheet ");
132             e.printStackTrace(log);
133             throw new StatusException("Couldn't get sheet", e);
134         } catch (com.sun.star.lang.IllegalArgumentException e) {
135             log.println("Couldn't get Sheet ");
136             e.printStackTrace(log);
137             throw new StatusException("Couldn't get sheet", e);
138         }
139 
140         log.println("Creating the Header") ;
141 
142         insertIntoCell(1,0,"JAN",oSheet,"");
143         insertIntoCell(2,0,"FEB",oSheet,"");
144         insertIntoCell(3,0,"MAR",oSheet,"");
145         insertIntoCell(4,0,"APR",oSheet,"");
146         insertIntoCell(5,0,"MAI",oSheet,"");
147         insertIntoCell(6,0,"JUN",oSheet,"");
148         insertIntoCell(7,0,"JUL",oSheet,"");
149         insertIntoCell(8,0,"AUG",oSheet,"");
150         insertIntoCell(9,0,"SEP",oSheet,"");
151         insertIntoCell(10,0,"OCT",oSheet,"");
152         insertIntoCell(11,0,"NOV",oSheet,"");
153         insertIntoCell(12,0,"DEC",oSheet,"");
154         insertIntoCell(13,0,"SUM",oSheet,"");
155 
156         log.println("Fill the lines");
157 
158         insertIntoCell(0,1,"Smith",oSheet,"");
159         insertIntoCell(1,1,"42",oSheet,"V");
160         insertIntoCell(2,1,"58.9",oSheet,"V");
161         insertIntoCell(3,1,"-66.5",oSheet,"V");
162         insertIntoCell(4,1,"43.4",oSheet,"V");
163         insertIntoCell(5,1,"44.5",oSheet,"V");
164         insertIntoCell(6,1,"45.3",oSheet,"V");
165         insertIntoCell(7,1,"-67.3",oSheet,"V");
166         insertIntoCell(8,1,"30.5",oSheet,"V");
167         insertIntoCell(9,1,"23.2",oSheet,"V");
168         insertIntoCell(10,1,"-97.3",oSheet,"V");
169         insertIntoCell(11,1,"22.4",oSheet,"V");
170         insertIntoCell(12,1,"23.5",oSheet,"V");
171         insertIntoCell(13,1,"=SUM(B2:M2)",oSheet,"");
172 
173         insertIntoCell(0,2,"Jones",oSheet,"");
174         insertIntoCell(1,2,"21",oSheet,"V");
175         insertIntoCell(2,2,"40.9",oSheet,"V");
176         insertIntoCell(3,2,"-57.5",oSheet,"V");
177         insertIntoCell(4,2,"-23.4",oSheet,"V");
178         insertIntoCell(5,2,"34.5",oSheet,"V");
179         insertIntoCell(6,2,"59.3",oSheet,"V");
180         insertIntoCell(7,2,"27.3",oSheet,"V");
181         insertIntoCell(8,2,"-38.5",oSheet,"V");
182         insertIntoCell(9,2,"43.2",oSheet,"V");
183         insertIntoCell(10,2,"57.3",oSheet,"V");
184         insertIntoCell(11,2,"25.4",oSheet,"V");
185         insertIntoCell(12,2,"28.5",oSheet,"V");
186         insertIntoCell(13,2,"=SUM(B3:M3)",oSheet,"");
187 
188         insertIntoCell(0,3,"Brown",oSheet,"");
189         insertIntoCell(1,3,"31.45",oSheet,"V");
190         insertIntoCell(2,3,"-20.9",oSheet,"V");
191         insertIntoCell(3,3,"-117.5",oSheet,"V");
192         insertIntoCell(4,3,"23.4",oSheet,"V");
193         insertIntoCell(5,3,"-114.5",oSheet,"V");
194         insertIntoCell(6,3,"115.3",oSheet,"V");
195         insertIntoCell(7,3,"-171.3",oSheet,"V");
196         insertIntoCell(8,3,"89.5",oSheet,"V");
197         insertIntoCell(9,3,"41.2",oSheet,"V");
198         insertIntoCell(10,3,"71.3",oSheet,"V");
199         insertIntoCell(11,3,"25.4",oSheet,"V");
200         insertIntoCell(12,3,"38.5",oSheet,"V");
201         insertIntoCell(13,3,"=SUM(A4:L4)",oSheet,"");
202 
203         // insert a chart
204 
205         Rectangle oRect = new Rectangle(500, 3000, 25000, 11000);
206 
207         XCellRange oRange = (XCellRange)
208             UnoRuntime.queryInterface(XCellRange.class, oSheet);
209         XCellRange myRange = oRange.getCellRangeByName("A1:N4");
210         XCellRangeAddressable oRangeAddr = (XCellRangeAddressable)
211             UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
212         CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
213 
214         CellRangeAddress[] oAddr = new CellRangeAddress[1];
215         oAddr[0] = myAddr;
216         XTableChartsSupplier oSupp = (XTableChartsSupplier)
217             UnoRuntime.queryInterface(XTableChartsSupplier.class, oSheet);
218 
219         log.println("Insert Chart");
220 
221         XTableCharts oCharts = oSupp.getCharts();
222         oCharts.addNewByName("ScChartObj", oRect, oAddr, true, true);
223 
224         // get the TableChart
225         XTableChart oChart = null;
226         try {
227             XNameAccess names = (XNameAccess) AnyConverter.toObject(
228                 new Type(XNameAccess.class),UnoRuntime.queryInterface(
229                     XNameAccess.class, oCharts));
230 
231             oChart = (XTableChart) AnyConverter.toObject(
232                 new Type(XTableChart.class),names.getByName("ScChartObj"));
233 
234         } catch (com.sun.star.lang.WrappedTargetException e) {
235             log.println("Couldn't get TableChart");
236             e.printStackTrace(log);
237             throw new StatusException("Couldn't get TableChart", e);
238         } catch (com.sun.star.container.NoSuchElementException e) {
239             log.println("Couldn't get TableChart");
240             e.printStackTrace(log);
241             throw new StatusException("Couldn't get TableChart", e);
242         } catch (com.sun.star.lang.IllegalArgumentException e) {
243             log.println("Couldn't get TableChart");
244             e.printStackTrace(log);
245             throw new StatusException("Couldn't get TableChart", e);
246         }
247 
248         log.println("creating a new environment for object");
249         TestEnvironment tEnv = new TestEnvironment(oChart);
250 
251         //adding ObjRelation 'setName' for 'XNamed'
252         tEnv.addObjRelation("setName", new Boolean(true));
253 
254         return tEnv;
255     }
256 
257     /**
258     * Inserts a value or a formula in the cell of the spreasheet.
259     * @param CellX is the column index of the cell
260     * @param CellY is the row index of the cell
261     * @param theValue string representation of the value
262     * @param TT1 specify the spreadsheet, the interface
263     * <code>com.sun.star.sheet.XSpreadsheet</code>
264     * @param flag if it's equal to <code>'V'</code> then the method inserts
265     * a double-value in the cell else it inserts a formula in the cell
266     */
insertIntoCell( int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag)267     public static void insertIntoCell(
268         int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag)
269     {
270         XCell oCell = null;
271 
272         try {
273             oCell = TT1.getCellByPosition(CellX, CellY);
274         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
275             System.out.println("Could not get Cell");
276         }
277 
278         if (flag.equals("V")) {
279             oCell.setValue(new Float(theValue).floatValue());
280         } else {
281             oCell.setFormula(theValue);
282         }
283     } // end of insertIntoCell
284 }
285 
286