1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package ifc.container; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import lib.MultiMethodTest; 31*cdf0e10cSrcweir import util.ValueComparer; 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir import com.sun.star.container.XIndexReplace; 34*cdf0e10cSrcweir import com.sun.star.container.XNameContainer; 35*cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException; 36*cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException; 37*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir /** 41*cdf0e10cSrcweir * Testing <code>com.sun.star.container.XIndexReplace</code> 42*cdf0e10cSrcweir * interface methods : 43*cdf0e10cSrcweir * <ul> 44*cdf0e10cSrcweir * <li><code> replaceByIndex()</code></li> 45*cdf0e10cSrcweir * </ul> 46*cdf0e10cSrcweir * This test needs the following object relations : 47*cdf0e10cSrcweir * <ul> 48*cdf0e10cSrcweir * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations 49*cdf0e10cSrcweir * which represents objects to be replaced with. See below 50*cdf0e10cSrcweir * for more information.</li> 51*cdf0e10cSrcweir * <li> <code>'XIndexReplaceINDEX'</code> : For internal test 52*cdf0e10cSrcweir * usage. Contains current thread number. </li> 53*cdf0e10cSrcweir * <li> Test environment variable <code>'THRCNT'</code> : number 54*cdf0e10cSrcweir * of interface threads running concurently. </li> 55*cdf0e10cSrcweir * <ul> <p> 56*cdf0e10cSrcweir * XIndexReplace needs n ObjectRelations "INSTANCEn" , where n = 1, ..., THRCNT. 57*cdf0e10cSrcweir * <p> 58*cdf0e10cSrcweir * When this interface tested by different threads, it must use different 59*cdf0e10cSrcweir * instances to replace - one for each thread.<p> 60*cdf0e10cSrcweir * That's why we use objRelation "XIndexReplaceINDEX" to store the number of 61*cdf0e10cSrcweir * last taken instance. If there is no such relation, it initialize with 1. 62*cdf0e10cSrcweir * <p> 63*cdf0e10cSrcweir * This ObjectRelations should be necessary to create an Object, 64*cdf0e10cSrcweir * which is can be replaced by index 65*cdf0e10cSrcweir * INSTANCEn are n Objectrelations so that every thread can isert it's own 66*cdf0e10cSrcweir * object. n depends on the variable THRCNT which and comes from API.INI 67*cdf0e10cSrcweir * <p> 68*cdf0e10cSrcweir * Why that: 69*cdf0e10cSrcweir * If you insert the same Object by replaceByIndex() several times you 70*cdf0e10cSrcweir * don't insert the Object several times. The first replaceByIndex() inserts 71*cdf0e10cSrcweir * the Object to the Container but all other replaceByIndex() changes 72*cdf0e10cSrcweir * the Index in the Continer because it's the same Object. <p> 73*cdf0e10cSrcweir * Test is multithread compilant. <p> 74*cdf0e10cSrcweir * @see com.sun.star.container.XIndexReplace 75*cdf0e10cSrcweir */ 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir public class _XIndexReplace extends MultiMethodTest { 78*cdf0e10cSrcweir public XIndexReplace oObj = null; 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir /** 81*cdf0e10cSrcweir * Primarily tries to replace elements in a proper way : 82*cdf0e10cSrcweir * replaces the first, middle and the last element then 83*cdf0e10cSrcweir * checks if elements were properly replaced. 84*cdf0e10cSrcweir * Then wrong parameters are passed : invalid index and 85*cdf0e10cSrcweir * null value for replacing, and test checks for proper 86*cdf0e10cSrcweir * exceptions to be thrown. <p> 87*cdf0e10cSrcweir * In different threads it replaces elements with different 88*cdf0e10cSrcweir * objects. 89*cdf0e10cSrcweir * Has <b>OK</b> status if in the first (correct) case 90*cdf0e10cSrcweir * elements were successfully replaced (i.e. values got 91*cdf0e10cSrcweir * after replacing must be equal to those replaced with), 92*cdf0e10cSrcweir * and in the second case proper exceptions were thrown. 93*cdf0e10cSrcweir */ 94*cdf0e10cSrcweir public void _replaceByIndex() { 95*cdf0e10cSrcweir boolean result = true; 96*cdf0e10cSrcweir Object old = null; 97*cdf0e10cSrcweir Object oInstance = null; 98*cdf0e10cSrcweir int Index = 0; 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir //get for every thread its own Object to insert it 101*cdf0e10cSrcweir log.println("get ObjRelation(\"XIndexReplaceINDEX\")"); 102*cdf0e10cSrcweir String sIndex = (String)tEnv.getObjRelation("XIndexReplaceINDEX"); 103*cdf0e10cSrcweir if (sIndex == null) { 104*cdf0e10cSrcweir log.println("No XIndexReplaceINDEX - so set it to 1."); 105*cdf0e10cSrcweir tEnv.addObjRelation("XIndexReplaceINDEX", Integer.toString(1)); 106*cdf0e10cSrcweir Index = 1; 107*cdf0e10cSrcweir } else { 108*cdf0e10cSrcweir Index = Integer.parseInt(sIndex); 109*cdf0e10cSrcweir Index++; 110*cdf0e10cSrcweir tEnv.addObjRelation("XIndexReplaceINDEX", Integer.toString(Index)); 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir log.println("get ObjRelation(\"INSTANCE" + Index +"\")"); 115*cdf0e10cSrcweir oInstance = tEnv.getObjRelation("INSTANCE"+ Index); 116*cdf0e10cSrcweir if (oInstance == null) { 117*cdf0e10cSrcweir log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a."); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir log.println("testing replaceByIndex(0)..."); 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir try { 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir log.println("Getting old object"); 125*cdf0e10cSrcweir old = oObj.getByIndex(0); 126*cdf0e10cSrcweir oObj.replaceByIndex(0, oInstance); 127*cdf0e10cSrcweir result = !(oObj.getByIndex(0)).equals(old); 128*cdf0e10cSrcweir result = ! ValueComparer.equalValue(oObj,old); 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 131*cdf0e10cSrcweir e.printStackTrace(log) ; 132*cdf0e10cSrcweir result = false; 133*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 134*cdf0e10cSrcweir e.printStackTrace(log) ; 135*cdf0e10cSrcweir result = false; 136*cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 137*cdf0e10cSrcweir e.printStackTrace(log) ; 138*cdf0e10cSrcweir result = false; 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir log.println("replace with a wrong Object occurs Exceptions ..."); 143*cdf0e10cSrcweir try { 144*cdf0e10cSrcweir oObj.replaceByIndex(999, oInstance); 145*cdf0e10cSrcweir result = false; 146*cdf0e10cSrcweir log.println("1. replaceByIndex(): Exception expected! - FAILED"); 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir XNameContainer xNC = (XNameContainer) 150*cdf0e10cSrcweir UnoRuntime.queryInterface(XNameContainer.class, oObj) ; 151*cdf0e10cSrcweir String[] names = xNC.getElementNames() ; 152*cdf0e10cSrcweir log.println("Element names :") ; 153*cdf0e10cSrcweir for (int i = 0; i<names.length; i++) { 154*cdf0e10cSrcweir log.println(" '" + names[i] + "'") ; 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir } catch (IndexOutOfBoundsException e) { 157*cdf0e10cSrcweir log.println("1. replaceByIndex(): Expected exception - OK"); 158*cdf0e10cSrcweir result &= true; 159*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 160*cdf0e10cSrcweir result = false; 161*cdf0e10cSrcweir log.println("1. replaceByIndex(): Unexpected exception! - " + 162*cdf0e10cSrcweir e + " - FAILED"); 163*cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 164*cdf0e10cSrcweir result = false; 165*cdf0e10cSrcweir log.println("1. replaceByIndex(): Unexpected exception! - " + 166*cdf0e10cSrcweir e + " - FAILED"); 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir log.println("replace with a wrong Object occurs Exceptions ..."); 170*cdf0e10cSrcweir try { 171*cdf0e10cSrcweir oObj.replaceByIndex(0, null); 172*cdf0e10cSrcweir result = false; 173*cdf0e10cSrcweir log.println("2. replaceByIndex(): Exception expected! - FAILED"); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir XNameContainer xNC = (XNameContainer) 177*cdf0e10cSrcweir UnoRuntime.queryInterface(XNameContainer.class, oObj) ; 178*cdf0e10cSrcweir String[] names = xNC.getElementNames() ; 179*cdf0e10cSrcweir log.println("Element names :") ; 180*cdf0e10cSrcweir for (int i = 0; i<names.length; i++) { 181*cdf0e10cSrcweir log.println(" '" + names[i] + "'") ; 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir } catch (IllegalArgumentException e) { 184*cdf0e10cSrcweir log.println("2. replaceByIndex(): Expected exception - OK"); 185*cdf0e10cSrcweir result &= true; 186*cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 187*cdf0e10cSrcweir result = false; 188*cdf0e10cSrcweir log.println("2. replaceByIndex(): Unexpected exception! - " + 189*cdf0e10cSrcweir e + " - FAILED"); 190*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 191*cdf0e10cSrcweir result = false; 192*cdf0e10cSrcweir log.println("2. replaceByIndex(): Unexpected exception! - " + 193*cdf0e10cSrcweir e + " - FAILED"); 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir log.println("replace with the old object"); 197*cdf0e10cSrcweir try { 198*cdf0e10cSrcweir oObj.replaceByIndex(0, old); 199*cdf0e10cSrcweir } catch (IllegalArgumentException e) { 200*cdf0e10cSrcweir e.printStackTrace(log) ; 201*cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 202*cdf0e10cSrcweir e.printStackTrace(log) ; 203*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 204*cdf0e10cSrcweir e.printStackTrace(log) ; 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir tRes.tested("replaceByIndex()", result); 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir 212