1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.container;
29 
30 import lib.MultiMethodTest;
31 
32 import com.sun.star.container.XIndexAccess;
33 import com.sun.star.lang.IndexOutOfBoundsException;
34 import com.sun.star.lang.WrappedTargetException;
35 
36 /**
37 * Testing <code>com.sun.star.container.XIndexAccess</code>
38 * interface methods :
39 * <ul>
40 *  <li><code> getCount()</code></li>
41 *  <li><code> getByIndex()</code></li>
42 * </ul> <p>
43 * Test seems to work properly in multithreaded environment.
44 * @see com.sun.star.container.XIndexAccess
45 */
46 public class _XIndexAccess extends MultiMethodTest {
47 
48     public XIndexAccess oObj = null;
49 
50     /**
51     * Number of elements in the container.
52     */
53     public int count = 0;
54 
55     /**
56     * Get number of element in the container. <p>
57     * Has <b> OK </b> status if method returns number lager than -1.
58     */
59     public void _getCount() {
60         boolean result = true;
61         log.println("getting the number of the elements");
62         // hope we haven't a count lower than zerro ;-)
63         count = -1;
64         count = oObj.getCount();
65         result = (count != -1);
66         tRes.tested("getCount()", result);
67     } //end getCount()
68 
69     /**
70     * This method tests the IndexAccess from the first element,
71     * the middle element and the last element. Finaly it test
72     * Exceptions which throws by a not available index. <p>
73     * Has <b> OK </b> status if first, middle and last elements
74     * successfully returned and has non null value; and if on
75     * invalid index parameter <code>IndexOutOfBoundException</code>
76     * is thrown.<p>
77     * The following method tests are to be completed successfully before :
78     * <ul>
79     *  <li> <code> getCount() </code> : to have number of elements
80     *   in container. </li>
81     * </ul>
82     */
83     public void _getByIndex() {
84         requiredMethod("getCount()");
85         // get count from holder
86 
87         try {
88             Thread.sleep(200);
89         }
90         catch(java.lang.InterruptedException e) {}
91 
92         boolean result = true;
93         boolean loc_result = true;
94         Object o = null;
95         log.println("Testing getByIndex()");
96 
97         if (count > 0) {
98             // Check the first element
99             log.println("Check the first element");
100             result &= checkGetByIndex(0);
101 
102             // Check the middle element
103             log.println("Check the middle element");
104             result &= checkGetByIndex(count /2);
105 
106             // Check the last element
107             log.println("Check the last element");
108             result &= checkGetByIndex(count -1);
109 
110             // Testing getByIndex with wrong params.
111             log.println("Testing getByIndex with wrong params.");
112             try {
113                 log.println("getByIndex(" + count + ")");
114                 loc_result = oObj.getByIndex(count) == null;
115                 log.println("no exception thrown - FAILED");
116                 result = false;
117             } catch (IndexOutOfBoundsException e) {
118                     log.println("Expected exception cought! " + e + " OK");
119             } catch (WrappedTargetException e) {
120                     log.println("Wrong exception! " + e + " FAILED");
121                     result = false;
122             }
123         }
124 
125         tRes.tested("getByIndex()", result);
126 
127     } // end getByIndex
128 
129     private boolean checkGetByIndex(int index){
130         Object o = null;
131         boolean result = true;
132         try {
133             log.println("getByIndex(" + index + ")");
134             o = oObj.getByIndex(index);
135 
136             if ( tEnv.getObjRelation("XIndexAccess.getByIndex.mustBeNull") != null){
137                 result = (o == null);
138                 if (result) log.println("OK"); else log.println("FAILED ->  not null");
139             } else {
140                 result = (o != null);
141                 if (result) log.println("OK"); else log.println("FAILED -> null");
142             }
143 
144         } catch (WrappedTargetException e) {
145                 log.println("Exception! " + e);
146                 result = false;
147         } catch (IndexOutOfBoundsException e) {
148                 log.println("Exception! " + e);
149                 result = false;
150         }
151 
152         return result;
153     }
154 
155 } // end XIndexAccess
156 
157 
158 
159