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