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