Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / container / _XContainer.java
blob6d0d372558e5bd1cbbe458df12d357be8d57ef4f
1 /*
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 lib.MultiMethodTest;
22 import lib.Status;
23 import lib.StatusException;
25 import com.sun.star.awt.XControl;
26 import com.sun.star.awt.XControlContainer;
27 import com.sun.star.container.ContainerEvent;
28 import com.sun.star.container.XContainer;
29 import com.sun.star.container.XContainerListener;
30 import com.sun.star.container.XNameContainer;
31 import com.sun.star.container.XNameReplace;
32 import com.sun.star.lang.EventObject;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.uno.XNamingService;
37 /**
38 * Testing <code>com.sun.star.container.XContainer</code>
39 * interface methods :
40 * <ul>
41 * <li><code> addContainerListener()</code></li>
42 * <li><code> removeContainerListener()</code></li>
43 * </ul>
44 * This test needs the following object relations :
45 * <ul>
46 * <li> <code>'INSTANCE'</code> : Object which can be inserted into
47 * container.</li>
48 * <li> <code>'INSTANCE2'</code> : <b>(optional)</b>
49 * Object which can be inserted into container. The relation
50 * must be specified when container can't contain two
51 * identical objects. Replaces the first instance.</li>
52 * <li> <code>'XContainer.Container'</code> (of type
53 * <code>com.sun.star.container.XNameContainer</code>)
54 * <b>optional</b> : is required when the tested component
55 * doesn't implement <code>XNameContainer</code> and is used
56 * for adding and removing elements.</li>
57 * <ul> <p>
58 * Object <b>must implement</b>
59 * <code>com.sun.star.container.XNameContainer</code>.
60 * <p>
61 * Test is <b> NOT </b> multithread compliant. <p>
62 * @see com.sun.star.container.XContainer
64 public class _XContainer extends MultiMethodTest {
66 public XContainer oObj = null;
67 private boolean bElementInserted = false;
68 private boolean bElementRemoved = false;
69 private boolean bElementReplaced = false;
70 private XNameContainer NC = null ;
71 private XControlContainer CC = null ;
72 private XNamingService NV = null ;
73 private XNameReplace NR = null ;
74 private Object inst = null ;
75 private Object inst2 = null ;
77 /**
78 * Retrieves object relations, and tries to query object for
79 * <code>XNameContainer</code> interface.
80 * @throws StatusException If one of relations not found or
81 * object doesn't implement <code>XNameContainer</code> interface.
83 @Override
84 public void before() throws StatusException {
85 // do this test with a different object
86 Object altObj = tEnv.getObjRelation("XContainer.AlternateObject");
87 if (altObj != null) {
88 oObj = UnoRuntime.queryInterface(XContainer.class, altObj);
91 NC = UnoRuntime.queryInterface
92 (XNameContainer.class, oObj) ;
94 Object container = null;
95 if (NC == null) {
96 container = tEnv.getObjRelation("XContainer.Container") ;
99 if (container != null) {
100 if (container instanceof com.sun.star.awt.XControlContainer) {
101 CC = (XControlContainer) container;
102 } else if (container instanceof com.sun.star.uno.XNamingService) {
103 NV = (XNamingService) container;
104 } else if (container instanceof com.sun.star.container.XNameReplace) {
105 NR = (XNameReplace) container;
106 inst2 = tEnv.getObjRelation("XContainer.NewValue");
107 inst = tEnv.getObjRelation("XContainer.ElementName");
108 } else if (container instanceof com.sun.star.container.XNameContainer) {
109 NC = (XNameContainer) container;
113 if (NC == null && CC == null && NV == null && NR == null)
114 throw new StatusException(
115 Status.failed("Neither object implements XNameContainer" +
116 " nor relation 'XContainer.Container' found.")) ;
118 if (inst == null)
119 inst = tEnv.getObjRelation("INSTANCE");
120 if (inst == null) {
121 log.println("No INSTANCE ObjRelation!!! ");
122 throw new StatusException(Status.failed("No INSTANCE ObjRelation!!!")) ;
124 if (inst2 == null)
125 inst2 = tEnv.getObjRelation("INSTANCE2");
129 * Listener implementation which just set flags on appropriate
130 * events.
132 public class MyListener implements XContainerListener {
133 public void elementInserted(ContainerEvent e) {
134 bElementInserted = true;
136 public void elementRemoved(ContainerEvent e) {
137 bElementRemoved = true;
139 public void elementReplaced(ContainerEvent e) {
140 bElementReplaced = true;
142 public void disposing (EventObject obj) {}
145 MyListener listener = new MyListener();
148 * Adds <code>MyListener</code> and performs all possible changes
149 * (insert, replace, remove) with container. The checks which
150 * events were called. <p>
151 * Has <b>OK</b> status if all methods of the listener were called.
153 public void _addContainerListener() {
154 boolean bResult = true;
156 oObj.addContainerListener(listener);
157 bResult &= performChanges();
158 //we can't replace if the container is XControlContainer
159 if (NC != null) bResult &= bElementReplaced;
160 // we do not remove and insert if the listener is triggered by XNameReplace
161 if (NR == null) bResult &= bElementRemoved;
162 if (NR == null) bResult &= bElementInserted;
164 if (!bResult) {
165 log.println("inserted was " + (bElementInserted ? "" : "NOT")
166 + " called.");
168 if (NC != null) {
169 log.println("replaced was " + (bElementReplaced ? "" : "NOT")
170 + " called.");
172 log.println("removed was " + (bElementRemoved ? "" : "NOT")
173 + " called.");
176 tRes.tested("addContainerListener()", bResult);
180 * Removes listener added before and performs all possible changes
181 * (insert, replace, remove) with container. The checks which
182 * events were called. <p>
183 * Has <b>OK</b> status if no methods of the listener were called. <p>
184 * The following method tests are to be completed successfully before :
185 * <ul>
186 * <li> <code> addContainerListener() </code> : to remove it now. </li>
187 * </ul>
189 public void _removeContainerListener() {
190 requiredMethod("addContainerListener()") ;
192 boolean bResult = true;
193 bElementReplaced = bElementRemoved = bElementInserted = false;
195 oObj.removeContainerListener(listener);
196 bResult &= performChanges();
197 bResult &= !bElementReplaced;
198 bResult &= !bElementRemoved;
199 bResult &= !bElementInserted;
201 tRes.tested("removeContainerListener()", bResult);
205 * Inserts, replaces and finally removes object from container.
206 * Object is gotten from <code>'INSTANCE'</code> relation. If
207 * the relation <code>'INSTANCE2'</code> exists then the first
208 * instance is replaced with second.
210 protected boolean performChanges() {
211 if (CC != null) return performChanges2();
212 if (NV != null) return performChanges3();
213 if (NR != null) return performChanges4();
214 boolean bResult = true;
215 try {
216 String[] names = NC.getElementNames();
217 log.println("Elements count = " + names.length);
218 NC.insertByName("XContainer_dummy", inst);
219 names = NC.getElementNames();
220 log.println("Elements count = " + names.length);
221 if (inst2 == null) {
222 NC.replaceByName("XContainer_dummy", inst);
223 } else {
224 NC.replaceByName("XContainer_dummy", inst2);
226 NC.removeByName("XContainer_dummy");
227 } catch (com.sun.star.lang.IllegalArgumentException ex) {
228 log.println("Exception occurred ");
229 ex.printStackTrace(log);
230 bResult = false;
231 } catch (com.sun.star.lang.WrappedTargetException ex) {
232 log.println("Exception occurred ");
233 ex.printStackTrace(log);
234 bResult = false;
235 } catch (com.sun.star.container.NoSuchElementException ex) {
236 log.println("Exception occurred ");
237 ex.printStackTrace(log);
238 bResult = false;
239 } catch (com.sun.star.container.ElementExistException ex) {
240 log.println("Exception occurred ");
241 ex.printStackTrace(log);
242 bResult = false;
245 return bResult;
249 * In case no XNameContainer is available, but a XControlContainer
250 * instead.
251 * the XControl instance is inserted
252 * Method returns true if the count of Controls is changed afterwards
254 protected boolean performChanges2() {
255 int precount = CC.getControls().length;
256 CC.addControl("NewControl",(XControl) inst);
257 waitForEventIdle();
258 int count = CC.getControls().length;
259 CC.removeControl(CC.getControl("NewControl"));
260 waitForEventIdle();
261 return count>precount;
265 * In case no XNameContainer is available, but a XNamingService
266 * instead.
267 * the instance is registered and revoked again
268 * Method return true if getRegisteredObject() works after
269 * registering and doesn't after revoke
271 protected boolean performChanges3() {
272 boolean res = true;
273 Object reg = null;
275 try {
276 reg = NV.getRegisteredObject("MyFactory");
277 NV.revokeObject("MyFactory");
278 } catch (Exception e) {
282 try {
283 NV.registerObject("MyFactory", inst);
284 reg = NV.getRegisteredObject("MyFactory");
285 res &= (reg != null);
286 } catch (Exception e) {
287 e.printStackTrace(log);
288 log.println("registerObject failed");
289 res &= false;
292 try {
293 NV.revokeObject("MyFactory");
294 reg = NV.getRegisteredObject("MyFactory");
295 log.println("revokeObject failed");
296 res &= false;
297 } catch (Exception e) {
298 res &= true;
301 return res;
305 * In case no XNameContainer is available, but a XNamingReplace
306 * instead.
308 protected boolean performChanges4() {
309 boolean res = true;
310 Object newValue = inst2;
311 Object originalValue = null;
312 String name = null;
314 try {
315 name = (String)inst;
317 catch(java.lang.ClassCastException e) {
318 log.write("Expected String as object relations 'XContainer.ElementName'.");
319 e.printStackTrace(log);
320 return false;
323 try {
324 originalValue = NR.getByName(name);
325 NR.replaceByName(name, newValue);
326 } catch (Exception e) {
327 e.printStackTrace(log);
328 res = false;
331 try {
332 NR.replaceByName(name, originalValue);
333 } catch (Exception e) {
334 e.printStackTrace(log);
335 res = false;
338 return res;