2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc
.container
;
21 import com
.sun
.star
.container
.NoSuchElementException
;
22 import com
.sun
.star
.container
.XNameContainer
;
23 import lib
.MultiMethodTest
;
24 import lib
.StatusException
;
27 * Testing <code>com.sun.star.container.XNameContainer</code>
30 * <li><code> insertByName()</code></li>
31 * <li><code> removeByName()</code></li>
33 * This test needs the following object relations :
35 * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
36 * which represents objects to be inserted. See below
37 * for more information.</li>
38 * <li> <code>'XNameContainerINDEX'</code> : For internal test
39 * usage. Contains current thread number. </li>
40 * <li> <code>'XNameContainer.AllowDuplicateNames'</code> <b>optional</b>:
41 * if this relation exists then container elements can have duplicate
43 * <li> Test environment variable <code>'THRCNT'</code> : number
44 * of interface threads running concurrently. </li>
46 * XNameContainer needs n ObjectRelations "INSTANCEn" , where n=1, ..., THRCNT.
48 * When this interface tested by different threads, it must use different
49 * instances to insert/remove - one for each thread.
51 * That's why we use objRelation "XNameContainerINDEX" to store the number of
52 * last taken instance. If there is no such relation, it initialize with 1.
54 * If you insert the same Object by insertByName() several times you
55 * don't insert the Object several times. The first insertByName() inserts
56 * the Object to the Container but all other insertByName() changes
57 * the Name in the Container because it's the same Object.
58 * @see com.sun.star.container.XNameContainer
61 public class _XNameContainer
extends MultiMethodTest
{
62 public XNameContainer oObj
= null;
63 String Name
= "XNameContainer";
66 * First inserts object by name (different objects for different threads)
67 * and checks if it exists. Second, if duplicate names are not allowed
68 * test tries to insert element with the same name and checks for
69 * proper exception. Third, tries to add <code>null</code> element and
70 * checks for proper exception. <p>
71 * Has <b>OK</b> status if in the first case element added exists in
72 * the container, in the second case <code>ElementExistException</code>
73 * is thrown, and in the third case <code>IllegalArgumentException</code>
76 public void _insertByName() {
77 boolean result
= true;
80 //get for every thread its own Object to insert it
81 log
.println("get ObjRelation(\"XNameContainerINDEX\")");
82 String sIndex
= null ;
84 sIndex
= (String
)tEnv
.getObjRelation("XNameContainerINDEX");
86 log
.println("No XNameContainerINDEX - so set it to 1.");
87 tEnv
.addObjRelation("XNameContainerINDEX",Integer
.toString(1));
90 Index
= Integer
.parseInt(sIndex
);
92 tEnv
.addObjRelation("XNameContainerINDEX",
93 Integer
.toString(Index
));
98 log
.println("get ObjRelation(\"INSTANCE" + Index
+"\")");
99 Object oInstance
= tEnv
.getObjRelation("INSTANCE"+ Index
);
100 log
.println("ObjRelation(\"INSTANCE" + Index
+"\") Object n.a.");
102 log
.println("testing insertByName(\""+Name
+"\")...");
104 String
[] names
= oObj
.getElementNames() ;
105 log
.println("Element names :") ;
106 for (int i
= 0; i
<names
.length
; i
++) {
107 log
.println(" '" + names
[i
] + "'") ;
110 oObj
.insertByName(Name
, oInstance
);
112 names
= oObj
.getElementNames() ;
113 log
.println("Element names :") ;
114 for (int i
= 0; i
<names
.length
; i
++) {
115 log
.println(" " + names
[i
]) ;
118 result
&= oObj
.hasByName(Name
) ;
119 log
.println("insertByName(\""+Name
+"\")...OK");
120 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
121 log
.println("insertByName(\""+Name
+"\"): " + e
+ " FALSE");
123 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
124 log
.println("insertByName(\""+Name
+"\"): " + e
+ " FALSE");
126 } catch (com
.sun
.star
.container
.ElementExistException e
) {
127 log
.println("insertByName(\""+Name
+"\"): " + e
+ " FALSE");
131 // if duplicate names is not allowed test for valid exception
132 if (tEnv
.getObjRelation("XNameContainer.AllowDuplicateNames")==null) {
133 Object secondInstance
= tEnv
.getObjRelation("SecondInstance");
134 if (secondInstance
!= null) {
135 oInstance
= secondInstance
;
137 log
.println("Trying to add element with the same name ...") ;
139 oObj
.insertByName(Name
, oInstance
);
141 log
.println("!!! No exception were thrown !!!");
142 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
143 log
.println("!!! Wrong exception : " + e
+ " FALSE");
145 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
146 log
.println("!!! Wrong exception : " + e
+ " FALSE");
148 } catch (com
.sun
.star
.container
.ElementExistException e
) {
149 log
.println("Right exception : " + e
+ " OK");
153 log
.println("inserting a wrong Object occurs Exceptions ...");
156 oObj
.insertByName("Dummy", dummy
);
157 log
.println("No Exception: -> FALSE");
159 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
160 log
.println("Dummy-Exception: " + e
+ " -> OK");
161 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
162 log
.println("!!! This exception not expected: " +e
+ " -> FAILED");
164 } catch (com
.sun
.star
.container
.ElementExistException e
) {
165 log
.println("!!! This exception not expected: " +e
+ " -> FAILED");
169 tRes
.tested("insertByName()", result
);
171 } // end insertByName()
174 * Test removes element inserted before and checks if element
175 * still exists in the container. Second test tries to remove
176 * element with non-existing name and checks for proper exception. <p>
177 * Has <b> OK </b> status if in the first case element doesn't
178 * exist anymore (or duplicate names are allowed), and in the
179 * second case <code>NoSuchElementException</code> is thrown. <p>
180 * The following method tests are to be completed successfully before :
182 * <li> <code> insertByName() </code> : to remove the element inserted
183 * in this test. </li>
186 public void _removeByName() {
188 requiredMethod("insertByName()");
189 } catch (StatusException e
) {
190 // removing the name anywhere
192 oObj
.removeByName(Name
);
193 } catch (com
.sun
.star
.container
.NoSuchElementException e1
) {
194 } catch (com
.sun
.star
.lang
.WrappedTargetException e1
) {
198 boolean result
= true;
200 log
.println("testing removeByName() ...");
203 log
.println("remove " + Name
);
204 String
[] names
= oObj
.getElementNames() ;
205 log
.println("Element names :") ;
206 for (int i
= 0; i
<names
.length
; i
++) {
207 log
.println(" " + names
[i
]) ;
209 oObj
.removeByName(Name
);
210 boolean loc_res
= !oObj
.hasByName(Name
) || tEnv
.getObjRelation
211 ("XNameContainer.AllowDuplicateNames") != null ;
214 log
.println("1. removeByName(\""+Name
+"\") ...OK");
216 log
.println("1. !!! Container still has element with name "
218 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
220 log
.println("1. removeByName:(\""+Name
+"\") " + e
+ " - FAILED");
221 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
223 log
.println("1. removeByName:(\""+Name
+"\") " + e
+ " - FAILED");
226 log
.println("removing a non existent object to get an exception");
228 oObj
.removeByName(Name
+ " dummy");
230 log
.println("2. removeByName(): Exception expected! - FAILED");
231 } catch (NoSuchElementException e
) {
232 log
.println("2. removeByName(): Expected exception - OK");
234 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
236 log
.println("2. removeByName(): Unexpected exception! - " +
240 tRes
.tested("removeByName()", result
);
241 } // end removeByName()