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