1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 package ifc.container;
29 
30 import com.sun.star.container.NoSuchElementException;
31 import com.sun.star.container.XNameContainer;
32 import lib.MultiMethodTest;
33 import lib.StatusException;
34 
35 /**
36 * Testing <code>com.sun.star.container.XNameContainer</code>
37 * interface methods :
38 * <ul>
39 *  <li><code> insertByName()</code></li>
40 *  <li><code> removeByName()</code></li>
41 * </ul>
42 * This test needs the following object relations :
43 * <ul>
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
51 *   names. </li>
52 *  <li> Test environment variable <code>'THRCNT'</code> : number
53 *   of interface threads running concurently. </li>
54 * <ul> <p>
55 * XNameComtainer needs n ObjectRelations "INSTANCEn" , where n=1, ..., THRCNT.
56 *
57 * When this interface tested by different threads, it must use different
58 * instances to insert/remove - one for each thread.
59 *
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.
62 *
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
68 */
69 
70 public class _XNameContainer extends MultiMethodTest {
71     public XNameContainer oObj = null;
72     String Name = "XNameContainer";
73 
74     /**
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>
83     * is thrown.
84     */
85     public void _insertByName() {
86         boolean result = true;
87         int Index = 0;
88 
89         //get for every thread its own Object to insert it
90         log.println("get ObjRelation(\"XNameContainerINDEX\")");
91         String sIndex = null ;
92         synchronized (tEnv) {
93             sIndex = (String)tEnv.getObjRelation("XNameContainerINDEX");
94             if (sIndex == null) {
95                 log.println("No XNameContainerINDEX - so set it to 1.");
96                 tEnv.addObjRelation("XNameContainerINDEX",Integer.toString(1));
97                 Index = 1;
98             } else {
99                 Index = Integer.parseInt(sIndex);
100                 Index++;
101                 tEnv.addObjRelation("XNameContainerINDEX",
102                     Integer.toString(Index));
103             }
104         }
105         Name += Index ;
106 
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.");
111         }
112 
113 
114         log.println("testing insertByName(\""+Name+"\")...");
115         try {
116             String[] names = oObj.getElementNames() ;
117             log.println("Element names :") ;
118             for (int i = 0; i<names.length; i++) {
119                  log.println("  '" + names[i] + "'") ;
120             }
121 
122             oObj.insertByName(Name, oInstance);
123 
124             names = oObj.getElementNames() ;
125             log.println("Element names :") ;
126             for (int i = 0; i<names.length; i++) {
127                  log.println("  " + names[i]) ;
128             }
129 
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");
134             result = false;
135         } catch (com.sun.star.lang.WrappedTargetException e) {
136             log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
137             result = false;
138         } catch (com.sun.star.container.ElementExistException e) {
139             log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
140             result = false;
141         }
142 
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;
148             }
149             log.println("Trying to add element with the same name ...") ;
150             try {
151                 oObj.insertByName(Name, oInstance);
152                 result = false ;
153                 log.println("!!! No exception were thrown !!!");
154             } catch (com.sun.star.lang.IllegalArgumentException e) {
155                 log.println("!!! Wrong exception : " + e + " FALSE");
156                 result = false;
157             } catch (com.sun.star.lang.WrappedTargetException e) {
158                 log.println("!!! Wrong exception : " + e + " FALSE");
159                 result = false;
160             } catch (com.sun.star.container.ElementExistException e) {
161                 log.println("Right exception : " + e + " OK");
162             }
163         }
164 
165         log.println("inserting a wrong Object occurs Exceptions ...");
166         try {
167             Object dummy = null;
168             oObj.insertByName("Dummy", dummy);
169             log.println("No Exception: -> FALSE");
170             result = 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");
175             result = false;
176         } catch (com.sun.star.container.ElementExistException e) {
177             log.println("!!! This exception not expected: " +e+ " -> FAILED");
178             result = false;
179         }
180 
181         tRes.tested("insertByName()", result);
182 
183     } // end insertByName()
184 
185     /**
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 :
193     * <ul>
194     *  <li> <code> insertByName() </code> : to remove the element inserted
195     *    in this test. </li>
196     * </ul>
197     */
198     public void _removeByName() {
199         try {
200             requiredMethod("insertByName()");
201         } catch (StatusException e) {
202             // removing the name anywhere
203             try {
204                 oObj.removeByName(Name);
205             } catch (com.sun.star.container.NoSuchElementException e1) {
206             } catch (com.sun.star.lang.WrappedTargetException e1) {
207             }
208         }
209 
210         boolean result = true;
211 
212         log.println("testing removeByName() ...");
213 
214         try {
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]) ;
220             }
221             oObj.removeByName(Name);
222             boolean loc_res = !oObj.hasByName(Name) || tEnv.getObjRelation
223                 ("XNameContainer.AllowDuplicateNames") != null ;
224             result &= loc_res ;
225             if (loc_res)
226                 log.println("1. removeByName(\""+Name+"\") ...OK");
227             else
228                 log.println("1. !!! Container still has element with name "
229                     + Name) ;
230         } catch (com.sun.star.lang.WrappedTargetException e) {
231             result = false;
232             log.println("1. removeByName:(\""+Name+"\") " + e + " - FAILED");
233         } catch (com.sun.star.container.NoSuchElementException e) {
234             result = false;
235             log.println("1. removeByName:(\""+Name+"\") " + e + " - FAILED");
236         }
237 
238         log.println("removing a non existent object to get an exception");
239         try {
240             oObj.removeByName(Name+ " dummy");
241             result = false;
242             log.println("2. removeByName(): Exception expected! - FAILED");
243         } catch (NoSuchElementException e) {
244             log.println("2. removeByName(): Expected exception - OK");
245             result &= true;
246         } catch (com.sun.star.lang.WrappedTargetException e) {
247             result = false;
248             log.println("2. removeByName(): Unexpected exception! - " +
249                 e + " - FAILED");
250         }
251 
252         tRes.tested("removeByName()", result);
253 
254         return;
255     } // end removeByName()
256 } //XNameContainer
257 
258 
259 
260