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.

BaseTestColumnShifting.java 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.ss.usermodel;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import static org.junit.Assert.assertEquals;
  23. import static org.junit.Assert.assertNull;
  24. import static org.junit.Assert.assertTrue;
  25. import org.apache.poi.ss.usermodel.helpers.ColumnShifter;
  26. public abstract class BaseTestColumnShifting {
  27. protected Workbook wb;
  28. protected Sheet sheet1;
  29. protected ColumnShifter columnShifter;
  30. @Before
  31. public void init() {
  32. int rowIndex = 0;
  33. sheet1 = wb.createSheet("sheet1");
  34. Row row = sheet1.createRow(rowIndex++);
  35. row.createCell(0, CellType.NUMERIC).setCellValue(0);
  36. row.createCell(3, CellType.NUMERIC).setCellValue(3);
  37. row.createCell(4, CellType.NUMERIC).setCellValue(4);
  38. row = sheet1.createRow(rowIndex++);
  39. row.createCell(0, CellType.NUMERIC).setCellValue(0.1);
  40. row.createCell(1, CellType.NUMERIC).setCellValue(1.1);
  41. row.createCell(2, CellType.NUMERIC).setCellValue(2.1);
  42. row.createCell(3, CellType.NUMERIC).setCellValue(3.1);
  43. row.createCell(4, CellType.NUMERIC).setCellValue(4.1);
  44. row.createCell(5, CellType.NUMERIC).setCellValue(5.1);
  45. row.createCell(6, CellType.NUMERIC).setCellValue(6.1);
  46. row.createCell(7, CellType.NUMERIC).setCellValue(7.1);
  47. row = sheet1.createRow(rowIndex++);
  48. row.createCell(3, CellType.NUMERIC).setCellValue(3.2);
  49. row.createCell(5, CellType.NUMERIC).setCellValue(5.2);
  50. row.createCell(7, CellType.NUMERIC).setCellValue(7.2);
  51. initColumnShifter();
  52. }
  53. protected void initColumnShifter() {}
  54. @Test
  55. public void testShift3ColumnsRight() {
  56. columnShifter.shiftColumns(1, 2, 3);
  57. Cell cell = sheet1.getRow(0).getCell(4);
  58. assertNull(cell);
  59. cell = sheet1.getRow(1).getCell(4);
  60. assertEquals(1.1, cell.getNumericCellValue(), 0.01);
  61. cell = sheet1.getRow(1).getCell(5);
  62. assertEquals(2.1, cell.getNumericCellValue(), 0.01);
  63. cell = sheet1.getRow(2).getCell(4);
  64. assertNull(cell);
  65. }
  66. @Test
  67. public void testShiftLeft() {
  68. try {
  69. columnShifter.shiftColumns(1, 2, -3);
  70. assertTrue("Shift to negative indices should throw exception", false);
  71. }
  72. catch(IllegalStateException e){
  73. assertTrue(true);
  74. }
  75. }
  76. }