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.

TestXSSFColGrouping.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.xssf.usermodel;
  16. import junit.framework.TestCase;
  17. import org.apache.poi.util.POILogFactory;
  18. import org.apache.poi.util.POILogger;
  19. import org.apache.poi.xssf.XSSFTestDataSamples;
  20. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
  21. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
  22. /**
  23. * Test asserts the POI produces <cols> element that could be read and properly interpreted by the MS Excel.
  24. * For specification of the "cols" element see the chapter 3.3.1.16 of the "Office Open XML Part 4 - Markup Language Reference.pdf".
  25. * The specification can be downloaded at http://www.ecma-international.org/publications/files/ECMA-ST/Office%20Open%20XML%201st%20edition%20Part%204%20(PDF).zip.
  26. *
  27. * <p><em>
  28. * The test saves xlsx file on a disk if the system property is set:
  29. * -Dpoi.test.xssf.output.dir=${workspace_loc}/poi/build/xssf-output
  30. * </em>
  31. *
  32. */
  33. public class TestXSSFColGrouping extends TestCase {
  34. private static final POILogger logger = POILogFactory.getLogger(TestXSSFColGrouping.class);
  35. /**
  36. * Tests that POI doesn't produce "col" elements without "width" attribute.
  37. * POI-52186
  38. */
  39. @SuppressWarnings("deprecation")
  40. public void testNoColsWithoutWidthWhenGrouping() {
  41. XSSFWorkbook wb = new XSSFWorkbook();
  42. XSSFSheet sheet = wb.createSheet("test");
  43. sheet.setColumnWidth(4, 5000);
  44. sheet.setColumnWidth(5, 5000);
  45. sheet.groupColumn((short) 4, (short) 7);
  46. sheet.groupColumn((short) 9, (short) 12);
  47. wb = XSSFTestDataSamples.writeOutAndReadBack(wb, "testNoColsWithoutWidthWhenGrouping");
  48. sheet = wb.getSheet("test");
  49. CTCols cols = sheet.getCTWorksheet().getColsArray(0);
  50. logger.log(POILogger.DEBUG, "test52186/cols:" + cols);
  51. for (CTCol col : cols.getColArray()) {
  52. assertTrue("Col width attribute is unset: " + col.toString(), col.isSetWidth());
  53. }
  54. }
  55. /**
  56. * Tests that POI doesn't produce "col" elements without "width" attribute.
  57. * POI-52186
  58. */
  59. @SuppressWarnings("deprecation")
  60. public void testNoColsWithoutWidthWhenGroupingAndCollapsing() {
  61. XSSFWorkbook wb = new XSSFWorkbook();
  62. XSSFSheet sheet = wb.createSheet("test");
  63. sheet.setColumnWidth(4, 5000);
  64. sheet.setColumnWidth(5, 5000);
  65. sheet.groupColumn((short) 4, (short) 5);
  66. sheet.setColumnGroupCollapsed(4, true);
  67. CTCols cols = sheet.getCTWorksheet().getColsArray(0);
  68. logger.log(POILogger.DEBUG, "test52186_2/cols:" + cols);
  69. wb = XSSFTestDataSamples.writeOutAndReadBack(wb, "testNoColsWithoutWidthWhenGroupingAndCollapsing");
  70. sheet = wb.getSheet("test");
  71. for (int i = 4; i <= 5; i++) {
  72. assertEquals("Unexpected width of column "+ i, 5000, sheet.getColumnWidth(i));
  73. }
  74. cols = sheet.getCTWorksheet().getColsArray(0);
  75. for (CTCol col : cols.getColArray()) {
  76. assertTrue("Col width attribute is unset: " + col.toString(), col.isSetWidth());
  77. }
  78. }
  79. /**
  80. * Test the cols element is correct in case of NumericRanges.OVERLAPS_2_WRAPS
  81. */
  82. public void testMergingOverlappingCols_OVERLAPS_2_WRAPS() {
  83. XSSFWorkbook wb = new XSSFWorkbook();
  84. XSSFSheet sheet = wb.createSheet("test");
  85. CTCols cols = sheet.getCTWorksheet().getColsArray(0);
  86. CTCol col = cols.addNewCol();
  87. col.setMin(1 + 1);
  88. col.setMax(4 + 1);
  89. col.setWidth(20);
  90. col.setCustomWidth(true);
  91. sheet.groupColumn((short) 2, (short) 3);
  92. sheet.getCTWorksheet().getColsArray(0);
  93. logger.log(POILogger.DEBUG, "testMergingOverlappingCols_OVERLAPS_2_WRAPS/cols:" + cols);
  94. assertEquals(0, cols.getColArray(0).getOutlineLevel());
  95. assertEquals(2, cols.getColArray(0).getMin()); // 1 based
  96. assertEquals(2, cols.getColArray(0).getMax()); // 1 based
  97. assertEquals(true, cols.getColArray(0).getCustomWidth());
  98. assertEquals(1, cols.getColArray(1).getOutlineLevel());
  99. assertEquals(3, cols.getColArray(1).getMin()); // 1 based
  100. assertEquals(4, cols.getColArray(1).getMax()); // 1 based
  101. assertEquals(true, cols.getColArray(1).getCustomWidth());
  102. assertEquals(0, cols.getColArray(2).getOutlineLevel());
  103. assertEquals(5, cols.getColArray(2).getMin()); // 1 based
  104. assertEquals(5, cols.getColArray(2).getMax()); // 1 based
  105. assertEquals(true, cols.getColArray(2).getCustomWidth());
  106. assertEquals(3, cols.sizeOfColArray());
  107. wb = XSSFTestDataSamples.writeOutAndReadBack(wb, "testMergingOverlappingCols_OVERLAPS_2_WRAPS");
  108. sheet = wb.getSheet("test");
  109. for (int i = 1; i <= 4; i++) {
  110. assertEquals("Unexpected width of column "+ i, 20 * 256, sheet.getColumnWidth(i));
  111. }
  112. }
  113. /**
  114. * Test the cols element is correct in case of NumericRanges.OVERLAPS_1_WRAPS
  115. */
  116. public void testMergingOverlappingCols_OVERLAPS_1_WRAPS() {
  117. XSSFWorkbook wb = new XSSFWorkbook();
  118. XSSFSheet sheet = wb.createSheet("test");
  119. CTCols cols = sheet.getCTWorksheet().getColsArray(0);
  120. CTCol col = cols.addNewCol();
  121. col.setMin(2 + 1);
  122. col.setMax(4 + 1);
  123. col.setWidth(20);
  124. col.setCustomWidth(true);
  125. sheet.groupColumn((short) 1, (short) 5);
  126. cols = sheet.getCTWorksheet().getColsArray(0);
  127. logger.log(POILogger.DEBUG, "testMergingOverlappingCols_OVERLAPS_1_WRAPS/cols:" + cols);
  128. assertEquals(1, cols.getColArray(0).getOutlineLevel());
  129. assertEquals(2, cols.getColArray(0).getMin()); // 1 based
  130. assertEquals(2, cols.getColArray(0).getMax()); // 1 based
  131. assertEquals(false, cols.getColArray(0).getCustomWidth());
  132. assertEquals(1, cols.getColArray(1).getOutlineLevel());
  133. assertEquals(3, cols.getColArray(1).getMin()); // 1 based
  134. assertEquals(5, cols.getColArray(1).getMax()); // 1 based
  135. assertEquals(true, cols.getColArray(1).getCustomWidth());
  136. assertEquals(1, cols.getColArray(2).getOutlineLevel());
  137. assertEquals(6, cols.getColArray(2).getMin()); // 1 based
  138. assertEquals(6, cols.getColArray(2).getMax()); // 1 based
  139. assertEquals(false, cols.getColArray(2).getCustomWidth());
  140. assertEquals(3, cols.sizeOfColArray());
  141. wb = XSSFTestDataSamples.writeOutAndReadBack(wb, "testMergingOverlappingCols_OVERLAPS_1_WRAPS");
  142. sheet = wb.getSheet("test");
  143. for (int i = 2; i <= 4; i++) {
  144. assertEquals("Unexpected width of column "+ i, 20 * 256, sheet.getColumnWidth(i));
  145. }
  146. }
  147. /**
  148. * Test the cols element is correct in case of NumericRanges.OVERLAPS_1_MINOR
  149. */
  150. public void testMergingOverlappingCols_OVERLAPS_1_MINOR() {
  151. XSSFWorkbook wb = new XSSFWorkbook();
  152. XSSFSheet sheet = wb.createSheet("test");
  153. CTCols cols = sheet.getCTWorksheet().getColsArray(0);
  154. CTCol col = cols.addNewCol();
  155. col.setMin(2 + 1);
  156. col.setMax(4 + 1);
  157. col.setWidth(20);
  158. col.setCustomWidth(true);
  159. sheet.groupColumn((short) 3, (short) 5);
  160. cols = sheet.getCTWorksheet().getColsArray(0);
  161. logger.log(POILogger.DEBUG, "testMergingOverlappingCols_OVERLAPS_1_MINOR/cols:" + cols);
  162. assertEquals(0, cols.getColArray(0).getOutlineLevel());
  163. assertEquals(3, cols.getColArray(0).getMin()); // 1 based
  164. assertEquals(3, cols.getColArray(0).getMax()); // 1 based
  165. assertEquals(true, cols.getColArray(0).getCustomWidth());
  166. assertEquals(1, cols.getColArray(1).getOutlineLevel());
  167. assertEquals(4, cols.getColArray(1).getMin()); // 1 based
  168. assertEquals(5, cols.getColArray(1).getMax()); // 1 based
  169. assertEquals(true, cols.getColArray(1).getCustomWidth());
  170. assertEquals(1, cols.getColArray(2).getOutlineLevel());
  171. assertEquals(6, cols.getColArray(2).getMin()); // 1 based
  172. assertEquals(6, cols.getColArray(2).getMax()); // 1 based
  173. assertEquals(false, cols.getColArray(2).getCustomWidth());
  174. assertEquals(3, cols.sizeOfColArray());
  175. wb = XSSFTestDataSamples.writeOutAndReadBack(wb, "testMergingOverlappingCols_OVERLAPS_1_MINOR");
  176. sheet = wb.getSheet("test");
  177. for (int i = 2; i <= 4; i++) {
  178. assertEquals("Unexpected width of column "+ i, 20 * 256, sheet.getColumnWidth(i));
  179. }
  180. assertEquals("Unexpected width of column "+ 5, sheet.getDefaultColumnWidth() * 256, sheet.getColumnWidth(5));
  181. }
  182. /**
  183. * Test the cols element is correct in case of NumericRanges.OVERLAPS_2_MINOR
  184. */
  185. public void testMergingOverlappingCols_OVERLAPS_2_MINOR() {
  186. XSSFWorkbook wb = new XSSFWorkbook();
  187. XSSFSheet sheet = wb.createSheet("test");
  188. CTCols cols = sheet.getCTWorksheet().getColsArray(0);
  189. CTCol col = cols.addNewCol();
  190. col.setMin(2 + 1);
  191. col.setMax(4 + 1);
  192. col.setWidth(20);
  193. col.setCustomWidth(true);
  194. sheet.groupColumn((short) 1, (short) 3);
  195. cols = sheet.getCTWorksheet().getColsArray(0);
  196. logger.log(POILogger.DEBUG, "testMergingOverlappingCols_OVERLAPS_2_MINOR/cols:" + cols);
  197. assertEquals(1, cols.getColArray(0).getOutlineLevel());
  198. assertEquals(2, cols.getColArray(0).getMin()); // 1 based
  199. assertEquals(2, cols.getColArray(0).getMax()); // 1 based
  200. assertEquals(false, cols.getColArray(0).getCustomWidth());
  201. assertEquals(1, cols.getColArray(1).getOutlineLevel());
  202. assertEquals(3, cols.getColArray(1).getMin()); // 1 based
  203. assertEquals(4, cols.getColArray(1).getMax()); // 1 based
  204. assertEquals(true, cols.getColArray(1).getCustomWidth());
  205. assertEquals(0, cols.getColArray(2).getOutlineLevel());
  206. assertEquals(5, cols.getColArray(2).getMin()); // 1 based
  207. assertEquals(5, cols.getColArray(2).getMax()); // 1 based
  208. assertEquals(true, cols.getColArray(2).getCustomWidth());
  209. assertEquals(3, cols.sizeOfColArray());
  210. wb = XSSFTestDataSamples.writeOutAndReadBack(wb, "testMergingOverlappingCols_OVERLAPS_2_MINOR");
  211. sheet = wb.getSheet("test");
  212. for (int i = 2; i <= 4; i++) {
  213. assertEquals("Unexpected width of column "+ i, 20 * 256, sheet.getColumnWidth(i));
  214. }
  215. assertEquals("Unexpected width of column "+ 1, sheet.getDefaultColumnWidth() * 256, sheet.getColumnWidth(1));
  216. }
  217. }