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