1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XContainer.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc
.container
;
33 import java
.io
.PrintWriter
;
35 import lib
.MultiMethodTest
;
37 import lib
.StatusException
;
39 import com
.sun
.star
.awt
.XControl
;
40 import com
.sun
.star
.awt
.XControlContainer
;
41 import com
.sun
.star
.container
.ContainerEvent
;
42 import com
.sun
.star
.container
.XContainer
;
43 import com
.sun
.star
.container
.XContainerListener
;
44 import com
.sun
.star
.container
.XNameContainer
;
45 import com
.sun
.star
.container
.XNameReplace
;
46 import com
.sun
.star
.lang
.EventObject
;
47 import com
.sun
.star
.uno
.UnoRuntime
;
48 import com
.sun
.star
.uno
.XNamingService
;
52 * Testing <code>com.sun.star.container.XContainer</code>
55 * <li><code> addContainerListener()</code></li>
56 * <li><code> removeContainerListener()</code></li>
58 * This test needs the following object relations :
60 * <li> <code>'INSTANCE'</code> : Object which can be inserted into
62 * <li> <code>'INSTANCE2'</code> : <b>(optional)</b>
63 * Object which can be inserted into container. The relation
64 * must be specified when container cann't contain two
65 * identical objects. Replaces the first instance.</li>
66 * <li> <code>'XContainer.Container'</code> (of type
67 * <code>com.sun.star.container.XNameContainer</code>)
68 * <b>optional</b> : is required when the tested component
69 * doesn't implement <code>XNameContainer</code> and is used
70 * for adding and removing elements.</li>
72 * Object <b>must implement</b>
73 * <code>com.sun.star.container.XNameContainer</code>.
75 * Test is <b> NOT </b> multithread compilant. <p>
76 * @see com.sun.star.container.XContainer
78 public class _XContainer
extends MultiMethodTest
{
80 public XContainer oObj
= null;
81 private boolean bElementInserted
= false;
82 private boolean bElementRemoved
= false;
83 private boolean bElementReplaced
= false;
84 private PrintWriter _log
= null;
85 private XNameContainer NC
= null ;
86 private XControlContainer CC
= null ;
87 private XNamingService NV
= null ;
88 private XNameReplace NR
= null ;
89 private Object inst
= null ;
90 private Object inst2
= null ;
93 * Retrieves object relations, and tries to query object for
94 * <code>XNameContainer</code> interface.
95 * @throws StatusException If one of relations not found or
96 * object doesn't implement <code>XNameContainer</code> interface.
98 public void before() throws StatusException
{
101 // do this test with a different object
102 Object altObj
= tEnv
.getObjRelation("XContainer.AlternateObject");
103 if (altObj
!= null) {
104 oObj
= (XContainer
)UnoRuntime
.queryInterface(XContainer
.class, altObj
);
107 NC
= (XNameContainer
) UnoRuntime
.queryInterface
108 (XNameContainer
.class, oObj
) ;
110 Object container
= null;
112 container
= tEnv
.getObjRelation("XContainer.Container") ;
115 if (container
!= null) {
116 if (container
instanceof com
.sun
.star
.awt
.XControlContainer
) {
117 CC
= (XControlContainer
) container
;
118 } else if (container
instanceof com
.sun
.star
.uno
.XNamingService
) {
119 NV
= (XNamingService
) container
;
120 } else if (container
instanceof com
.sun
.star
.container
.XNameReplace
) {
121 NR
= (XNameReplace
) container
;
122 inst2
= tEnv
.getObjRelation("XContainer.NewValue");
123 inst
= tEnv
.getObjRelation("XContainer.ElementName");
124 } else if (container
instanceof com
.sun
.star
.container
.XNameContainer
) {
125 NC
= (XNameContainer
) container
;
129 if (NC
== null && CC
== null && NV
== null && NR
== null)
130 throw new StatusException(
131 Status
.failed("Neither object implements XNameContainer" +
132 " nor relation 'XContainer.Container' found.")) ;
135 inst
= tEnv
.getObjRelation("INSTANCE");
137 log
.println("No INSTANCE ObjRelation!!! ");
138 throw new StatusException(Status
.failed("No INSTANCE ObjRelation!!!")) ;
141 inst2
= tEnv
.getObjRelation("INSTANCE2");
145 * Listener implementation which just set flags on appropriate
148 public class MyListener
implements XContainerListener
{
149 public void elementInserted(ContainerEvent e
) {
150 //_log.println("Element was inserted");
151 bElementInserted
= true;
153 public void elementRemoved(ContainerEvent e
) {
154 //_log.println("Element was removed");
155 bElementRemoved
= true;
157 public void elementReplaced(ContainerEvent e
) {
158 //_log.println("Element was replaced");
159 bElementReplaced
= true;
161 public void disposing (EventObject obj
) {}
164 MyListener listener
= new MyListener();
167 * Adds <code>MyListener</code> and performs all possible changes
168 * (insert, replace, remove) with container. The checks which
169 * events were called. <p>
170 * Has <b>OK</b> status if all methods of the listener were called.
172 public void _addContainerListener() {
173 boolean bResult
= true;
175 oObj
.addContainerListener(listener
);
176 bResult
&= performChanges();
177 //we can't replace if the container is XControlContainer
178 if (NC
!= null) bResult
&= bElementReplaced
;
179 // we do not remove and insert if the listener is triggered by XNameReplace
180 if (NR
== null) bResult
&= bElementRemoved
;
181 if (NR
== null) bResult
&= bElementInserted
;
184 log
.println("inserted was " + (bElementInserted ?
"" : "NOT")
188 log
.println("replaced was " + (bElementReplaced ?
"" : "NOT")
191 log
.println("removed was " + (bElementRemoved ?
"" : "NOT")
195 tRes
.tested("addContainerListener()", bResult
);
199 * Removes listener added before and performs all possible changes
200 * (insert, replace, remove) with container. The checks which
201 * events were called. <p>
202 * Has <b>OK</b> status if no methods of the listener were called. <p>
203 * The following method tests are to be completed successfully before :
205 * <li> <code> addContainerListener() </code> : to remove it now. </li>
208 public void _removeContainerListener() {
209 requiredMethod("addContainerListener()") ;
211 boolean bResult
= true;
212 bElementReplaced
= bElementRemoved
= bElementInserted
= false;
214 oObj
.removeContainerListener(listener
);
215 bResult
&= performChanges();
216 bResult
&= !bElementReplaced
;
217 bResult
&= !bElementRemoved
;
218 bResult
&= !bElementInserted
;
220 tRes
.tested("removeContainerListener()", bResult
);
224 * Inserts, replaces and finally removes object from container.
225 * Object is gotten from <code>'INSTANCE'</code> relation. If
226 * the relation <code>'INSTANCE2'</code> exists then the first
227 * instance is replaced with second.
229 protected boolean performChanges() {
230 if (CC
!= null) return performChanges2();
231 if (NV
!= null) return performChanges3();
232 if (NR
!= null) return performChanges4();
233 boolean bResult
= true;
235 String
[] names
= NC
.getElementNames();
236 log
.println("Elements count = " + names
.length
);
237 NC
.insertByName("XContainer_dummy", inst
);
238 names
= NC
.getElementNames();
239 log
.println("Elements count = " + names
.length
);
241 NC
.replaceByName("XContainer_dummy", inst
);
243 NC
.replaceByName("XContainer_dummy", inst2
);
245 NC
.removeByName("XContainer_dummy");
246 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
247 log
.println("Exception occured ");
248 ex
.printStackTrace(log
);
250 } catch (com
.sun
.star
.lang
.WrappedTargetException ex
) {
251 log
.println("Exception occured ");
252 ex
.printStackTrace(log
);
254 } catch (com
.sun
.star
.container
.NoSuchElementException ex
) {
255 log
.println("Exception occured ");
256 ex
.printStackTrace(log
);
258 } catch (com
.sun
.star
.container
.ElementExistException ex
) {
259 log
.println("Exception occured ");
260 ex
.printStackTrace(log
);
268 * In case no XNameContainer is available, but a XControlContainer
270 * the XControl instance is inserted
271 * Method returns true if the count of Controls is changed afterwards
273 protected boolean performChanges2() {
274 int precount
= CC
.getControls().length
;
275 CC
.addControl("NewControl",(XControl
) inst
);
277 int count
= CC
.getControls().length
;
278 CC
.removeControl(CC
.getControl("NewControl"));
280 return count
>precount
;
284 * In case no XNameContainer is available, but a XNamingService
286 * the instance is registered and revoked again
287 * Method return true if getRegisteredObject() works after
288 * registering and doesn't after revoke
290 protected boolean performChanges3() {
295 reg
= NV
.getRegisteredObject("MyFactory");
296 NV
.revokeObject("MyFactory");
297 } catch (Exception e
) {
302 NV
.registerObject("MyFactory", inst
);
303 reg
= NV
.getRegisteredObject("MyFactory");
304 res
&= (reg
!= null);
305 } catch (Exception e
) {
306 e
.printStackTrace(log
);
307 log
.println("registerObject failed");
312 NV
.revokeObject("MyFactory");
313 reg
= NV
.getRegisteredObject("MyFactory");
314 log
.println("revokeObject failed");
316 } catch (Exception e
) {
324 * In case no XNameContainer is available, but a XNamingReplace
327 protected boolean performChanges4() {
329 Object newValue
= inst2
;
330 Object originalValue
= null;
336 catch(java
.lang
.ClassCastException e
) {
337 log
.write("Expected String as object relations 'XContainer.ElementName'.");
338 e
.printStackTrace(log
);
343 originalValue
= NR
.getByName(name
);
344 NR
.replaceByName(name
, newValue
);
345 } catch (Exception e
) {
346 e
.printStackTrace(log
);
351 NR
.replaceByName(name
, originalValue
);
352 } catch (Exception e
) {
353 e
.printStackTrace(log
);
361 * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
364 private void shortWait() {
367 } catch (InterruptedException e
) {
368 log
.println("While waiting :" + e
) ;