1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.container;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
27cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
28cdf0e10cSrcweir import lib.MultiMethodTest;
29cdf0e10cSrcweir import lib.StatusException;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /**
32cdf0e10cSrcweir * Testing <code>com.sun.star.container.XNameContainer</code>
33cdf0e10cSrcweir * interface methods :
34cdf0e10cSrcweir * <ul>
35cdf0e10cSrcweir *  <li><code> insertByName()</code></li>
36cdf0e10cSrcweir *  <li><code> removeByName()</code></li>
37cdf0e10cSrcweir * </ul>
38cdf0e10cSrcweir * This test needs the following object relations :
39cdf0e10cSrcweir * <ul>
40cdf0e10cSrcweir *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
41cdf0e10cSrcweir *   which represents objects to be inserted. See below
42cdf0e10cSrcweir *   for more information.</li>
43cdf0e10cSrcweir *  <li> <code>'XNameContainerINDEX'</code> : For internal test
44cdf0e10cSrcweir *   usage. Contains current thread number. </li>
45cdf0e10cSrcweir *  <li> <code>'XNameContainer.AllowDuplicateNames'</code> <b>optional</b>:
46cdf0e10cSrcweir *   if this relation exists then container elements can have duplicate
47cdf0e10cSrcweir *   names. </li>
48cdf0e10cSrcweir *  <li> Test environment variable <code>'THRCNT'</code> : number
49cdf0e10cSrcweir *   of interface threads running concurently. </li>
50cdf0e10cSrcweir * <ul> <p>
51cdf0e10cSrcweir * XNameComtainer needs n ObjectRelations "INSTANCEn" , where n=1, ..., THRCNT.
52cdf0e10cSrcweir *
53cdf0e10cSrcweir * When this interface tested by different threads, it must use different
54cdf0e10cSrcweir * instances to insert/remove - one for each thread.
55cdf0e10cSrcweir *
56cdf0e10cSrcweir * That's why we use objRelation "XNameContainerINDEX" to store the number of
57cdf0e10cSrcweir * last taken instance. If there is no such relation, it initialize with 1.
58cdf0e10cSrcweir *
59cdf0e10cSrcweir * If you insert the same Object by insertByName() several times you
60cdf0e10cSrcweir * don't insert the Object several times. The first insertByName() inserts
61cdf0e10cSrcweir * the Object to the Container but all other insertByName() changes
62cdf0e10cSrcweir * the Name in the Continer because it's the same Object.
63cdf0e10cSrcweir * @see com.sun.star.container.XNameContainer
64cdf0e10cSrcweir */
65cdf0e10cSrcweir 
66cdf0e10cSrcweir public class _XNameContainer extends MultiMethodTest {
67cdf0e10cSrcweir     public XNameContainer oObj = null;
68cdf0e10cSrcweir     String Name = "XNameContainer";
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     /**
71cdf0e10cSrcweir     * First inserts object by name (different objects for different threads)
72cdf0e10cSrcweir     * and checks if it exists. Second, if duplicate names are not allowed
73cdf0e10cSrcweir     * test tries to insert element with the same name and checks for
74cdf0e10cSrcweir     * proper exception. Third, tries to add <code>null</code> element and
75cdf0e10cSrcweir     * checks for proper exception. <p>
76cdf0e10cSrcweir     * Has <b>OK</b> status if in the first case element added exists in
77cdf0e10cSrcweir     * the container, in the second case <code>ElementExistException</code>
78cdf0e10cSrcweir     * is thrown, and in the third case <code>IllegalArgumentException</code>
79cdf0e10cSrcweir     * is thrown.
80cdf0e10cSrcweir     */
_insertByName()81cdf0e10cSrcweir     public void _insertByName() {
82cdf0e10cSrcweir         boolean result = true;
83cdf0e10cSrcweir         int Index = 0;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         //get for every thread its own Object to insert it
86cdf0e10cSrcweir         log.println("get ObjRelation(\"XNameContainerINDEX\")");
87cdf0e10cSrcweir         String sIndex = null ;
88cdf0e10cSrcweir         synchronized (tEnv) {
89cdf0e10cSrcweir             sIndex = (String)tEnv.getObjRelation("XNameContainerINDEX");
90cdf0e10cSrcweir             if (sIndex == null) {
91cdf0e10cSrcweir                 log.println("No XNameContainerINDEX - so set it to 1.");
92cdf0e10cSrcweir                 tEnv.addObjRelation("XNameContainerINDEX",Integer.toString(1));
93cdf0e10cSrcweir                 Index = 1;
94cdf0e10cSrcweir             } else {
95cdf0e10cSrcweir                 Index = Integer.parseInt(sIndex);
96cdf0e10cSrcweir                 Index++;
97cdf0e10cSrcweir                 tEnv.addObjRelation("XNameContainerINDEX",
98cdf0e10cSrcweir                     Integer.toString(Index));
99cdf0e10cSrcweir             }
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir         Name += Index ;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         log.println("get ObjRelation(\"INSTANCE" + Index +"\")");
104cdf0e10cSrcweir         Object oInstance = tEnv.getObjRelation("INSTANCE"+ Index);
105cdf0e10cSrcweir         if (oInstance == null) {
106cdf0e10cSrcweir             log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a.");
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         log.println("testing insertByName(\""+Name+"\")...");
111cdf0e10cSrcweir         try {
112cdf0e10cSrcweir             String[] names = oObj.getElementNames() ;
113cdf0e10cSrcweir             log.println("Element names :") ;
114cdf0e10cSrcweir             for (int i = 0; i<names.length; i++) {
115cdf0e10cSrcweir                  log.println("  '" + names[i] + "'") ;
116cdf0e10cSrcweir             }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir             oObj.insertByName(Name, oInstance);
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             names = oObj.getElementNames() ;
121cdf0e10cSrcweir             log.println("Element names :") ;
122cdf0e10cSrcweir             for (int i = 0; i<names.length; i++) {
123cdf0e10cSrcweir                  log.println("  " + names[i]) ;
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir             result &= oObj.hasByName(Name) ;
127cdf0e10cSrcweir             log.println("insertByName(\""+Name+"\")...OK");
128cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
129cdf0e10cSrcweir             log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
130cdf0e10cSrcweir             result = false;
131cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
132cdf0e10cSrcweir             log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
133cdf0e10cSrcweir             result = false;
134cdf0e10cSrcweir         } catch (com.sun.star.container.ElementExistException e) {
135cdf0e10cSrcweir             log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
136cdf0e10cSrcweir             result = false;
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir         // if duplicate names is not allowed test for valid exception
140cdf0e10cSrcweir         if (tEnv.getObjRelation("XNameContainer.AllowDuplicateNames")==null) {
141cdf0e10cSrcweir             Object secondInstance = tEnv.getObjRelation("SecondInstance");
142cdf0e10cSrcweir             if (secondInstance != null) {
143cdf0e10cSrcweir                 oInstance = secondInstance;
144cdf0e10cSrcweir             }
145cdf0e10cSrcweir             log.println("Trying to add element with the same name ...") ;
146cdf0e10cSrcweir             try {
147cdf0e10cSrcweir                 oObj.insertByName(Name, oInstance);
148cdf0e10cSrcweir                 result = false ;
149cdf0e10cSrcweir                 log.println("!!! No exception were thrown !!!");
150cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException e) {
151cdf0e10cSrcweir                 log.println("!!! Wrong exception : " + e + " FALSE");
152cdf0e10cSrcweir                 result = false;
153cdf0e10cSrcweir             } catch (com.sun.star.lang.WrappedTargetException e) {
154cdf0e10cSrcweir                 log.println("!!! Wrong exception : " + e + " FALSE");
155cdf0e10cSrcweir                 result = false;
156cdf0e10cSrcweir             } catch (com.sun.star.container.ElementExistException e) {
157cdf0e10cSrcweir                 log.println("Right exception : " + e + " OK");
158cdf0e10cSrcweir             }
159cdf0e10cSrcweir         }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         log.println("inserting a wrong Object occurs Exceptions ...");
162cdf0e10cSrcweir         try {
163cdf0e10cSrcweir             Object dummy = null;
164cdf0e10cSrcweir             oObj.insertByName("Dummy", dummy);
165cdf0e10cSrcweir             log.println("No Exception: -> FALSE");
166cdf0e10cSrcweir             result = false;
167cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
168cdf0e10cSrcweir             log.println("Dummy-Exception: " + e + " -> OK");
169cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
170cdf0e10cSrcweir             log.println("!!! This exception not expected: " +e+ " -> FAILED");
171cdf0e10cSrcweir             result = false;
172cdf0e10cSrcweir         } catch (com.sun.star.container.ElementExistException e) {
173cdf0e10cSrcweir             log.println("!!! This exception not expected: " +e+ " -> FAILED");
174cdf0e10cSrcweir             result = false;
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir         tRes.tested("insertByName()", result);
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     } // end insertByName()
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     /**
182cdf0e10cSrcweir     * Test removes element inserted before and checks if element
183cdf0e10cSrcweir     * still exists in the container. Second test tries to remove
184cdf0e10cSrcweir     * element with non-existing name and checks for proper exception. <p>
185cdf0e10cSrcweir     * Has <b> OK </b> status if in the first case element doesn't
186cdf0e10cSrcweir     * exist anymore (or duplicate names are allowed), and in the
187cdf0e10cSrcweir     * second case <code>NoSuchElementException</code> is thrown. <p>
188cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
189cdf0e10cSrcweir     * <ul>
190cdf0e10cSrcweir     *  <li> <code> insertByName() </code> : to remove the element inserted
191cdf0e10cSrcweir     *    in this test. </li>
192cdf0e10cSrcweir     * </ul>
193cdf0e10cSrcweir     */
_removeByName()194cdf0e10cSrcweir     public void _removeByName() {
195cdf0e10cSrcweir         try {
196cdf0e10cSrcweir             requiredMethod("insertByName()");
197cdf0e10cSrcweir         } catch (StatusException e) {
198cdf0e10cSrcweir             // removing the name anywhere
199cdf0e10cSrcweir             try {
200cdf0e10cSrcweir                 oObj.removeByName(Name);
201cdf0e10cSrcweir             } catch (com.sun.star.container.NoSuchElementException e1) {
202cdf0e10cSrcweir             } catch (com.sun.star.lang.WrappedTargetException e1) {
203cdf0e10cSrcweir             }
204cdf0e10cSrcweir         }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         boolean result = true;
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         log.println("testing removeByName() ...");
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         try {
211cdf0e10cSrcweir             log.println("remove " + Name);
212cdf0e10cSrcweir             String[] names = oObj.getElementNames() ;
213cdf0e10cSrcweir             log.println("Element names :") ;
214cdf0e10cSrcweir             for (int i = 0; i<names.length; i++) {
215cdf0e10cSrcweir                 log.println("  " + names[i]) ;
216cdf0e10cSrcweir             }
217cdf0e10cSrcweir             oObj.removeByName(Name);
218cdf0e10cSrcweir             boolean loc_res = !oObj.hasByName(Name) || tEnv.getObjRelation
219cdf0e10cSrcweir                 ("XNameContainer.AllowDuplicateNames") != null ;
220cdf0e10cSrcweir             result &= loc_res ;
221cdf0e10cSrcweir             if (loc_res)
222cdf0e10cSrcweir                 log.println("1. removeByName(\""+Name+"\") ...OK");
223cdf0e10cSrcweir             else
224cdf0e10cSrcweir                 log.println("1. !!! Container still has element with name "
225cdf0e10cSrcweir                     + Name) ;
226cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
227cdf0e10cSrcweir             result = false;
228cdf0e10cSrcweir             log.println("1. removeByName:(\""+Name+"\") " + e + " - FAILED");
229cdf0e10cSrcweir         } catch (com.sun.star.container.NoSuchElementException e) {
230cdf0e10cSrcweir             result = false;
231cdf0e10cSrcweir             log.println("1. removeByName:(\""+Name+"\") " + e + " - FAILED");
232cdf0e10cSrcweir         }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         log.println("removing a non existent object to get an exception");
235cdf0e10cSrcweir         try {
236cdf0e10cSrcweir             oObj.removeByName(Name+ " dummy");
237cdf0e10cSrcweir             result = false;
238cdf0e10cSrcweir             log.println("2. removeByName(): Exception expected! - FAILED");
239cdf0e10cSrcweir         } catch (NoSuchElementException e) {
240cdf0e10cSrcweir             log.println("2. removeByName(): Expected exception - OK");
241cdf0e10cSrcweir             result &= true;
242cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
243cdf0e10cSrcweir             result = false;
244cdf0e10cSrcweir             log.println("2. removeByName(): Unexpected exception! - " +
245cdf0e10cSrcweir                 e + " - FAILED");
246cdf0e10cSrcweir         }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         tRes.tested("removeByName()", result);
249cdf0e10cSrcweir 
250cdf0e10cSrcweir         return;
251cdf0e10cSrcweir     } // end removeByName()
252cdf0e10cSrcweir } //XNameContainer
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 
256