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.sheet;
25 
26 import lib.MultiMethodTest;
27 import lib.StatusException;
28 
29 import com.sun.star.sheet.XSheetCellRangeContainer;
30 import com.sun.star.table.CellRangeAddress;
31 
32 /**
33 * Testing <code>com.sun.star.sheet.XSheetCellRangeContainer</code>
34 * interface methods :
35 * <ul>
36 *  <li><code> addRangeAddress() </code></li>
37 *  <li><code> removeRangeAddress() </code></li>
38 *  <li><code> addRangeAddresses() </code></li>
39 *  <li><code> removeRangeAddresses() </code></li>
40 * </ul> <p>
41 * Test is <b> NOT </b> multithread compilant. <p>
42 * @see com.sun.star.sheet.XSheetCellRangeContainer
43 */
44 public class _XSheetCellRangeContainer extends MultiMethodTest {
45     public XSheetCellRangeContainer oObj = null;
46     public CellRangeAddress[] rAddr = new CellRangeAddress[3];
47 
48     /**
49     * After method called, the new array of structures 'CellRangeAddress'
50     *  is created. Then container is cleared.
51     */
before()52     public void before() {
53         for ( short i=0; i<=2; i++ ) {
54             rAddr[i] = new CellRangeAddress();
55             rAddr[i].Sheet = i;
56             rAddr[i].StartColumn = i;
57             rAddr[i].StartRow = i;
58             rAddr[i].EndColumn = i + 3;
59             rAddr[i].EndRow = i + 3;
60             try {
61                 oObj.removeRangeAddresses(oObj.getRangeAddresses());
62             } catch (com.sun.star.uno.Exception e) {
63                 e.printStackTrace(log);
64                 throw new StatusException("Error: Cannot remove "+
65                     "range addresses." ,e);
66             }
67         }
68     }
69 
70     /**
71     * The method called. Then new value is added to Container.
72     * Next we try to obtain back added value and check it. <p>
73     *
74     * Has <b> OK </b> status if the range just added presents among
75     * all ranges in the container.
76     */
_addRangeAddress()77     public void _addRangeAddress() {
78         boolean result = true;
79 
80         log.println("Elements before adding: " + oObj.getCount());
81         oObj.addRangeAddress(rAddr[0], false);
82         log.println("Elements after adding: " + oObj.getCount());
83         CellRangeAddress[] addr = oObj.getRangeAddresses();
84         boolean exist = false ;
85         for (int i=0; i<=oObj.getCount()-1; i++) {
86             if ( addr[i].Sheet == rAddr[0].Sheet &&
87                  addr[i].StartColumn == rAddr[0].StartColumn &&
88                  addr[i].StartRow == rAddr[0].StartRow &&
89                  addr[i].EndColumn == rAddr[0].EndColumn &&
90                  addr[i].EndRow == rAddr[0].EndRow) {
91 
92                 exist = true;
93             }
94         }
95 
96         result &= exist ;
97 
98         tRes.tested("addRangeAddress()" ,result);
99     }
100 
101     /**
102     * The method called. Then a value added before is removed.
103     * Next we check Container for existence of removed value. <p>
104     * Has <b> OK </b> status if the range just removed doesn't presents among
105     * all ranges in the container.
106     */
_removeRangeAddress()107     public void _removeRangeAddress() {
108         boolean result = true;
109 
110         log.println("Elements before removing: " + oObj.getCount());
111         try {
112             oObj.removeRangeAddress(rAddr[0]);
113         } catch (com.sun.star.container.NoSuchElementException e) {
114             e.printStackTrace(log);
115             result = false;
116         }
117         log.println("Elements after removing: " + oObj.getCount());
118         CellRangeAddress[] addr = oObj.getRangeAddresses();
119         for (int i=0; i<=oObj.getCount()-1; i++) {
120             if ( (addr[i].Sheet == rAddr[0].Sheet) &&
121                     (addr[i].StartColumn == rAddr[0].StartColumn) &&
122                     (addr[i].StartRow == rAddr[0].StartRow) &&
123                     (addr[i].EndColumn == rAddr[0].EndColumn) &&
124                     (addr[i].EndRow == rAddr[0].EndRow) ) {
125                 result = false;
126             }
127         }
128         tRes.tested("removeRangeAddress()" ,result);
129     }
130 
131     /**
132      * The method called. Then new values are added to Container.
133      * Next we try to obtain back all added values and check it. <p>
134      *
135      * Has <b> OK </b> status if the count of ranges increases by
136      * number of added ranges - 1 (one of ranges already exists in the
137      * container). And if all of ranges added exist in the container.
138      */
_addRangeAddresses()139     public void _addRangeAddresses() {
140         executeMethod("addRangeAddress()");
141 
142         boolean result = true;
143 
144         int cntBefore = oObj.getCount();
145         log.println("Elements before adding: " + cntBefore);
146         oObj.addRangeAddresses(rAddr, false);
147         log.println("Elements after adding: " + oObj.getCount());
148         CellRangeAddress[] addr = oObj.getRangeAddresses();
149 
150         result &= cntBefore + rAddr.length == oObj.getCount();
151 
152         for (int j = 0; j < rAddr.length; j++) {
153             boolean exist = false ;
154             for (int i=0; i < oObj.getCount(); i++) {
155                 if ( addr[i].Sheet == rAddr[j].Sheet &&
156                      addr[i].StartColumn == rAddr[j].StartColumn &&
157                      addr[i].StartRow == rAddr[j].StartRow &&
158                      addr[i].EndColumn == rAddr[j].EndColumn &&
159                      addr[i].EndRow == rAddr[j].EndRow ) {
160 
161                     exist = true;
162                     break;
163                 }
164             }
165             result &= exist;
166         }
167 
168         tRes.tested("addRangeAddresses()" ,result);
169     }
170 
171     /**
172      * All ranges are remover from contaier.
173      *
174      * Has <b> OK </b> status if there are no more ranges in the container.
175      */
_removeRangeAddresses()176     public void _removeRangeAddresses() {
177         boolean result = false;
178         int cnt;
179 
180         log.println("Elements before removing: " + oObj.getCount());
181         try {
182             oObj.removeRangeAddresses(oObj.getRangeAddresses());
183         } catch (com.sun.star.container.NoSuchElementException e) {
184             e.printStackTrace(log);
185             result = false;
186         }
187         if ( (cnt = oObj.getCount()) == 0) {
188             result = true;
189         }
190         log.println("Elements after removing: " + cnt);
191         tRes.tested("removeRangeAddresses()" ,result);
192     }
193 
194     /**
195     * Forces environment recreation.
196     */
after()197     protected void after() {
198         disposeEnvironment();
199     }
200 
201 }
202