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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 java.io.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import junit.framework.TestCase;
  20. import org.apache.poi.POIDataSamples;
  21. import org.apache.poi.hslf.HSLFSlideShow;
  22. import org.apache.poi.hslf.extractor.PowerPointExtractor;
  23. import org.apache.poi.hslf.record.TextHeaderAtom;
  24. import org.apache.poi.hslf.usermodel.SlideShow;
  25. import org.junit.Test;
  26. /**
  27. * Test <code>Table</code> object.
  28. *
  29. * @author Yegor Kozlov
  30. */
  31. public final class TestTable extends TestCase {
  32. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  33. /**
  34. * Test that ShapeFactory works properly and returns <code>Table</code>
  35. */
  36. public void testShapeFactory() throws Exception {
  37. SlideShow ppt = new SlideShow();
  38. Slide slide = ppt.createSlide();
  39. Table tbl = new Table(2, 5);
  40. slide.addShape(tbl);
  41. TableCell cell = tbl.getCell(0, 0);
  42. //table cells have type=TextHeaderAtom.OTHER_TYPE, see bug #46033
  43. assertEquals(TextHeaderAtom.OTHER_TYPE, cell.getTextRun().getRunType());
  44. assertTrue(slide.getShapes()[0] instanceof Table);
  45. Table tbl2 = (Table)slide.getShapes()[0];
  46. assertEquals(tbl.getNumberOfColumns(), tbl2.getNumberOfColumns());
  47. assertEquals(tbl.getNumberOfRows(), tbl2.getNumberOfRows());
  48. ByteArrayOutputStream out = new ByteArrayOutputStream();
  49. ppt.write(out);
  50. out.close();
  51. ppt = new SlideShow(new ByteArrayInputStream(out.toByteArray()));
  52. slide = ppt.getSlides()[0];
  53. assertTrue(slide.getShapes()[0] instanceof Table);
  54. Table tbl3 = (Table)slide.getShapes()[0];
  55. assertEquals(tbl.getNumberOfColumns(), tbl3.getNumberOfColumns());
  56. assertEquals(tbl.getNumberOfRows(), tbl3.getNumberOfRows());
  57. }
  58. /**
  59. * Error constructing Table when rownum=1
  60. */
  61. public void test45889(){
  62. SlideShow ppt = new SlideShow();
  63. Slide slide = ppt.createSlide();
  64. Shape[] shapes;
  65. Table tbl1 = new Table(1, 5);
  66. assertEquals(5, tbl1.getNumberOfColumns());
  67. assertEquals(1, tbl1.getNumberOfRows());
  68. slide.addShape(tbl1);
  69. shapes = slide.getShapes();
  70. assertEquals(1, shapes.length);
  71. Table tbl2 = (Table)shapes[0];
  72. assertSame(tbl1.getSpContainer(), tbl2.getSpContainer());
  73. assertEquals(tbl1.getNumberOfColumns(), tbl2.getNumberOfColumns());
  74. assertEquals(tbl1.getNumberOfRows(), tbl2.getNumberOfRows());
  75. }
  76. public void testIllegalCOnstruction(){
  77. try {
  78. new Table(0, 5);
  79. fail("Table(rownum, colnum) must throw IllegalArgumentException if any of tghe arguments is less than 1");
  80. } catch (IllegalArgumentException e){
  81. }
  82. try {
  83. new Table(5, 0);
  84. fail("Table(rownum, colnum) must throw IllegalArgumentException if any of tghe arguments is less than 1");
  85. } catch (IllegalArgumentException e){
  86. }
  87. }
  88. /**
  89. * Bug 57820: initTable throws NullPointerException
  90. * when the table is positioned with its top at -1
  91. */
  92. @Test
  93. public void test57820() throws Exception {
  94. SlideShow ppt = new SlideShow(new HSLFSlideShow(_slTests.openResourceAsStream("bug57820-initTableNullRefrenceException.ppt")));
  95. Slide[] slides = ppt.getSlides();
  96. assertEquals(1, slides.length);
  97. Shape[] shapes = slides[0].getShapes(); //throws NullPointerException
  98. Table tbl = null;
  99. for(int idx = 0; idx < shapes.length; idx++) {
  100. if(shapes[idx] instanceof Table) {
  101. tbl = (Table)shapes[idx];
  102. break;
  103. }
  104. }
  105. assertNotNull(tbl);
  106. assertEquals(-1, tbl.getAnchor().y);
  107. }
  108. }