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