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