bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScNamedRangeObj.java
blob02c851b3958e8d52eb7dc0a03f9db3c61fb9696e
1 /*
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 .
19 package mod._sc;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.lang.XComponent;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.sheet.XNamedRanges;
33 import com.sun.star.sheet.XSpreadsheetDocument;
34 import com.sun.star.table.CellAddress;
35 import com.sun.star.table.CellRangeAddress;
36 import com.sun.star.uno.AnyConverter;
37 import com.sun.star.uno.Type;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
41 /**
42 * Test for object which is represented by service
43 * <code>com.sun.star.sheet.NamedRange</code>. <p>
44 * Object implements the following interfaces :
45 * <ul>
46 * <li> <code>com::sun::star::container::XNamed</code></li>
47 * <li> <code>com::sun::star::sheet::XNamedRange</code></li>
48 * <li> <code>com::sun::star::sheet::XCellRangeReferrer</code></li>
49 * </ul>
50 * @see com.sun.star.sheet.NamedRange
51 * @see com.sun.star.container.XNamed
52 * @see com.sun.star.sheet.XNamedRange
53 * @see com.sun.star.sheet.XCellRangeReferrer
54 * @see ifc.container._XNamed
55 * @see ifc.sheet._XNamedRange
56 * @see ifc.sheet._XCellRangeReferrer
58 public class ScNamedRangeObj extends TestCase {
59 private XSpreadsheetDocument xSheetDoc = null;
61 /**
62 * Creates Spreadsheet document.
64 protected void initialize( TestParameters tParam, PrintWriter log ) {
65 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
67 try {
68 log.println( "creating a Spreadsheet document" );
69 xSheetDoc = SOF.createCalcDoc(null);
70 } catch ( com.sun.star.uno.Exception e ) {
71 // Some exception occurs.FAILED
72 e.printStackTrace( log );
73 throw new StatusException( "Couldn't create document", e );
78 /**
79 * Disposes Spreadsheet document.
81 protected void cleanup( TestParameters tParam, PrintWriter log ) {
82 log.println( " disposing xSheetDoc " );
83 XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSheetDoc) ;
84 util.DesktopTools.closeDoc(oComp);
87 /**
88 * Creating a Testenvironment for the interfaces to be tested.
89 * Retrieves a collection of spreadsheets from a document
90 * and takes one of them. Obtains the value of the property
91 * <code>'NamedRanges'</code> that is the collection of named ranges.
92 * Creates and adds new range to the collection. This new range is the instance of the
93 * service <code>com.sun.star.sheet.NamedRange</code>.
94 * Object relations created :
95 * <ul>
96 * <li> <code>'DATAAREA'</code> for
97 * {@link ifc.sheet._XCellRangeReferrer} (the cell range address of the
98 * created range) </li>
99 * </ul>
101 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
103 XInterface oObj = null;
105 // creation of testobject here
106 // first we write what we are intend to do to log file
107 log.println( "Creating a test environment" );
109 log.println("Getting test object ") ;
111 // Getting named ranges.
112 XPropertySet docProps = UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
114 Object ranges = null;
115 try {
116 ranges = docProps.getPropertyValue("NamedRanges");
117 } catch(com.sun.star.lang.WrappedTargetException e){
118 e.printStackTrace(log);
119 throw new StatusException("Couldn't get NamedRanges", e);
120 } catch(com.sun.star.beans.UnknownPropertyException e){
121 e.printStackTrace(log);
122 throw new StatusException("Couldn't get NamedRanges", e);
125 XNamedRanges xNamedRanges = UnoRuntime.queryInterface(XNamedRanges.class, ranges);
127 CellRangeAddress DataArea = new CellRangeAddress((short)0, 0, 0, 1, 1);
128 CellAddress base = new CellAddress(DataArea.Sheet,
129 DataArea.StartColumn,
130 DataArea.StartRow);
132 if (xNamedRanges.hasByName("ANamedRange")) {
133 xNamedRanges.removeByName("ANamedRange");
136 xNamedRanges.addNewByName("ANamedRange", "A1:B2", base, 0);
138 CellAddress listOutputPosition = new CellAddress((short)0, 1, 1);
139 xNamedRanges.outputList(listOutputPosition);
141 try {
142 oObj = (XInterface) AnyConverter.toObject(
143 new Type(XInterface.class),xNamedRanges.getByName("ANamedRange"));
144 } catch(com.sun.star.lang.WrappedTargetException e){
145 e.printStackTrace(log);
146 throw new StatusException("Couldn't get by name", e);
147 } catch(com.sun.star.container.NoSuchElementException e){
148 e.printStackTrace(log);
149 throw new StatusException("Couldn't get by name", e);
150 } catch(com.sun.star.lang.IllegalArgumentException e){
151 e.printStackTrace(log);
152 throw new StatusException("Couldn't get by name", e);
155 TestEnvironment tEnv = new TestEnvironment( oObj );
157 // Other parameters required for interface tests
158 tEnv.addObjRelation("DATAAREA", DataArea);
160 return tEnv;