1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XTableColumns.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import lib
.MultiMethodTest
;
35 import lib
.StatusException
;
37 import com
.sun
.star
.table
.XCell
;
38 import com
.sun
.star
.table
.XCellRange
;
39 import com
.sun
.star
.table
.XTableColumns
;
40 import com
.sun
.star
.text
.XSimpleText
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
44 * Testing <code>com.sun.star.table.XTableColumns</code>
47 * <li><code> insertByIndex()</code></li>
48 * <li><code> removeByIndex()</code></li>
51 * This test needs the following object relations :
53 * <li> <code>'XTableColumns.XCellRange'</code> : <code>
54 * com.sun.star.table.XCellRange</code> the cell range of
58 * Test is multithread compilant. <p>
59 * @see com.sun.star.table.XTableColumns
61 public class _XTableColumns
extends MultiMethodTest
{
63 public XTableColumns oObj
= null;
64 private XCellRange xCellRange
= null;
65 private int lastColumn
= 0;
67 public void before() {
68 xCellRange
= (XCellRange
)
69 tEnv
.getObjRelation("XTableColumns.XCellRange") ;
71 if (xCellRange
== null) throw new
72 StatusException(Status
.failed("Relation missing"));
74 lastColumn
= oObj
.getCount() - 1 ;
78 * First a number of cells in cell range are filled with data.
80 * Then columns inserted to valid positions : 1 column at 1,
81 * 1 column at 0, 2 columns at 0. <p>
83 * Then columns inserted to invalid positions : position -1,
84 * the column after last, and 0 columns inserted. <p>
86 * Has <b> OK </b> status if for valid cases :
88 * <li> content of other cells are properly shifted </li>
89 * <li> inserted columns are empty </li>
90 * <li> number of columns increases (in case if it is not the whole
91 * spreadsheet) by proper number. </li>
93 * and for invalid cases exception is thrown.
95 public void _insertByIndex() {
97 boolean result
= true;
98 int origCnt
= oObj
.getCount();
101 log
.println("Filling range ... ");
102 fillRange(xCellRange
);
104 log
.println("Inserting 1 column at position 1 ...");
105 oObj
.insertByIndex(1,1);
107 result
&= checkColumn(0, 0);
108 result
&= checkColumnEmpty(1);
109 result
&= checkColumn(2, 1);
110 result
&= checkColumn(3, 2);
111 result
&= checkColumnEmpty(4);
113 if (lastColumn
< 200) {
114 result
&= checkColumn(lastColumn
+ 1, lastColumn
);
115 result
&= oObj
.getCount() == origCnt
+ 1;
117 result
&= checkColumnEmpty(lastColumn
);
120 log
.println("Inserting 1 column at position 0 ...");
121 oObj
.insertByIndex(0,1);
123 result
&= checkColumnEmpty(0);
124 result
&= checkColumn(1, 0);
125 result
&= checkColumnEmpty(2);
126 result
&= checkColumn(3, 1);
127 result
&= checkColumn(4, 2);
128 result
&= checkColumnEmpty(5);
129 if (lastColumn
< 200) {
130 result
&= checkColumn(lastColumn
+ 2, lastColumn
);
131 result
&= oObj
.getCount() == origCnt
+ 2;
134 log
.println("Inserting 2 columns at position 0 ...");
135 oObj
.insertByIndex(0,2);
137 result
&= checkColumnEmpty(0);
138 result
&= checkColumnEmpty(1);
139 result
&= checkColumnEmpty(2);
140 result
&= checkColumn(3, 0);
141 result
&= checkColumnEmpty(4);
142 result
&= checkColumn(5, 1);
143 result
&= checkColumn(6, 2);
144 result
&= checkColumnEmpty(7);
145 if (lastColumn
< 200) {
146 result
&= checkColumn(lastColumn
+ 4, lastColumn
);
149 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
150 e
.printStackTrace(log
);
155 // spreadsheet supports 256 columns and after inserting
156 // or removing a column their number remains the same
158 log
.println("Checking that number of column increased.");
159 result
&= oObj
.getCount() == origCnt
+ 4;
160 log
.println("Before: " + origCnt
+ ", After: " + oObj
.getCount());
162 log
.println("Number of columns is " + origCnt
+ ",") ;
163 log
.println("supposing that this is the whole spreadsheet and ");
164 log
.println("number of columns should not change.");
168 oObj
.insertByIndex(-1,1);
169 log
.println("No Exception occurred while inserting column at -1");
171 } catch (Exception e
) {
172 log
.println("Inserting column at Index -1 ... OK");
176 int cnt
= oObj
.getCount();
178 oObj
.insertByIndex(cnt
, 1);
179 log
.println("No Exception occurred while inserting column at "
182 } catch (Exception e
) {
183 log
.println("Inserting column at Index " + cnt
+ " ... OK");
187 if (tEnv
.getTestCase().getObjectName().equals("ScTableColumnsObj")) {
190 oObj
.insertByIndex(0,0);
191 log
.println("No Exception occurred while inserting 0 columns");
193 } catch (Exception e
) {
194 log
.println("Inserting 0 columns ... OK");
200 tRes
.tested( "insertByIndex()", result
);
202 } // end insertByIndex()
205 * Columns removed from valid positions : 1 column at 1,
206 * 1 column at 0, 2 columns at 0. <p>
208 * Then columns removed from invalid positions : position -1,
209 * the column after last, and 0 columns removed. <p>
211 * Has <b> OK </b> status if for valid cases :
213 * <li> content of other cells are properly shifted </li>
214 * <li> columns which are shifted left are empty </li>
215 * <li> number of columns decreases (in case if it is not the whole
216 * spreadsheet) by proper number. </li>
218 * and for invalid cases exception is thrown.
220 public void _removeByIndex() {
221 executeMethod("insertByIndex()");
223 boolean result
= true;
224 int origCnt
= oObj
.getCount();
227 log
.println("Filling range ... ");
229 log
.println("Removing 2 columns at position 0 ...");
230 oObj
.removeByIndex(0,2);
232 result
&= checkColumnEmpty(0);
233 result
&= checkColumn(1, 0);
234 result
&= checkColumnEmpty(2);
235 result
&= checkColumn(3, 1);
236 result
&= checkColumn(4, 2);
237 result
&= checkColumnEmpty(5);
238 if (lastColumn
< 200) {
239 result
&= checkColumn(lastColumn
+ 2, lastColumn
);
240 result
&= oObj
.getCount() == origCnt
- 2;
243 log
.println("Removing 1 column at position 0 ...");
244 oObj
.removeByIndex(0,1);
246 result
&= checkColumn(0, 0);
247 result
&= checkColumnEmpty(1);
248 result
&= checkColumn(2, 1);
249 result
&= checkColumn(3, 2);
250 result
&= checkColumnEmpty(4);
251 if (lastColumn
< 200) {
252 result
&= checkColumn(lastColumn
+ 1, lastColumn
);
253 result
&= oObj
.getCount() == origCnt
- 3;
256 log
.println("Removing 1 column at position 1 ...");
257 oObj
.removeByIndex(1,1);
259 result
&= checkColumn(0, 0);
260 result
&= checkColumn(1, 1);
261 result
&= checkColumn(2, 2);
262 result
&= checkColumnEmpty(3);
263 if (lastColumn
< 200) {
264 result
&= checkColumn(lastColumn
, lastColumn
);
267 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
268 e
.printStackTrace(log
);
273 // spreadsheet supports 256 columns and after inserting
274 // or removing a column their number remains the same
276 log
.println("Checking that number of column increased.");
277 result
&= oObj
.getCount() == origCnt
- 4;
278 log
.println("Before: " + origCnt
+ ", After: " + oObj
.getCount());
280 log
.println("Number of columns is " + origCnt
+ ",") ;
281 log
.println("supposing that this is the whole spreadsheet and ");
282 log
.println("number of columns should not change.");
286 oObj
.removeByIndex(-1,1);
287 log
.println("No Exception occurred while removing column at -1");
289 } catch (Exception e
) {
290 log
.println("removing column at Index -1 ... OK");
294 int cnt
= oObj
.getCount();
296 oObj
.removeByIndex(cnt
, 1);
297 log
.println("No Exception occurred while removing column at "
300 } catch (Exception e
) {
301 log
.println("Removing column at Index " + cnt
+ " ... OK");
305 if (tEnv
.getTestCase().getObjectName().equals("ScTableColumnsObj")) {
307 oObj
.removeByIndex(0,0);
308 log
.println("No Exception occurred while removing 0 columns");
310 } catch (Exception e
) {
311 log
.println("removing 0 columns ... OK");
316 tRes
.tested( "removeByIndex()", result
);
317 } // end removeByIndex()
319 private void setCellText(XCell cell
, String text
) {
320 XSimpleText xText
= (XSimpleText
) UnoRuntime
.queryInterface
321 (XSimpleText
.class, cell
) ;
322 xText
.setString(text
);
324 private String
getCellText(XCell cell
) {
325 XSimpleText xText
= (XSimpleText
) UnoRuntime
.queryInterface
326 (XSimpleText
.class, cell
) ;
327 return xText
.getString();
331 * Fills the range with some data : two rows and 3 columns, and
332 * some columns are cleared.
334 * @param xRange Range to fill
335 * @throws IndexOutOfBoundsException if any errors occur during filling.
337 private void fillRange(XCellRange xRange
)
338 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
{
340 for (int i
= 0; i
<= lastColumn
&& i
< 3; i
++) {
341 setCellText(xRange
.getCellByPosition(i
, 0), "" + i
+ "a");
342 setCellText(xRange
.getCellByPosition(i
, 1), "" + i
+ "b");
345 for (int i
= 3; i
<= lastColumn
&& i
< 10; i
++) {
346 setCellText(xRange
.getCellByPosition(i
, 0), "");
347 setCellText(xRange
.getCellByPosition(i
, 1), "");
352 * Check the column (first two rows) if it has values with
355 * @param col Column to check
356 * @param idx What indexes must be in cells
357 * @return <code>true</code> if expected indexes are found,
358 * <code>false</code> otherwise.
359 * @throws IndexOutOfBoundsException
361 private boolean checkColumn(int col
, int idx
)
362 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
{
364 if (col
>= oObj
.getCount()) return true;
366 String c1
= getCellText(xCellRange
.getCellByPosition(col
, 0));
367 String c2
= getCellText(xCellRange
.getCellByPosition(col
, 1));
369 if (!((""+ idx
+ "a").equals(c1
) && (""+ idx
+ "b").equals(c2
))) {
371 log
.println("FAILED for column " + col
+ " and index " + idx
+ "("
372 + c1
+ "," + c2
+ ")");
379 * Checks if the column (first two rows) has no data in its cells.
381 * @param col Column to check
382 * @return <code>true</code> if the column is empty, <code>false</code>
383 * if first two cells contains some strings.
384 * @throws IndexOutOfBoundsException
386 private boolean checkColumnEmpty(int col
)
387 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
{
389 if (col
>= oObj
.getCount()) return true;
391 String c1
= getCellText(xCellRange
.getCellByPosition(col
, 0));
392 String c2
= getCellText(xCellRange
.getCellByPosition(col
, 1));
393 if (!("".equals(c1
) && "".equals(c2
))) {
394 log
.println("FAILED for column " + col
+ " is not empty ("
395 + c1
+ "," + c2
+ ")");
401 } //finish class _XTableColumns