1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.container;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import lib.MultiMethodTest;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.container.XIndexContainer;
29cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /**
32cdf0e10cSrcweir * Testing <code>com.sun.star.container.XIndexContainer</code>
33cdf0e10cSrcweir * interface methods :
34cdf0e10cSrcweir * <ul>
35cdf0e10cSrcweir *  <li><code> insertByIndex()</code></li>
36cdf0e10cSrcweir *  <li><code> removeByIndex()</code></li>
37cdf0e10cSrcweir * </ul> <p>
38cdf0e10cSrcweir *
39cdf0e10cSrcweir * This test needs the following object relations :
40cdf0e10cSrcweir * <ul>
41cdf0e10cSrcweir *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
42cdf0e10cSrcweir *   which represents objects to be inserted. See below
43cdf0e10cSrcweir *   for more information.</li>
44cdf0e10cSrcweir *  <li> <code>'XIndexContainerINDEX'</code> : For internal test
45cdf0e10cSrcweir *   usage. Contains current thread number. </li>
46cdf0e10cSrcweir *  <li> Test environment variable <code>'THRCNT'</code> : number
47cdf0e10cSrcweir *   of interface threads running concurently. </li>
48cdf0e10cSrcweir * <ul> <p>
49cdf0e10cSrcweir * XIndexComtainer needs n ObjectRelations "INSTANCEn" , where n=1, ...,
50cdf0e10cSrcweir * THRCNT.<p>
51cdf0e10cSrcweir * When this interface tested by different threads, it must use different
52cdf0e10cSrcweir * instances to insert/remove - one for each thread.
53cdf0e10cSrcweir * <p>
54cdf0e10cSrcweir * That's why we use objRelation "XIndexComtainerINDEX" to store the number of
55cdf0e10cSrcweir * last taken instance. If there is no such relation, it initialize with 1.
56cdf0e10cSrcweir * <p>
57cdf0e10cSrcweir * This ObjectRelations should be necessary to create an Object,
58cdf0e10cSrcweir * which is insertable by insterByIndex()
59cdf0e10cSrcweir * INSTANCEn are n Objectrelations so that every thread can isert it's own
60cdf0e10cSrcweir * object. n depends on the variable THRCNT which and comes from API.INI
61cdf0e10cSrcweir * <p>
62cdf0e10cSrcweir * Why that:
63cdf0e10cSrcweir * If you insert the same Object by insertByIndex() several times you
64cdf0e10cSrcweir * don't insert the Object several times. The first insertByIndex() inserts
65cdf0e10cSrcweir * the Object to the Container but all other insertByIndex() changes
66cdf0e10cSrcweir * the Index in the Continer because it's the same Object. <p>
67cdf0e10cSrcweir * Test is multithread compilant. <p>
68cdf0e10cSrcweir * @see com.sun.star.container.XIndexContainer
69cdf0e10cSrcweir */
70cdf0e10cSrcweir 
71cdf0e10cSrcweir public class _XIndexContainer extends MultiMethodTest {
72cdf0e10cSrcweir     public XIndexContainer oObj = null;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir      int Index = 0;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     /**
77cdf0e10cSrcweir     * First tries to insert proper object. Second tries to insert
78cdf0e10cSrcweir     * null value. For each test thread different objects are inserted
79cdf0e10cSrcweir     * on different indexes. For exmaple for the first started test index
80cdf0e10cSrcweir     * is 0 and object is get from relation 'INCTANCE1', and so on. <p>
81cdf0e10cSrcweir     * Has <b>OK</b> status if in the first case <code>getByIndex</code>
82cdf0e10cSrcweir     * method returns non null value and in the second <code>
83cdf0e10cSrcweir     * IndexOutOfBoundsException</code> was thrown.
84cdf0e10cSrcweir     */
_insertByIndex()85cdf0e10cSrcweir     public void _insertByIndex() {
86cdf0e10cSrcweir         boolean result = true;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         log.println("get ObjRelation(\"XIndexContainerINDEX\")");
89cdf0e10cSrcweir         String sIndex = (String)tEnv.getObjRelation("XIndexContainerINDEX");
90cdf0e10cSrcweir         if (sIndex == null) {
91cdf0e10cSrcweir             log.println("No XIndexContainerINDEX - so set it to 1.");
92cdf0e10cSrcweir             tEnv.addObjRelation("XIndexContainerINDEX", Integer.toString(1));
93cdf0e10cSrcweir             Index = 1;
94cdf0e10cSrcweir         } else {
95cdf0e10cSrcweir             Index = Integer.parseInt(sIndex);
96cdf0e10cSrcweir             Index++;
97cdf0e10cSrcweir             tEnv.addObjRelation("XIndexContainerINDEX",
98cdf0e10cSrcweir                 Integer.toString(Index));
99cdf0e10cSrcweir         }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         log.println("get ObjRelation(\"INSTANCE" + Index +"\")");
103cdf0e10cSrcweir         Object oInstance = tEnv.getObjRelation("INSTANCE"+ Index);
104cdf0e10cSrcweir         if (oInstance == null) {
105cdf0e10cSrcweir             log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a.");
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         log.println("testing insertByIndex(\"" + Index + "\")...");
109cdf0e10cSrcweir         try {
110cdf0e10cSrcweir             oObj.insertByIndex(Index, oInstance);
111cdf0e10cSrcweir             result &= oObj.getByIndex(Index) != null ;
112cdf0e10cSrcweir             log.println("insertByIndex(\""+Index+"\")...OK");
113cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
114cdf0e10cSrcweir             log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE");
115cdf0e10cSrcweir             result = false;
116cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
117cdf0e10cSrcweir             log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE");
118cdf0e10cSrcweir             result = false;
119cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
120cdf0e10cSrcweir             log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE");
121cdf0e10cSrcweir             result = false;
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         log.println("inserting a wrong Object occurs Exceptions ...");
125cdf0e10cSrcweir         try {
126cdf0e10cSrcweir             Object dummy = null;
127cdf0e10cSrcweir             oObj.insertByIndex(0, dummy);
128cdf0e10cSrcweir             log.println("No Exception: -> FALSE");
129cdf0e10cSrcweir             result = false;
130cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
131cdf0e10cSrcweir             log.println("Dummy-Exception: " + e + " -> OK");
132cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
133cdf0e10cSrcweir             log.println("!!! Wrong Exception: " + e + " -> FAILED");
134cdf0e10cSrcweir             result = false;
135cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
136cdf0e10cSrcweir             log.println("!!! Wrong Exception: " + e + " -> FAILED");
137cdf0e10cSrcweir             result = false;
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         tRes.tested("insertByIndex()", result);
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     /**
144cdf0e10cSrcweir     * Removes the element inserted by <code>insertByIndex</code> method test.
145cdf0e10cSrcweir     * The number of elements is checked before and after removing.
146cdf0e10cSrcweir     * Then tries to remove an element with invalid index and checks exceptions.
147cdf0e10cSrcweir     * <p>
148cdf0e10cSrcweir     * Has <b>OK</b> status if after removing number of elements decreases by
149cdf0e10cSrcweir     * one and <code>IndexOutOfBoundsException</code> is thrown on invalid index
150cdf0e10cSrcweir     * removing.
151cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
152cdf0e10cSrcweir     * <ul>
153cdf0e10cSrcweir     *  <li> <code> insertByIndex </code> : to have an object which can be
154cdf0e10cSrcweir     * removed.</li>
155cdf0e10cSrcweir     * </ul>
156cdf0e10cSrcweir     */
_removeByIndex()157cdf0e10cSrcweir     public void _removeByIndex() {
158cdf0e10cSrcweir         requiredMethod("insertByIndex()");
159cdf0e10cSrcweir         boolean result = true;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         log.println("testing removeByIndex() ...");
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         try {
164cdf0e10cSrcweir             log.println("remove " +Index);
165cdf0e10cSrcweir             int cnt1 = -1 , cnt2 = -1 ;
166cdf0e10cSrcweir             synchronized (oObj) {
167cdf0e10cSrcweir                 cnt1 = oObj.getCount() ;
168cdf0e10cSrcweir                 oObj.removeByIndex(Index);
169cdf0e10cSrcweir                 cnt2 = oObj.getCount() ;
170cdf0e10cSrcweir             }
171cdf0e10cSrcweir             log.println("Count before removing : " + cnt1 +
172cdf0e10cSrcweir                 ", and after : " + cnt2) ;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir             result &= cnt1 == cnt2 + 1 ;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir             log.println("1. removeByIndex(\""+Index+"\") ...OK");
177cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
178cdf0e10cSrcweir             result = false;
179cdf0e10cSrcweir             log.println("1. removeByIndex:(\""+Index+"\") " +
180cdf0e10cSrcweir                 e + " - FAILED");
181cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
182cdf0e10cSrcweir             result = false;
183cdf0e10cSrcweir             log.println("1. removeByIndex:(\""+Index+"\") " +
184cdf0e10cSrcweir                 e + " - FAILED");
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         log.println("removing a non existent object to get an exception");
188cdf0e10cSrcweir         try {
189cdf0e10cSrcweir             oObj.removeByIndex(100);
190cdf0e10cSrcweir             result = false;
191cdf0e10cSrcweir             log.println("2. removeByIndex(): Exception expected! - FAILED");
192cdf0e10cSrcweir         } catch (IndexOutOfBoundsException e) {
193cdf0e10cSrcweir             log.println("2. removeByIndex(): Expected exception - OK");
194cdf0e10cSrcweir             result &= true;
195cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
196cdf0e10cSrcweir             result = false;
197cdf0e10cSrcweir             log.println("2. removeByIndex(): Unexpected exception! - " +
198cdf0e10cSrcweir                 e + " - FAILED");
199cdf0e10cSrcweir         }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir         tRes.tested("removeByIndex()", result);
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 
207