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