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