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 lib.MultiMethodTest; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.container.XIndexContainer; 27cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException; 28cdf0e10cSrcweir 29cdf0e10cSrcweir /** 30cdf0e10cSrcweir * Testing <code>com.sun.star.container.XIndexContainer</code> 31cdf0e10cSrcweir * interface methods : 32cdf0e10cSrcweir * <ul> 33cdf0e10cSrcweir * <li><code> insertByIndex()</code></li> 34cdf0e10cSrcweir * <li><code> removeByIndex()</code></li> 35cdf0e10cSrcweir * </ul> <p> 36cdf0e10cSrcweir * 37cdf0e10cSrcweir * This test needs the following object relations : 38cdf0e10cSrcweir * <ul> 39cdf0e10cSrcweir * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations 40cdf0e10cSrcweir * which represents objects to be inserted. See below 41cdf0e10cSrcweir * for more information.</li> 42cdf0e10cSrcweir * <li> <code>'XIndexContainerINDEX'</code> : For internal test 43cdf0e10cSrcweir * usage. Contains current thread number. </li> 44cdf0e10cSrcweir * <li> Test environment variable <code>'THRCNT'</code> : number 45*7b628b34Smseidel * of interface threads running concurrently. </li> 46cdf0e10cSrcweir * <ul> <p> 47cdf0e10cSrcweir * XIndexComtainer needs n ObjectRelations "INSTANCEn" , where n=1, ..., 48cdf0e10cSrcweir * THRCNT.<p> 49cdf0e10cSrcweir * When this interface tested by different threads, it must use different 50cdf0e10cSrcweir * instances to insert/remove - one for each thread. 51cdf0e10cSrcweir * <p> 52cdf0e10cSrcweir * That's why we use objRelation "XIndexComtainerINDEX" to store the number of 53cdf0e10cSrcweir * last taken instance. If there is no such relation, it initialize with 1. 54cdf0e10cSrcweir * <p> 55cdf0e10cSrcweir * This ObjectRelations should be necessary to create an Object, 56cdf0e10cSrcweir * which is insertable by insterByIndex() 57*7b628b34Smseidel * INSTANCEn are n Objectrelations so that every thread can insert its own 58cdf0e10cSrcweir * object. n depends on the variable THRCNT which and comes from API.INI 59cdf0e10cSrcweir * <p> 60cdf0e10cSrcweir * Why that: 61cdf0e10cSrcweir * If you insert the same Object by insertByIndex() several times you 62cdf0e10cSrcweir * don't insert the Object several times. The first insertByIndex() inserts 63cdf0e10cSrcweir * the Object to the Container but all other insertByIndex() changes 64*7b628b34Smseidel * the Index in the container because it's the same Object. <p> 65688b6054SJohn Bampton * Test is multithread compliant. <p> 66cdf0e10cSrcweir * @see com.sun.star.container.XIndexContainer 67cdf0e10cSrcweir */ 68cdf0e10cSrcweir 69cdf0e10cSrcweir public class _XIndexContainer extends MultiMethodTest { 70cdf0e10cSrcweir public XIndexContainer oObj = null; 71cdf0e10cSrcweir 72cdf0e10cSrcweir int Index = 0; 73cdf0e10cSrcweir 74cdf0e10cSrcweir /** 75cdf0e10cSrcweir * First tries to insert proper object. Second tries to insert 76cdf0e10cSrcweir * null value. For each test thread different objects are inserted 77bb6af6bcSPedro Giffuni * on different indexes. For example for the first started test index 78cdf0e10cSrcweir * is 0 and object is get from relation 'INCTANCE1', and so on. <p> 79cdf0e10cSrcweir * Has <b>OK</b> status if in the first case <code>getByIndex</code> 80cdf0e10cSrcweir * method returns non null value and in the second <code> 81cdf0e10cSrcweir * IndexOutOfBoundsException</code> was thrown. 82cdf0e10cSrcweir */ _insertByIndex()83cdf0e10cSrcweir public void _insertByIndex() { 84cdf0e10cSrcweir boolean result = true; 85cdf0e10cSrcweir 86cdf0e10cSrcweir log.println("get ObjRelation(\"XIndexContainerINDEX\")"); 87cdf0e10cSrcweir String sIndex = (String)tEnv.getObjRelation("XIndexContainerINDEX"); 88cdf0e10cSrcweir if (sIndex == null) { 89cdf0e10cSrcweir log.println("No XIndexContainerINDEX - so set it to 1."); 90cdf0e10cSrcweir tEnv.addObjRelation("XIndexContainerINDEX", Integer.toString(1)); 91cdf0e10cSrcweir Index = 1; 92cdf0e10cSrcweir } else { 93cdf0e10cSrcweir Index = Integer.parseInt(sIndex); 94cdf0e10cSrcweir Index++; 95cdf0e10cSrcweir tEnv.addObjRelation("XIndexContainerINDEX", 96cdf0e10cSrcweir Integer.toString(Index)); 97cdf0e10cSrcweir } 98cdf0e10cSrcweir 99cdf0e10cSrcweir 100cdf0e10cSrcweir log.println("get ObjRelation(\"INSTANCE" + Index +"\")"); 101cdf0e10cSrcweir Object oInstance = tEnv.getObjRelation("INSTANCE"+ Index); 102cdf0e10cSrcweir if (oInstance == null) { 103cdf0e10cSrcweir log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a."); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir 106cdf0e10cSrcweir log.println("testing insertByIndex(\"" + Index + "\")..."); 107cdf0e10cSrcweir try { 108cdf0e10cSrcweir oObj.insertByIndex(Index, oInstance); 109cdf0e10cSrcweir result &= oObj.getByIndex(Index) != null ; 110cdf0e10cSrcweir log.println("insertByIndex(\""+Index+"\")...OK"); 111cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 112cdf0e10cSrcweir log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE"); 113cdf0e10cSrcweir result = false; 114cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 115cdf0e10cSrcweir log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE"); 116cdf0e10cSrcweir result = false; 117cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 118cdf0e10cSrcweir log.println("insertByIndex(\""+Index+"\"): " + e + " FLASE"); 119cdf0e10cSrcweir result = false; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir log.println("inserting a wrong Object occurs Exceptions ..."); 123cdf0e10cSrcweir try { 124cdf0e10cSrcweir Object dummy = null; 125cdf0e10cSrcweir oObj.insertByIndex(0, dummy); 126cdf0e10cSrcweir log.println("No Exception: -> FALSE"); 127cdf0e10cSrcweir result = false; 128cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 129cdf0e10cSrcweir log.println("Dummy-Exception: " + e + " -> OK"); 130cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 131cdf0e10cSrcweir log.println("!!! Wrong Exception: " + e + " -> FAILED"); 132cdf0e10cSrcweir result = false; 133cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 134cdf0e10cSrcweir log.println("!!! Wrong Exception: " + e + " -> FAILED"); 135cdf0e10cSrcweir result = false; 136cdf0e10cSrcweir } 137cdf0e10cSrcweir 138cdf0e10cSrcweir tRes.tested("insertByIndex()", result); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir 141cdf0e10cSrcweir /** 142cdf0e10cSrcweir * Removes the element inserted by <code>insertByIndex</code> method test. 143cdf0e10cSrcweir * The number of elements is checked before and after removing. 144cdf0e10cSrcweir * Then tries to remove an element with invalid index and checks exceptions. 145cdf0e10cSrcweir * <p> 146cdf0e10cSrcweir * Has <b>OK</b> status if after removing number of elements decreases by 147cdf0e10cSrcweir * one and <code>IndexOutOfBoundsException</code> is thrown on invalid index 148cdf0e10cSrcweir * removing. 149cdf0e10cSrcweir * The following method tests are to be completed successfully before : 150cdf0e10cSrcweir * <ul> 151cdf0e10cSrcweir * <li> <code> insertByIndex </code> : to have an object which can be 152cdf0e10cSrcweir * removed.</li> 153cdf0e10cSrcweir * </ul> 154cdf0e10cSrcweir */ _removeByIndex()155cdf0e10cSrcweir public void _removeByIndex() { 156cdf0e10cSrcweir requiredMethod("insertByIndex()"); 157cdf0e10cSrcweir boolean result = true; 158cdf0e10cSrcweir 159cdf0e10cSrcweir log.println("testing removeByIndex() ..."); 160cdf0e10cSrcweir 161cdf0e10cSrcweir try { 162cdf0e10cSrcweir log.println("remove " +Index); 163cdf0e10cSrcweir int cnt1 = -1 , cnt2 = -1 ; 164cdf0e10cSrcweir synchronized (oObj) { 165cdf0e10cSrcweir cnt1 = oObj.getCount() ; 166cdf0e10cSrcweir oObj.removeByIndex(Index); 167cdf0e10cSrcweir cnt2 = oObj.getCount() ; 168cdf0e10cSrcweir } 169cdf0e10cSrcweir log.println("Count before removing : " + cnt1 + 170cdf0e10cSrcweir ", and after : " + cnt2) ; 171cdf0e10cSrcweir 172cdf0e10cSrcweir result &= cnt1 == cnt2 + 1 ; 173cdf0e10cSrcweir 174cdf0e10cSrcweir log.println("1. removeByIndex(\""+Index+"\") ...OK"); 175cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 176cdf0e10cSrcweir result = false; 177cdf0e10cSrcweir log.println("1. removeByIndex:(\""+Index+"\") " + 178cdf0e10cSrcweir e + " - FAILED"); 179cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 180cdf0e10cSrcweir result = false; 181cdf0e10cSrcweir log.println("1. removeByIndex:(\""+Index+"\") " + 182cdf0e10cSrcweir e + " - FAILED"); 183cdf0e10cSrcweir } 184cdf0e10cSrcweir 185cdf0e10cSrcweir log.println("removing a non existent object to get an exception"); 186cdf0e10cSrcweir try { 187cdf0e10cSrcweir oObj.removeByIndex(100); 188cdf0e10cSrcweir result = false; 189cdf0e10cSrcweir log.println("2. removeByIndex(): Exception expected! - FAILED"); 190cdf0e10cSrcweir } catch (IndexOutOfBoundsException e) { 191cdf0e10cSrcweir log.println("2. removeByIndex(): Expected exception - OK"); 192cdf0e10cSrcweir result &= true; 193cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 194cdf0e10cSrcweir result = false; 195cdf0e10cSrcweir log.println("2. removeByIndex(): Unexpected exception! - " + 196cdf0e10cSrcweir e + " - FAILED"); 197cdf0e10cSrcweir } 198cdf0e10cSrcweir 199cdf0e10cSrcweir tRes.tested("removeByIndex()", result); 200cdf0e10cSrcweir } 201cdf0e10cSrcweir } 202