merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / container / _XIndexContainer.java
blob80939f42c0a0b44c58d1cfea960f3cd3829aa879
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XIndexContainer.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.container;
33 import lib.MultiMethodTest;
35 import com.sun.star.container.XIndexContainer;
36 import com.sun.star.lang.IndexOutOfBoundsException;
38 /**
39 * Testing <code>com.sun.star.container.XIndexContainer</code>
40 * interface methods :
41 * <ul>
42 * <li><code> insertByIndex()</code></li>
43 * <li><code> removeByIndex()</code></li>
44 * </ul> <p>
46 * This test needs the following object relations :
47 * <ul>
48 * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
49 * which represents objects to be inserted. See below
50 * for more information.</li>
51 * <li> <code>'XIndexContainerINDEX'</code> : For internal test
52 * usage. Contains current thread number. </li>
53 * <li> Test environment variable <code>'THRCNT'</code> : number
54 * of interface threads running concurently. </li>
55 * <ul> <p>
56 * XIndexComtainer needs n ObjectRelations "INSTANCEn" , where n=1, ...,
57 * THRCNT.<p>
58 * When this interface tested by different threads, it must use different
59 * instances to insert/remove - one for each thread.
60 * <p>
61 * That's why we use objRelation "XIndexComtainerINDEX" to store the number of
62 * last taken instance. If there is no such relation, it initialize with 1.
63 * <p>
64 * This ObjectRelations should be necessary to create an Object,
65 * which is insertable by insterByIndex()
66 * INSTANCEn are n Objectrelations so that every thread can isert it's own
67 * object. n depends on the variable THRCNT which and comes from API.INI
68 * <p>
69 * Why that:
70 * If you insert the same Object by insertByIndex() several times you
71 * don't insert the Object several times. The first insertByIndex() inserts
72 * the Object to the Container but all other insertByIndex() changes
73 * the Index in the Continer because it's the same Object. <p>
74 * Test is multithread compilant. <p>
75 * @see com.sun.star.container.XIndexContainer
78 public class _XIndexContainer extends MultiMethodTest {
79 public XIndexContainer oObj = null;
81 int Index = 0;
83 /**
84 * First tries to insert proper object. Second tries to insert
85 * null value. For each test thread different objects are inserted
86 * on different indexes. For exmaple for the first started test index
87 * is 0 and object is get from relation 'INCTANCE1', and so on. <p>
88 * Has <b>OK</b> status if in the first case <code>getByIndex</code>
89 * method returns non null value and in the second <code>
90 * IndexOutOfBoundsException</code> was thrown.
92 public void _insertByIndex() {
93 boolean result = true;
95 log.println("get ObjRelation(\"XIndexContainerINDEX\")");
96 String sIndex = (String)tEnv.getObjRelation("XIndexContainerINDEX");
97 if (sIndex == null) {
98 log.println("No XIndexContainerINDEX - so set it to 1.");
99 tEnv.addObjRelation("XIndexContainerINDEX", Integer.toString(1));
100 Index = 1;
101 } else {
102 Index = Integer.parseInt(sIndex);
103 Index++;
104 tEnv.addObjRelation("XIndexContainerINDEX",
105 Integer.toString(Index));
109 log.println("get ObjRelation(\"INSTANCE" + Index +"\")");
110 Object oInstance = tEnv.getObjRelation("INSTANCE"+ Index);
111 if (oInstance == null) {
112 log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a.");
115 log.println("testing insertByIndex(\"" + Index + "\")...");
116 try {
117 oObj.insertByIndex(Index, oInstance);
118 result &= oObj.getByIndex(Index) != null ;
119 log.println("insertByIndex(\""+Index+"\")...OK");
120 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
121 log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE");
122 result = false;
123 } catch (com.sun.star.lang.IllegalArgumentException e) {
124 log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE");
125 result = false;
126 } catch (com.sun.star.lang.WrappedTargetException e) {
127 log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE");
128 result = false;
131 log.println("inserting a wrong Object occurs Exceptions ...");
132 try {
133 Object dummy = null;
134 oObj.insertByIndex(0, dummy);
135 log.println("No Exception: -> FALSE");
136 result = false;
137 } catch (com.sun.star.lang.IllegalArgumentException e) {
138 log.println("Dummy-Exception: " + e + " -> OK");
139 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
140 log.println("!!! Wrong Exception: " + e + " -> FAILED");
141 result = false;
142 } catch (com.sun.star.lang.WrappedTargetException e) {
143 log.println("!!! Wrong Exception: " + e + " -> FAILED");
144 result = false;
147 tRes.tested("insertByIndex()", result);
151 * Removes the element inserted by <code>insertByIndex</code> method test.
152 * The number of elements is checked before and after removing.
153 * Then tries to remove an element with invalid index and checks exceptions.
154 * <p>
155 * Has <b>OK</b> status if after removing number of elements decreases by
156 * one and <code>IndexOutOfBoundsException</code> is thrown on invalid index
157 * removing.
158 * The following method tests are to be completed successfully before :
159 * <ul>
160 * <li> <code> insertByIndex </code> : to have an object which can be
161 * removed.</li>
162 * </ul>
164 public void _removeByIndex() {
165 requiredMethod("insertByIndex()");
166 boolean result = true;
168 log.println("testing removeByIndex() ...");
170 try {
171 log.println("remove " +Index);
172 int cnt1 = -1 , cnt2 = -1 ;
173 synchronized (oObj) {
174 cnt1 = oObj.getCount() ;
175 oObj.removeByIndex(Index);
176 cnt2 = oObj.getCount() ;
178 log.println("Count before removing : " + cnt1 +
179 ", and after : " + cnt2) ;
181 result &= cnt1 == cnt2 + 1 ;
183 log.println("1. removeByIndex(\""+Index+"\") ...OK");
184 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
185 result = false;
186 log.println("1. removeByIndex:(\""+Index+"\") " +
187 e + " - FAILED");
188 } catch (com.sun.star.lang.WrappedTargetException e) {
189 result = false;
190 log.println("1. removeByIndex:(\""+Index+"\") " +
191 e + " - FAILED");
194 log.println("removing a non existent object to get an exception");
195 try {
196 oObj.removeByIndex(100);
197 result = false;
198 log.println("2. removeByIndex(): Exception expected! - FAILED");
199 } catch (IndexOutOfBoundsException e) {
200 log.println("2. removeByIndex(): Expected exception - OK");
201 result &= true;
202 } catch (com.sun.star.lang.WrappedTargetException e) {
203 result = false;
204 log.println("2. removeByIndex(): Unexpected exception! - " +
205 e + " - FAILED");
208 tRes.tested("removeByIndex()", result);