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