merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / container / _XNameContainer.java
blobf427eefcda624451d8484f4da5a0768aa6021524
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: _XNameContainer.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 com.sun.star.container.NoSuchElementException;
34 import com.sun.star.container.XNameContainer;
35 import lib.MultiMethodTest;
36 import lib.StatusException;
38 /**
39 * Testing <code>com.sun.star.container.XNameContainer</code>
40 * interface methods :
41 * <ul>
42 * <li><code> insertByName()</code></li>
43 * <li><code> removeByName()</code></li>
44 * </ul>
45 * This test needs the following object relations :
46 * <ul>
47 * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
48 * which represents objects to be inserted. See below
49 * for more information.</li>
50 * <li> <code>'XNameContainerINDEX'</code> : For internal test
51 * usage. Contains current thread number. </li>
52 * <li> <code>'XNameContainer.AllowDuplicateNames'</code> <b>optional</b>:
53 * if this relation exists then container elements can have duplicate
54 * names. </li>
55 * <li> Test environment variable <code>'THRCNT'</code> : number
56 * of interface threads running concurently. </li>
57 * <ul> <p>
58 * XNameComtainer needs n ObjectRelations "INSTANCEn" , where n=1, ..., THRCNT.
60 * When this interface tested by different threads, it must use different
61 * instances to insert/remove - one for each thread.
63 * That's why we use objRelation "XNameContainerINDEX" to store the number of
64 * last taken instance. If there is no such relation, it initialize with 1.
66 * If you insert the same Object by insertByName() several times you
67 * don't insert the Object several times. The first insertByName() inserts
68 * the Object to the Container but all other insertByName() changes
69 * the Name in the Continer because it's the same Object.
70 * @see com.sun.star.container.XNameContainer
73 public class _XNameContainer extends MultiMethodTest {
74 public XNameContainer oObj = null;
75 String Name = "XNameContainer";
77 /**
78 * First inserts object by name (different objects for different threads)
79 * and checks if it exists. Second, if duplicate names are not allowed
80 * test tries to insert element with the same name and checks for
81 * proper exception. Third, tries to add <code>null</code> element and
82 * checks for proper exception. <p>
83 * Has <b>OK</b> status if in the first case element added exists in
84 * the container, in the second case <code>ElementExistException</code>
85 * is thrown, and in the third case <code>IllegalArgumentException</code>
86 * is thrown.
88 public void _insertByName() {
89 boolean result = true;
90 int Index = 0;
92 //get for every thread its own Object to insert it
93 log.println("get ObjRelation(\"XNameContainerINDEX\")");
94 String sIndex = null ;
95 synchronized (tEnv) {
96 sIndex = (String)tEnv.getObjRelation("XNameContainerINDEX");
97 if (sIndex == null) {
98 log.println("No XNameContainerINDEX - so set it to 1.");
99 tEnv.addObjRelation("XNameContainerINDEX",Integer.toString(1));
100 Index = 1;
101 } else {
102 Index = Integer.parseInt(sIndex);
103 Index++;
104 tEnv.addObjRelation("XNameContainerINDEX",
105 Integer.toString(Index));
108 Name += Index ;
110 log.println("get ObjRelation(\"INSTANCE" + Index +"\")");
111 Object oInstance = tEnv.getObjRelation("INSTANCE"+ Index);
112 if (oInstance == null) {
113 log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a.");
117 log.println("testing insertByName(\""+Name+"\")...");
118 try {
119 String[] names = oObj.getElementNames() ;
120 log.println("Element names :") ;
121 for (int i = 0; i<names.length; i++) {
122 log.println(" '" + names[i] + "'") ;
125 oObj.insertByName(Name, oInstance);
127 names = oObj.getElementNames() ;
128 log.println("Element names :") ;
129 for (int i = 0; i<names.length; i++) {
130 log.println(" " + names[i]) ;
133 result &= oObj.hasByName(Name) ;
134 log.println("insertByName(\""+Name+"\")...OK");
135 } catch (com.sun.star.lang.IllegalArgumentException e) {
136 log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
137 result = false;
138 } catch (com.sun.star.lang.WrappedTargetException e) {
139 log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
140 result = false;
141 } catch (com.sun.star.container.ElementExistException e) {
142 log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
143 result = false;
146 // if duplicate names is not allowed test for valid exception
147 if (tEnv.getObjRelation("XNameContainer.AllowDuplicateNames")==null) {
148 Object secondInstance = tEnv.getObjRelation("SecondInstance");
149 if (secondInstance != null) {
150 oInstance = secondInstance;
152 log.println("Trying to add element with the same name ...") ;
153 try {
154 oObj.insertByName(Name, oInstance);
155 result = false ;
156 log.println("!!! No exception were thrown !!!");
157 } catch (com.sun.star.lang.IllegalArgumentException e) {
158 log.println("!!! Wrong exception : " + e + " FALSE");
159 result = false;
160 } catch (com.sun.star.lang.WrappedTargetException e) {
161 log.println("!!! Wrong exception : " + e + " FALSE");
162 result = false;
163 } catch (com.sun.star.container.ElementExistException e) {
164 log.println("Right exception : " + e + " OK");
168 log.println("inserting a wrong Object occurs Exceptions ...");
169 try {
170 Object dummy = null;
171 oObj.insertByName("Dummy", dummy);
172 log.println("No Exception: -> FALSE");
173 result = false;
174 } catch (com.sun.star.lang.IllegalArgumentException e) {
175 log.println("Dummy-Exception: " + e + " -> OK");
176 } catch (com.sun.star.lang.WrappedTargetException e) {
177 log.println("!!! This exception not expected: " +e+ " -> FAILED");
178 result = false;
179 } catch (com.sun.star.container.ElementExistException e) {
180 log.println("!!! This exception not expected: " +e+ " -> FAILED");
181 result = false;
184 tRes.tested("insertByName()", result);
186 } // end insertByName()
189 * Test removes element inserted before and checks if element
190 * still exists in the container. Second test tries to remove
191 * element with non-existing name and checks for proper exception. <p>
192 * Has <b> OK </b> status if in the first case element doesn't
193 * exist anymore (or duplicate names are allowed), and in the
194 * second case <code>NoSuchElementException</code> is thrown. <p>
195 * The following method tests are to be completed successfully before :
196 * <ul>
197 * <li> <code> insertByName() </code> : to remove the element inserted
198 * in this test. </li>
199 * </ul>
201 public void _removeByName() {
202 try {
203 requiredMethod("insertByName()");
204 } catch (StatusException e) {
205 // removing the name anywhere
206 try {
207 oObj.removeByName(Name);
208 } catch (com.sun.star.container.NoSuchElementException e1) {
209 } catch (com.sun.star.lang.WrappedTargetException e1) {
213 boolean result = true;
215 log.println("testing removeByName() ...");
217 try {
218 log.println("remove " + Name);
219 String[] names = oObj.getElementNames() ;
220 log.println("Element names :") ;
221 for (int i = 0; i<names.length; i++) {
222 log.println(" " + names[i]) ;
224 oObj.removeByName(Name);
225 boolean loc_res = !oObj.hasByName(Name) || tEnv.getObjRelation
226 ("XNameContainer.AllowDuplicateNames") != null ;
227 result &= loc_res ;
228 if (loc_res)
229 log.println("1. removeByName(\""+Name+"\") ...OK");
230 else
231 log.println("1. !!! Container still has element with name "
232 + Name) ;
233 } catch (com.sun.star.lang.WrappedTargetException e) {
234 result = false;
235 log.println("1. removeByName:(\""+Name+"\") " + e + " - FAILED");
236 } catch (com.sun.star.container.NoSuchElementException e) {
237 result = false;
238 log.println("1. removeByName:(\""+Name+"\") " + e + " - FAILED");
241 log.println("removing a non existent object to get an exception");
242 try {
243 oObj.removeByName(Name+ " dummy");
244 result = false;
245 log.println("2. removeByName(): Exception expected! - FAILED");
246 } catch (NoSuchElementException e) {
247 log.println("2. removeByName(): Expected exception - OK");
248 result &= true;
249 } catch (com.sun.star.lang.WrappedTargetException e) {
250 result = false;
251 log.println("2. removeByName(): Unexpected exception! - " +
252 e + " - FAILED");
255 tRes.tested("removeByName()", result);
257 return;
258 } // end removeByName()
259 } //XNameContainer