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 
23 
24 package ifc.container;
25 
26 import lib.MultiMethodTest;
27 
28 import com.sun.star.container.XIndexAccess;
29 import com.sun.star.lang.IndexOutOfBoundsException;
30 import com.sun.star.lang.WrappedTargetException;
31 
32 /**
33 * Testing <code>com.sun.star.container.XIndexAccess</code>
34 * interface methods :
35 * <ul>
36 *  <li><code> getCount()</code></li>
37 *  <li><code> getByIndex()</code></li>
38 * </ul> <p>
39 * Test seems to work properly in multithreaded environment.
40 * @see com.sun.star.container.XIndexAccess
41 */
42 public class _XIndexAccess extends MultiMethodTest {
43 
44     public XIndexAccess oObj = null;
45 
46     /**
47     * Number of elements in the container.
48     */
49     public int count = 0;
50 
51     /**
52     * Get number of element in the container. <p>
53     * Has <b> OK </b> status if method returns number lager than -1.
54     */
_getCount()55     public void _getCount() {
56         boolean result = true;
57         log.println("getting the number of the elements");
58         // hope we haven't a count lower than zerro ;-)
59         count = -1;
60         count = oObj.getCount();
61         result = (count != -1);
62         tRes.tested("getCount()", result);
63     } //end getCount()
64 
65     /**
66     * This method tests the IndexAccess from the first element,
67     * the middle element and the last element. Finaly it test
68     * Exceptions which throws by a not available index. <p>
69     * Has <b> OK </b> status if first, middle and last elements
70     * successfully returned and has non null value; and if on
71     * invalid index parameter <code>IndexOutOfBoundException</code>
72     * is thrown.<p>
73     * The following method tests are to be completed successfully before :
74     * <ul>
75     *  <li> <code> getCount() </code> : to have number of elements
76     *   in container. </li>
77     * </ul>
78     */
_getByIndex()79     public void _getByIndex() {
80         requiredMethod("getCount()");
81         // get count from holder
82 
83         try {
84             Thread.sleep(200);
85         }
86         catch(java.lang.InterruptedException e) {}
87 
88         boolean result = true;
89         boolean loc_result = true;
90         Object o = null;
91         log.println("Testing getByIndex()");
92 
93         if (count > 0) {
94             // Check the first element
95             log.println("Check the first element");
96             result &= checkGetByIndex(0);
97 
98             // Check the middle element
99             log.println("Check the middle element");
100             result &= checkGetByIndex(count /2);
101 
102             // Check the last element
103             log.println("Check the last element");
104             result &= checkGetByIndex(count -1);
105 
106             // Testing getByIndex with wrong params.
107             log.println("Testing getByIndex with wrong params.");
108             try {
109                 log.println("getByIndex(" + count + ")");
110                 loc_result = oObj.getByIndex(count) == null;
111                 log.println("no exception thrown - FAILED");
112                 result = false;
113             } catch (IndexOutOfBoundsException e) {
114                     log.println("Expected exception cought! " + e + " OK");
115             } catch (WrappedTargetException e) {
116                     log.println("Wrong exception! " + e + " FAILED");
117                     result = false;
118             }
119         }
120 
121         tRes.tested("getByIndex()", result);
122 
123     } // end getByIndex
124 
checkGetByIndex(int index)125     private boolean checkGetByIndex(int index){
126         Object o = null;
127         boolean result = true;
128         try {
129             log.println("getByIndex(" + index + ")");
130             o = oObj.getByIndex(index);
131 
132             if ( tEnv.getObjRelation("XIndexAccess.getByIndex.mustBeNull") != null){
133                 result = (o == null);
134                 if (result) log.println("OK"); else log.println("FAILED ->  not null");
135             } else {
136                 result = (o != null);
137                 if (result) log.println("OK"); else log.println("FAILED -> null");
138             }
139 
140         } catch (WrappedTargetException e) {
141                 log.println("Exception! " + e);
142                 result = false;
143         } catch (IndexOutOfBoundsException e) {
144                 log.println("Exception! " + e);
145                 result = false;
146         }
147 
148         return result;
149     }
150 
151 } // end XIndexAccess
152 
153 
154 
155