2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.io
.PrintWriter
;
23 import lib
.StatusException
;
25 import lib
.TestEnvironment
;
26 import lib
.TestParameters
;
27 import util
.SOfficeFactory
;
29 import com
.sun
.star
.beans
.XPropertySet
;
30 import com
.sun
.star
.container
.XIndexAccess
;
31 import com
.sun
.star
.lang
.XComponent
;
32 import com
.sun
.star
.lang
.XMultiServiceFactory
;
33 import com
.sun
.star
.sheet
.XNamedRanges
;
34 import com
.sun
.star
.sheet
.XSpreadsheet
;
35 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
36 import com
.sun
.star
.sheet
.XSpreadsheets
;
37 import com
.sun
.star
.table
.CellAddress
;
38 import com
.sun
.star
.table
.CellRangeAddress
;
39 import com
.sun
.star
.uno
.AnyConverter
;
40 import com
.sun
.star
.uno
.Type
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
42 import com
.sun
.star
.uno
.XInterface
;
45 * Test for object which is represented by service
46 * <code>com.sun.star.sheet.NamedRanges</code>. <p>
47 * Object implements the following interfaces :
49 * <li> <code>com::sun::star::sheet::XNamedRanges</code></li>
50 * <li> <code>com::sun::star::container::XNameAccess</code></li>
51 * <li> <code>com::sun::star::container::XElementAccess</code></li>
53 * @see com.sun.star.sheet.NamedRanges
54 * @see com.sun.star.sheet.XNamedRanges
55 * @see com.sun.star.container.XNameAccess
56 * @see com.sun.star.container.XElementAccess
57 * @see ifc.sheet._XNamedRanges
58 * @see ifc.container._XNameAccess
59 * @see ifc.container._XElementAccess
61 public class ScNamedRangesObj
extends TestCase
{
62 private XSpreadsheetDocument xSheetDoc
= null;
65 * Creates Spreadsheet document.
67 protected void initialize( TestParameters tParam
, PrintWriter log
) {
68 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF() );
71 log
.println( "creating a Spreadsheet document" );
72 xSheetDoc
= SOF
.createCalcDoc(null);
73 } catch ( com
.sun
.star
.uno
.Exception e
) {
74 // Some exception occurs.FAILED
75 e
.printStackTrace( log
);
76 throw new StatusException( "Couldn't create document", e
);
81 * Disposes Spreadsheet document.
83 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
84 log
.println( " disposing xSheetDoc " );
85 XComponent oComp
= UnoRuntime
.queryInterface (XComponent
.class, xSheetDoc
) ;
86 util
.DesktopTools
.closeDoc(oComp
);
90 * Creating a Testenvironment for the interfaces to be tested.
91 * Retrieves a collection of spreadsheets from a document
92 * and takes one of them. Obtains the value of the property
93 * <code>'NamedRanges'</code> that is the collection of named ranges.
94 * This collection is the instance of the service
95 * <code>com.sun.star.sheet.NamedRanges</code>. Creates and adds new range to
97 * Object relations created :
99 * <li> <code>'SHEET'</code> for
100 * {@link ifc.sheet._XNamedRanges} (the retrieved spreadsheet) </li>
102 * @see com.sun.star.sheet.NamedRanges
104 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
106 XInterface oObj
= null;
108 // creation of testobject here
109 // first we write what we are intend to do to log file
110 log
.println( "Creating a test environment" );
111 XSpreadsheet oSheet
= null;
113 log
.println("Getting test object ");
114 XSpreadsheets oSheets
= xSheetDoc
.getSheets();
115 XIndexAccess oIndexSheets
= UnoRuntime
.queryInterface(XIndexAccess
.class, oSheets
);
117 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
118 new Type(XSpreadsheet
.class),oIndexSheets
.getByIndex(0));
119 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
120 e
.printStackTrace(log
);
121 throw new StatusException( "Couldn't get a spreadsheet", e
);
122 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
123 e
.printStackTrace(log
);
124 throw new StatusException( "Couldn't get a spreadsheet", e
);
125 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
126 e
.printStackTrace(log
);
127 throw new StatusException( "Couldn't get a spreadsheet", e
);
130 // Getting named ranges.
131 XPropertySet docProps
= UnoRuntime
.queryInterface(XPropertySet
.class, xSheetDoc
);
132 Object ranges
= null;
134 ranges
= docProps
.getPropertyValue("NamedRanges");
135 } catch(com
.sun
.star
.lang
.WrappedTargetException e
){
136 e
.printStackTrace(log
);
137 throw new StatusException("Couldn't get NamedRanges", e
);
138 } catch(com
.sun
.star
.beans
.UnknownPropertyException e
){
139 e
.printStackTrace(log
);
140 throw new StatusException("Couldn't get NamedRanges", e
);
143 XNamedRanges xNamedRanges
= UnoRuntime
.queryInterface(XNamedRanges
.class, ranges
);
145 CellRangeAddress DataArea
= new CellRangeAddress((short)0, 0, 0, 2, 2);
146 CellAddress base
= new CellAddress(DataArea
.Sheet
,
147 DataArea
.StartColumn
,
150 xNamedRanges
.addNewByName("ANamedRange", "A1:B2", base
, 0);
152 CellAddress listOutputPosition
= new CellAddress((short)0, 1, 1);
153 xNamedRanges
.outputList(listOutputPosition
);
157 TestEnvironment tEnv
= new TestEnvironment( oObj
);
159 // Other parameters required for interface tests
160 tEnv
.addObjRelation("SHEET", oSheet
);