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

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