Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / container / _XNameContainer.java
blobea2ab3ea3e7ef16d23ce50a4b6130458ea7e218e
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 com.sun.star.container.NoSuchElementException;
22 import com.sun.star.container.XNameContainer;
23 import lib.MultiMethodTest;
24 import lib.StatusException;
26 /**
27 * Testing <code>com.sun.star.container.XNameContainer</code>
28 * interface methods :
29 * <ul>
30 * <li><code> insertByName()</code></li>
31 * <li><code> removeByName()</code></li>
32 * </ul>
33 * This test needs the following object relations :
34 * <ul>
35 * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
36 * which represents objects to be inserted. See below
37 * for more information.</li>
38 * <li> <code>'XNameContainerINDEX'</code> : For internal test
39 * usage. Contains current thread number. </li>
40 * <li> <code>'XNameContainer.AllowDuplicateNames'</code> <b>optional</b>:
41 * if this relation exists then container elements can have duplicate
42 * names. </li>
43 * <li> Test environment variable <code>'THRCNT'</code> : number
44 * of interface threads running concurently. </li>
45 * <ul> <p>
46 * XNameComtainer needs n ObjectRelations "INSTANCEn" , where n=1, ..., THRCNT.
48 * When this interface tested by different threads, it must use different
49 * instances to insert/remove - one for each thread.
51 * That's why we use objRelation "XNameContainerINDEX" to store the number of
52 * last taken instance. If there is no such relation, it initialize with 1.
54 * If you insert the same Object by insertByName() several times you
55 * don't insert the Object several times. The first insertByName() inserts
56 * the Object to the Container but all other insertByName() changes
57 * the Name in the Continer because it's the same Object.
58 * @see com.sun.star.container.XNameContainer
61 public class _XNameContainer extends MultiMethodTest {
62 public XNameContainer oObj = null;
63 String Name = "XNameContainer";
65 /**
66 * First inserts object by name (different objects for different threads)
67 * and checks if it exists. Second, if duplicate names are not allowed
68 * test tries to insert element with the same name and checks for
69 * proper exception. Third, tries to add <code>null</code> element and
70 * checks for proper exception. <p>
71 * Has <b>OK</b> status if in the first case element added exists in
72 * the container, in the second case <code>ElementExistException</code>
73 * is thrown, and in the third case <code>IllegalArgumentException</code>
74 * is thrown.
76 public void _insertByName() {
77 boolean result = true;
78 int Index = 0;
80 //get for every thread its own Object to insert it
81 log.println("get ObjRelation(\"XNameContainerINDEX\")");
82 String sIndex = null ;
83 synchronized (tEnv) {
84 sIndex = (String)tEnv.getObjRelation("XNameContainerINDEX");
85 if (sIndex == null) {
86 log.println("No XNameContainerINDEX - so set it to 1.");
87 tEnv.addObjRelation("XNameContainerINDEX",Integer.toString(1));
88 Index = 1;
89 } else {
90 Index = Integer.parseInt(sIndex);
91 Index++;
92 tEnv.addObjRelation("XNameContainerINDEX",
93 Integer.toString(Index));
96 Name += Index ;
98 log.println("get ObjRelation(\"INSTANCE" + Index +"\")");
99 Object oInstance = tEnv.getObjRelation("INSTANCE"+ Index);
100 if (oInstance == null) {
101 log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a.");
105 log.println("testing insertByName(\""+Name+"\")...");
106 try {
107 String[] names = oObj.getElementNames() ;
108 log.println("Element names :") ;
109 for (int i = 0; i<names.length; i++) {
110 log.println(" '" + names[i] + "'") ;
113 oObj.insertByName(Name, oInstance);
115 names = oObj.getElementNames() ;
116 log.println("Element names :") ;
117 for (int i = 0; i<names.length; i++) {
118 log.println(" " + names[i]) ;
121 result &= oObj.hasByName(Name) ;
122 log.println("insertByName(\""+Name+"\")...OK");
123 } catch (com.sun.star.lang.IllegalArgumentException e) {
124 log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
125 result = false;
126 } catch (com.sun.star.lang.WrappedTargetException e) {
127 log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
128 result = false;
129 } catch (com.sun.star.container.ElementExistException e) {
130 log.println("insertByName(\""+Name+"\"): " + e + " FALSE");
131 result = false;
134 // if duplicate names is not allowed test for valid exception
135 if (tEnv.getObjRelation("XNameContainer.AllowDuplicateNames")==null) {
136 Object secondInstance = tEnv.getObjRelation("SecondInstance");
137 if (secondInstance != null) {
138 oInstance = secondInstance;
140 log.println("Trying to add element with the same name ...") ;
141 try {
142 oObj.insertByName(Name, oInstance);
143 result = false ;
144 log.println("!!! No exception were thrown !!!");
145 } catch (com.sun.star.lang.IllegalArgumentException e) {
146 log.println("!!! Wrong exception : " + e + " FALSE");
147 result = false;
148 } catch (com.sun.star.lang.WrappedTargetException e) {
149 log.println("!!! Wrong exception : " + e + " FALSE");
150 result = false;
151 } catch (com.sun.star.container.ElementExistException e) {
152 log.println("Right exception : " + e + " OK");
156 log.println("inserting a wrong Object occurs Exceptions ...");
157 try {
158 Object dummy = null;
159 oObj.insertByName("Dummy", dummy);
160 log.println("No Exception: -> FALSE");
161 result = false;
162 } catch (com.sun.star.lang.IllegalArgumentException e) {
163 log.println("Dummy-Exception: " + e + " -> OK");
164 } catch (com.sun.star.lang.WrappedTargetException e) {
165 log.println("!!! This exception not expected: " +e+ " -> FAILED");
166 result = false;
167 } catch (com.sun.star.container.ElementExistException e) {
168 log.println("!!! This exception not expected: " +e+ " -> FAILED");
169 result = false;
172 tRes.tested("insertByName()", result);
174 } // end insertByName()
177 * Test removes element inserted before and checks if element
178 * still exists in the container. Second test tries to remove
179 * element with non-existing name and checks for proper exception. <p>
180 * Has <b> OK </b> status if in the first case element doesn't
181 * exist anymore (or duplicate names are allowed), and in the
182 * second case <code>NoSuchElementException</code> is thrown. <p>
183 * The following method tests are to be completed successfully before :
184 * <ul>
185 * <li> <code> insertByName() </code> : to remove the element inserted
186 * in this test. </li>
187 * </ul>
189 public void _removeByName() {
190 try {
191 requiredMethod("insertByName()");
192 } catch (StatusException e) {
193 // removing the name anywhere
194 try {
195 oObj.removeByName(Name);
196 } catch (com.sun.star.container.NoSuchElementException e1) {
197 } catch (com.sun.star.lang.WrappedTargetException e1) {
201 boolean result = true;
203 log.println("testing removeByName() ...");
205 try {
206 log.println("remove " + Name);
207 String[] names = oObj.getElementNames() ;
208 log.println("Element names :") ;
209 for (int i = 0; i<names.length; i++) {
210 log.println(" " + names[i]) ;
212 oObj.removeByName(Name);
213 boolean loc_res = !oObj.hasByName(Name) || tEnv.getObjRelation
214 ("XNameContainer.AllowDuplicateNames") != null ;
215 result &= loc_res ;
216 if (loc_res)
217 log.println("1. removeByName(\""+Name+"\") ...OK");
218 else
219 log.println("1. !!! Container still has element with name "
220 + Name) ;
221 } catch (com.sun.star.lang.WrappedTargetException e) {
222 result = false;
223 log.println("1. removeByName:(\""+Name+"\") " + e + " - FAILED");
224 } catch (com.sun.star.container.NoSuchElementException e) {
225 result = false;
226 log.println("1. removeByName:(\""+Name+"\") " + e + " - FAILED");
229 log.println("removing a non existent object to get an exception");
230 try {
231 oObj.removeByName(Name+ " dummy");
232 result = false;
233 log.println("2. removeByName(): Exception expected! - FAILED");
234 } catch (NoSuchElementException e) {
235 log.println("2. removeByName(): Expected exception - OK");
236 result &= true;
237 } catch (com.sun.star.lang.WrappedTargetException e) {
238 result = false;
239 log.println("2. removeByName(): Unexpected exception! - " +
240 e + " - FAILED");
243 tRes.tested("removeByName()", result);
244 } // end removeByName()
245 } //XNameContainer