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