Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / container / _XIndexContainer.java
blob9b3a23f974097841774dd6cc0acf2ef37e6dddfe
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 lib.MultiMethodTest;
23 import com.sun.star.container.XIndexContainer;
24 import com.sun.star.lang.IndexOutOfBoundsException;
26 /**
27 * Testing <code>com.sun.star.container.XIndexContainer</code>
28 * interface methods :
29 * <ul>
30 * <li><code> insertByIndex()</code></li>
31 * <li><code> removeByIndex()</code></li>
32 * </ul> <p>
34 * This test needs the following object relations :
35 * <ul>
36 * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
37 * which represents objects to be inserted. See below
38 * for more information.</li>
39 * <li> <code>'XIndexContainerINDEX'</code> : For internal test
40 * usage. Contains current thread number. </li>
41 * <li> Test environment variable <code>'THRCNT'</code> : number
42 * of interface threads running concurrently. </li>
43 * <ul> <p>
44 * XIndexContainer needs n ObjectRelations "INSTANCEn" , where n=1, ...,
45 * THRCNT.<p>
46 * When this interface tested by different threads, it must use different
47 * instances to insert/remove - one for each thread.
48 * <p>
49 * That's why we use objRelation "XIndexContainerINDEX" to store the number of
50 * last taken instance. If there is no such relation, it initialize with 1.
51 * <p>
52 * This ObjectRelations should be necessary to create an Object,
53 * which is insertable by insterByIndex()
54 * INSTANCEn are n Objectrelations so that every thread can insert its own
55 * object. n depends on the variable THRCNT which and comes from API.INI
56 * <p>
57 * Why that:
58 * If you insert the same Object by insertByIndex() several times you
59 * don't insert the Object several times. The first insertByIndex() inserts
60 * the Object to the Container but all other insertByIndex() changes
61 * the Index in the Container because it's the same Object. <p>
62 * Test is multithread compliant. <p>
63 * @see com.sun.star.container.XIndexContainer
66 public class _XIndexContainer extends MultiMethodTest {
67 public XIndexContainer oObj = null;
69 int Index = 0;
71 /**
72 * First tries to insert proper object. Second tries to insert
73 * null value. For each test thread different objects are inserted
74 * on different indexes. For example for the first started test index
75 * is 0 and object is get from relation 'INSTANCE1', and so on. <p>
76 * Has <b>OK</b> status if in the first case <code>getByIndex</code>
77 * method returns non null value and in the second <code>
78 * IndexOutOfBoundsException</code> was thrown.
80 public void _insertByIndex() {
81 boolean result = true;
83 log.println("get ObjRelation(\"XIndexContainerINDEX\")");
84 String sIndex = (String)tEnv.getObjRelation("XIndexContainerINDEX");
85 if (sIndex == null) {
86 log.println("No XIndexContainerINDEX - so set it to 1.");
87 tEnv.addObjRelation("XIndexContainerINDEX", Integer.toString(1));
88 Index = 1;
89 } else {
90 Index = Integer.parseInt(sIndex);
91 Index++;
92 tEnv.addObjRelation("XIndexContainerINDEX",
93 Integer.toString(Index));
97 log.println("get ObjRelation(\"INSTANCE" + Index +"\")");
98 Object oInstance = tEnv.getObjRelation("INSTANCE"+ Index);
99 if (oInstance == null) {
100 log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a.");
103 log.println("testing insertByIndex(\"" + Index + "\")...");
104 try {
105 oObj.insertByIndex(Index, oInstance);
106 result &= oObj.getByIndex(Index) != null ;
107 log.println("insertByIndex(\""+Index+"\")...OK");
108 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
109 log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE");
110 result = false;
111 } catch (com.sun.star.lang.IllegalArgumentException e) {
112 log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE");
113 result = false;
114 } catch (com.sun.star.lang.WrappedTargetException e) {
115 log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE");
116 result = false;
119 log.println("inserting a wrong Object occurs Exceptions ...");
120 try {
121 Object dummy = null;
122 oObj.insertByIndex(0, dummy);
123 log.println("No Exception: -> FALSE");
124 result = false;
125 } catch (com.sun.star.lang.IllegalArgumentException e) {
126 log.println("Dummy-Exception: " + e + " -> OK");
127 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
128 log.println("!!! Wrong Exception: " + e + " -> FAILED");
129 result = false;
130 } catch (com.sun.star.lang.WrappedTargetException e) {
131 log.println("!!! Wrong Exception: " + e + " -> FAILED");
132 result = false;
135 tRes.tested("insertByIndex()", result);
139 * Removes the element inserted by <code>insertByIndex</code> method test.
140 * The number of elements is checked before and after removing.
141 * Then tries to remove an element with invalid index and checks exceptions.
142 * <p>
143 * Has <b>OK</b> status if after removing number of elements decreases by
144 * one and <code>IndexOutOfBoundsException</code> is thrown on invalid index
145 * removing.
146 * The following method tests are to be completed successfully before :
147 * <ul>
148 * <li> <code> insertByIndex </code> : to have an object which can be
149 * removed.</li>
150 * </ul>
152 public void _removeByIndex() {
153 requiredMethod("insertByIndex()");
154 boolean result = true;
156 log.println("testing removeByIndex() ...");
158 try {
159 log.println("remove " +Index);
160 int cnt1 = -1 , cnt2 = -1 ;
161 synchronized (oObj) {
162 cnt1 = oObj.getCount() ;
163 oObj.removeByIndex(Index);
164 cnt2 = oObj.getCount() ;
166 log.println("Count before removing : " + cnt1 +
167 ", and after : " + cnt2) ;
169 result &= cnt1 == cnt2 + 1 ;
171 log.println("1. removeByIndex(\""+Index+"\") ...OK");
172 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
173 result = false;
174 log.println("1. removeByIndex:(\""+Index+"\") " +
175 e + " - FAILED");
176 } catch (com.sun.star.lang.WrappedTargetException e) {
177 result = false;
178 log.println("1. removeByIndex:(\""+Index+"\") " +
179 e + " - FAILED");
182 log.println("removing a non existent object to get an exception");
183 try {
184 oObj.removeByIndex(100);
185 result = false;
186 log.println("2. removeByIndex(): Exception expected! - FAILED");
187 } catch (IndexOutOfBoundsException e) {
188 log.println("2. removeByIndex(): Expected exception - OK");
189 result &= true;
190 } catch (com.sun.star.lang.WrappedTargetException e) {
191 result = false;
192 log.println("2. removeByIndex(): Unexpected exception! - " +
193 e + " - FAILED");
196 tRes.tested("removeByIndex()", result);