Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScNamedRangesObj.java
blob45f0454bee275a6b756ff33b548b45f05e96e8b4
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.container.XIndexAccess;
31 import com.sun.star.lang.XComponent;
32 import com.sun.star.sheet.XNamedRanges;
33 import com.sun.star.sheet.XSpreadsheet;
34 import com.sun.star.sheet.XSpreadsheetDocument;
35 import com.sun.star.sheet.XSpreadsheets;
36 import com.sun.star.table.CellAddress;
37 import com.sun.star.table.CellRangeAddress;
38 import com.sun.star.uno.AnyConverter;
39 import com.sun.star.uno.Type;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
43 /**
44 * Test for object which is represented by service
45 * <code>com.sun.star.sheet.NamedRanges</code>. <p>
46 * Object implements the following interfaces :
47 * <ul>
48 * <li> <code>com::sun::star::sheet::XNamedRanges</code></li>
49 * <li> <code>com::sun::star::container::XNameAccess</code></li>
50 * <li> <code>com::sun::star::container::XElementAccess</code></li>
51 * </ul>
52 * @see com.sun.star.sheet.NamedRanges
53 * @see com.sun.star.sheet.XNamedRanges
54 * @see com.sun.star.container.XNameAccess
55 * @see com.sun.star.container.XElementAccess
56 * @see ifc.sheet._XNamedRanges
57 * @see ifc.container._XNameAccess
58 * @see ifc.container._XElementAccess
60 public class ScNamedRangesObj extends TestCase {
61 private XSpreadsheetDocument xSheetDoc = null;
63 /**
64 * Creates Spreadsheet document.
66 @Override
67 protected void initialize( TestParameters tParam, PrintWriter log ) {
68 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
70 try {
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 );
80 /**
81 * Disposes Spreadsheet document.
83 @Override
84 protected void cleanup( TestParameters tParam, PrintWriter log ) {
85 log.println( " disposing xSheetDoc " );
86 XComponent oComp = UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
87 util.DesktopTools.closeDoc(oComp);
90 /**
91 * Creating a Testenvironment for the interfaces to be tested.
92 * Retrieves a collection of spreadsheets from a document
93 * and takes one of them. Obtains the value of the property
94 * <code>'NamedRanges'</code> that is the collection of named ranges.
95 * This collection is the instance of the service
96 * <code>com.sun.star.sheet.NamedRanges</code>. Creates and adds new range to
97 * the collection.
98 * Object relations created :
99 * <ul>
100 * <li> <code>'SHEET'</code> for
101 * {@link ifc.sheet._XNamedRanges} (the retrieved spreadsheet) </li>
102 * </ul>
103 * @see com.sun.star.sheet.NamedRanges
105 @Override
106 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
108 XInterface oObj = null;
110 // creation of testobject here
111 // first we write what we are intend to do to log file
112 log.println( "Creating a test environment" );
113 XSpreadsheet oSheet = null;
115 log.println("Getting test object ");
116 XSpreadsheets oSheets = xSheetDoc.getSheets();
117 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
118 try {
119 oSheet = (XSpreadsheet) AnyConverter.toObject(
120 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
121 } catch (com.sun.star.lang.WrappedTargetException e) {
122 e.printStackTrace(log);
123 throw new StatusException( "Couldn't get a spreadsheet", e);
124 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
125 e.printStackTrace(log);
126 throw new StatusException( "Couldn't get a spreadsheet", e);
127 } catch (com.sun.star.lang.IllegalArgumentException e) {
128 e.printStackTrace(log);
129 throw new StatusException( "Couldn't get a spreadsheet", e);
132 // Getting named ranges.
133 XPropertySet docProps = UnoRuntime.queryInterface(XPropertySet.class, xSheetDoc);
134 Object ranges = null;
135 try {
136 ranges = docProps.getPropertyValue("NamedRanges");
137 } catch(com.sun.star.lang.WrappedTargetException e){
138 e.printStackTrace(log);
139 throw new StatusException("Couldn't get NamedRanges", e);
140 } catch(com.sun.star.beans.UnknownPropertyException e){
141 e.printStackTrace(log);
142 throw new StatusException("Couldn't get NamedRanges", e);
145 XNamedRanges xNamedRanges = UnoRuntime.queryInterface(XNamedRanges.class, ranges);
147 CellRangeAddress DataArea = new CellRangeAddress((short)0, 0, 0, 2, 2);
148 CellAddress base = new CellAddress(DataArea.Sheet,
149 DataArea.StartColumn,
150 DataArea.StartRow);
152 xNamedRanges.addNewByName("ANamedRange", "A1:B2", base, 0);
154 CellAddress listOutputPosition = new CellAddress((short)0, 1, 1);
155 xNamedRanges.outputList(listOutputPosition);
157 oObj = xNamedRanges;
159 TestEnvironment tEnv = new TestEnvironment( oObj );
161 // Other parameters required for interface tests
162 tEnv.addObjRelation("SHEET", oSheet);
164 return tEnv;