1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package ifc.container; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import lib.MultiMethodTest; 27cdf0e10cSrcweir 28cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 29cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException; 30cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException; 31cdf0e10cSrcweir 32cdf0e10cSrcweir /** 33cdf0e10cSrcweir * Testing <code>com.sun.star.container.XIndexAccess</code> 34cdf0e10cSrcweir * interface methods : 35cdf0e10cSrcweir * <ul> 36cdf0e10cSrcweir * <li><code> getCount()</code></li> 37cdf0e10cSrcweir * <li><code> getByIndex()</code></li> 38cdf0e10cSrcweir * </ul> <p> 39cdf0e10cSrcweir * Test seems to work properly in multithreaded environment. 40cdf0e10cSrcweir * @see com.sun.star.container.XIndexAccess 41cdf0e10cSrcweir */ 42cdf0e10cSrcweir public class _XIndexAccess extends MultiMethodTest { 43cdf0e10cSrcweir 44cdf0e10cSrcweir public XIndexAccess oObj = null; 45cdf0e10cSrcweir 46cdf0e10cSrcweir /** 47cdf0e10cSrcweir * Number of elements in the container. 48cdf0e10cSrcweir */ 49cdf0e10cSrcweir public int count = 0; 50cdf0e10cSrcweir 51cdf0e10cSrcweir /** 52cdf0e10cSrcweir * Get number of element in the container. <p> 53cdf0e10cSrcweir * Has <b> OK </b> status if method returns number lager than -1. 54cdf0e10cSrcweir */ _getCount()55cdf0e10cSrcweir public void _getCount() { 56cdf0e10cSrcweir boolean result = true; 57cdf0e10cSrcweir log.println("getting the number of the elements"); 58cdf0e10cSrcweir // hope we haven't a count lower than zerro ;-) 59cdf0e10cSrcweir count = -1; 60cdf0e10cSrcweir count = oObj.getCount(); 61cdf0e10cSrcweir result = (count != -1); 62cdf0e10cSrcweir tRes.tested("getCount()", result); 63cdf0e10cSrcweir } //end getCount() 64cdf0e10cSrcweir 65cdf0e10cSrcweir /** 66cdf0e10cSrcweir * This method tests the IndexAccess from the first element, 67cdf0e10cSrcweir * the middle element and the last element. Finaly it test 68cdf0e10cSrcweir * Exceptions which throws by a not available index. <p> 69cdf0e10cSrcweir * Has <b> OK </b> status if first, middle and last elements 70cdf0e10cSrcweir * successfully returned and has non null value; and if on 71cdf0e10cSrcweir * invalid index parameter <code>IndexOutOfBoundException</code> 72cdf0e10cSrcweir * is thrown.<p> 73cdf0e10cSrcweir * The following method tests are to be completed successfully before : 74cdf0e10cSrcweir * <ul> 75cdf0e10cSrcweir * <li> <code> getCount() </code> : to have number of elements 76cdf0e10cSrcweir * in container. </li> 77cdf0e10cSrcweir * </ul> 78cdf0e10cSrcweir */ _getByIndex()79cdf0e10cSrcweir public void _getByIndex() { 80cdf0e10cSrcweir requiredMethod("getCount()"); 81cdf0e10cSrcweir // get count from holder 82cdf0e10cSrcweir 83cdf0e10cSrcweir try { 84cdf0e10cSrcweir Thread.sleep(200); 85cdf0e10cSrcweir } 86cdf0e10cSrcweir catch(java.lang.InterruptedException e) {} 87cdf0e10cSrcweir 88cdf0e10cSrcweir boolean result = true; 89cdf0e10cSrcweir boolean loc_result = true; 90cdf0e10cSrcweir Object o = null; 91cdf0e10cSrcweir log.println("Testing getByIndex()"); 92cdf0e10cSrcweir 93cdf0e10cSrcweir if (count > 0) { 94cdf0e10cSrcweir // Check the first element 95cdf0e10cSrcweir log.println("Check the first element"); 96cdf0e10cSrcweir result &= checkGetByIndex(0); 97cdf0e10cSrcweir 98cdf0e10cSrcweir // Check the middle element 99cdf0e10cSrcweir log.println("Check the middle element"); 100cdf0e10cSrcweir result &= checkGetByIndex(count /2); 101cdf0e10cSrcweir 102cdf0e10cSrcweir // Check the last element 103cdf0e10cSrcweir log.println("Check the last element"); 104cdf0e10cSrcweir result &= checkGetByIndex(count -1); 105cdf0e10cSrcweir 106cdf0e10cSrcweir // Testing getByIndex with wrong params. 107cdf0e10cSrcweir log.println("Testing getByIndex with wrong params."); 108cdf0e10cSrcweir try { 109cdf0e10cSrcweir log.println("getByIndex(" + count + ")"); 110cdf0e10cSrcweir loc_result = oObj.getByIndex(count) == null; 111cdf0e10cSrcweir log.println("no exception thrown - FAILED"); 112cdf0e10cSrcweir result = false; 113cdf0e10cSrcweir } catch (IndexOutOfBoundsException e) { 114cdf0e10cSrcweir log.println("Expected exception cought! " + e + " OK"); 115cdf0e10cSrcweir } catch (WrappedTargetException e) { 116cdf0e10cSrcweir log.println("Wrong exception! " + e + " FAILED"); 117cdf0e10cSrcweir result = false; 118cdf0e10cSrcweir } 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir tRes.tested("getByIndex()", result); 122cdf0e10cSrcweir 123cdf0e10cSrcweir } // end getByIndex 124cdf0e10cSrcweir checkGetByIndex(int index)125cdf0e10cSrcweir private boolean checkGetByIndex(int index){ 126cdf0e10cSrcweir Object o = null; 127cdf0e10cSrcweir boolean result = true; 128cdf0e10cSrcweir try { 129cdf0e10cSrcweir log.println("getByIndex(" + index + ")"); 130cdf0e10cSrcweir o = oObj.getByIndex(index); 131cdf0e10cSrcweir 132cdf0e10cSrcweir if ( tEnv.getObjRelation("XIndexAccess.getByIndex.mustBeNull") != null){ 133cdf0e10cSrcweir result = (o == null); 134cdf0e10cSrcweir if (result) log.println("OK"); else log.println("FAILED -> not null"); 135cdf0e10cSrcweir } else { 136cdf0e10cSrcweir result = (o != null); 137cdf0e10cSrcweir if (result) log.println("OK"); else log.println("FAILED -> null"); 138cdf0e10cSrcweir } 139cdf0e10cSrcweir 140cdf0e10cSrcweir } catch (WrappedTargetException e) { 141cdf0e10cSrcweir log.println("Exception! " + e); 142cdf0e10cSrcweir result = false; 143cdf0e10cSrcweir } catch (IndexOutOfBoundsException e) { 144cdf0e10cSrcweir log.println("Exception! " + e); 145cdf0e10cSrcweir result = false; 146cdf0e10cSrcweir } 147cdf0e10cSrcweir 148cdf0e10cSrcweir return result; 149cdf0e10cSrcweir } 150cdf0e10cSrcweir 151cdf0e10cSrcweir } // end XIndexAccess 152cdf0e10cSrcweir 153cdf0e10cSrcweir 154cdf0e10cSrcweir 155