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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertSame;
  19. import static org.junit.Assert.assertTrue;
  20. import static org.junit.Assert.fail;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.util.List;
  25. import org.apache.poi.POIDataSamples;
  26. import org.apache.poi.hslf.record.TextHeaderAtom;
  27. import org.apache.poi.hslf.usermodel.HSLFShape;
  28. import org.apache.poi.hslf.usermodel.HSLFSlide;
  29. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  30. import org.apache.poi.hslf.usermodel.HSLFTable;
  31. import org.apache.poi.hslf.usermodel.HSLFTableCell;
  32. import org.apache.poi.sl.usermodel.Shape;
  33. import org.apache.poi.sl.usermodel.Slide;
  34. import org.apache.poi.sl.usermodel.SlideShow;
  35. import org.apache.poi.sl.usermodel.TableShape;
  36. import org.junit.Test;
  37. /**
  38. * Test <code>Table</code> object.
  39. */
  40. public final class TestTable {
  41. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  42. /**
  43. * Test that ShapeFactory works properly and returns <code>Table</code>
  44. */
  45. @Test
  46. public void testShapeFactory() throws IOException {
  47. HSLFSlideShow ppt = new HSLFSlideShow();
  48. HSLFSlide slide = ppt.createSlide();
  49. HSLFTable tbl = slide.createTable(2, 5);
  50. HSLFTableCell cell = tbl.getCell(0, 0);
  51. //table cells have type=TextHeaderAtom.OTHER_TYPE, see bug #46033
  52. assertEquals(TextHeaderAtom.OTHER_TYPE, cell.getTextParagraphs().get(0).getRunType());
  53. HSLFShape tblSh = slide.getShapes().get(0);
  54. assertTrue(tblSh instanceof HSLFTable);
  55. HSLFTable tbl2 = (HSLFTable)tblSh;
  56. assertEquals(tbl.getNumberOfColumns(), tbl2.getNumberOfColumns());
  57. assertEquals(tbl.getNumberOfRows(), tbl2.getNumberOfRows());
  58. ByteArrayOutputStream out = new ByteArrayOutputStream();
  59. ppt.write(out);
  60. out.close();
  61. ppt.close();
  62. ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
  63. slide = ppt.getSlides().get(0);
  64. assertTrue(slide.getShapes().get(0) instanceof HSLFTable);
  65. HSLFTable tbl3 = (HSLFTable)slide.getShapes().get(0);
  66. assertEquals(tbl.getNumberOfColumns(), tbl3.getNumberOfColumns());
  67. assertEquals(tbl.getNumberOfRows(), tbl3.getNumberOfRows());
  68. ppt.close();
  69. }
  70. /**
  71. * Error constructing Table when rownum=1
  72. */
  73. @Test
  74. public void test45889() throws IOException {
  75. HSLFSlideShow ppt = new HSLFSlideShow();
  76. HSLFSlide slide = ppt.createSlide();
  77. List<HSLFShape> shapes;
  78. HSLFTable tbl1 = slide.createTable(1, 5);
  79. assertEquals(5, tbl1.getNumberOfColumns());
  80. assertEquals(1, tbl1.getNumberOfRows());
  81. shapes = slide.getShapes();
  82. assertEquals(1, shapes.size());
  83. HSLFTable tbl2 = (HSLFTable)shapes.get(0);
  84. assertSame(tbl1.getSpContainer(), tbl2.getSpContainer());
  85. assertEquals(tbl1.getNumberOfColumns(), tbl2.getNumberOfColumns());
  86. assertEquals(tbl1.getNumberOfRows(), tbl2.getNumberOfRows());
  87. ppt.close();
  88. }
  89. @Test(expected=IllegalArgumentException.class)
  90. public void testIllegalRowCnstruction() throws IOException {
  91. HSLFSlideShow ppt = new HSLFSlideShow();
  92. HSLFSlide slide = ppt.createSlide();
  93. slide.createTable(0, 5);
  94. fail("Table(rownum, colnum) must throw IllegalArgumentException if any of tghe arguments is less than 1");
  95. ppt.close();
  96. }
  97. @Test(expected=IllegalArgumentException.class)
  98. public void testIllegalColConstruction() throws IOException {
  99. HSLFSlideShow ppt = new HSLFSlideShow();
  100. HSLFSlide slide = ppt.createSlide();
  101. slide.createTable(5, 0);
  102. fail("Table(rownum, colnum) must throw IllegalArgumentException if any of tghe arguments is less than 1");
  103. ppt.close();
  104. }
  105. /**
  106. * Bug 57820: initTable throws NullPointerException
  107. * when the table is positioned with its top at -1
  108. */
  109. @Test
  110. public void test57820() throws IOException {
  111. SlideShow<?,?> ppt = new HSLFSlideShow(_slTests.openResourceAsStream("bug57820-initTableNullRefrenceException.ppt"));
  112. List<? extends Slide<?,?>> slides = ppt.getSlides();
  113. assertEquals(1, slides.size());
  114. List<? extends Shape<?,?>> shapes = slides.get(0).getShapes(); //throws NullPointerException
  115. TableShape<?,?> tbl = null;
  116. for(Shape<?,?> s : shapes) {
  117. if(s instanceof TableShape) {
  118. tbl = (TableShape<?,?>)s;
  119. break;
  120. }
  121. }
  122. assertNotNull(tbl);
  123. assertEquals(-1, tbl.getAnchor().getY(), 0);
  124. ppt.close();
  125. }
  126. }