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