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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.sl;
  20. import static org.apache.poi.sl.SLCommonUtils.openSampleSlideshow;
  21. import static org.apache.poi.sl.SLCommonUtils.xslfOnly;
  22. import static org.junit.Assert.assertEquals;
  23. import static org.junit.Assert.assertFalse;
  24. import static org.junit.Assert.assertNotNull;
  25. import static org.junit.Assert.assertNull;
  26. import static org.junit.Assert.assertTrue;
  27. import static org.junit.Assume.assumeFalse;
  28. import java.awt.geom.Rectangle2D;
  29. import java.io.ByteArrayInputStream;
  30. import java.io.ByteArrayOutputStream;
  31. import java.io.IOException;
  32. import java.io.InputStream;
  33. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  34. import org.apache.poi.sl.usermodel.Slide;
  35. import org.apache.poi.sl.usermodel.SlideShow;
  36. import org.apache.poi.sl.usermodel.SlideShowFactory;
  37. import org.apache.poi.sl.usermodel.TableCell;
  38. import org.apache.poi.sl.usermodel.TableShape;
  39. import org.apache.poi.sl.usermodel.TextShape.TextDirection;
  40. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  41. import org.junit.Test;
  42. public class TestTable {
  43. @Test
  44. public void colWidthRowHeight() throws IOException {
  45. assumeFalse(xslfOnly());
  46. // Test of table dimensions of same slideshow saved as ppt/x
  47. // to check if both return similar (points) value
  48. SlideShow<?,?> ppt = openSampleSlideshow("table_test.ppt");
  49. TableShape<?,?> ts = (TableShape<?,?>)ppt.getSlides().get(0).getShapes().get(0);
  50. SlideShow<?,?> pptx = openSampleSlideshow("table_test.pptx");
  51. TableShape<?,?> tsx = (TableShape<?,?>)pptx.getSlides().get(0).getShapes().get(0);
  52. // assume table shape should be equal to itself
  53. confirmTableShapeEqual(ts, ts);
  54. confirmTableShapeEqual(tsx, tsx);
  55. // assert ppt and pptx versions of the same table have the same shape
  56. confirmTableShapeEqual(ts, tsx);
  57. // change row height and validate again
  58. tsx.setRowHeight(1, 50);
  59. ts.setRowHeight(1, 50);
  60. confirmTableShapeEqual(ts, tsx);
  61. pptx.close();
  62. ppt.close();
  63. }
  64. private void confirmTableShapeEqual(TableShape<?,?> tableA, TableShape<?,?> tableB) {
  65. int cols = tableA.getNumberOfColumns();
  66. int rows = tableA.getNumberOfRows();
  67. int colsx = tableB.getNumberOfColumns();
  68. int rowsx = tableB.getNumberOfRows();
  69. assertEquals("tables should have same number of columns", cols, colsx);
  70. assertEquals("tables should have same number of rows", rows, rowsx);
  71. for (int i=0; i<cols; i++) {
  72. assertEquals("Width of column " + i + " should be equal",
  73. tableA.getColumnWidth(i), tableB.getColumnWidth(i), 0.2);
  74. }
  75. for (int i=0; i<rows; i++) {
  76. assertEquals("Height of row " + i + " should be equal",
  77. tableA.getRowHeight(i), tableB.getRowHeight(i), 0.3);
  78. }
  79. }
  80. @Test
  81. public void directionHSLF() throws IOException {
  82. assumeFalse(xslfOnly());
  83. SlideShow<?,?> ppt1 = new HSLFSlideShow();
  84. testTextDirection(ppt1);
  85. ppt1.close();
  86. }
  87. @Test
  88. public void directionXSLF() throws IOException {
  89. SlideShow<?,?> ppt1 = new XMLSlideShow();
  90. testTextDirection(ppt1);
  91. ppt1.close();
  92. }
  93. private void testTextDirection(SlideShow<?,?> ppt1) throws IOException {
  94. TextDirection tds[] = {
  95. TextDirection.HORIZONTAL,
  96. TextDirection.VERTICAL,
  97. TextDirection.VERTICAL_270,
  98. // TextDirection.STACKED is not supported on HSLF
  99. };
  100. TableShape<?,?> tbl1 = ppt1.createSlide().createTable(1, 3);
  101. tbl1.setAnchor(new Rectangle2D.Double(50, 50, 200, 200));
  102. int col = 0;
  103. for (TextDirection td : tds) {
  104. TableCell<?,?> c = tbl1.getCell(0, col++);
  105. if (c != null) {
  106. c.setTextDirection(td);
  107. c.setText("bla");
  108. }
  109. }
  110. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  111. ppt1.write(bos);
  112. ppt1.close();
  113. InputStream is = new ByteArrayInputStream(bos.toByteArray());
  114. SlideShow<?,?> ppt2 = SlideShowFactory.create(is);
  115. TableShape<?,?> tbl2 = (TableShape<?,?>)ppt2.getSlides().get(0).getShapes().get(0);
  116. col = 0;
  117. for (TextDirection td : tds) {
  118. TableCell<?,?> c = tbl2.getCell(0, col++);
  119. assertEquals(td, c.getTextDirection());
  120. }
  121. ppt2.close();
  122. }
  123. @Test
  124. public void tableSpan() throws IOException {
  125. String files[] = (xslfOnly()) ? new String[]{ "bug60993.pptx" } : new String[]{ "bug60993.pptx", "bug60993.ppt" };
  126. for (String f : files) {
  127. SlideShow<?,?> ppt = openSampleSlideshow(f);
  128. Slide<?,?> slide = ppt.getSlides().get(0);
  129. TableShape<?,?> ts = (TableShape<?,?>)slide.getShapes().get(0);
  130. int cols = ts.getNumberOfColumns();
  131. int rows = ts.getNumberOfRows();
  132. for (int r=0; r<rows; r++) {
  133. for (int c=0; c<cols; c++) {
  134. TableCell<?,?> tc = ts.getCell(r, c);
  135. int rc = r*10+c;
  136. String msg = f+" (r"+r+",c"+c+")";
  137. switch (rc) {
  138. case 22:
  139. case 51:
  140. if (f.endsWith("ppt")) {
  141. assertNull(msg, tc);
  142. } else {
  143. assertNotNull(msg, tc);
  144. assertTrue(msg, tc.isMerged());
  145. }
  146. break;
  147. case 21:
  148. assertNotNull(msg, tc);
  149. assertEquals(msg, 1, tc.getRowSpan());
  150. assertEquals(msg, 2, tc.getGridSpan());
  151. assertFalse(msg, tc.isMerged());
  152. break;
  153. case 41:
  154. assertNotNull(msg, tc);
  155. assertEquals(msg, 2, tc.getRowSpan());
  156. assertEquals(msg, 1, tc.getGridSpan());
  157. assertFalse(msg, tc.isMerged());
  158. break;
  159. default:
  160. assertNotNull(msg, tc);
  161. assertEquals(msg, 1, tc.getRowSpan());
  162. assertEquals(msg, 1, tc.getGridSpan());
  163. assertFalse(msg, tc.isMerged());
  164. break;
  165. }
  166. }
  167. }
  168. ppt.close();
  169. }
  170. }
  171. }