1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XIndexContainer.java,v $
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
;
39 * Testing <code>com.sun.star.container.XIndexContainer</code>
42 * <li><code> insertByIndex()</code></li>
43 * <li><code> removeByIndex()</code></li>
46 * This test needs the following object relations :
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>
56 * XIndexComtainer needs n ObjectRelations "INSTANCEn" , where n=1, ...,
58 * When this interface tested by different threads, it must use different
59 * instances to insert/remove - one for each thread.
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.
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
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;
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");
98 log
.println("No XIndexContainerINDEX - so set it to 1.");
99 tEnv
.addObjRelation("XIndexContainerINDEX", Integer
.toString(1));
102 Index
= Integer
.parseInt(sIndex
);
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
+ "\")...");
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");
123 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
124 log
.println("insertByIndex(\""+Index
+"\"): " + e
+ " FLASE");
126 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
127 log
.println("insertByIndex(\""+Index
+"\"): " + e
+ " FLASE");
131 log
.println("inserting a wrong Object occurs Exceptions ...");
134 oObj
.insertByIndex(0, dummy
);
135 log
.println("No Exception: -> 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");
142 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
143 log
.println("!!! Wrong Exception: " + e
+ " -> FAILED");
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.
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
158 * The following method tests are to be completed successfully before :
160 * <li> <code> insertByIndex </code> : to have an object which can be
164 public void _removeByIndex() {
165 requiredMethod("insertByIndex()");
166 boolean result
= true;
168 log
.println("testing removeByIndex() ...");
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
) {
186 log
.println("1. removeByIndex:(\""+Index
+"\") " +
188 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
190 log
.println("1. removeByIndex:(\""+Index
+"\") " +
194 log
.println("removing a non existent object to get an exception");
196 oObj
.removeByIndex(100);
198 log
.println("2. removeByIndex(): Exception expected! - FAILED");
199 } catch (IndexOutOfBoundsException e
) {
200 log
.println("2. removeByIndex(): Expected exception - OK");
202 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
204 log
.println("2. removeByIndex(): Unexpected exception! - " +
208 tRes
.tested("removeByIndex()", result
);