1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package ifc
.container
;
30 import com
.sun
.star
.container
.NoSuchElementException
;
31 import com
.sun
.star
.container
.XNameContainer
;
32 import lib
.MultiMethodTest
;
33 import lib
.StatusException
;
36 * Testing <code>com.sun.star.container.XNameContainer</code>
39 * <li><code> insertByName()</code></li>
40 * <li><code> removeByName()</code></li>
42 * This test needs the following object relations :
44 * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
45 * which represents objects to be inserted. See below
46 * for more information.</li>
47 * <li> <code>'XNameContainerINDEX'</code> : For internal test
48 * usage. Contains current thread number. </li>
49 * <li> <code>'XNameContainer.AllowDuplicateNames'</code> <b>optional</b>:
50 * if this relation exists then container elements can have duplicate
52 * <li> Test environment variable <code>'THRCNT'</code> : number
53 * of interface threads running concurently. </li>
55 * XNameComtainer needs n ObjectRelations "INSTANCEn" , where n=1, ..., THRCNT.
57 * When this interface tested by different threads, it must use different
58 * instances to insert/remove - one for each thread.
60 * That's why we use objRelation "XNameContainerINDEX" to store the number of
61 * last taken instance. If there is no such relation, it initialize with 1.
63 * If you insert the same Object by insertByName() several times you
64 * don't insert the Object several times. The first insertByName() inserts
65 * the Object to the Container but all other insertByName() changes
66 * the Name in the Continer because it's the same Object.
67 * @see com.sun.star.container.XNameContainer
70 public class _XNameContainer
extends MultiMethodTest
{
71 public XNameContainer oObj
= null;
72 String Name
= "XNameContainer";
75 * First inserts object by name (different objects for different threads)
76 * and checks if it exists. Second, if duplicate names are not allowed
77 * test tries to insert element with the same name and checks for
78 * proper exception. Third, tries to add <code>null</code> element and
79 * checks for proper exception. <p>
80 * Has <b>OK</b> status if in the first case element added exists in
81 * the container, in the second case <code>ElementExistException</code>
82 * is thrown, and in the third case <code>IllegalArgumentException</code>
85 public void _insertByName() {
86 boolean result
= true;
89 //get for every thread its own Object to insert it
90 log
.println("get ObjRelation(\"XNameContainerINDEX\")");
91 String sIndex
= null ;
93 sIndex
= (String
)tEnv
.getObjRelation("XNameContainerINDEX");
95 log
.println("No XNameContainerINDEX - so set it to 1.");
96 tEnv
.addObjRelation("XNameContainerINDEX",Integer
.toString(1));
99 Index
= Integer
.parseInt(sIndex
);
101 tEnv
.addObjRelation("XNameContainerINDEX",
102 Integer
.toString(Index
));
107 log
.println("get ObjRelation(\"INSTANCE" + Index
+"\")");
108 Object oInstance
= tEnv
.getObjRelation("INSTANCE"+ Index
);
109 if (oInstance
== null) {
110 log
.println("ObjRelation(\"INSTANCE" + Index
+"\") Object n.a.");
114 log
.println("testing insertByName(\""+Name
+"\")...");
116 String
[] names
= oObj
.getElementNames() ;
117 log
.println("Element names :") ;
118 for (int i
= 0; i
<names
.length
; i
++) {
119 log
.println(" '" + names
[i
] + "'") ;
122 oObj
.insertByName(Name
, oInstance
);
124 names
= oObj
.getElementNames() ;
125 log
.println("Element names :") ;
126 for (int i
= 0; i
<names
.length
; i
++) {
127 log
.println(" " + names
[i
]) ;
130 result
&= oObj
.hasByName(Name
) ;
131 log
.println("insertByName(\""+Name
+"\")...OK");
132 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
133 log
.println("insertByName(\""+Name
+"\"): " + e
+ " FALSE");
135 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
136 log
.println("insertByName(\""+Name
+"\"): " + e
+ " FALSE");
138 } catch (com
.sun
.star
.container
.ElementExistException e
) {
139 log
.println("insertByName(\""+Name
+"\"): " + e
+ " FALSE");
143 // if duplicate names is not allowed test for valid exception
144 if (tEnv
.getObjRelation("XNameContainer.AllowDuplicateNames")==null) {
145 Object secondInstance
= tEnv
.getObjRelation("SecondInstance");
146 if (secondInstance
!= null) {
147 oInstance
= secondInstance
;
149 log
.println("Trying to add element with the same name ...") ;
151 oObj
.insertByName(Name
, oInstance
);
153 log
.println("!!! No exception were thrown !!!");
154 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
155 log
.println("!!! Wrong exception : " + e
+ " FALSE");
157 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
158 log
.println("!!! Wrong exception : " + e
+ " FALSE");
160 } catch (com
.sun
.star
.container
.ElementExistException e
) {
161 log
.println("Right exception : " + e
+ " OK");
165 log
.println("inserting a wrong Object occurs Exceptions ...");
168 oObj
.insertByName("Dummy", dummy
);
169 log
.println("No Exception: -> FALSE");
171 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
172 log
.println("Dummy-Exception: " + e
+ " -> OK");
173 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
174 log
.println("!!! This exception not expected: " +e
+ " -> FAILED");
176 } catch (com
.sun
.star
.container
.ElementExistException e
) {
177 log
.println("!!! This exception not expected: " +e
+ " -> FAILED");
181 tRes
.tested("insertByName()", result
);
183 } // end insertByName()
186 * Test removes element inserted before and checks if element
187 * still exists in the container. Second test tries to remove
188 * element with non-existing name and checks for proper exception. <p>
189 * Has <b> OK </b> status if in the first case element doesn't
190 * exist anymore (or duplicate names are allowed), and in the
191 * second case <code>NoSuchElementException</code> is thrown. <p>
192 * The following method tests are to be completed successfully before :
194 * <li> <code> insertByName() </code> : to remove the element inserted
195 * in this test. </li>
198 public void _removeByName() {
200 requiredMethod("insertByName()");
201 } catch (StatusException e
) {
202 // removing the name anywhere
204 oObj
.removeByName(Name
);
205 } catch (com
.sun
.star
.container
.NoSuchElementException e1
) {
206 } catch (com
.sun
.star
.lang
.WrappedTargetException e1
) {
210 boolean result
= true;
212 log
.println("testing removeByName() ...");
215 log
.println("remove " + Name
);
216 String
[] names
= oObj
.getElementNames() ;
217 log
.println("Element names :") ;
218 for (int i
= 0; i
<names
.length
; i
++) {
219 log
.println(" " + names
[i
]) ;
221 oObj
.removeByName(Name
);
222 boolean loc_res
= !oObj
.hasByName(Name
) || tEnv
.getObjRelation
223 ("XNameContainer.AllowDuplicateNames") != null ;
226 log
.println("1. removeByName(\""+Name
+"\") ...OK");
228 log
.println("1. !!! Container still has element with name "
230 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
232 log
.println("1. removeByName:(\""+Name
+"\") " + e
+ " - FAILED");
233 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
235 log
.println("1. removeByName:(\""+Name
+"\") " + e
+ " - FAILED");
238 log
.println("removing a non existent object to get an exception");
240 oObj
.removeByName(Name
+ " dummy");
242 log
.println("2. removeByName(): Exception expected! - FAILED");
243 } catch (NoSuchElementException e
) {
244 log
.println("2. removeByName(): Expected exception - OK");
246 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
248 log
.println("2. removeByName(): Unexpected exception! - " +
252 tRes
.tested("removeByName()", result
);
255 } // end removeByName()