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