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