Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TestColumn.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.xwpf.usermodel;
  16. import org.apache.poi.xwpf.XWPFTestDataSamples;
  17. import org.junit.jupiter.api.Test;
  18. import java.io.IOException;
  19. import static org.junit.jupiter.api.Assertions.assertEquals;
  20. class TestColumn {
  21. @Test
  22. void testAddNewColWithCorrectAmountOfColumns() throws IOException {
  23. XWPFDocument doc = new XWPFDocument();
  24. XWPFTable table = doc.createTable(2, 4);
  25. table.addNewCol();
  26. int expectedNumberOfColumns = 5;
  27. for (int i = 0; i < table.tableRows.size(); i++) {
  28. assertEquals(expectedNumberOfColumns, table.tableRows.get(i).getTableCells().size());
  29. }
  30. doc.close();
  31. }
  32. @Test
  33. void testAddNewColWithEmptyTable() throws IOException {
  34. XWPFDocument doc = new XWPFDocument();
  35. XWPFTable table = doc.createTable(0, 0);
  36. table.removeRow(0);
  37. table.addNewCol();
  38. int expectedNumberOfColumnsInRow1 = 1;
  39. int actualNumberOfColumnsInRow1 = table.tableRows.get(0).getTableCells().size();
  40. assertEquals(expectedNumberOfColumnsInRow1, actualNumberOfColumnsInRow1);
  41. doc.close();
  42. }
  43. @Test
  44. void testAddNewColWithDocx() throws Exception {
  45. try (XWPFDocument doc = XWPFTestDataSamples
  46. .openSampleDocument("TestTableColumns.docx")) {
  47. XWPFTable table = doc.getTables().get(0);
  48. table.addNewCol();
  49. int expectedNumberOfColumns = 5;
  50. for (int i = 0; i < table.tableRows.size(); i++) {
  51. assertEquals(expectedNumberOfColumns, table.tableRows.get(i).getTableCells().size());
  52. }
  53. }
  54. }
  55. @Test
  56. void testAddNewColWhenRowsHaveDifferentNumbersOfColumnsWithDocx() throws Exception {
  57. try (XWPFDocument doc = XWPFTestDataSamples
  58. .openSampleDocument("TestTableColumns.docx")) {
  59. XWPFTable table = doc.getTables().get(1);
  60. table.addNewCol();
  61. int expectedNumberOfColumnsInRow1 = 5;
  62. int actualNumberOfColumnsInRow1 = table.tableRows.get(0).getTableCells().size();
  63. assertEquals(expectedNumberOfColumnsInRow1, actualNumberOfColumnsInRow1);
  64. int expectedNumberOfColumnsInRow2 = 4;
  65. int actualNumberOfColumnsInRow2 = table.tableRows.get(1).getTableCells().size();
  66. assertEquals(expectedNumberOfColumnsInRow2, actualNumberOfColumnsInRow2);
  67. }
  68. }
  69. }