You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseTestRow.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.usermodel;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;
  20. import static org.junit.jupiter.api.Assertions.assertNull;
  21. import static org.junit.jupiter.api.Assertions.assertSame;
  22. import static org.junit.jupiter.api.Assertions.assertThrows;
  23. import static org.junit.jupiter.api.Assertions.assertTrue;
  24. import static org.junit.jupiter.api.Assertions.fail;
  25. import java.io.IOException;
  26. import java.util.Iterator;
  27. import java.util.Spliterator;
  28. import org.apache.poi.ss.ITestDataProvider;
  29. import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
  30. import org.junit.jupiter.api.Test;
  31. /**
  32. * A base class for testing implementations of
  33. * {@link org.apache.poi.ss.usermodel.Row}
  34. */
  35. public abstract class BaseTestRow {
  36. protected final ITestDataProvider _testDataProvider;
  37. protected BaseTestRow(ITestDataProvider testDataProvider) {
  38. _testDataProvider = testDataProvider;
  39. }
  40. @Test
  41. void testLastAndFirstColumns() throws IOException {
  42. Workbook workbook = _testDataProvider.createWorkbook();
  43. Sheet sheet = workbook.createSheet();
  44. Row row = sheet.createRow(0);
  45. assertEquals(-1, row.getFirstCellNum());
  46. assertEquals(-1, row.getLastCellNum());
  47. //getting cells from an empty row should returns null
  48. for(int i=0; i < 10; i++) assertNull(row.getCell(i));
  49. row.createCell(2);
  50. assertEquals(2, row.getFirstCellNum());
  51. assertEquals(3, row.getLastCellNum());
  52. row.createCell(1);
  53. assertEquals(1, row.getFirstCellNum());
  54. assertEquals(3, row.getLastCellNum());
  55. // check the exact case reported in 'bug' 43901 - notice that the cellNum is '0' based
  56. row.createCell(3);
  57. assertEquals(1, row.getFirstCellNum());
  58. assertEquals(4, row.getLastCellNum());
  59. workbook.close();
  60. }
  61. /**
  62. * Make sure that there is no cross-talk between rows especially with getFirstCellNum and getLastCellNum
  63. * This test was added in response to bug report 44987.
  64. */
  65. @Test
  66. void testBoundsInMultipleRows() throws IOException {
  67. Workbook workbook = _testDataProvider.createWorkbook();
  68. Sheet sheet = workbook.createSheet();
  69. Row rowA = sheet.createRow(0);
  70. rowA.createCell(10);
  71. rowA.createCell(5);
  72. assertEquals(5, rowA.getFirstCellNum());
  73. assertEquals(11, rowA.getLastCellNum());
  74. Row rowB = sheet.createRow(1);
  75. rowB.createCell(15);
  76. rowB.createCell(30);
  77. assertEquals(15, rowB.getFirstCellNum());
  78. assertEquals(31, rowB.getLastCellNum());
  79. assertEquals(5, rowA.getFirstCellNum());
  80. assertEquals(11, rowA.getLastCellNum());
  81. rowA.createCell(50);
  82. assertEquals(51, rowA.getLastCellNum());
  83. assertEquals(31, rowB.getLastCellNum());
  84. workbook.close();
  85. }
  86. @Test
  87. void testRemoveCell() throws IOException {
  88. Workbook wb1 = _testDataProvider.createWorkbook();
  89. {
  90. Sheet sheet = wb1.createSheet();
  91. Row row = sheet.createRow(0);
  92. assertEquals(0, row.getPhysicalNumberOfCells());
  93. assertEquals(-1, row.getLastCellNum());
  94. assertEquals(-1, row.getFirstCellNum());
  95. row.createCell(1);
  96. assertEquals(2, row.getLastCellNum());
  97. assertEquals(1, row.getFirstCellNum());
  98. assertEquals(1, row.getPhysicalNumberOfCells());
  99. row.createCell(3);
  100. assertEquals(4, row.getLastCellNum());
  101. assertEquals(1, row.getFirstCellNum());
  102. assertEquals(2, row.getPhysicalNumberOfCells());
  103. row.removeCell(row.getCell(3));
  104. assertEquals(2, row.getLastCellNum());
  105. assertEquals(1, row.getFirstCellNum());
  106. assertEquals(1, row.getPhysicalNumberOfCells());
  107. row.removeCell(row.getCell(1));
  108. assertEquals(-1, row.getLastCellNum());
  109. assertEquals(-1, row.getFirstCellNum());
  110. assertEquals(0, row.getPhysicalNumberOfCells());
  111. }
  112. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  113. wb1.close();
  114. {
  115. Sheet sheet = wb2.getSheetAt(0);
  116. Row row = sheet.getRow(0);
  117. assertEquals(-1, row.getLastCellNum());
  118. assertEquals(-1, row.getFirstCellNum());
  119. assertEquals(0, row.getPhysicalNumberOfCells());
  120. }
  121. wb2.close();
  122. }
  123. @Test
  124. void testRowBounds() throws IOException {
  125. int maxRowNum = _testDataProvider.getSpreadsheetVersion().getLastRowIndex();
  126. try (Workbook workbook = _testDataProvider.createWorkbook()) {
  127. Sheet sheet = workbook.createSheet();
  128. //Test low row bound
  129. sheet.createRow(0);
  130. //Test low row bound exception
  131. IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> sheet.createRow(-1));
  132. assertTrue(e.getMessage().startsWith("Invalid row number (-1)"));
  133. //Test high row bound
  134. sheet.createRow(maxRowNum);
  135. //Test high row bound exception
  136. e = assertThrows(IllegalArgumentException.class, () -> sheet.createRow(maxRowNum + 1));
  137. assertEquals("Invalid row number (" + (maxRowNum + 1) + ") outside allowable range (0.." + maxRowNum + ")", e.getMessage());
  138. }
  139. }
  140. @Test
  141. protected void testCellBounds() throws IOException {
  142. int maxCellNum = _testDataProvider.getSpreadsheetVersion().getLastColumnIndex();
  143. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  144. Sheet sheet = wb1.createSheet();
  145. Row row1 = sheet.createRow(0);
  146. //Test low cell bound
  147. IllegalArgumentException e;
  148. e = assertThrows(IllegalArgumentException.class, () -> row1.createCell(-1));
  149. assertTrue(e.getMessage().startsWith("Invalid column index (-1)"));
  150. //Test high cell bound
  151. e = assertThrows(IllegalArgumentException.class, () -> row1.createCell(maxCellNum + 1));
  152. assertTrue(e.getMessage().startsWith("Invalid column index (" + (maxCellNum + 1) + ")"));
  153. for (int i = 0; i < maxCellNum; i++) {
  154. row1.createCell(i);
  155. }
  156. assertEquals(maxCellNum, row1.getPhysicalNumberOfCells());
  157. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  158. sheet = wb2.getSheetAt(0);
  159. Row row2 = sheet.getRow(0);
  160. assertEquals(maxCellNum, row2.getPhysicalNumberOfCells());
  161. for (int i = 0; i < maxCellNum; i++) {
  162. Cell cell = row2.getCell(i);
  163. assertEquals(i, cell.getColumnIndex());
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * Prior to patch 43901, POI was producing files with the wrong last-column
  170. * number on the row
  171. */
  172. @Test
  173. void testLastCellNumIsCorrectAfterAddCell_bug43901() throws IOException {
  174. Workbook workbook = _testDataProvider.createWorkbook();
  175. Sheet sheet = workbook.createSheet("test");
  176. Row row = sheet.createRow(0);
  177. // New row has last col -1
  178. assertEquals(-1, row.getLastCellNum());
  179. assertNotEquals(0, row.getLastCellNum(), "Identified bug 43901");
  180. // Create two cells, will return one higher
  181. // than that for the last number
  182. row.createCell(0);
  183. assertEquals(1, row.getLastCellNum());
  184. row.createCell(255);
  185. assertEquals(256, row.getLastCellNum());
  186. workbook.close();
  187. }
  188. /**
  189. * Tests for the missing/blank cell policy stuff
  190. */
  191. @Test
  192. void testGetCellPolicy() throws IOException {
  193. Workbook workbook = _testDataProvider.createWorkbook();
  194. Sheet sheet = workbook.createSheet("test");
  195. Row row = sheet.createRow(0);
  196. // 0 -> string
  197. // 1 -> num
  198. // 2 missing
  199. // 3 missing
  200. // 4 -> blank
  201. // 5 -> num
  202. row.createCell(0).setCellValue("test");
  203. row.createCell(1).setCellValue(3.2);
  204. row.createCell(4, CellType.BLANK);
  205. row.createCell(5).setCellValue(4);
  206. // First up, no policy given, uses default
  207. assertEquals(CellType.STRING, row.getCell(0).getCellType());
  208. assertEquals(CellType.NUMERIC, row.getCell(1).getCellType());
  209. assertNull(row.getCell(2));
  210. assertNull(row.getCell(3));
  211. assertEquals(CellType.BLANK, row.getCell(4).getCellType());
  212. assertEquals(CellType.NUMERIC, row.getCell(5).getCellType());
  213. // RETURN_NULL_AND_BLANK - same as default
  214. assertEquals(CellType.STRING, row.getCell(0, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType());
  215. assertEquals(CellType.NUMERIC, row.getCell(1, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType());
  216. assertNull(row.getCell(2, MissingCellPolicy.RETURN_NULL_AND_BLANK));
  217. assertNull(row.getCell(3, MissingCellPolicy.RETURN_NULL_AND_BLANK));
  218. assertEquals(CellType.BLANK, row.getCell(4, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType());
  219. assertEquals(CellType.NUMERIC, row.getCell(5, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType());
  220. // RETURN_BLANK_AS_NULL - nearly the same
  221. assertEquals(CellType.STRING, row.getCell(0, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType());
  222. assertEquals(CellType.NUMERIC, row.getCell(1, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType());
  223. assertNull(row.getCell(2, MissingCellPolicy.RETURN_BLANK_AS_NULL));
  224. assertNull(row.getCell(3, MissingCellPolicy.RETURN_BLANK_AS_NULL));
  225. assertNull(row.getCell(4, MissingCellPolicy.RETURN_BLANK_AS_NULL));
  226. assertEquals(CellType.NUMERIC, row.getCell(5, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType());
  227. // CREATE_NULL_AS_BLANK - creates as needed
  228. assertEquals(CellType.STRING, row.getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
  229. assertEquals(CellType.NUMERIC, row.getCell(1, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
  230. assertEquals(CellType.BLANK, row.getCell(2, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
  231. assertEquals(CellType.BLANK, row.getCell(3, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
  232. assertEquals(CellType.BLANK, row.getCell(4, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
  233. assertEquals(CellType.NUMERIC, row.getCell(5, MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType());
  234. // Check created ones get the right column
  235. assertEquals(0, row.getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
  236. assertEquals(1, row.getCell(1, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
  237. assertEquals(2, row.getCell(2, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
  238. assertEquals(3, row.getCell(3, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
  239. assertEquals(4, row.getCell(4, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
  240. assertEquals(5, row.getCell(5, MissingCellPolicy.CREATE_NULL_AS_BLANK).getColumnIndex());
  241. // Now change the cell policy on the workbook, check
  242. // that that is now used if no policy given
  243. workbook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
  244. assertEquals(CellType.STRING, row.getCell(0).getCellType());
  245. assertEquals(CellType.NUMERIC, row.getCell(1).getCellType());
  246. assertNull(row.getCell(2));
  247. assertNull(row.getCell(3));
  248. assertNull(row.getCell(4));
  249. assertEquals(CellType.NUMERIC, row.getCell(5).getCellType());
  250. workbook.close();
  251. }
  252. @Test
  253. protected void testRowHeight() throws IOException {
  254. Workbook wb1 = _testDataProvider.createWorkbook();
  255. Sheet sheet = wb1.createSheet();
  256. Row row1 = sheet.createRow(0);
  257. assertEquals(sheet.getDefaultRowHeight(), row1.getHeight());
  258. sheet.setDefaultRowHeightInPoints(20);
  259. row1.setHeight((short)-1); //reset the row height
  260. assertEquals(20.0f, row1.getHeightInPoints(), 0F);
  261. assertEquals(20*20, row1.getHeight());
  262. Row row2 = sheet.createRow(1);
  263. assertEquals(sheet.getDefaultRowHeight(), row2.getHeight());
  264. row2.setHeight((short)310);
  265. assertEquals(310, row2.getHeight());
  266. assertEquals(310F/20, row2.getHeightInPoints(), 0F);
  267. Row row3 = sheet.createRow(2);
  268. row3.setHeightInPoints(25.5f);
  269. assertEquals((short)(25.5f*20), row3.getHeight());
  270. assertEquals(25.5f, row3.getHeightInPoints(), 0F);
  271. Row row4 = sheet.createRow(3);
  272. assertFalse(row4.getZeroHeight());
  273. row4.setZeroHeight(true);
  274. assertTrue(row4.getZeroHeight());
  275. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  276. wb1.close();
  277. sheet = wb2.getSheetAt(0);
  278. row1 = sheet.getRow(0);
  279. row2 = sheet.getRow(1);
  280. row3 = sheet.getRow(2);
  281. row4 = sheet.getRow(3);
  282. assertEquals(20.0f, row1.getHeightInPoints(), 0F);
  283. assertEquals(20*20, row1.getHeight());
  284. assertEquals(310, row2.getHeight());
  285. assertEquals(310F/20, row2.getHeightInPoints(), 0F);
  286. assertEquals((short)(25.5f*20), row3.getHeight());
  287. assertEquals(25.5f, row3.getHeightInPoints(), 0F);
  288. assertFalse(row1.getZeroHeight());
  289. assertFalse(row2.getZeroHeight());
  290. assertFalse(row3.getZeroHeight());
  291. assertTrue(row4.getZeroHeight());
  292. wb2.close();
  293. }
  294. /**
  295. * Test adding cells to a row in various places and see if we can find them again.
  296. */
  297. @Test
  298. void testCellIterator() throws IOException {
  299. Workbook wb = _testDataProvider.createWorkbook();
  300. Sheet sheet = wb.createSheet();
  301. Row row = sheet.createRow(0);
  302. // One cell at the beginning
  303. Cell cell1 = row.createCell(1);
  304. Iterator<Cell> it = row.cellIterator();
  305. assertTrue(it.hasNext());
  306. assertSame(cell1, it.next());
  307. assertFalse(it.hasNext());
  308. // Add another cell at the end
  309. Cell cell2 = row.createCell(99);
  310. it = row.cellIterator();
  311. assertTrue(it.hasNext());
  312. assertSame(cell1, it.next());
  313. assertTrue(it.hasNext());
  314. assertSame(cell2, it.next());
  315. // Add another cell at the beginning
  316. Cell cell3 = row.createCell(0);
  317. it = row.cellIterator();
  318. assertTrue(it.hasNext());
  319. assertSame(cell3, it.next());
  320. assertTrue(it.hasNext());
  321. assertSame(cell1, it.next());
  322. assertTrue(it.hasNext());
  323. assertSame(cell2, it.next());
  324. // Replace cell1
  325. Cell cell4 = row.createCell(1);
  326. it = row.cellIterator();
  327. assertTrue(it.hasNext());
  328. assertSame(cell3, it.next());
  329. assertTrue(it.hasNext());
  330. assertSame(cell4, it.next());
  331. assertTrue(it.hasNext());
  332. assertSame(cell2, it.next());
  333. assertFalse(it.hasNext());
  334. // Add another cell, specifying the cellType
  335. Cell cell5 = row.createCell(2, CellType.STRING);
  336. it = row.cellIterator();
  337. assertNotNull(cell5);
  338. assertTrue(it.hasNext());
  339. assertSame(cell3, it.next());
  340. assertTrue(it.hasNext());
  341. assertSame(cell4, it.next());
  342. assertTrue(it.hasNext());
  343. assertSame(cell5, it.next());
  344. assertTrue(it.hasNext());
  345. assertSame(cell2, it.next());
  346. assertEquals(CellType.STRING, cell5.getCellType());
  347. wb.close();
  348. }
  349. /**
  350. * Test adding cells to a row in various places and see if we can find them again.
  351. */
  352. @Test
  353. void testSpliterator() throws IOException {
  354. Workbook wb = _testDataProvider.createWorkbook();
  355. Sheet sheet = wb.createSheet();
  356. Row row = sheet.createRow(0);
  357. // One cell at the beginning
  358. Cell cell1 = row.createCell(1);
  359. Spliterator<Cell> split = row.spliterator();
  360. assertTrue(split.tryAdvance(cell -> assertSame(cell1, cell)));
  361. assertFalse(split.tryAdvance(cell -> fail()));
  362. // Add another cell at the end
  363. Cell cell2 = row.createCell(99);
  364. split = row.spliterator();
  365. assertTrue(split.tryAdvance(cell -> assertSame(cell1, cell)));
  366. assertTrue(split.tryAdvance(cell -> assertSame(cell2, cell)));
  367. // Add another cell at the beginning
  368. Cell cell3 = row.createCell(0);
  369. split = row.spliterator();
  370. assertTrue(split.tryAdvance(cell -> assertSame(cell3, cell)));
  371. assertTrue(split.tryAdvance(cell -> assertSame(cell1, cell)));
  372. assertTrue(split.tryAdvance(cell -> assertSame(cell2, cell)));
  373. // Replace cell1
  374. Cell cell4 = row.createCell(1);
  375. split = row.spliterator();
  376. assertTrue(split.tryAdvance(cell -> assertSame(cell3, cell)));
  377. assertTrue(split.tryAdvance(cell -> assertSame(cell4, cell)));
  378. assertTrue(split.tryAdvance(cell -> assertSame(cell2, cell)));
  379. assertFalse(split.tryAdvance(cell -> fail()));
  380. // Add another cell, specifying the cellType
  381. Cell cell5 = row.createCell(2, CellType.STRING);
  382. split = row.spliterator();
  383. assertNotNull(cell5);
  384. assertTrue(split.tryAdvance(cell -> assertSame(cell3, cell)));
  385. assertTrue(split.tryAdvance(cell -> assertSame(cell4, cell)));
  386. assertTrue(split.tryAdvance(cell -> assertSame(cell5, cell)));
  387. assertTrue(split.tryAdvance(cell -> assertSame(cell2, cell)));
  388. assertEquals(CellType.STRING, cell5.getCellType());
  389. wb.close();
  390. }
  391. @Test
  392. void testRowStyle() throws IOException {
  393. Workbook wb1 = _testDataProvider.createWorkbook();
  394. Sheet sheet = wb1.createSheet("test");
  395. Row row1 = sheet.createRow(0);
  396. Row row2 = sheet.createRow(1);
  397. // Won't be styled currently
  398. assertFalse(row1.isFormatted());
  399. assertFalse(row2.isFormatted());
  400. assertNull(row1.getRowStyle());
  401. assertNull(row2.getRowStyle());
  402. // Style one
  403. CellStyle style = wb1.createCellStyle();
  404. style.setDataFormat((short)4);
  405. row2.setRowStyle(style);
  406. // Check
  407. assertFalse(row1.isFormatted());
  408. assertTrue(row2.isFormatted());
  409. assertNull(row1.getRowStyle());
  410. assertEquals(style, row2.getRowStyle());
  411. // Save, load and re-check
  412. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  413. wb1.close();
  414. sheet = wb2.getSheetAt(0);
  415. row1 = sheet.getRow(0);
  416. row2 = sheet.getRow(1);
  417. style = wb2.getCellStyleAt(style.getIndex());
  418. assertFalse(row1.isFormatted());
  419. assertTrue(row2.isFormatted());
  420. assertNull(row1.getRowStyle());
  421. assertEquals(style, row2.getRowStyle());
  422. assertEquals(4, style.getDataFormat());
  423. wb2.close();
  424. }
  425. @Test
  426. protected void testCellShiftingRight() {
  427. Workbook wb = _testDataProvider.createWorkbook();
  428. Sheet sheet = wb.createSheet("sheet1");
  429. Row row = sheet.createRow(0);
  430. row.createCell(0, CellType.NUMERIC).setCellValue(0);
  431. row.createCell(1, CellType.NUMERIC).setCellValue(1);
  432. row.createCell(2, CellType.NUMERIC).setCellValue(2);//C
  433. row.createCell(3, CellType.NUMERIC).setCellValue(3);//D
  434. row.createCell(4, CellType.NUMERIC).setCellValue(4);//E
  435. row.createCell(5, CellType.NUMERIC).setCellValue(5);//F
  436. row.createCell(6, CellType.NUMERIC).setCellValue(6);//G
  437. assertThrows(IllegalArgumentException.class, () -> row.shiftCellsLeft(6, 4, 2), "range [6-4] is illegal");
  438. row.shiftCellsRight(2, 4, 1);
  439. //should be [0.0, 1.0, null, 2.0, 3.0, 4.0, 6.0, null]
  440. Cell h1 = row.getCell(7);
  441. assertNull(h1);
  442. Cell g1 = row.getCell(6);
  443. assertEquals(6, g1.getNumericCellValue(), 0.01);
  444. Cell f1 = row.getCell(5);
  445. assertEquals(4, f1.getNumericCellValue(), 0.01);
  446. Cell e1 = row.getCell(4);
  447. assertEquals(3, e1.getNumericCellValue(), 0.01);
  448. Cell d1 = row.getCell(3);
  449. assertEquals(2, d1.getNumericCellValue(), 0.01);
  450. Cell c1 = row.getCell(2);
  451. assertNull(c1);
  452. }
  453. @Test
  454. protected void testCellShiftingLeft() {
  455. Workbook wb = _testDataProvider.createWorkbook();
  456. Sheet sheet = wb.createSheet("sheet1");
  457. Row row = sheet.createRow(0);
  458. row.createCell(0, CellType.NUMERIC).setCellValue(0);
  459. row.createCell(1, CellType.NUMERIC).setCellValue(1);
  460. row.createCell(2, CellType.NUMERIC).setCellValue(2);//C
  461. row.createCell(3, CellType.NUMERIC).setCellValue(3);//D
  462. row.createCell(4, CellType.NUMERIC).setCellValue(4);//E
  463. row.createCell(5, CellType.NUMERIC).setCellValue(5);//F
  464. row.createCell(6, CellType.NUMERIC).setCellValue(6);//G
  465. assertThrows(IllegalArgumentException.class, () -> row.shiftCellsLeft(4, 6, -2), "step = -1 is illegal");
  466. row.shiftCellsLeft(4, 6, 2);
  467. //should be [0.0, 1.0, 4.0, 5.0, 6.0, null, null, null]
  468. Cell b1 = row.getCell(1);
  469. assertEquals(1, b1.getNumericCellValue(), 0.01);
  470. Cell c1 = row.getCell(2);
  471. assertEquals(4, c1.getNumericCellValue(), 0.01);
  472. Cell d1 = row.getCell(3);
  473. assertEquals(5, d1.getNumericCellValue(), 0.01);
  474. Cell e1 = row.getCell(4);
  475. assertEquals(6, e1.getNumericCellValue(), 0.01);
  476. Cell f1 = row.getCell(5);
  477. assertNull(f1);
  478. }
  479. @Test
  480. void testLastRowEmptySheet() {
  481. Workbook wb = _testDataProvider.createWorkbook();
  482. Sheet sheet = wb.createSheet("sheet1");
  483. assertEquals(-1, sheet.getLastRowNum(), "Sheet without rows should return -1 as lastRowNum");
  484. Row row = sheet.createRow(0);
  485. assertNotNull(row);
  486. assertEquals(0, sheet.getLastRowNum(), "Sheet with one row should return 0 as lastRowNum");
  487. }
  488. @Test
  489. void testFirstRowEmptySheet() {
  490. Workbook wb = _testDataProvider.createWorkbook();
  491. Sheet sheet = wb.createSheet("sheet1");
  492. assertEquals(-1, sheet.getFirstRowNum(), "Sheet without rows should return -1 as firstRowNum");
  493. Row row = sheet.createRow(0);
  494. assertNotNull(row);
  495. assertEquals(0, sheet.getFirstRowNum(), "Sheet with one row should return 0 as firstRowNum");
  496. }
  497. }