1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XNameContainer.java,v $
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
;
39 * Testing <code>com.sun.star.container.XNameContainer</code>
42 * <li><code> insertByName()</code></li>
43 * <li><code> removeByName()</code></li>
45 * This test needs the following object relations :
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
55 * <li> Test environment variable <code>'THRCNT'</code> : number
56 * of interface threads running concurently. </li>
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";
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>
88 public void _insertByName() {
89 boolean result
= true;
92 //get for every thread its own Object to insert it
93 log
.println("get ObjRelation(\"XNameContainerINDEX\")");
94 String sIndex
= null ;
96 sIndex
= (String
)tEnv
.getObjRelation("XNameContainerINDEX");
98 log
.println("No XNameContainerINDEX - so set it to 1.");
99 tEnv
.addObjRelation("XNameContainerINDEX",Integer
.toString(1));
102 Index
= Integer
.parseInt(sIndex
);
104 tEnv
.addObjRelation("XNameContainerINDEX",
105 Integer
.toString(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
+"\")...");
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");
138 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
139 log
.println("insertByName(\""+Name
+"\"): " + e
+ " FALSE");
141 } catch (com
.sun
.star
.container
.ElementExistException e
) {
142 log
.println("insertByName(\""+Name
+"\"): " + e
+ " 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 ...") ;
154 oObj
.insertByName(Name
, oInstance
);
156 log
.println("!!! No exception were thrown !!!");
157 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
158 log
.println("!!! Wrong exception : " + e
+ " FALSE");
160 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
161 log
.println("!!! Wrong exception : " + e
+ " 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 ...");
171 oObj
.insertByName("Dummy", dummy
);
172 log
.println("No Exception: -> 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");
179 } catch (com
.sun
.star
.container
.ElementExistException e
) {
180 log
.println("!!! This exception not expected: " +e
+ " -> FAILED");
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 :
197 * <li> <code> insertByName() </code> : to remove the element inserted
198 * in this test. </li>
201 public void _removeByName() {
203 requiredMethod("insertByName()");
204 } catch (StatusException e
) {
205 // removing the name anywhere
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() ...");
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 ;
229 log
.println("1. removeByName(\""+Name
+"\") ...OK");
231 log
.println("1. !!! Container still has element with name "
233 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
235 log
.println("1. removeByName:(\""+Name
+"\") " + e
+ " - FAILED");
236 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
238 log
.println("1. removeByName:(\""+Name
+"\") " + e
+ " - FAILED");
241 log
.println("removing a non existent object to get an exception");
243 oObj
.removeByName(Name
+ " dummy");
245 log
.println("2. removeByName(): Exception expected! - FAILED");
246 } catch (NoSuchElementException e
) {
247 log
.println("2. removeByName(): Expected exception - OK");
249 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
251 log
.println("2. removeByName(): Unexpected exception! - " +
255 tRes
.tested("removeByName()", result
);
258 } // end removeByName()