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.

TestSXSSFCell.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.xssf.streaming;
  20. import static org.junit.jupiter.api.Assertions.assertEquals;
  21. import static org.junit.jupiter.api.Assertions.assertFalse;
  22. import static org.junit.jupiter.api.Assertions.assertNotNull;
  23. import static org.junit.jupiter.api.Assertions.assertNull;
  24. import static org.junit.jupiter.api.Assertions.assertThrows;
  25. import static org.mockito.Mockito.spy;
  26. import java.io.IOException;
  27. import java.nio.charset.StandardCharsets;
  28. import javax.xml.namespace.QName;
  29. import org.apache.poi.ss.SpreadsheetVersion;
  30. import org.apache.poi.ss.tests.usermodel.BaseTestXCell;
  31. import org.apache.poi.ss.usermodel.Cell;
  32. import org.apache.poi.ss.usermodel.CellType;
  33. import org.apache.poi.ss.usermodel.RichTextString;
  34. import org.apache.poi.ss.usermodel.Workbook;
  35. import org.apache.poi.ss.util.CellRangeAddress;
  36. import org.apache.poi.xssf.SXSSFITestDataProvider;
  37. import org.apache.poi.xssf.usermodel.XSSFCell;
  38. import org.apache.poi.xssf.usermodel.XSSFRichTextString;
  39. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  40. import org.apache.xmlbeans.XmlCursor;
  41. import org.junit.jupiter.api.AfterAll;
  42. import org.junit.jupiter.api.Disabled;
  43. import org.junit.jupiter.api.Test;
  44. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
  45. /**
  46. * Tests various functionality having to do with {@link SXSSFCell}. For instance support for
  47. * particular datatypes, etc.
  48. */
  49. class TestSXSSFCell extends BaseTestXCell {
  50. public TestSXSSFCell() {
  51. super(SXSSFITestDataProvider.instance);
  52. }
  53. @AfterAll
  54. public static void tearDownClass() {
  55. SXSSFITestDataProvider.instance.cleanup();
  56. }
  57. @Test
  58. void testPreserveSpaces() throws IOException {
  59. String[] samplesWithSpaces = {
  60. " POI",
  61. "POI ",
  62. " POI ",
  63. "\nPOI",
  64. "\n\nPOI \n",
  65. };
  66. for (String str : samplesWithSpaces) {
  67. try (Workbook swb = _testDataProvider.createWorkbook()) {
  68. Cell sCell = swb.createSheet().createRow(0).createCell(0);
  69. sCell.setCellValue(str);
  70. assertEquals(sCell.getStringCellValue(), str);
  71. // read back as XSSF and check that xml:spaces="preserve" is set
  72. try (XSSFWorkbook xwb = (XSSFWorkbook) _testDataProvider.writeOutAndReadBack(swb)) {
  73. XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
  74. CTRst is = xCell.getCTCell().getIs();
  75. assertNotNull(is);
  76. try (XmlCursor c = is.newCursor()) {
  77. c.toNextToken();
  78. String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
  79. assertEquals( "preserve", t, "expected xml:spaces=\"preserve\" \"" + str + "\"" );
  80. }
  81. }
  82. }
  83. }
  84. }
  85. @Test
  86. void getCachedFormulaResultType_throwsISE_whenNotAFormulaCell() {
  87. SXSSFCell instance = new SXSSFCell(null, CellType.BLANK);
  88. assertThrows(IllegalStateException.class, instance::getCachedFormulaResultType);
  89. }
  90. @Test
  91. void setCellValue_withTooLongRichTextString_throwsIAE() {
  92. Cell cell = spy(new SXSSFCell(null, CellType.BLANK));
  93. int length = SpreadsheetVersion.EXCEL2007.getMaxTextLength() + 1;
  94. String string = new String(new byte[length], StandardCharsets.UTF_8).replace("\0", "x");
  95. RichTextString richTextString = new XSSFRichTextString(string);
  96. assertThrows(IllegalArgumentException.class, () -> cell.setCellValue(richTextString));
  97. }
  98. @Test
  99. void getArrayFormulaRange_returnsNull() {
  100. Cell cell = new SXSSFCell(null, CellType.BLANK);
  101. CellRangeAddress result = cell.getArrayFormulaRange();
  102. assertNull(result);
  103. }
  104. @Test
  105. void isPartOfArrayFormulaGroup_returnsFalse() {
  106. Cell cell = new SXSSFCell(null, CellType.BLANK);
  107. boolean result = cell.isPartOfArrayFormulaGroup();
  108. assertFalse(result);
  109. }
  110. @Test
  111. void getErrorCellValue_returns0_onABlankCell() {
  112. Cell cell = new SXSSFCell(null, CellType.BLANK);
  113. assertEquals(CellType.BLANK, cell.getCellType());
  114. byte result = cell.getErrorCellValue();
  115. assertEquals(0, result);
  116. }
  117. /**
  118. * For now, {@link SXSSFCell} doesn't support array formulas.
  119. * However, this test should be enabled if array formulas are implemented for SXSSF.
  120. */
  121. @Override
  122. @Disabled
  123. protected void setBlank_removesArrayFormula_ifCellIsPartOfAnArrayFormulaGroupContainingOnlyThisCell() {
  124. }
  125. /**
  126. * For now, {@link SXSSFCell} doesn't support array formulas.
  127. * However, this test should be enabled if array formulas are implemented for SXSSF.
  128. */
  129. @Override
  130. @Disabled
  131. protected void setBlank_throwsISE_ifCellIsPartOfAnArrayFormulaGroupContainingOtherCells() {
  132. }
  133. @Override
  134. @Disabled
  135. protected void setCellFormula_throwsISE_ifCellIsPartOfAnArrayFormulaGroupContainingOtherCells() {
  136. }
  137. @Override
  138. @Disabled
  139. protected void removeFormula_turnsCellToBlank_whenFormulaWasASingleCellArrayFormula() {
  140. }
  141. @Override
  142. @Disabled
  143. protected void setCellFormula_onASingleCellArrayFormulaCell_preservesTheValue() {
  144. }
  145. @Disabled
  146. protected void setCellFormula_isExceptionSafe_onBlankCell() {
  147. }
  148. @Disabled
  149. protected void setCellType_FORMULA_onAnArrayFormulaCell_doesNothing() {
  150. }
  151. }