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