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