merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / container / _XIndexReplace.java
blob82f79958469c21bc7faa3384220f308d515400d9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XIndexReplace.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.container;
33 import lib.MultiMethodTest;
34 import util.ValueComparer;
36 import com.sun.star.container.XIndexReplace;
37 import com.sun.star.container.XNameContainer;
38 import com.sun.star.lang.IllegalArgumentException;
39 import com.sun.star.lang.IndexOutOfBoundsException;
40 import com.sun.star.uno.UnoRuntime;
43 /**
44 * Testing <code>com.sun.star.container.XIndexReplace</code>
45 * interface methods :
46 * <ul>
47 * <li><code> replaceByIndex()</code></li>
48 * </ul>
49 * This test needs the following object relations :
50 * <ul>
51 * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
52 * which represents objects to be replaced with. See below
53 * for more information.</li>
54 * <li> <code>'XIndexReplaceINDEX'</code> : For internal test
55 * usage. Contains current thread number. </li>
56 * <li> Test environment variable <code>'THRCNT'</code> : number
57 * of interface threads running concurently. </li>
58 * <ul> <p>
59 * XIndexReplace needs n ObjectRelations "INSTANCEn" , where n = 1, ..., THRCNT.
60 * <p>
61 * When this interface tested by different threads, it must use different
62 * instances to replace - one for each thread.<p>
63 * That's why we use objRelation "XIndexReplaceINDEX" to store the number of
64 * last taken instance. If there is no such relation, it initialize with 1.
65 * <p>
66 * This ObjectRelations should be necessary to create an Object,
67 * which is can be replaced by index
68 * INSTANCEn are n Objectrelations so that every thread can isert it's own
69 * object. n depends on the variable THRCNT which and comes from API.INI
70 * <p>
71 * Why that:
72 * If you insert the same Object by replaceByIndex() several times you
73 * don't insert the Object several times. The first replaceByIndex() inserts
74 * the Object to the Container but all other replaceByIndex() changes
75 * the Index in the Continer because it's the same Object. <p>
76 * Test is multithread compilant. <p>
77 * @see com.sun.star.container.XIndexReplace
80 public class _XIndexReplace extends MultiMethodTest {
81 public XIndexReplace oObj = null;
83 /**
84 * Primarily tries to replace elements in a proper way :
85 * replaces the first, middle and the last element then
86 * checks if elements were properly replaced.
87 * Then wrong parameters are passed : invalid index and
88 * null value for replacing, and test checks for proper
89 * exceptions to be thrown. <p>
90 * In different threads it replaces elements with different
91 * objects.
92 * Has <b>OK</b> status if in the first (correct) case
93 * elements were successfully replaced (i.e. values got
94 * after replacing must be equal to those replaced with),
95 * and in the second case proper exceptions were thrown.
97 public void _replaceByIndex() {
98 boolean result = true;
99 Object old = null;
100 Object oInstance = null;
101 int Index = 0;
103 //get for every thread its own Object to insert it
104 log.println("get ObjRelation(\"XIndexReplaceINDEX\")");
105 String sIndex = (String)tEnv.getObjRelation("XIndexReplaceINDEX");
106 if (sIndex == null) {
107 log.println("No XIndexReplaceINDEX - so set it to 1.");
108 tEnv.addObjRelation("XIndexReplaceINDEX", Integer.toString(1));
109 Index = 1;
110 } else {
111 Index = Integer.parseInt(sIndex);
112 Index++;
113 tEnv.addObjRelation("XIndexReplaceINDEX", Integer.toString(Index));
117 log.println("get ObjRelation(\"INSTANCE" + Index +"\")");
118 oInstance = tEnv.getObjRelation("INSTANCE"+ Index);
119 if (oInstance == null) {
120 log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a.");
123 log.println("testing replaceByIndex(0)...");
125 try {
127 log.println("Getting old object");
128 old = oObj.getByIndex(0);
129 oObj.replaceByIndex(0, oInstance);
130 result = !(oObj.getByIndex(0)).equals(old);
131 result = ! ValueComparer.equalValue(oObj,old);
133 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
134 e.printStackTrace(log) ;
135 result = false;
136 } catch (com.sun.star.lang.IllegalArgumentException e) {
137 e.printStackTrace(log) ;
138 result = false;
139 } catch (com.sun.star.lang.WrappedTargetException e) {
140 e.printStackTrace(log) ;
141 result = false;
145 log.println("replace with a wrong Object occurs Exceptions ...");
146 try {
147 oObj.replaceByIndex(999, oInstance);
148 result = false;
149 log.println("1. replaceByIndex(): Exception expected! - FAILED");
152 XNameContainer xNC = (XNameContainer)
153 UnoRuntime.queryInterface(XNameContainer.class, oObj) ;
154 String[] names = xNC.getElementNames() ;
155 log.println("Element names :") ;
156 for (int i = 0; i<names.length; i++) {
157 log.println(" '" + names[i] + "'") ;
159 } catch (IndexOutOfBoundsException e) {
160 log.println("1. replaceByIndex(): Expected exception - OK");
161 result &= true;
162 } catch (com.sun.star.lang.IllegalArgumentException e) {
163 result = false;
164 log.println("1. replaceByIndex(): Unexpected exception! - " +
165 e + " - FAILED");
166 } catch (com.sun.star.lang.WrappedTargetException e) {
167 result = false;
168 log.println("1. replaceByIndex(): Unexpected exception! - " +
169 e + " - FAILED");
172 log.println("replace with a wrong Object occurs Exceptions ...");
173 try {
174 oObj.replaceByIndex(0, null);
175 result = false;
176 log.println("2. replaceByIndex(): Exception expected! - FAILED");
179 XNameContainer xNC = (XNameContainer)
180 UnoRuntime.queryInterface(XNameContainer.class, oObj) ;
181 String[] names = xNC.getElementNames() ;
182 log.println("Element names :") ;
183 for (int i = 0; i<names.length; i++) {
184 log.println(" '" + names[i] + "'") ;
186 } catch (IllegalArgumentException e) {
187 log.println("2. replaceByIndex(): Expected exception - OK");
188 result &= true;
189 } catch (com.sun.star.lang.WrappedTargetException e) {
190 result = false;
191 log.println("2. replaceByIndex(): Unexpected exception! - " +
192 e + " - FAILED");
193 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
194 result = false;
195 log.println("2. replaceByIndex(): Unexpected exception! - " +
196 e + " - FAILED");
199 log.println("replace with the old object");
200 try {
201 oObj.replaceByIndex(0, old);
202 } catch (IllegalArgumentException e) {
203 e.printStackTrace(log) ;
204 } catch (com.sun.star.lang.WrappedTargetException e) {
205 e.printStackTrace(log) ;
206 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
207 e.printStackTrace(log) ;
210 tRes.tested("replaceByIndex()", result);