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.

TestTable.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.hslf.model;
  16. import static org.junit.Assert.*;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.util.List;
  20. import org.apache.poi.hslf.record.TextHeaderAtom;
  21. import org.apache.poi.hslf.usermodel.*;
  22. import org.junit.Test;
  23. /**
  24. * Test <code>Table</code> object.
  25. *
  26. * @author Yegor Kozlov
  27. */
  28. public final class TestTable {
  29. /**
  30. * Test that ShapeFactory works properly and returns <code>Table</code>
  31. */
  32. @Test
  33. public void testShapeFactory() throws Exception {
  34. HSLFSlideShow ppt = new HSLFSlideShow();
  35. HSLFSlide slide = ppt.createSlide();
  36. HSLFTable tbl = new HSLFTable(2, 5);
  37. slide.addShape(tbl);
  38. HSLFTableCell cell = tbl.getCell(0, 0);
  39. //table cells have type=TextHeaderAtom.OTHER_TYPE, see bug #46033
  40. assertEquals(TextHeaderAtom.OTHER_TYPE, cell.getTextParagraphs().get(0).getRunType());
  41. assertTrue(slide.getShapes().get(0) instanceof HSLFTable);
  42. HSLFTable tbl2 = (HSLFTable)slide.getShapes().get(0);
  43. assertEquals(tbl.getNumberOfColumns(), tbl2.getNumberOfColumns());
  44. assertEquals(tbl.getNumberOfRows(), tbl2.getNumberOfRows());
  45. ByteArrayOutputStream out = new ByteArrayOutputStream();
  46. ppt.write(out);
  47. out.close();
  48. ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
  49. slide = ppt.getSlides().get(0);
  50. assertTrue(slide.getShapes().get(0) instanceof HSLFTable);
  51. HSLFTable tbl3 = (HSLFTable)slide.getShapes().get(0);
  52. assertEquals(tbl.getNumberOfColumns(), tbl3.getNumberOfColumns());
  53. assertEquals(tbl.getNumberOfRows(), tbl3.getNumberOfRows());
  54. }
  55. /**
  56. * Error constructing Table when rownum=1
  57. */
  58. @Test
  59. public void test45889(){
  60. HSLFSlideShow ppt = new HSLFSlideShow();
  61. HSLFSlide slide = ppt.createSlide();
  62. List<HSLFShape> shapes;
  63. HSLFTable tbl1 = new HSLFTable(1, 5);
  64. assertEquals(5, tbl1.getNumberOfColumns());
  65. assertEquals(1, tbl1.getNumberOfRows());
  66. slide.addShape(tbl1);
  67. shapes = slide.getShapes();
  68. assertEquals(1, shapes.size());
  69. HSLFTable tbl2 = (HSLFTable)shapes.get(0);
  70. assertSame(tbl1.getSpContainer(), tbl2.getSpContainer());
  71. assertEquals(tbl1.getNumberOfColumns(), tbl2.getNumberOfColumns());
  72. assertEquals(tbl1.getNumberOfRows(), tbl2.getNumberOfRows());
  73. }
  74. @Test
  75. public void testIllegalCOnstruction(){
  76. try {
  77. new HSLFTable(0, 5);
  78. fail("Table(rownum, colnum) must throw IllegalArgumentException if any of tghe arguments is less than 1");
  79. } catch (IllegalArgumentException e){
  80. }
  81. try {
  82. new HSLFTable(5, 0);
  83. fail("Table(rownum, colnum) must throw IllegalArgumentException if any of tghe arguments is less than 1");
  84. } catch (IllegalArgumentException e){
  85. }
  86. }
  87. }