Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / container / _XNameReplace.java
blobd0ca7097147aee82e747cce795db06d5c955ea8b
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 ifc.container;
21 import com.sun.star.sheet.XCellRangeAddressable;
22 import lib.MultiMethodTest;
23 import util.ValueComparer;
25 import com.sun.star.container.XNameAccess;
26 import com.sun.star.container.XNameReplace;
27 import com.sun.star.uno.UnoRuntime;
28 /**
29 * Testing <code>com.sun.star.container.XNameReplace</code>
30 * interface methods :
31 * <ul>
32 * <li><code> replaceByName()</code></li>
33 * </ul>
34 * This test needs the following object relations :
35 * <ul>
36 * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
37 * which represents objects to be replaced with. See below
38 * for more information.</li>
39 * <li> <code>'NAMEREPLACE'</code> <b>optional</b>: <code>String</code>
40 * relation which represents element name to be replaced.
41 * Some Objects can't replace the first that comes along, i.e.
42 * SwXStyleFamily. It has some pool styles which can't be replaced.
43 * So the test need a special object to replace it by name. </li>
44 * <li> <code>'XNameReplaceINDEX'</code> : For internal test
45 * usage. Contains current thread number. </li>
46 * <li> Test environment variable <code>'THRCNT'</code> : number
47 * of interface threads running concurrently. </li>
48 * <ul> <p>
49 * XNameReplace needs n ObjectRelations "INSTANCEn" , where n = 1, ..., THRCNT.
50 * <p>
51 * When this interface tested by different threads, it must use different instances
52 * to replace - one for each thread.
53 * <p>
54 * That's why we use objRelation "XNameReplaceINDEX" to store the number of last
55 * taken instance. If there is no such relation, it initialize with 1.
56 * <p>
57 * In one of the last steps the replaced object will be compared with the old
58 * object. For that it is necessary that every thread replace its own object.
59 * INSTANCEn are n Objectrelations so that every thread can insert its own
60 * object. n depends on the variable THRCNT which and comes from API.INI
61 * Some Object-Container can't replace the first that comes belong. So in
62 * NAMEREPLACE you can determine a container object, which is replaceable. <p>
64 * Test is <b> NOT </b> multithread compliant. <p>
65 * After test completion object environment has to be recreated.
66 * @see com.sun.star.container.XNameReplace
68 public class _XNameReplace extends MultiMethodTest {
70 public XNameReplace oObj = null;
72 /**
73 * First test retrieves instance to be replaced with for each interface thread.
74 * Then list of element names is retrieved, the first of them will
75 * be replaced. In special case when <code>'NAMEREPLACE'</code> relation
76 * exists, element with the specified name is replaced.
77 * Test replaces element and checks values of element with the
78 * specified name before and after replacement. <p>
79 * Has <b>OK</b> status if values before and after replacement are
80 * different.
82 public void _replaceByName(){
83 boolean result = true;
84 String[] oNames = null;
86 int Index = 0;
88 //get for every thread its own Object to insert it
89 log.println("get ObjRelation(\"XNameReplaceINDEX\")");
90 String sIndex = (String)tEnv.getObjRelation("XNameReplaceINDEX");
91 System.out.println("Index: "+sIndex);
92 if (sIndex == null) {
93 log.println("No XNameReplaceINDEX - so set it to 1.");
94 tEnv.addObjRelation("XNameReplaceINDEX", Integer.toString(1));
95 Index = 1;
96 } else {
97 Index = Integer.parseInt(sIndex);
98 Index++;
99 tEnv.addObjRelation("XNameReplaceINDEX", Integer.toString(Index));
102 log.println("get ObjRelation(\"INSTANCE" + Index +"\")");
103 Object oInstance = tEnv.getObjRelation("INSTANCE"+ Index);
104 if (oInstance == null) {
105 log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a.");
108 log.println("getting the existing object's name");
109 XNameAccess oNameAccess = UnoRuntime.queryInterface(
110 XNameAccess.class, oObj);
111 oNames = oNameAccess.getElementNames();
112 /* Some Objects can't replace the first that comes along, i.e.
113 SwXStyleFamily. It has some pool styles which can't be replaced.
114 So the test need a special object to replace it by name.
116 log.println("get ObjRelation(\"NAMEREPLACE\")");
117 Object oNameReplace = tEnv.getObjRelation("NAMEREPLACE");
118 if (oNameReplace != null) {
119 oNames[0] = oNameReplace.toString();
122 log.println("replaceByName()");
123 try {
124 boolean ok;
125 log.println("get current object '" + oNames[0] + "'");
126 Object old = oObj.getByName(oNames[0]) ;
127 log.println("replace object '" + oNames[0] + "' with another instance");
128 oObj.replaceByName(oNames[0],oInstance);
129 Object newEl = oObj.getByName(oNames[0]) ;
131 if (tEnv.getTestCase().getObjectName().equals("ScCellRangesObj")) {
132 ok = compareRanges(old, newEl);
133 } else {
134 ok = ! ValueComparer.equalValue(old, newEl);
136 result &= ok;
137 log.println("result of replace: " + ok);
138 log.println("replace back the old object");
139 oObj.replaceByName(oNames[0],old);
140 Object origEl = oObj.getByName(oNames[0]) ;
142 if (tEnv.getTestCase().getObjectName().equals("ScCellRangesObj")) {
143 ok = ! compareRanges(old, origEl);
144 } else {
145 ok = ValueComparer.equalValue(old, origEl);
148 result &= ok;
149 log.println("result of replace back: " + ok);
150 } catch (com.sun.star.lang.IllegalArgumentException e ) {
151 result = false;
152 e.printStackTrace(log) ;
153 } catch (com.sun.star.container.NoSuchElementException e ) {
154 result = false;
155 e.printStackTrace(log) ;
156 } catch (com.sun.star.lang.WrappedTargetException e ) {
157 result = false;
158 e.printStackTrace(log) ;
161 tRes.tested("replaceByName()", result);
163 } // end replaceByName()
166 * Forces object environment recreation.
168 @Override
169 public void after() {
170 disposeEnvironment() ;
173 // method returns false if the ranges are equal and true otherwise
175 private boolean compareRanges(Object old, Object newEl) {
176 XCellRangeAddressable xCRA = UnoRuntime.queryInterface(XCellRangeAddressable.class,old);
178 XCellRangeAddressable xCRA2 = UnoRuntime.queryInterface(XCellRangeAddressable.class,newEl);
180 int orgStartCol = xCRA.getRangeAddress().StartColumn;
181 int orgEndCol = xCRA.getRangeAddress().EndColumn;
182 int orgStartRow = xCRA.getRangeAddress().StartRow;
183 int orgEndRow = xCRA.getRangeAddress().EndRow;
185 int newStartCol = xCRA2.getRangeAddress().StartColumn;
186 int newEndCol = xCRA2.getRangeAddress().EndColumn;
187 int newStartRow = xCRA2.getRangeAddress().StartRow;
188 int newEndRow = xCRA2.getRangeAddress().EndRow;
190 boolean ret = true;
192 if (orgStartCol == newStartCol) {
193 log.println("\t StartColumn is the same");
194 ret = false;
197 if (orgEndCol == newEndCol) {
198 log.println("\t EndColumn is the same");
199 ret = false;
201 if (orgStartRow == newStartRow) {
202 log.println("\t StartRow is the same");
203 ret = false;
206 if (orgEndRow == newEndRow) {
207 log.println("\t EndRow is the same");
208 ret = false;
211 return ret;