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
;
27 * Testing <code>com.sun.star.container.XIndexContainer</code>
30 * <li><code> insertByIndex()</code></li>
31 * <li><code> removeByIndex()</code></li>
34 * This test needs the following object relations :
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 concurently. </li>
44 * XIndexComtainer needs n ObjectRelations "INSTANCEn" , where n=1, ...,
46 * When this interface tested by different threads, it must use different
47 * instances to insert/remove - one for each thread.
49 * That's why we use objRelation "XIndexComtainerINDEX" to store the number of
50 * last taken instance. If there is no such relation, it initialize with 1.
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 isert it's own
55 * object. n depends on the variable THRCNT which and comes from API.INI
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 Continer 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;
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 'INCTANCE1', 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");
86 log
.println("No XIndexContainerINDEX - so set it to 1.");
87 tEnv
.addObjRelation("XIndexContainerINDEX", Integer
.toString(1));
90 Index
= Integer
.parseInt(sIndex
);
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
+ "\")...");
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");
111 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
112 log
.println("insertByIndex(\""+Index
+"\"): " + e
+ " FLASE");
114 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
115 log
.println("insertByIndex(\""+Index
+"\"): " + e
+ " FLASE");
119 log
.println("inserting a wrong Object occurs Exceptions ...");
122 oObj
.insertByIndex(0, dummy
);
123 log
.println("No Exception: -> 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");
130 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
131 log
.println("!!! Wrong Exception: " + e
+ " -> FAILED");
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.
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
146 * The following method tests are to be completed successfully before :
148 * <li> <code> insertByIndex </code> : to have an object which can be
152 public void _removeByIndex() {
153 requiredMethod("insertByIndex()");
154 boolean result
= true;
156 log
.println("testing removeByIndex() ...");
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
) {
174 log
.println("1. removeByIndex:(\""+Index
+"\") " +
176 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
178 log
.println("1. removeByIndex:(\""+Index
+"\") " +
182 log
.println("removing a non existent object to get an exception");
184 oObj
.removeByIndex(100);
186 log
.println("2. removeByIndex(): Exception expected! - FAILED");
187 } catch (IndexOutOfBoundsException e
) {
188 log
.println("2. removeByIndex(): Expected exception - OK");
190 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
192 log
.println("2. removeByIndex(): Unexpected exception! - " +
196 tRes
.tested("removeByIndex()", result
);