Fixed offset of compare picture in resize_compare.xlsx git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885280 13f79535-47bb-0310-9956-ffa450edef68tags/REL_5_0_0
opens org.apache.poi.hpsf.basic to org.junit.platform.commons; | opens org.apache.poi.hpsf.basic to org.junit.platform.commons; | ||||
opens org.apache.poi.hssf.record.pivot to org.junit.platform.commons; | opens org.apache.poi.hssf.record.pivot to org.junit.platform.commons; | ||||
opens org.apache.poi.hssf.dev to org.junit.platform.commons; | opens org.apache.poi.hssf.dev to org.junit.platform.commons; | ||||
opens org.apache.poi.ss.usermodel to org.junit.platform.commons; | |||||
} | } |
package org.apache.poi.ss.tests.usermodel; | package org.apache.poi.ss.tests.usermodel; | ||||
import java.io.IOException; | |||||
import org.apache.poi.ss.ITestDataProvider; | import org.apache.poi.ss.ITestDataProvider; | ||||
import org.apache.poi.ss.usermodel.BaseTestRow; | import org.apache.poi.ss.usermodel.BaseTestRow; | ||||
import org.apache.poi.xssf.streaming.SXSSFRow; | import org.apache.poi.xssf.streaming.SXSSFRow; | ||||
import org.apache.poi.xssf.usermodel.XSSFRow; | import org.apache.poi.xssf.usermodel.XSSFRow; | ||||
import org.junit.jupiter.api.Test; | |||||
/** | /** | ||||
* Class for combined testing of XML-specific functionality of | * Class for combined testing of XML-specific functionality of | ||||
protected BaseTestXRow(ITestDataProvider testDataProvider) { | protected BaseTestXRow(ITestDataProvider testDataProvider) { | ||||
super(testDataProvider); | super(testDataProvider); | ||||
} | } | ||||
@Test | |||||
public void testRowBounds() throws IOException { | |||||
baseTestRowBounds(_testDataProvider.getSpreadsheetVersion().getLastRowIndex()); | |||||
} | |||||
@Test | |||||
public void testCellBounds() throws IOException { | |||||
baseTestCellBounds(_testDataProvider.getSpreadsheetVersion().getLastColumnIndex()); | |||||
} | |||||
} | } |
}); | }); | ||||
} | } | ||||
} | } | ||||
public void changeSheetNameWithSharedFormulas() { | |||||
/* not implemented */ | |||||
} | |||||
} | } |
import static org.junit.jupiter.api.Assertions.assertEquals; | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
import static org.junit.jupiter.api.Assertions.assertFalse; | import static org.junit.jupiter.api.Assertions.assertFalse; | ||||
import static org.junit.jupiter.api.Assertions.assertThrows; | import static org.junit.jupiter.api.Assertions.assertThrows; | ||||
import static org.junit.jupiter.api.Assertions.fail; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
super(SXSSFITestDataProvider.instance); | super(SXSSFITestDataProvider.instance); | ||||
} | } | ||||
public void testSharedFormulas() throws IOException { | |||||
/* not implemented */ | |||||
} | |||||
/** | /** | ||||
* EvaluateAll will normally fail, as any reference or | * EvaluateAll will normally fail, as any reference or | ||||
* formula outside of the window will fail, and any | * formula outside of the window will fail, and any |
import static org.junit.jupiter.api.Assertions.assertSame; | import static org.junit.jupiter.api.Assertions.assertSame; | ||||
import static org.junit.jupiter.api.Assertions.assertThrows; | import static org.junit.jupiter.api.Assertions.assertThrows; | ||||
import static org.junit.jupiter.api.Assertions.assertTrue; | import static org.junit.jupiter.api.Assertions.assertTrue; | ||||
import static org.junit.jupiter.api.Assertions.fail; | |||||
import java.io.ByteArrayInputStream; | import java.io.ByteArrayInputStream; | ||||
import java.io.ByteArrayOutputStream; | import java.io.ByteArrayOutputStream; | ||||
wb.close(); | wb.close(); | ||||
} | } | ||||
public void changeSheetNameWithSharedFormulas() { | |||||
/* not implemented */ | |||||
} | |||||
} | } |
package org.apache.poi.xssf.usermodel; | package org.apache.poi.xssf.usermodel; | ||||
import static org.junit.jupiter.api.Assertions.*; | |||||
import static org.apache.poi.xssf.XSSFTestDataSamples.openSampleWorkbook; | |||||
import static org.junit.jupiter.api.Assertions.assertEquals; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import org.apache.poi.ss.formula.ConditionalFormattingEvaluator; | import org.apache.poi.ss.formula.ConditionalFormattingEvaluator; | ||||
import org.apache.poi.ss.formula.WorkbookEvaluatorProvider; | import org.apache.poi.ss.formula.WorkbookEvaluatorProvider; | ||||
import org.apache.poi.ss.usermodel.BaseTestDataFormat; | import org.apache.poi.ss.usermodel.BaseTestDataFormat; | ||||
import org.apache.poi.ss.usermodel.BuiltinFormats; | |||||
import org.apache.poi.ss.usermodel.Cell; | import org.apache.poi.ss.usermodel.Cell; | ||||
import org.apache.poi.ss.usermodel.CellStyle; | import org.apache.poi.ss.usermodel.CellStyle; | ||||
import org.apache.poi.ss.usermodel.DataFormat; | |||||
import org.apache.poi.ss.usermodel.DataFormatter; | import org.apache.poi.ss.usermodel.DataFormatter; | ||||
import org.apache.poi.ss.usermodel.FormulaEvaluator; | import org.apache.poi.ss.usermodel.FormulaEvaluator; | ||||
import org.apache.poi.ss.usermodel.Workbook; | import org.apache.poi.ss.usermodel.Workbook; | ||||
super(XSSFITestDataProvider.instance); | super(XSSFITestDataProvider.instance); | ||||
} | } | ||||
/** | |||||
* [Bug 49928] formatCellValue returns incorrect value for \u00a3 formatted cells | |||||
*/ | |||||
@Override | |||||
@Test | |||||
public void test49928() throws IOException { | |||||
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49928.xlsx"); | |||||
doTest49928Core(wb); | |||||
DataFormat dataFormat = wb.createDataFormat(); | |||||
// As of 2015-12-27, there is no way to override a built-in number format with POI XSSFWorkbook | |||||
// 49928.xlsx has been saved with a poundFmt that overrides the default value (dollar) | |||||
short poundFmtIdx = wb.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getDataFormat(); | |||||
assertEquals(poundFmtIdx, dataFormat.getFormat(poundFmt)); | |||||
// now create a custom format with Pound (\u00a3) | |||||
String customFmt = "\u00a3##.00[Yellow]"; | |||||
assertNotBuiltInFormat(customFmt); | |||||
short customFmtIdx = dataFormat.getFormat(customFmt); | |||||
assertTrue(customFmtIdx >= BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX); | |||||
assertEquals(customFmt, dataFormat.getFormat(customFmtIdx)); | |||||
wb.close(); | |||||
} | |||||
/** | |||||
* [Bug 58532] Handle formats that go numnum, numK, numM etc | |||||
*/ | |||||
@Override | |||||
@Test | |||||
public void test58532() throws IOException { | |||||
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("FormatKM.xlsx"); | |||||
doTest58532Core(wb); | |||||
wb.close(); | |||||
} | |||||
/** | /** | ||||
* [Bug 58778] Built-in number formats can be overridden with XSSFDataFormat.putFormat(int id, String fmt) | * [Bug 58778] Built-in number formats can be overridden with XSSFDataFormat.putFormat(int id, String fmt) | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void test58778() throws IOException { | public void test58778() throws IOException { | ||||
XSSFWorkbook wb1 = new XSSFWorkbook(); | |||||
Cell cell = wb1.createSheet("bug58778").createRow(0).createCell(0); | |||||
cell.setCellValue(5.25); | |||||
CellStyle style = wb1.createCellStyle(); | |||||
XSSFDataFormat dataFormat = wb1.createDataFormat(); | |||||
short poundFmtIdx = 6; | |||||
dataFormat.putFormat(poundFmtIdx, poundFmt); | |||||
style.setDataFormat(poundFmtIdx); | |||||
cell.setCellStyle(style); | |||||
// Cell should appear as "<poundsymbol>5" | |||||
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutCloseAndReadBack(wb1); | |||||
cell = wb2.getSheet("bug58778").getRow(0).getCell(0); | |||||
assertEquals(5.25, cell.getNumericCellValue(), 0); | |||||
style = cell.getCellStyle(); | |||||
assertEquals(poundFmt, style.getDataFormatString()); | |||||
assertEquals(poundFmtIdx, style.getDataFormat()); | |||||
// manually check the file to make sure the cell is rendered as "<poundsymbol>5" | |||||
// Verified with LibreOffice 4.2.8.2 on 2015-12-28 | |||||
wb2.close(); | |||||
wb1.close(); | |||||
try (XSSFWorkbook wb1 = new XSSFWorkbook()) { | |||||
Cell cell = wb1.createSheet("bug58778").createRow(0).createCell(0); | |||||
cell.setCellValue(5.25); | |||||
CellStyle style = wb1.createCellStyle(); | |||||
XSSFDataFormat dataFormat = wb1.createDataFormat(); | |||||
short poundFmtIdx = 6; | |||||
dataFormat.putFormat(poundFmtIdx, POUND_FMT); | |||||
style.setDataFormat(poundFmtIdx); | |||||
cell.setCellStyle(style); | |||||
// Cell should appear as "<poundsymbol>5" | |||||
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutCloseAndReadBack(wb1)) { | |||||
cell = wb2.getSheet("bug58778").getRow(0).getCell(0); | |||||
assertEquals(5.25, cell.getNumericCellValue(), 0); | |||||
style = cell.getCellStyle(); | |||||
assertEquals(POUND_FMT, style.getDataFormatString()); | |||||
assertEquals(poundFmtIdx, style.getDataFormat()); | |||||
// manually check the file to make sure the cell is rendered as "<poundsymbol>5" | |||||
// Verified with LibreOffice 4.2.8.2 on 2015-12-28 | |||||
} | |||||
} | |||||
} | } | ||||
@Test | @Test | ||||
public void testConditionalFormattingEvaluation() throws IOException { | public void testConditionalFormattingEvaluation() throws IOException { | ||||
final Workbook wb = XSSFTestDataSamples.openSampleWorkbook("61060-conditional-number-formatting.xlsx"); | |||||
final DataFormatter formatter = new DataFormatter(); | |||||
final FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | |||||
final ConditionalFormattingEvaluator cfEvaluator = new ConditionalFormattingEvaluator(wb, (WorkbookEvaluatorProvider) evaluator); | |||||
CellReference ref = new CellReference("A1"); | |||||
Cell cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol()); | |||||
assertEquals("0.10", formatter.formatCellValue(cell, evaluator, cfEvaluator)); | |||||
// verify cell format without the conditional rule applied | |||||
assertEquals("0.1", formatter.formatCellValue(cell, evaluator)); | |||||
ref = new CellReference("A3"); | |||||
cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol()); | |||||
assertEquals("-2.00E+03", formatter.formatCellValue(cell, evaluator, cfEvaluator)); | |||||
// verify cell format without the conditional rule applied | |||||
assertEquals("-2000", formatter.formatCellValue(cell, evaluator)); | |||||
ref = new CellReference("A4"); | |||||
cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol()); | |||||
assertEquals("100", formatter.formatCellValue(cell, evaluator, cfEvaluator)); | |||||
ref = new CellReference("A5"); | |||||
cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol()); | |||||
assertEquals("$1,000", formatter.formatCellValue(cell, evaluator, cfEvaluator)); | |||||
// verify cell format without the conditional rule applied | |||||
assertEquals("1000", formatter.formatCellValue(cell, evaluator)); | |||||
wb.close(); | |||||
try (Workbook wb = openSampleWorkbook("61060-conditional-number-formatting.xlsx")) { | |||||
final DataFormatter formatter = new DataFormatter(); | |||||
final FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | |||||
final ConditionalFormattingEvaluator cfEvaluator = new ConditionalFormattingEvaluator(wb, (WorkbookEvaluatorProvider) evaluator); | |||||
CellReference ref = new CellReference("A1"); | |||||
Cell cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol()); | |||||
assertEquals("0.10", formatter.formatCellValue(cell, evaluator, cfEvaluator)); | |||||
// verify cell format without the conditional rule applied | |||||
assertEquals("0.1", formatter.formatCellValue(cell, evaluator)); | |||||
ref = new CellReference("A3"); | |||||
cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol()); | |||||
assertEquals("-2.00E+03", formatter.formatCellValue(cell, evaluator, cfEvaluator)); | |||||
// verify cell format without the conditional rule applied | |||||
assertEquals("-2000", formatter.formatCellValue(cell, evaluator)); | |||||
ref = new CellReference("A4"); | |||||
cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol()); | |||||
assertEquals("100", formatter.formatCellValue(cell, evaluator, cfEvaluator)); | |||||
ref = new CellReference("A5"); | |||||
cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol()); | |||||
assertEquals("$1,000", formatter.formatCellValue(cell, evaluator, cfEvaluator)); | |||||
// verify cell format without the conditional rule applied | |||||
assertEquals("1000", formatter.formatCellValue(cell, evaluator)); | |||||
} | |||||
} | } | ||||
} | } |
import static org.junit.jupiter.api.Assertions.*; | import static org.junit.jupiter.api.Assertions.*; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.stream.Stream; | |||||
import org.apache.poi.common.usermodel.fonts.FontCharset; | import org.apache.poi.common.usermodel.fonts.FontCharset; | ||||
import org.apache.poi.ooxml.POIXMLException; | import org.apache.poi.ooxml.POIXMLException; | ||||
import org.apache.poi.xssf.XSSFITestDataProvider; | import org.apache.poi.xssf.XSSFITestDataProvider; | ||||
import org.apache.poi.xssf.XSSFTestDataSamples; | import org.apache.poi.xssf.XSSFTestDataSamples; | ||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
import org.junit.jupiter.params.provider.Arguments; | |||||
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun; | import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun; | ||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; | import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; | ||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; | import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; | ||||
super(XSSFITestDataProvider.instance); | super(XSSFITestDataProvider.instance); | ||||
} | } | ||||
@Test | |||||
public void testDefaultFont() throws IOException { | |||||
baseTestDefaultFont("Calibri", (short) 220, IndexedColors.BLACK.getIndex()); | |||||
@SuppressWarnings("unused") | |||||
public static Stream<Arguments> defaultFont() { | |||||
return Stream.of(Arguments.of("Calibri", (short) 220, IndexedColors.BLACK.getIndex())); | |||||
} | } | ||||
@Test | @Test | ||||
// Now try with a few sample files | // Now try with a few sample files | ||||
// Normal charset | // Normal charset | ||||
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx"); | |||||
assertEquals(0, | |||||
try (XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx")) { | |||||
assertEquals(0, | |||||
wb1.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet() | wb1.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet() | ||||
); | |||||
wb1.close(); | |||||
); | |||||
} | |||||
// GB2312 charset | // GB2312 charset | ||||
XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("49273.xlsx"); | |||||
assertEquals(134, | |||||
try (XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("49273.xlsx")) { | |||||
assertEquals(134, | |||||
wb2.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet() | wb2.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet() | ||||
); | |||||
wb2.close(); | |||||
); | |||||
} | |||||
} | } | ||||
@Test | @Test | ||||
// store test from TestSheetUtil here as it uses XSSF | // store test from TestSheetUtil here as it uses XSSF | ||||
@Test | @Test | ||||
public void testCanComputeWidthXSSF() throws IOException { | public void testCanComputeWidthXSSF() throws IOException { | ||||
Workbook wb = new XSSFWorkbook(); | |||||
try (Workbook wb = new XSSFWorkbook()) { | |||||
// cannot check on result because on some machines we get back false here! | |||||
SheetUtil.canComputeColumnWidth(wb.getFontAt(0)); | |||||
// cannot check on result because on some machines we get back false here! | |||||
SheetUtil.canComputeColumnWidth(wb.getFontAt(0)); | |||||
wb.close(); | |||||
} | |||||
} | } | ||||
// store test from TestSheetUtil here as it uses XSSF | // store test from TestSheetUtil here as it uses XSSF | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void testFindFont() throws IOException { | public void testFindFont() throws IOException { | ||||
XSSFWorkbook wb = new XSSFWorkbook(); | |||||
assertEquals(1, wb.getNumberOfFonts()); | |||||
try (XSSFWorkbook wb = new XSSFWorkbook()) { | |||||
assertEquals(1, wb.getNumberOfFonts()); | |||||
XSSFSheet s = wb.createSheet(); | |||||
s.createRow(0); | |||||
s.createRow(1); | |||||
s.getRow(0).createCell(0); | |||||
s.getRow(1).createCell(0); | |||||
XSSFSheet s = wb.createSheet(); | |||||
s.createRow(0); | |||||
s.createRow(1); | |||||
s.getRow(0).createCell(0); | |||||
s.getRow(1).createCell(0); | |||||
assertEquals(1, wb.getNumberOfFonts()); | |||||
assertEquals(1, wb.getNumberOfFonts()); | |||||
XSSFFont f1 = wb.getFontAt(0); | |||||
assertFalse(f1.getBold()); | |||||
XSSFFont f1 = wb.getFontAt(0); | |||||
assertFalse(f1.getBold()); | |||||
// Check that asking for the same font | |||||
// multiple times gives you the same thing. | |||||
// Otherwise, our tests wouldn't work! | |||||
assertSame(wb.getFontAt(0), wb.getFontAt(0)); | |||||
assertEquals( | |||||
// Check that asking for the same font | |||||
// multiple times gives you the same thing. | |||||
// Otherwise, our tests wouldn't work! | |||||
assertSame(wb.getFontAt(0), wb.getFontAt(0)); | |||||
assertEquals( | |||||
wb.getFontAt(0), | wb.getFontAt(0), | ||||
wb.getFontAt(0) | wb.getFontAt(0) | ||||
); | |||||
); | |||||
// Look for a new font we have | |||||
// yet to add | |||||
assertNull( | |||||
// Look for a new font we have | |||||
// yet to add | |||||
assertNull( | |||||
wb.findFont( | wb.findFont( | ||||
false, IndexedColors.INDIGO.getIndex(), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
false, IndexedColors.INDIGO.getIndex(), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
) | ) | ||||
); | |||||
assertNull( | |||||
); | |||||
assertNull( | |||||
wb.getStylesSource().findFont( | wb.getStylesSource().findFont( | ||||
false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
) | ) | ||||
); | |||||
); | |||||
XSSFFont nf = wb.createFont(); | |||||
assertEquals(2, wb.getNumberOfFonts()); | |||||
XSSFFont nf = wb.createFont(); | |||||
assertEquals(2, wb.getNumberOfFonts()); | |||||
assertEquals(1, nf.getIndex()); | |||||
assertEquals(nf, wb.getFontAt(1)); | |||||
assertEquals(1, nf.getIndex()); | |||||
assertEquals(nf, wb.getFontAt(1)); | |||||
nf.setBold(false); | |||||
nf.setColor(IndexedColors.INDIGO.getIndex()); | |||||
nf.setFontHeight((short) 22); | |||||
nf.setFontName("Thingy"); | |||||
nf.setItalic(false); | |||||
nf.setStrikeout(true); | |||||
nf.setTypeOffset((short) 2); | |||||
nf.setUnderline((byte) 2); | |||||
nf.setBold(false); | |||||
nf.setColor(IndexedColors.INDIGO.getIndex()); | |||||
nf.setFontHeight((short) 22); | |||||
nf.setFontName("Thingy"); | |||||
nf.setItalic(false); | |||||
nf.setStrikeout(true); | |||||
nf.setTypeOffset((short) 2); | |||||
nf.setUnderline((byte) 2); | |||||
assertEquals(2, wb.getNumberOfFonts()); | |||||
assertEquals(nf, wb.getFontAt(1)); | |||||
assertEquals(2, wb.getNumberOfFonts()); | |||||
assertEquals(nf, wb.getFontAt(1)); | |||||
assertNotSame(wb.getFontAt(0), wb.getFontAt(1)); | |||||
assertNotSame(wb.getFontAt(0), wb.getFontAt(1)); | |||||
// Find it now | |||||
assertNotNull( | |||||
// Find it now | |||||
assertNotNull( | |||||
wb.findFont( | wb.findFont( | ||||
false, IndexedColors.INDIGO.getIndex(), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
false, IndexedColors.INDIGO.getIndex(), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
) | ) | ||||
); | |||||
assertNotNull( | |||||
); | |||||
assertNotNull( | |||||
wb.getStylesSource().findFont( | wb.getStylesSource().findFont( | ||||
false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
) | ) | ||||
); | |||||
); | |||||
XSSFFont font = wb.findFont( | |||||
XSSFFont font = wb.findFont( | |||||
false, IndexedColors.INDIGO.getIndex(), (short) 22, | false, IndexedColors.INDIGO.getIndex(), (short) 22, | ||||
"Thingy", false, true, (short) 2, (byte) 2 | "Thingy", false, true, (short) 2, (byte) 2 | ||||
); | |||||
assertNotNull(font); | |||||
assertEquals( | |||||
); | |||||
assertNotNull(font); | |||||
assertEquals( | |||||
1, | 1, | ||||
font.getIndex() | font.getIndex() | ||||
); | |||||
assertEquals(nf, | |||||
); | |||||
assertEquals(nf, | |||||
wb.findFont( | wb.findFont( | ||||
false, IndexedColors.INDIGO.getIndex(), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
false, IndexedColors.INDIGO.getIndex(), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
) | ) | ||||
); | |||||
assertEquals(nf, | |||||
); | |||||
assertEquals(nf, | |||||
wb.getStylesSource().findFont( | wb.getStylesSource().findFont( | ||||
false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22, | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
) | ) | ||||
); | |||||
); | |||||
wb.close(); | |||||
} | |||||
} | } | ||||
@Test | @Test |
package org.apache.poi.xssf.usermodel; | package org.apache.poi.xssf.usermodel; | ||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | |||||
import static org.junit.jupiter.api.Assertions.assertEquals; | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
import static org.junit.jupiter.api.Assertions.assertNotNull; | import static org.junit.jupiter.api.Assertions.assertNotNull; | ||||
import static org.junit.jupiter.api.Assertions.assertSame; | import static org.junit.jupiter.api.Assertions.assertSame; | ||||
import static org.junit.jupiter.api.Assertions.assertThrows; | import static org.junit.jupiter.api.Assertions.assertThrows; | ||||
import static org.junit.jupiter.api.Assertions.fail; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.HashMap; | import java.util.HashMap; | ||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.function.Function; | |||||
import org.apache.poi.hssf.HSSFTestDataSamples; | import org.apache.poi.hssf.HSSFTestDataSamples; | ||||
import org.apache.poi.ss.usermodel.BaseTestFormulaEvaluator; | import org.apache.poi.ss.usermodel.BaseTestFormulaEvaluator; | ||||
import org.apache.poi.xssf.XSSFTestDataSamples; | import org.apache.poi.xssf.XSSFTestDataSamples; | ||||
import org.junit.jupiter.api.Disabled; | import org.junit.jupiter.api.Disabled; | ||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
import org.junit.jupiter.params.ParameterizedTest; | |||||
import org.junit.jupiter.params.provider.ValueSource; | |||||
public final class TestXSSFFormulaEvaluation extends BaseTestFormulaEvaluator { | public final class TestXSSFFormulaEvaluation extends BaseTestFormulaEvaluator { | ||||
super(XSSFITestDataProvider.instance); | super(XSSFITestDataProvider.instance); | ||||
} | } | ||||
@Test | |||||
public void testSharedFormulas() throws IOException { | |||||
baseTestSharedFormulas("shared_formulas.xlsx"); | |||||
} | |||||
@Test | @Test | ||||
public void testSharedFormulas_evaluateInCell() throws IOException { | public void testSharedFormulas_evaluateInCell() throws IOException { | ||||
try (XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("49872.xlsx")) { | |||||
try (Workbook wb = _testDataProvider.openSampleWorkbook("49872.xlsx")) { | |||||
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | ||||
XSSFSheet sheet = wb.getSheetAt(0); | |||||
Sheet sheet = wb.getSheetAt(0); | |||||
double result = 3.0; | double result = 3.0; | ||||
// C3 and D3: <f t="shared" si="0"/> | // C3 and D3: <f t="shared" si="0"/> | ||||
// get B3 and evaluate it in the cell | // get B3 and evaluate it in the cell | ||||
XSSFCell b3 = sheet.getRow(2).getCell(1); | |||||
Cell b3 = sheet.getRow(2).getCell(1); | |||||
assertEquals(result, evaluator.evaluateInCell(b3).getNumericCellValue(), 0); | assertEquals(result, evaluator.evaluateInCell(b3).getNumericCellValue(), 0); | ||||
//at this point the master formula is gone, but we are still able to evaluate dependent cells | //at this point the master formula is gone, but we are still able to evaluate dependent cells | ||||
XSSFCell c3 = sheet.getRow(2).getCell(2); | |||||
Cell c3 = sheet.getRow(2).getCell(2); | |||||
assertEquals(result, evaluator.evaluateInCell(c3).getNumericCellValue(), 0); | assertEquals(result, evaluator.evaluateInCell(c3).getNumericCellValue(), 0); | ||||
XSSFCell d3 = sheet.getRow(2).getCell(3); | |||||
Cell d3 = sheet.getRow(2).getCell(3); | |||||
assertEquals(result, evaluator.evaluateInCell(d3).getNumericCellValue(), 0); | assertEquals(result, evaluator.evaluateInCell(d3).getNumericCellValue(), 0); | ||||
} | } | ||||
} | } | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void testEvaluateColumnGreaterThan255() throws IOException { | public void testEvaluateColumnGreaterThan255() throws IOException { | ||||
try (XSSFWorkbook wb = (XSSFWorkbook) _testDataProvider.openSampleWorkbook("50096.xlsx")) { | |||||
XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | |||||
try (Workbook wb = _testDataProvider.openSampleWorkbook("50096.xlsx")) { | |||||
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | |||||
/* | /* | ||||
* The first row simply contains the numbers 1 - 300. | * The first row simply contains the numbers 1 - 300. | ||||
* The second row simply refers to the cell value above in the first row by a simple formula. | * The second row simply refers to the cell value above in the first row by a simple formula. | ||||
*/ | */ | ||||
for (int i = 245; i < 265; i++) { | for (int i = 245; i < 265; i++) { | ||||
XSSFCell cell_noformula = wb.getSheetAt(0).getRow(0).getCell(i); | |||||
XSSFCell cell_formula = wb.getSheetAt(0).getRow(1).getCell(i); | |||||
Cell cell_noformula = wb.getSheetAt(0).getRow(0).getCell(i); | |||||
Cell cell_formula = wb.getSheetAt(0).getRow(1).getCell(i); | |||||
CellReference ref_noformula = new CellReference(cell_noformula.getRowIndex(), cell_noformula.getColumnIndex()); | CellReference ref_noformula = new CellReference(cell_noformula.getRowIndex(), cell_noformula.getColumnIndex()); | ||||
CellReference ref_formula = new CellReference(cell_noformula.getRowIndex(), cell_noformula.getColumnIndex()); | CellReference ref_formula = new CellReference(cell_noformula.getRowIndex(), cell_noformula.getColumnIndex()); | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void testReferencesToOtherWorkbooks() throws Exception { | public void testReferencesToOtherWorkbooks() throws Exception { | ||||
try (XSSFWorkbook wb = (XSSFWorkbook) _testDataProvider.openSampleWorkbook("ref2-56737.xlsx")) { | |||||
XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | |||||
XSSFSheet s = wb.getSheetAt(0); | |||||
try (Workbook wb = _testDataProvider.openSampleWorkbook("ref2-56737.xlsx")) { | |||||
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | |||||
Sheet s = wb.getSheetAt(0); | |||||
// References to a .xlsx file | // References to a .xlsx file | ||||
Row rXSLX = s.getRow(2); | Row rXSLX = s.getRow(2); | ||||
} | } | ||||
} | } | ||||
/** | |||||
/* | |||||
* If a formula references cells or named ranges in another workbook, | * If a formula references cells or named ranges in another workbook, | ||||
* but that isn't available at evaluation time, the cached values | * but that isn't available at evaluation time, the cached values | ||||
* should be used instead | * should be used instead | ||||
* TODO Add the support then add a unit test | * TODO Add the support then add a unit test | ||||
* See bug #56752 | * See bug #56752 | ||||
*/ | */ | ||||
@Disabled | |||||
public void testCachedReferencesToOtherWorkbooks() { | |||||
} | |||||
// @Disabled | |||||
// public void testCachedReferencesToOtherWorkbooks() { | |||||
// } | |||||
/** | /** | ||||
* A handful of functions (such as SUM, COUNTA, MIN) support | * A handful of functions (such as SUM, COUNTA, MIN) support | ||||
* This test, based on common test files for HSSF and XSSF, checks | * This test, based on common test files for HSSF and XSSF, checks | ||||
* that we can correctly evaluate these | * that we can correctly evaluate these | ||||
*/ | */ | ||||
@Test | |||||
public void testMultiSheetReferencesHSSFandXSSF() throws Exception { | |||||
Workbook wb1 = HSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xls"); | |||||
Workbook wb2 = XSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xlsx"); | |||||
@ParameterizedTest | |||||
@ValueSource(strings = {"55906-MultiSheetRefs.xls","55906-MultiSheetRefs.xlsx"}) | |||||
public void testMultiSheetReferencesHSSFandXSSF(String sampleFileName) throws Exception { | |||||
Function<String, Workbook> fun = sampleFileName.endsWith("x") | |||||
? XSSFTestDataSamples::openSampleWorkbook : HSSFTestDataSamples::openSampleWorkbook; | |||||
for (Workbook wb : new Workbook[] {wb1,wb2}) { | |||||
try (Workbook wb = fun.apply(sampleFileName)) { | |||||
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | ||||
Sheet s1 = wb.getSheetAt(0); | Sheet s1 = wb.getSheetAt(0); | ||||
assertEquals("COUNTA(Sheet1:Sheet3!E1)", countA_3F.getCellFormula()); | assertEquals("COUNTA(Sheet1:Sheet3!E1)", countA_3F.getCellFormula()); | ||||
assertEquals("3.0", evaluator.evaluate(countA_3F).formatAsString()); | assertEquals("3.0", evaluator.evaluate(countA_3F).formatAsString()); | ||||
} | } | ||||
wb2.close(); | |||||
wb1.close(); | |||||
} | } | ||||
/** | /** | ||||
* This test, based on common test files for HSSF and XSSF, checks | * This test, based on common test files for HSSF and XSSF, checks | ||||
* that we can correctly evaluate these | * that we can correctly evaluate these | ||||
*/ | */ | ||||
@Test | |||||
public void testMultiSheetAreasHSSFandXSSF() throws IOException { | |||||
Workbook wb1 = HSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xls"); | |||||
Workbook wb2 = XSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xlsx"); | |||||
@ParameterizedTest | |||||
@ValueSource(strings = {"55906-MultiSheetRefs.xls","55906-MultiSheetRefs.xlsx"}) | |||||
public void testMultiSheetAreasHSSFandXSSF(String sampleFileName) throws Exception { | |||||
Function<String, Workbook> fun = sampleFileName.endsWith("x") | |||||
? XSSFTestDataSamples::openSampleWorkbook : HSSFTestDataSamples::openSampleWorkbook; | |||||
for (Workbook wb : new Workbook[]{wb1,wb2}) { | |||||
try (Workbook wb = fun.apply(sampleFileName)) { | |||||
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | ||||
Sheet s1 = wb.getSheetAt(0); | Sheet s1 = wb.getSheetAt(0); | ||||
assertEquals("COUNT(Sheet1:Sheet3!$A$1:$B$2)", countFA.getCellFormula()); | assertEquals("COUNT(Sheet1:Sheet3!$A$1:$B$2)", countFA.getCellFormula()); | ||||
assertEquals("4.0", evaluator.evaluate(countFA).formatAsString()); | assertEquals("4.0", evaluator.evaluate(countFA).formatAsString()); | ||||
} | } | ||||
wb2.close(); | |||||
wb1.close(); | |||||
} | } | ||||
// bug 57721 | |||||
@Test | |||||
public void structuredReferences() throws IOException { | |||||
verifyAllFormulasInWorkbookCanBeEvaluated("evaluate_formula_with_structured_table_references.xlsx"); | |||||
} | |||||
@ParameterizedTest | |||||
@ValueSource(strings = { | |||||
// bug 57721 | |||||
"evaluate_formula_with_structured_table_references.xlsx" | |||||
// bug 57840 | |||||
@Disabled("Takes over a minute to evaluate all formulas in this large workbook. Run this test when profiling for formula evaluation speed.") | |||||
@Test | |||||
public void testLotsOfFormulasWithStructuredReferencesToCalculatedTableColumns() throws IOException { | |||||
verifyAllFormulasInWorkbookCanBeEvaluated("StructuredRefs-lots-with-lookups.xlsx"); | |||||
} | |||||
// FIXME: use junit4 parametrization | |||||
private static void verifyAllFormulasInWorkbookCanBeEvaluated(String sampleWorkbook) throws IOException { | |||||
// bug 57840: | |||||
// Takes over a minute to evaluate all formulas in this large workbook. Run this test when profiling for formula evaluation speed. | |||||
// , "StructuredRefs-lots-with-lookups.xlsx" | |||||
}) | |||||
public void verifyAllFormulasInWorkbookCanBeEvaluated(String sampleWorkbook) throws IOException { | |||||
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(sampleWorkbook)) { | try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(sampleWorkbook)) { | ||||
XSSFFormulaEvaluator.evaluateAllFormulaCells(wb); | |||||
assertDoesNotThrow(() -> XSSFFormulaEvaluator.evaluateAllFormulaCells(wb)); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
@Test | @Test | ||||
@Disabled // this is from an open bug/discussion over handling localization for number formats | |||||
@Disabled("this is from an open bug/discussion over handling localization for number formats") | |||||
public void testBug61495() throws IOException { | public void testBug61495() throws IOException { | ||||
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61495-test.xlsm")) { | try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61495-test.xlsm")) { | ||||
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | ||||
Cell cell = wb.getSheetAt(0).getRow(0).getCell(1); | Cell cell = wb.getSheetAt(0).getRow(0).getCell(1); | ||||
// assertEquals("D 67.10", cell.getStringCellValue()); | |||||
// assertEquals("D 67.10", cell.getStringCellValue()); | |||||
CellValue value = evaluator.evaluate(cell); | |||||
assertEquals("D 67.10", | |||||
value.getStringValue()); | |||||
String act = evaluator.evaluate(cell).getStringValue(); | |||||
assertEquals("D 67.10", act); | |||||
assertEquals("D 0,068", | |||||
evaluator.evaluate(wb.getSheetAt(0).getRow(1).getCell(1)).getStringValue()); | |||||
cell = wb.getSheetAt(0).getRow(1).getCell(1); | |||||
act = evaluator.evaluate(cell).getStringValue(); | |||||
assertEquals("D 0,068", act); | |||||
} | } | ||||
} | } | ||||
Cell value = evaluator.evaluateInCell(a2); | Cell value = evaluator.evaluateInCell(a2); | ||||
assertEquals("a value", value.getStringCellValue(), "wrong value A2"); | assertEquals("a value", value.getStringCellValue(), "wrong value A2"); | ||||
// evaluator.clearAllCachedResultValues(); | |||||
// evaluator.clearAllCachedResultValues(); | |||||
Cell a3 = wb.getSheetAt(0).getRow(2).getCell(0); | Cell a3 = wb.getSheetAt(0).getRow(2).getCell(0); | ||||
value = evaluator.evaluateInCell(a3); | value = evaluator.evaluateInCell(a3); |
import org.apache.poi.ss.usermodel.BaseTestPicture; | import org.apache.poi.ss.usermodel.BaseTestPicture; | ||||
import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType; | import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType; | ||||
import org.apache.poi.ss.usermodel.Drawing; | |||||
import org.apache.poi.ss.usermodel.Picture; | |||||
import org.apache.poi.util.LocaleUtil; | import org.apache.poi.util.LocaleUtil; | ||||
import org.apache.poi.xssf.XSSFITestDataProvider; | import org.apache.poi.xssf.XSSFITestDataProvider; | ||||
import org.apache.poi.xssf.XSSFTestDataSamples; | import org.apache.poi.xssf.XSSFTestDataSamples; | ||||
super(XSSFITestDataProvider.instance); | super(XSSFITestDataProvider.instance); | ||||
} | } | ||||
@Test | |||||
public void resize() throws Exception { | |||||
try (XSSFWorkbook wb = XSSFITestDataProvider.instance.openSampleWorkbook("resize_compare.xlsx")) { | |||||
XSSFDrawing dp = wb.getSheetAt(0).createDrawingPatriarch(); | |||||
List<XSSFShape> pics = dp.getShapes(); | |||||
XSSFPicture inpPic = (XSSFPicture) pics.get(0); | |||||
XSSFPicture cmpPic = (XSSFPicture) pics.get(0); | |||||
baseTestResize(inpPic, cmpPic, 2.0, 2.0); | |||||
} | |||||
protected Picture getPictureShape(Drawing<?> pat, int picIdx) { | |||||
return (Picture)((XSSFDrawing)pat).getShapes().get(picIdx); | |||||
} | } | ||||
@Test | @Test | ||||
public void create() throws IOException { | public void create() throws IOException { | ||||
try (XSSFWorkbook wb = new XSSFWorkbook()) { | try (XSSFWorkbook wb = new XSSFWorkbook()) { |
//super.defaultColumnStyle(); | //super.defaultColumnStyle(); | ||||
} | } | ||||
@Test | |||||
public void getSetMargin() throws IOException { | |||||
baseTestGetSetMargin(new double[]{0.7, 0.7, 0.75, 0.75, 0.3, 0.3}); | |||||
} | |||||
@Test | @Test | ||||
public void existingHeaderFooter() throws IOException { | public void existingHeaderFooter() throws IOException { | ||||
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("45540_classic_Header.xlsx"); | XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("45540_classic_Header.xlsx"); | ||||
// Save and re-load | // Save and re-load | ||||
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) { | try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1)) { | ||||
sheet1 = wb2.getSheetAt(0); | sheet1 = wb2.getSheetAt(0); | ||||
// FIXME: forgot to reassign! | |||||
//cols = sheet1.getCTWorksheet().getColsArray(0); | |||||
cols = sheet1.getCTWorksheet().getColsArray(0); | |||||
assertEquals(6, cols.sizeOfColArray()); | assertEquals(6, cols.sizeOfColArray()); | ||||
checkColumnGroup(cols.getColArray(0), 4, 7); // false, true | |||||
checkColumnGroup(cols.getColArray(1), 8, 8, false, false); | |||||
checkColumnGroup(cols.getColArray(2), 9, 9); // false, true | |||||
checkColumnGroup(cols.getColArray(3), 10, 11); // false, true | |||||
checkColumnGroup(cols.getColArray(4), 12, 12, false, false); | |||||
checkColumnGroup(cols.getColArray(5), 13, 13, false, false); | |||||
checkColumnGroup(cols.getColArray(0), 4, 7, false, true); | |||||
checkColumnGroup(cols.getColArray(1), 8, 8, false, true); | |||||
checkColumnGroup(cols.getColArray(2), 9, 9, false, true); | |||||
checkColumnGroup(cols.getColArray(3), 10, 11, false, true); | |||||
checkColumnGroup(cols.getColArray(4), 12, 12, false, true); | |||||
checkColumnGroup(cols.getColArray(5), 13, 13, false, true); | |||||
} | } | ||||
} | } | ||||
} | } |
import static org.junit.jupiter.api.Assertions.assertSame; | import static org.junit.jupiter.api.Assertions.assertSame; | ||||
import static org.junit.jupiter.api.Assertions.assertThrows; | import static org.junit.jupiter.api.Assertions.assertThrows; | ||||
import static org.junit.jupiter.api.Assertions.assertTrue; | import static org.junit.jupiter.api.Assertions.assertTrue; | ||||
import static org.junit.jupiter.api.Assertions.fail; | |||||
import java.io.ByteArrayInputStream; | import java.io.ByteArrayInputStream; | ||||
import java.io.ByteArrayOutputStream; | import java.io.ByteArrayOutputStream; | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void changeSheetNameWithSharedFormulas() throws IOException { | |||||
changeSheetNameWithSharedFormulas("shared_formulas.xlsx"); | |||||
} | |||||
@Test | @Test | ||||
public void columnWidthPOI52233() throws Exception { | public void columnWidthPOI52233() throws Exception { | ||||
XSSFWorkbook workbook = new XSSFWorkbook(); | XSSFWorkbook workbook = new XSSFWorkbook(); |
package org.apache.poi.hssf.usermodel; | package org.apache.poi.hssf.usermodel; | ||||
import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook; | |||||
import static org.junit.jupiter.api.Assertions.assertEquals; | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
import static org.junit.jupiter.api.Assertions.assertTrue; | |||||
import static org.junit.jupiter.api.Assertions.assertNotNull; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.Arrays; | |||||
import java.util.List; | |||||
import org.apache.poi.hssf.HSSFITestDataProvider; | import org.apache.poi.hssf.HSSFITestDataProvider; | ||||
import org.apache.poi.hssf.HSSFTestDataSamples; | |||||
import org.apache.poi.ss.usermodel.BaseTestDataFormat; | import org.apache.poi.ss.usermodel.BaseTestDataFormat; | ||||
import org.apache.poi.ss.usermodel.BuiltinFormats; | |||||
import org.apache.poi.ss.usermodel.Cell; | import org.apache.poi.ss.usermodel.Cell; | ||||
import org.apache.poi.ss.usermodel.CellStyle; | import org.apache.poi.ss.usermodel.CellStyle; | ||||
import org.apache.poi.ss.usermodel.DataFormat; | |||||
import org.apache.poi.ss.usermodel.Row; | import org.apache.poi.ss.usermodel.Row; | ||||
import org.apache.poi.ss.usermodel.Sheet; | |||||
import org.apache.poi.util.POILogFactory; | import org.apache.poi.util.POILogFactory; | ||||
import org.apache.poi.util.POILogger; | import org.apache.poi.util.POILogger; | ||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
* Tests for {@link HSSFDataFormat} | * Tests for {@link HSSFDataFormat} | ||||
*/ | */ | ||||
public final class TestHSSFDataFormat extends BaseTestDataFormat { | public final class TestHSSFDataFormat extends BaseTestDataFormat { | ||||
private static POILogger _logger = POILogFactory.getLogger(TestHSSFDataFormat.class); | |||||
private static final POILogger _logger = POILogFactory.getLogger(TestHSSFDataFormat.class); | |||||
public TestHSSFDataFormat() { | public TestHSSFDataFormat() { | ||||
super(HSSFITestDataProvider.instance); | super(HSSFITestDataProvider.instance); | ||||
} | } | ||||
/** | |||||
* [Bug 49928] formatCellValue returns incorrect value for \u00a3 formatted cells | |||||
*/ | |||||
@Override | |||||
@Test | |||||
public void test49928() throws IOException { | |||||
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49928.xls"); | |||||
doTest49928Core(wb); | |||||
// an attempt to register an existing format returns its index | |||||
int poundFmtIdx = wb.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getDataFormat(); | |||||
assertEquals(poundFmtIdx, wb.createDataFormat().getFormat(poundFmt)); | |||||
// now create a custom format with Pound (\u00a3) | |||||
DataFormat dataFormat = wb.createDataFormat(); | |||||
short customFmtIdx = dataFormat.getFormat("\u00a3##.00[Yellow]"); | |||||
assertTrue(customFmtIdx >= BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX ); | |||||
assertEquals("\u00a3##.00[Yellow]", dataFormat.getFormat(customFmtIdx)); | |||||
wb.close(); | |||||
} | |||||
/** | |||||
* [Bug 58532] Handle formats that go numnum, numK, numM etc | |||||
*/ | |||||
@Override | |||||
@Test | |||||
public void test58532() throws IOException { | |||||
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("FormatKM.xls"); | |||||
doTest58532Core(wb); | |||||
wb.close(); | |||||
} | |||||
/** | /** | ||||
* Bug 51378: getDataFormatString method call crashes when reading the test file | * Bug 51378: getDataFormatString method call crashes when reading the test file | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void test51378() throws IOException { | public void test51378() throws IOException { | ||||
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("12561-1.xls"); | |||||
for (int i = 0; i < wb.getNumberOfSheets(); i++) { | |||||
HSSFSheet sheet = wb.getSheetAt(i); | |||||
for (Row row : sheet) { | |||||
for (Cell cell : row) { | |||||
CellStyle style = cell.getCellStyle(); | |||||
String fmt = style.getDataFormatString(); | |||||
if(fmt == null) { | |||||
_logger.log(POILogger.WARN, cell + ": " + fmt); | |||||
List<String> expNull = Arrays.asList( "0-3-0","0-43-11" ); | |||||
try (HSSFWorkbook wb = openSampleWorkbook("12561-1.xls")) { | |||||
for (Sheet sheet : wb) { | |||||
for (Row row : sheet) { | |||||
for (Cell cell : row) { | |||||
CellStyle style = cell.getCellStyle(); | |||||
assertNotNull(style); | |||||
String coord = wb.getSheetIndex(sheet)+"-"+cell.getRowIndex()+"-"+cell.getColumnIndex(); | |||||
String fmt = style.getDataFormatString(); | |||||
assertEquals(expNull.contains(coord), fmt == null, coord+" unexpected"); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
wb.close(); | |||||
} | } | ||||
} | } |
package org.apache.poi.hssf.usermodel; | package org.apache.poi.hssf.usermodel; | ||||
import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook; | |||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | |||||
import java.io.IOException; | |||||
import java.util.AbstractMap; | import java.util.AbstractMap; | ||||
import java.util.Map; | import java.util.Map; | ||||
import org.apache.poi.hssf.HSSFTestDataSamples; | |||||
import org.apache.poi.ss.formula.EvaluationSheet; | import org.apache.poi.ss.formula.EvaluationSheet; | ||||
import org.apache.poi.ss.usermodel.BaseTestXEvaluationSheet; | import org.apache.poi.ss.usermodel.BaseTestXEvaluationSheet; | ||||
import org.apache.poi.ss.usermodel.Name; | |||||
import org.apache.poi.ss.usermodel.Sheet; | import org.apache.poi.ss.usermodel.Sheet; | ||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
} | } | ||||
@Test | @Test | ||||
public void testMissingExternalName() { | |||||
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("external_name.xls"); | |||||
for (Name name : wb.getAllNames()) { | |||||
public void testMissingExternalName() throws IOException { | |||||
try (HSSFWorkbook wb = openSampleWorkbook("external_name.xls")) { | |||||
// this sometimes causes exceptions | // this sometimes causes exceptions | ||||
if(!name.isFunctionName()) { | |||||
name.getRefersToFormula(); | |||||
} | |||||
wb.getAllNames().stream().filter(n -> !n.isFunctionName()).forEach( | |||||
n -> assertDoesNotThrow(n::getRefersToFormula) | |||||
); | |||||
} | } | ||||
} | } | ||||
} | } |
package org.apache.poi.hssf.usermodel; | package org.apache.poi.hssf.usermodel; | ||||
import java.io.IOException; | |||||
import java.util.stream.Stream; | |||||
import org.apache.poi.hssf.HSSFITestDataProvider; | import org.apache.poi.hssf.HSSFITestDataProvider; | ||||
import org.apache.poi.ss.usermodel.BaseTestFont; | import org.apache.poi.ss.usermodel.BaseTestFont; | ||||
import org.apache.poi.ss.usermodel.Font; | import org.apache.poi.ss.usermodel.Font; | ||||
import org.junit.jupiter.api.Test; | |||||
import org.junit.jupiter.params.provider.Arguments; | |||||
/** | /** | ||||
* Tests various functionality having to do with {@link org.apache.poi.ss.usermodel.Name}. | * Tests various functionality having to do with {@link org.apache.poi.ss.usermodel.Name}. | ||||
* | |||||
* @author Yegor Kozlov | |||||
*/ | */ | ||||
public final class TestHSSFFont extends BaseTestFont { | public final class TestHSSFFont extends BaseTestFont { | ||||
super(HSSFITestDataProvider.instance); | super(HSSFITestDataProvider.instance); | ||||
} | } | ||||
@Test | |||||
public void testDefaultFont() throws IOException { | |||||
baseTestDefaultFont(HSSFFont.FONT_ARIAL, (short)200, Font.COLOR_NORMAL); | |||||
@SuppressWarnings("unused") | |||||
public static Stream<Arguments> defaultFont() { | |||||
return Stream.of(Arguments.of(HSSFFont.FONT_ARIAL, (short)200, Font.COLOR_NORMAL)); | |||||
} | } | ||||
} | } |
} | } | ||||
} | } | ||||
} | } | ||||
@Test | |||||
public void testSharedFormulas() throws IOException { | |||||
baseTestSharedFormulas("shared_formulas.xls"); | |||||
} | |||||
} | } |
package org.apache.poi.hssf.usermodel; | package org.apache.poi.hssf.usermodel; | ||||
import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook; | |||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||||
import static org.junit.jupiter.api.Assertions.assertEquals; | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
import org.apache.poi.ss.usermodel.BaseTestPicture; | import org.apache.poi.ss.usermodel.BaseTestPicture; | ||||
import org.apache.poi.ss.usermodel.ClientAnchor; | import org.apache.poi.ss.usermodel.ClientAnchor; | ||||
import org.apache.poi.ss.usermodel.CreationHelper; | import org.apache.poi.ss.usermodel.CreationHelper; | ||||
import org.apache.poi.ss.usermodel.Drawing; | |||||
import org.apache.poi.ss.usermodel.Picture; | |||||
import org.apache.poi.ss.usermodel.PictureData; | import org.apache.poi.ss.usermodel.PictureData; | ||||
import org.apache.poi.ss.usermodel.Workbook; | import org.apache.poi.ss.usermodel.Workbook; | ||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
super(HSSFITestDataProvider.instance); | super(HSSFITestDataProvider.instance); | ||||
} | } | ||||
@Test | |||||
public void resize() throws Exception { | |||||
try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("resize_compare.xls")) { | |||||
HSSFPatriarch dp = wb.getSheetAt(0).createDrawingPatriarch(); | |||||
List<HSSFShape> pics = dp.getChildren(); | |||||
HSSFPicture inpPic = (HSSFPicture) pics.get(0); | |||||
HSSFPicture cmpPic = (HSSFPicture) pics.get(1); | |||||
baseTestResize(inpPic, cmpPic, 2.0, 2.0); | |||||
} | |||||
protected Picture getPictureShape(Drawing<?> pat, int picIdx) { | |||||
return (Picture)((HSSFPatriarch)pat).getChildren().get(picIdx); | |||||
} | } | ||||
/** | /** | ||||
@Test | @Test | ||||
public void readExistingImage() throws IOException { | public void readExistingImage() throws IOException { | ||||
try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls")) { | |||||
try (HSSFWorkbook wb = openSampleWorkbook("drawings.xls")) { | |||||
HSSFSheet sheet = wb.getSheet("picture"); | HSSFSheet sheet = wb.getSheet("picture"); | ||||
HSSFPatriarch drawing = sheet.getDrawingPatriarch(); | HSSFPatriarch drawing = sheet.getDrawingPatriarch(); | ||||
assertEquals(1, drawing.getChildren().size()); | assertEquals(1, drawing.getChildren().size()); |
import org.apache.poi.hssf.HSSFITestDataProvider; | import org.apache.poi.hssf.HSSFITestDataProvider; | ||||
import org.apache.poi.hssf.record.BlankRecord; | import org.apache.poi.hssf.record.BlankRecord; | ||||
import org.apache.poi.hssf.record.RowRecord; | import org.apache.poi.hssf.record.RowRecord; | ||||
import org.apache.poi.ss.SpreadsheetVersion; | |||||
import org.apache.poi.ss.usermodel.BaseTestRow; | import org.apache.poi.ss.usermodel.BaseTestRow; | ||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
super(HSSFITestDataProvider.instance); | super(HSSFITestDataProvider.instance); | ||||
} | } | ||||
@Test | |||||
public void testRowBounds() throws IOException { | |||||
baseTestRowBounds(SpreadsheetVersion.EXCEL97.getLastRowIndex()); | |||||
} | |||||
@Test | |||||
public void testCellBounds() throws IOException { | |||||
baseTestCellBounds(SpreadsheetVersion.EXCEL97.getLastColumnIndex()); | |||||
} | |||||
@Test | @Test | ||||
public void testLastAndFirstColumns_bug46654() throws IOException { | public void testLastAndFirstColumns_bug46654() throws IOException { | ||||
int ROW_IX = 10; | int ROW_IX = 10; |
} | } | ||||
} | } | ||||
@Test | |||||
public void getSetMargin() throws IOException { | |||||
baseTestGetSetMargin(new double[]{0.75, 0.75, 1.0, 1.0, 0.3, 0.3}); | |||||
} | |||||
/** | /** | ||||
* Test the gridset field gets set as expected. | * Test the gridset field gets set as expected. | ||||
*/ | */ | ||||
@Test | @Test | ||||
public void addEmptyRow() throws IOException { | public void addEmptyRow() throws IOException { | ||||
//try to add 5 empty rows to a new sheet | //try to add 5 empty rows to a new sheet | ||||
try (HSSFWorkbook wb = new HSSFWorkbook()) { | |||||
HSSFSheet sheet = wb.createSheet(); | |||||
try (HSSFWorkbook wb1 = new HSSFWorkbook()) { | |||||
HSSFSheet sheet = wb1.createSheet(); | |||||
for (int i = 0; i < 5; i++) { | for (int i = 0; i < 5; i++) { | ||||
sheet.createRow(i); | sheet.createRow(i); | ||||
} | } | ||||
writeOutAndReadBack(wb).close(); | |||||
try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { | |||||
HSSFSheet sheet2 = wb2.getSheetAt(0); | |||||
assertNotNull(sheet2.getRow(4)); | |||||
} | |||||
} | } | ||||
//try adding empty rows in an existing worksheet | //try adding empty rows in an existing worksheet | ||||
try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("Simple.xls")) { | |||||
HSSFSheet sheet = wb.getSheetAt(0); | |||||
for (int i = 3; i < 10; i++) sheet.createRow(i); | |||||
try (HSSFWorkbook wb1 = HSSFTestDataSamples.openSampleWorkbook("Simple.xls")) { | |||||
HSSFSheet sheet = wb1.getSheetAt(0); | |||||
for (int i = 3; i < 10; i++) { | |||||
sheet.createRow(i); | |||||
} | |||||
writeOutAndReadBack(wb).close(); | |||||
try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { | |||||
HSSFSheet sheet2 = wb2.getSheetAt(0); | |||||
assertNotNull(sheet2.getRow(4)); | |||||
} | |||||
} | } | ||||
} | } | ||||
import static org.apache.poi.POITestCase.assertContains; | import static org.apache.poi.POITestCase.assertContains; | ||||
import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook; | import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook; | ||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | |||||
import static org.junit.jupiter.api.Assertions.assertEquals; | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
import static org.junit.jupiter.api.Assertions.assertFalse; | import static org.junit.jupiter.api.Assertions.assertFalse; | ||||
import static org.junit.jupiter.api.Assertions.assertNotNull; | import static org.junit.jupiter.api.Assertions.assertNotNull; | ||||
import org.apache.poi.ss.util.CellRangeAddress; | import org.apache.poi.ss.util.CellRangeAddress; | ||||
import org.apache.poi.util.IOUtils; | import org.apache.poi.util.IOUtils; | ||||
import org.apache.poi.util.TempFile; | import org.apache.poi.util.TempFile; | ||||
import org.junit.jupiter.api.Disabled; | |||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
import org.junit.jupiter.params.ParameterizedTest; | import org.junit.jupiter.params.ParameterizedTest; | ||||
import org.junit.jupiter.params.provider.CsvSource; | import org.junit.jupiter.params.provider.CsvSource; | ||||
wb.close(); | wb.close(); | ||||
} | } | ||||
@Test | |||||
public void changeSheetNameWithSharedFormulas() throws IOException { | |||||
changeSheetNameWithSharedFormulas("shared_formulas.xls"); | |||||
} | |||||
// Should throw exception about invalid POIFSFileSystem | // Should throw exception about invalid POIFSFileSystem | ||||
@Test | @Test | ||||
public void emptyDirectoryNode() throws IOException { | public void emptyDirectoryNode() throws IOException { | ||||
@Test | @Test | ||||
public void testMethods() throws IOException { | public void testMethods() throws IOException { | ||||
HSSFWorkbook wb=new HSSFWorkbook(); | |||||
wb.insertChartRecord(); | |||||
//wb.dumpDrawingGroupRecords(true); | |||||
//wb.dumpDrawingGroupRecords(false); | |||||
wb.close(); | |||||
try (HSSFWorkbook wb=new HSSFWorkbook()) { | |||||
assertDoesNotThrow(wb::insertChartRecord); | |||||
//wb.dumpDrawingGroupRecords(true); | |||||
//wb.dumpDrawingGroupRecords(false); | |||||
} | |||||
} | } | ||||
@Test | @Test | ||||
@Test | @Test | ||||
public void setSheetOrderToEnd() throws Exception { | public void setSheetOrderToEnd() throws Exception { | ||||
final HSSFWorkbook workbook = new HSSFWorkbook(); | |||||
workbook.createSheet("A"); | |||||
try { | |||||
for (int i = 0; i < 2 * workbook.getInternalWorkbook().getRecords().size(); i++) { | |||||
workbook.setSheetOrder("A", 0); | |||||
} | |||||
} catch (Exception e) { | |||||
throw new Exception("Moving a sheet to the end should not throw an exception, but threw ", e); | |||||
try (HSSFWorkbook workbook = new HSSFWorkbook()) { | |||||
workbook.createSheet("A"); | |||||
workbook.createSheet("B"); | |||||
assertEquals("A", workbook.getSheetName(0)); | |||||
workbook.setSheetOrder("A", 1); | |||||
assertEquals("A", workbook.getSheetName(1)); | |||||
} | } | ||||
} | } | ||||
@Test | @Test | ||||
public void testWriteToNewFile() throws Exception { | public void testWriteToNewFile() throws Exception { | ||||
// Open from a Stream | |||||
HSSFWorkbook wb = new HSSFWorkbook( | |||||
samples.openResourceAsStream("SampleSS.xls")); | |||||
// Save to a new temp file | // Save to a new temp file | ||||
final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls"); | final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls"); | ||||
wb.write(file); | |||||
wb.close(); | |||||
// Open from a Stream | |||||
try (HSSFWorkbook wb = new HSSFWorkbook( | |||||
samples.openResourceAsStream("SampleSS.xls"))) { | |||||
wb.write(file); | |||||
} | |||||
// Read and check | // Read and check | ||||
wb = new HSSFWorkbook(new POIFSFileSystem(file)); | |||||
assertEquals(3, wb.getNumberOfSheets()); | |||||
wb.close(); | |||||
try (HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(file))) { | |||||
assertEquals(3, wb.getNumberOfSheets()); | |||||
} | |||||
} | } | ||||
@Disabled | |||||
@Test | |||||
@Override | |||||
public void createDrawing() throws Exception { | public void createDrawing() throws Exception { | ||||
super.createDrawing(); | |||||
// the dimensions for this image are different than for XSSF and SXSSF | // the dimensions for this image are different than for XSSF and SXSSF | ||||
} | } | ||||
} | } |
package org.apache.poi.hssf.usermodel; | package org.apache.poi.hssf.usermodel; | ||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | |||||
import static org.junit.jupiter.api.Assertions.assertEquals; | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
import java.io.ByteArrayInputStream; | import java.io.ByteArrayInputStream; | ||||
import org.apache.poi.util.LocaleUtil; | import org.apache.poi.util.LocaleUtil; | ||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
/** | |||||
* | |||||
*/ | |||||
public final class TestOLE2Embeding { | public final class TestOLE2Embeding { | ||||
@Test | @Test | ||||
// This used to break, until bug #43116 was fixed | // This used to break, until bug #43116 was fixed | ||||
try (HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("ole2-embedding.xls")) { | try (HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("ole2-embedding.xls")) { | ||||
// Check we can get at the Escher layer still | // Check we can get at the Escher layer still | ||||
workbook.getAllPictures(); | |||||
assertDoesNotThrow(workbook::getAllPictures); | |||||
} | } | ||||
} | } | ||||
circle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL); | circle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL); | ||||
circle.setNoFill(true); | circle.setNoFill(true); | ||||
// if (false) { | |||||
// FileOutputStream fos = new FileOutputStream("embed.xls"); | |||||
// wb.write(fos); | |||||
// fos.close(); | |||||
// } | |||||
HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); | HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); | ||||
wb1.close(); | wb1.close(); | ||||
*/ | */ | ||||
public abstract class BaseTestDataFormat { | public abstract class BaseTestDataFormat { | ||||
protected static final String POUND_FMT = "\"\u00a3\"#,##0;[Red]\\-\"\u00a3\"#,##0"; | |||||
private final ITestDataProvider _testDataProvider; | private final ITestDataProvider _testDataProvider; | ||||
protected BaseTestDataFormat(ITestDataProvider testDataProvider) { | protected BaseTestDataFormat(ITestDataProvider testDataProvider) { | ||||
@Test | @Test | ||||
public final void testBuiltinFormats() throws IOException { | public final void testBuiltinFormats() throws IOException { | ||||
Workbook wb = _testDataProvider.createWorkbook(); | |||||
DataFormat df = wb.createDataFormat(); | |||||
try (Workbook wb = _testDataProvider.createWorkbook()) { | |||||
String[] formats = BuiltinFormats.getAll(); | |||||
for (int idx = 0; idx < formats.length; idx++) { | |||||
String fmt = formats[idx]; | |||||
assertEquals(idx, df.getFormat(fmt)); | |||||
DataFormat df = wb.createDataFormat(); | |||||
String[] formats = BuiltinFormats.getAll(); | |||||
for (int idx = 0; idx < formats.length; idx++) { | |||||
String fmt = formats[idx]; | |||||
assertEquals(idx, df.getFormat(fmt)); | |||||
} | |||||
//default format for new cells is General | |||||
Sheet sheet = wb.createSheet(); | |||||
Cell cell = sheet.createRow(0).createCell(0); | |||||
assertEquals(0, cell.getCellStyle().getDataFormat()); | |||||
assertEquals("General", cell.getCellStyle().getDataFormatString()); | |||||
//create a custom data format | |||||
String customFmt = "#0.00 AM/PM"; | |||||
//check it is not in built-in formats | |||||
assertNotBuiltInFormat(customFmt); | |||||
int customIdx = df.getFormat(customFmt); | |||||
//The first user-defined format starts at 164. | |||||
assertTrue(customIdx >= BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX); | |||||
//read and verify the string representation | |||||
assertEquals(customFmt, df.getFormat((short) customIdx)); | |||||
} | } | ||||
//default format for new cells is General | |||||
Sheet sheet = wb.createSheet(); | |||||
Cell cell = sheet.createRow(0).createCell(0); | |||||
assertEquals(0, cell.getCellStyle().getDataFormat()); | |||||
assertEquals("General", cell.getCellStyle().getDataFormatString()); | |||||
//create a custom data format | |||||
String customFmt = "#0.00 AM/PM"; | |||||
//check it is not in built-in formats | |||||
assertNotBuiltInFormat(customFmt); | |||||
int customIdx = df.getFormat(customFmt); | |||||
//The first user-defined format starts at 164. | |||||
assertTrue(customIdx >= BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX); | |||||
//read and verify the string representation | |||||
assertEquals(customFmt, df.getFormat((short)customIdx)); | |||||
wb.close(); | |||||
} | } | ||||
/** | /** | ||||
* [Bug 49928] formatCellValue returns incorrect value for \u00a3 formatted cells | * [Bug 49928] formatCellValue returns incorrect value for \u00a3 formatted cells | ||||
*/ | */ | ||||
@Test | @Test | ||||
public abstract void test49928() throws IOException; | |||||
protected final static String poundFmt = "\"\u00a3\"#,##0;[Red]\\-\"\u00a3\"#,##0"; | |||||
public void doTest49928Core(Workbook wb){ | |||||
DataFormatter df = new DataFormatter(); | |||||
public void test49928() throws IOException { | |||||
String fileName = "49928.xls" + (getClass().getName().contains("xssf") ? "x" : ""); | |||||
try (Workbook wb = _testDataProvider.openSampleWorkbook(fileName)) { | |||||
DataFormatter df = new DataFormatter(); | |||||
Sheet sheet = wb.getSheetAt(0); | |||||
Cell cell = sheet.getRow(0).getCell(0); | |||||
CellStyle style = cell.getCellStyle(); | |||||
// not expected normally, id of a custom format should be greater | |||||
// than BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX | |||||
short poundFmtIdx = 6; | |||||
Sheet sheet = wb.getSheetAt(0); | |||||
Cell cell = sheet.getRow(0).getCell(0); | |||||
CellStyle style = cell.getCellStyle(); | |||||
assertEquals(POUND_FMT, style.getDataFormatString()); | |||||
assertEquals(poundFmtIdx, style.getDataFormat()); | |||||
assertEquals("\u00a31", df.formatCellValue(cell)); | |||||
// not expected normally, id of a custom format should be greater | |||||
// than BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX | |||||
short poundFmtIdx = 6; | |||||
assertEquals(poundFmt, style.getDataFormatString()); | |||||
assertEquals(poundFmtIdx, style.getDataFormat()); | |||||
assertEquals("\u00a31", df.formatCellValue(cell)); | |||||
DataFormat dataFormat = wb.createDataFormat(); | |||||
assertEquals(poundFmtIdx, dataFormat.getFormat(POUND_FMT)); | |||||
assertEquals(POUND_FMT, dataFormat.getFormat(poundFmtIdx)); | |||||
// As of 2015-12-27, there is no way to override a built-in number format with POI XSSFWorkbook | |||||
// 49928.xlsx has been saved with a poundFmt that overrides the default value (dollar) | |||||
poundFmtIdx = wb.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getDataFormat(); | |||||
assertEquals(poundFmtIdx, dataFormat.getFormat(POUND_FMT)); | |||||
DataFormat dataFormat = wb.createDataFormat(); | |||||
assertEquals(poundFmtIdx, dataFormat.getFormat(poundFmt)); | |||||
assertEquals(poundFmt, dataFormat.getFormat(poundFmtIdx)); | |||||
// now create a custom format with Pound (\u00a3) | |||||
String customFmt = "\u00a3##.00[Yellow]"; | |||||
assertNotBuiltInFormat(customFmt); | |||||
short customFmtIdx = dataFormat.getFormat(customFmt); | |||||
assertTrue(customFmtIdx >= BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX); | |||||
assertEquals(customFmt, dataFormat.getFormat(customFmtIdx)); | |||||
} | |||||
} | } | ||||
@Test | @Test | ||||
public void testReadbackFormat() throws IOException { | public void testReadbackFormat() throws IOException { | ||||
readbackFormat("built-in format", "0.00"); | readbackFormat("built-in format", "0.00"); | ||||
readbackFormat("overridden built-in format", poundFmt); | |||||
readbackFormat("overridden built-in format", POUND_FMT); | |||||
String customFormat = "#0.00 AM/PM"; | String customFormat = "#0.00 AM/PM"; | ||||
assertNotBuiltInFormat(customFormat); | assertNotBuiltInFormat(customFormat); | ||||
} | } | ||||
} | } | ||||
/** | |||||
* [Bug 58532] Handle formats that go numnum, numK, numM etc | |||||
*/ | |||||
@Test | @Test | ||||
public abstract void test58532() throws IOException; | |||||
public void doTest58532Core(Workbook wb) { | |||||
Sheet s = wb.getSheetAt(0); | |||||
DataFormatter fmt = new DataFormatter(); | |||||
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator(); | |||||
// Column A is the raw values | |||||
// Column B is the ##/#K/#M values | |||||
// Column C is strings of what they should look like | |||||
// Column D is the #.##/#.#K/#.#M values | |||||
// Column E is strings of what they should look like | |||||
String formatKMWhole = "[>999999]#,,\"M\";[>999]#,\"K\";#"; | |||||
String formatKM3dp = "[>999999]#.000,,\"M\";[>999]#.000,\"K\";#.000"; | |||||
// Check the formats are as expected | |||||
Row headers = s.getRow(0); | |||||
assertNotNull(headers); | |||||
assertEquals(formatKMWhole, headers.getCell(1).getStringCellValue()); | |||||
assertEquals(formatKM3dp, headers.getCell(3).getStringCellValue()); | |||||
Row r2 = s.getRow(1); | |||||
assertNotNull(r2); | |||||
assertEquals(formatKMWhole, r2.getCell(1).getCellStyle().getDataFormatString()); | |||||
assertEquals(formatKM3dp, r2.getCell(3).getCellStyle().getDataFormatString()); | |||||
// For all of the contents rows, check that DataFormatter is able | |||||
// to format the cells to the same value as the one next to it | |||||
for (int rn=1; rn<s.getLastRowNum(); rn++) { | |||||
Row r = s.getRow(rn); | |||||
if (r == null) break; | |||||
double value = r.getCell(0).getNumericCellValue(); | |||||
String expWhole = r.getCell(2).getStringCellValue(); | |||||
String exp3dp = r.getCell(4).getStringCellValue(); | |||||
assertEquals(expWhole, fmt.formatCellValue(r.getCell(1), eval), | |||||
"Wrong formatting of " + value + " for row " + rn); | |||||
assertEquals(exp3dp, fmt.formatCellValue(r.getCell(3), eval), | |||||
"Wrong formatting of " + value + " for row " + rn); | |||||
public void test58532() throws IOException { | |||||
String fileName = "FormatKM.xls" + (getClass().getName().contains("xssf") ? "x" : ""); | |||||
try (Workbook wb = _testDataProvider.openSampleWorkbook(fileName)) { | |||||
Sheet s = wb.getSheetAt(0); | |||||
DataFormatter fmt = new DataFormatter(); | |||||
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator(); | |||||
// Column A is the raw values | |||||
// Column B is the ##/#K/#M values | |||||
// Column C is strings of what they should look like | |||||
// Column D is the #.##/#.#K/#.#M values | |||||
// Column E is strings of what they should look like | |||||
String formatKMWhole = "[>999999]#,,\"M\";[>999]#,\"K\";#"; | |||||
String formatKM3dp = "[>999999]#.000,,\"M\";[>999]#.000,\"K\";#.000"; | |||||
// Check the formats are as expected | |||||
Row headers = s.getRow(0); | |||||
assertNotNull(headers); | |||||
assertEquals(formatKMWhole, headers.getCell(1).getStringCellValue()); | |||||
assertEquals(formatKM3dp, headers.getCell(3).getStringCellValue()); | |||||
Row r2 = s.getRow(1); | |||||
assertNotNull(r2); | |||||
assertEquals(formatKMWhole, r2.getCell(1).getCellStyle().getDataFormatString()); | |||||
assertEquals(formatKM3dp, r2.getCell(3).getCellStyle().getDataFormatString()); | |||||
// For all of the contents rows, check that DataFormatter is able | |||||
// to format the cells to the same value as the one next to it | |||||
for (int rn = 1; rn < s.getLastRowNum(); rn++) { | |||||
Row r = s.getRow(rn); | |||||
if (r == null) break; | |||||
double value = r.getCell(0).getNumericCellValue(); | |||||
String expWhole = r.getCell(2).getStringCellValue(); | |||||
String exp3dp = r.getCell(4).getStringCellValue(); | |||||
assertEquals(expWhole, fmt.formatCellValue(r.getCell(1), eval), | |||||
"Wrong formatting of " + value + " for row " + rn); | |||||
assertEquals(exp3dp, fmt.formatCellValue(r.getCell(3), eval), | |||||
"Wrong formatting of " + value + " for row " + rn); | |||||
} | |||||
} | } | ||||
} | } | ||||
*/ | */ | ||||
@Test | @Test | ||||
public final void test58536() throws IOException { | public final void test58536() throws IOException { | ||||
Workbook wb = _testDataProvider.createWorkbook(); | |||||
DataFormatter formatter = new DataFormatter(); | |||||
DataFormat fmt = wb.createDataFormat(); | |||||
Sheet sheet = wb.createSheet(); | |||||
Row r = sheet.createRow(0); | |||||
char pound = '\u00A3'; | |||||
String formatUK = "_-[$"+pound+"-809]* #,##0_-;\\-[$"+pound+"-809]* #,##0_-;_-[$"+pound+"-809]* \"-\"??_-;_-@_-"; | |||||
try (Workbook wb = _testDataProvider.createWorkbook()) { | |||||
DataFormatter formatter = new DataFormatter(); | |||||
DataFormat fmt = wb.createDataFormat(); | |||||
Sheet sheet = wb.createSheet(); | |||||
Row r = sheet.createRow(0); | |||||
CellStyle cs = wb.createCellStyle(); | |||||
cs.setDataFormat(fmt.getFormat(formatUK)); | |||||
char pound = '\u00A3'; | |||||
String formatUK = "_-[$" + pound + "-809]* #,##0_-;\\-[$" + pound + "-809]* #,##0_-;_-[$" + pound + "-809]* \"-\"??_-;_-@_-"; | |||||
Cell pve = r.createCell(0); | |||||
pve.setCellValue(12345); | |||||
pve.setCellStyle(cs); | |||||
CellStyle cs = wb.createCellStyle(); | |||||
cs.setDataFormat(fmt.getFormat(formatUK)); | |||||
Cell nve = r.createCell(1); | |||||
nve.setCellValue(-12345); | |||||
nve.setCellStyle(cs); | |||||
Cell pve = r.createCell(0); | |||||
pve.setCellValue(12345); | |||||
pve.setCellStyle(cs); | |||||
Cell zero = r.createCell(2); | |||||
zero.setCellValue(0); | |||||
zero.setCellStyle(cs); | |||||
Cell nve = r.createCell(1); | |||||
nve.setCellValue(-12345); | |||||
nve.setCellStyle(cs); | |||||
assertEquals(pound+" 12,345", formatter.formatCellValue(pve)); | |||||
assertEquals("-"+pound+" 12,345", formatter.formatCellValue(nve)); | |||||
// TODO Fix this to not have an extra 0 at the end | |||||
//assertEquals(pound+" - ", formatter.formatCellValue(zero)); | |||||
Cell zero = r.createCell(2); | |||||
zero.setCellValue(0); | |||||
zero.setCellStyle(cs); | |||||
wb.close(); | |||||
assertEquals(pound + " 12,345", formatter.formatCellValue(pve)); | |||||
assertEquals("-" + pound + " 12,345", formatter.formatCellValue(nve)); | |||||
// TODO Fix this to not have an extra 0 at the end | |||||
//assertEquals(pound+" - ", formatter.formatCellValue(zero)); | |||||
} | |||||
} | } | ||||
/** | /** | ||||
*/ | */ | ||||
@Test | @Test | ||||
public final void test55265() throws IOException { | public final void test55265() throws IOException { | ||||
Workbook wb = _testDataProvider.createWorkbook(); | |||||
DataFormatter formatter = new DataFormatter(); | |||||
DataFormat fmt = wb.createDataFormat(); | |||||
Sheet sheet = wb.createSheet(); | |||||
Row r = sheet.createRow(0); | |||||
CellStyle cs = wb.createCellStyle(); | |||||
cs.setDataFormat(fmt.getFormat("#'##0")); | |||||
Cell zero = r.createCell(0); | |||||
zero.setCellValue(0); | |||||
zero.setCellStyle(cs); | |||||
Cell sml = r.createCell(1); | |||||
sml.setCellValue(12); | |||||
sml.setCellStyle(cs); | |||||
Cell med = r.createCell(2); | |||||
med.setCellValue(1234); | |||||
med.setCellStyle(cs); | |||||
Cell lge = r.createCell(3); | |||||
lge.setCellValue(12345678); | |||||
lge.setCellStyle(cs); | |||||
assertEquals("0", formatter.formatCellValue(zero)); | |||||
assertEquals("12", formatter.formatCellValue(sml)); | |||||
assertEquals("1'234", formatter.formatCellValue(med)); | |||||
assertEquals("12'345'678", formatter.formatCellValue(lge)); | |||||
wb.close(); | |||||
try (Workbook wb = _testDataProvider.createWorkbook()) { | |||||
DataFormatter formatter = new DataFormatter(); | |||||
DataFormat fmt = wb.createDataFormat(); | |||||
Sheet sheet = wb.createSheet(); | |||||
Row r = sheet.createRow(0); | |||||
CellStyle cs = wb.createCellStyle(); | |||||
cs.setDataFormat(fmt.getFormat("#'##0")); | |||||
Cell zero = r.createCell(0); | |||||
zero.setCellValue(0); | |||||
zero.setCellStyle(cs); | |||||
Cell sml = r.createCell(1); | |||||
sml.setCellValue(12); | |||||
sml.setCellStyle(cs); | |||||
Cell med = r.createCell(2); | |||||
med.setCellValue(1234); | |||||
med.setCellStyle(cs); | |||||
Cell lge = r.createCell(3); | |||||
lge.setCellValue(12345678); | |||||
lge.setCellStyle(cs); | |||||
assertEquals("0", formatter.formatCellValue(zero)); | |||||
assertEquals("12", formatter.formatCellValue(sml)); | |||||
assertEquals("1'234", formatter.formatCellValue(med)); | |||||
assertEquals("12'345'678", formatter.formatCellValue(lge)); | |||||
} | |||||
} | } | ||||
} | } |
import org.apache.poi.ss.ITestDataProvider; | import org.apache.poi.ss.ITestDataProvider; | ||||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||
import org.junit.jupiter.params.ParameterizedTest; | |||||
import org.junit.jupiter.params.provider.MethodSource; | |||||
/** | /** | ||||
* @author Yegor Kozlov | * @author Yegor Kozlov | ||||
_testDataProvider = testDataProvider; | _testDataProvider = testDataProvider; | ||||
} | } | ||||
@SuppressWarnings("JUnit5MalformedParameterized") | |||||
@ParameterizedTest | |||||
@MethodSource("defaultFont") | |||||
protected final void baseTestDefaultFont(String defaultName, short defaultSize, short defaultColor) throws IOException { | protected final void baseTestDefaultFont(String defaultName, short defaultSize, short defaultColor) throws IOException { | ||||
//get default font and check against default value | //get default font and check against default value | ||||
Workbook workbook = _testDataProvider.createWorkbook(); | |||||
Font fontFind=workbook.findFont(false, defaultColor, defaultSize, defaultName, false, false, Font.SS_NONE, Font.U_NONE); | |||||
assertNotNull(fontFind); | |||||
//get default font, then change 2 values and check against different values (height changes) | |||||
Font font=workbook.createFont(); | |||||
font.setBold(true); | |||||
assertTrue(font.getBold()); | |||||
font.setUnderline(Font.U_DOUBLE); | |||||
assertEquals(Font.U_DOUBLE, font.getUnderline()); | |||||
font.setFontHeightInPoints((short)15); | |||||
assertEquals(15*20, font.getFontHeight()); | |||||
assertEquals(15, font.getFontHeightInPoints()); | |||||
fontFind=workbook.findFont(true, defaultColor, (short)(15*20), defaultName, false, false, Font.SS_NONE, Font.U_DOUBLE); | |||||
assertNotNull(fontFind); | |||||
workbook.close(); | |||||
try (Workbook workbook = _testDataProvider.createWorkbook()) { | |||||
Font fontFind = workbook.findFont(false, defaultColor, defaultSize, defaultName, false, false, Font.SS_NONE, Font.U_NONE); | |||||
assertNotNull(fontFind); | |||||
//get default font, then change 2 values and check against different values (height changes) | |||||
Font font = workbook.createFont(); | |||||
font.setBold(true); | |||||
assertTrue(font.getBold()); | |||||
font.setUnderline(Font.U_DOUBLE); | |||||
assertEquals(Font.U_DOUBLE, font.getUnderline()); | |||||
font.setFontHeightInPoints((short) 15); | |||||
assertEquals(15 * 20, font.getFontHeight()); | |||||
assertEquals(15, font.getFontHeightInPoints()); | |||||
fontFind = workbook.findFont(true, defaultColor, (short) (15 * 20), defaultName, false, false, Font.SS_NONE, Font.U_DOUBLE); | |||||
assertNotNull(fontFind); | |||||
} | |||||
} | } | ||||
@Test | @Test | ||||
public final void testGetNumberOfFonts() throws IOException { | public final void testGetNumberOfFonts() throws IOException { | ||||
Workbook wb = _testDataProvider.createWorkbook(); | |||||
int num0 = wb.getNumberOfFonts(); | |||||
Font f1=wb.createFont(); | |||||
f1.setBold(true); | |||||
int idx1 = f1.getIndex(); | |||||
wb.createCellStyle().setFont(f1); | |||||
Font f2=wb.createFont(); | |||||
f2.setUnderline(Font.U_DOUBLE); | |||||
int idx2 = f2.getIndex(); | |||||
wb.createCellStyle().setFont(f2); | |||||
Font f3=wb.createFont(); | |||||
f3.setFontHeightInPoints((short)23); | |||||
int idx3 = f3.getIndex(); | |||||
wb.createCellStyle().setFont(f3); | |||||
assertEquals(num0 + 3,wb.getNumberOfFonts()); | |||||
assertTrue(wb.getFontAt(idx1).getBold()); | |||||
assertEquals(Font.U_DOUBLE,wb.getFontAt(idx2).getUnderline()); | |||||
assertEquals(23,wb.getFontAt(idx3).getFontHeightInPoints()); | |||||
wb.close(); | |||||
} | |||||
try (Workbook wb = _testDataProvider.createWorkbook()) { | |||||
int num0 = wb.getNumberOfFonts(); | |||||
Font f1 = wb.createFont(); | |||||
f1.setBold(true); | |||||
int idx1 = f1.getIndex(); | |||||
wb.createCellStyle().setFont(f1); | |||||
Font f2 = wb.createFont(); | |||||
f2.setUnderline(Font.U_DOUBLE); | |||||
int idx2 = f2.getIndex(); | |||||
wb.createCellStyle().setFont(f2); | |||||
Font f3 = wb.createFont(); | |||||
f3.setFontHeightInPoints((short) 23); | |||||
int idx3 = f3.getIndex(); | |||||
wb.createCellStyle().setFont(f3); | |||||
assertEquals(num0 + 3, wb.getNumberOfFonts()); | |||||
assertTrue(wb.getFontAt(idx1).getBold()); | |||||
assertEquals(Font.U_DOUBLE, wb.getFontAt(idx2).getUnderline()); | |||||
assertEquals(23, wb.getFontAt(idx3).getFontHeightInPoints()); | |||||
} | |||||
} | |||||
/** | /** | ||||
* Tests that we can define fonts to a new | * Tests that we can define fonts to a new | ||||
* file, save, load, and still see them | |||||
* file, save, load, and still see them | |||||
*/ | */ | ||||
@Test | @Test | ||||
public final void testCreateSave() throws IOException { | public final void testCreateSave() throws IOException { | ||||
Workbook wb1 = _testDataProvider.createWorkbook(); | |||||
Sheet s1 = wb1.createSheet(); | |||||
Row r1 = s1.createRow(0); | |||||
Cell r1c1 = r1.createCell(0); | |||||
r1c1.setCellValue(2.2); | |||||
int num0 = wb1.getNumberOfFonts(); | |||||
Font font=wb1.createFont(); | |||||
font.setBold(true); | |||||
font.setStrikeout(true); | |||||
font.setColor(IndexedColors.YELLOW.getIndex()); | |||||
font.setFontName("Courier"); | |||||
int font1Idx = font.getIndex(); | |||||
wb1.createCellStyle().setFont(font); | |||||
assertEquals(num0 + 1, wb1.getNumberOfFonts()); | |||||
CellStyle cellStyleTitle=wb1.createCellStyle(); | |||||
cellStyleTitle.setFont(font); | |||||
r1c1.setCellStyle(cellStyleTitle); | |||||
// Save and re-load | |||||
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); | |||||
wb1.close(); | |||||
s1 = wb2.getSheetAt(0); | |||||
assertEquals(num0 + 1, wb2.getNumberOfFonts()); | |||||
int idx = s1.getRow(0).getCell(0).getCellStyle().getFontIndex(); | |||||
Font fnt = wb2.getFontAt(idx); | |||||
assertNotNull(fnt); | |||||
assertEquals(IndexedColors.YELLOW.getIndex(), fnt.getColor()); | |||||
assertEquals("Courier", fnt.getFontName()); | |||||
// Now add an orphaned one | |||||
Font font2 = wb2.createFont(); | |||||
font2.setItalic(true); | |||||
font2.setFontHeightInPoints((short)15); | |||||
int font2Idx = font2.getIndex(); | |||||
wb2.createCellStyle().setFont(font2); | |||||
assertEquals(num0 + 2, wb2.getNumberOfFonts()); | |||||
// Save and re-load | |||||
Workbook wb3 = _testDataProvider.writeOutAndReadBack(wb2); | |||||
wb2.close(); | |||||
s1 = wb3.getSheetAt(0); | |||||
assertNotNull(s1); | |||||
assertEquals(num0 + 2, wb3.getNumberOfFonts()); | |||||
assertNotNull(wb3.getFontAt(font1Idx)); | |||||
assertNotNull(wb3.getFontAt(font2Idx)); | |||||
assertEquals(15, wb3.getFontAt(font2Idx).getFontHeightInPoints()); | |||||
assertTrue(wb3.getFontAt(font2Idx).getItalic()); | |||||
wb3.close(); | |||||
try (Workbook wb1 = _testDataProvider.createWorkbook()) { | |||||
Sheet s1 = wb1.createSheet(); | |||||
Row r1 = s1.createRow(0); | |||||
Cell r1c1 = r1.createCell(0); | |||||
r1c1.setCellValue(2.2); | |||||
int num0 = wb1.getNumberOfFonts(); | |||||
Font font = wb1.createFont(); | |||||
font.setBold(true); | |||||
font.setStrikeout(true); | |||||
font.setColor(IndexedColors.YELLOW.getIndex()); | |||||
font.setFontName("Courier"); | |||||
int font1Idx = font.getIndex(); | |||||
wb1.createCellStyle().setFont(font); | |||||
assertEquals(num0 + 1, wb1.getNumberOfFonts()); | |||||
CellStyle cellStyleTitle = wb1.createCellStyle(); | |||||
cellStyleTitle.setFont(font); | |||||
r1c1.setCellStyle(cellStyleTitle); | |||||
// Save and re-load | |||||
try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) { | |||||
s1 = wb2.getSheetAt(0); | |||||
assertEquals(num0 + 1, wb2.getNumberOfFonts()); | |||||
int idx = s1.getRow(0).getCell(0).getCellStyle().getFontIndex(); | |||||
Font fnt = wb2.getFontAt(idx); | |||||
assertNotNull(fnt); | |||||
assertEquals(IndexedColors.YELLOW.getIndex(), fnt.getColor()); | |||||
assertEquals("Courier", fnt.getFontName()); | |||||
// Now add an orphaned one | |||||
Font font2 = wb2.createFont(); | |||||
font2.setItalic(true); | |||||
font2.setFontHeightInPoints((short) 15); | |||||
int font2Idx = font2.getIndex(); | |||||
wb2.createCellStyle().setFont(font2); | |||||
assertEquals(num0 + 2, wb2.getNumberOfFonts()); | |||||
// Save and re-load | |||||
try (Workbook wb3 = _testDataProvider.writeOutAndReadBack(wb2)) { | |||||
s1 = wb3.getSheetAt(0); | |||||
assertNotNull(s1); | |||||
assertEquals(num0 + 2, wb3.getNumberOfFonts()); | |||||
assertNotNull(wb3.getFontAt(font1Idx)); | |||||
assertNotNull(wb3.getFontAt(font2Idx)); | |||||
assertEquals(15, wb3.getFontAt(font2Idx).getFontHeightInPoints()); | |||||
assertTrue(wb3.getFontAt(font2Idx).getItalic()); | |||||
} | |||||
} | |||||
} | |||||
} | } | ||||
/** | /** | ||||
*/ | */ | ||||
@Test | @Test | ||||
public final void test45338() throws IOException { | public final void test45338() throws IOException { | ||||
Workbook wb = _testDataProvider.createWorkbook(); | |||||
int num0 = wb.getNumberOfFonts(); | |||||
Sheet s = wb.createSheet(); | |||||
s.createRow(0); | |||||
s.createRow(1); | |||||
s.getRow(0).createCell(0); | |||||
s.getRow(1).createCell(0); | |||||
//default font | |||||
Font f1 = wb.getFontAt(0); | |||||
assertFalse(f1.getBold()); | |||||
// Check that asking for the same font | |||||
// multiple times gives you the same thing. | |||||
// Otherwise, our tests wouldn't work! | |||||
assertSame(wb.getFontAt(0), wb.getFontAt(0)); | |||||
// Look for a new font we have | |||||
// yet to add | |||||
assertNull( | |||||
wb.findFont( | |||||
true, (short)123, (short)(22*20), | |||||
"Thingy", false, true, (short)2, (byte)2 | |||||
) | |||||
); | |||||
Font nf = wb.createFont(); | |||||
int nfIdx = nf.getIndex(); | |||||
assertEquals(num0 + 1, wb.getNumberOfFonts()); | |||||
assertSame(nf, wb.getFontAt(nfIdx)); | |||||
nf.setBold(true); | |||||
nf.setColor((short)123); | |||||
nf.setFontHeightInPoints((short)22); | |||||
nf.setFontName("Thingy"); | |||||
nf.setItalic(false); | |||||
nf.setStrikeout(true); | |||||
nf.setTypeOffset((short)2); | |||||
nf.setUnderline((byte)2); | |||||
assertEquals(num0 + 1, wb.getNumberOfFonts()); | |||||
assertEquals(nf, wb.getFontAt(nfIdx)); | |||||
assertEquals(wb.getFontAt(nfIdx), wb.getFontAt(nfIdx)); | |||||
assertNotSame(wb.getFontAt(0), wb.getFontAt(nfIdx)); | |||||
// Find it now | |||||
assertNotNull( | |||||
wb.findFont( | |||||
true, (short)123, (short)(22*20), | |||||
"Thingy", false, true, (short)2, (byte)2 | |||||
) | |||||
); | |||||
assertSame(nf, | |||||
wb.findFont( | |||||
true, (short)123, (short)(22*20), | |||||
"Thingy", false, true, (short)2, (byte)2 | |||||
) | |||||
); | |||||
wb.close(); | |||||
try (Workbook wb = _testDataProvider.createWorkbook()) { | |||||
int num0 = wb.getNumberOfFonts(); | |||||
Sheet s = wb.createSheet(); | |||||
s.createRow(0); | |||||
s.createRow(1); | |||||
s.getRow(0).createCell(0); | |||||
s.getRow(1).createCell(0); | |||||
//default font | |||||
Font f1 = wb.getFontAt(0); | |||||
assertFalse(f1.getBold()); | |||||
// Check that asking for the same font | |||||
// multiple times gives you the same thing. | |||||
// Otherwise, our tests wouldn't work! | |||||
assertSame(wb.getFontAt(0), wb.getFontAt(0)); | |||||
// Look for a new font we have | |||||
// yet to add | |||||
assertNull( | |||||
wb.findFont( | |||||
true, (short) 123, (short) (22 * 20), | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
) | |||||
); | |||||
Font nf = wb.createFont(); | |||||
int nfIdx = nf.getIndex(); | |||||
assertEquals(num0 + 1, wb.getNumberOfFonts()); | |||||
assertSame(nf, wb.getFontAt(nfIdx)); | |||||
nf.setBold(true); | |||||
nf.setColor((short) 123); | |||||
nf.setFontHeightInPoints((short) 22); | |||||
nf.setFontName("Thingy"); | |||||
nf.setItalic(false); | |||||
nf.setStrikeout(true); | |||||
nf.setTypeOffset((short) 2); | |||||
nf.setUnderline((byte) 2); | |||||
assertEquals(num0 + 1, wb.getNumberOfFonts()); | |||||
assertEquals(nf, wb.getFontAt(nfIdx)); | |||||
assertEquals(wb.getFontAt(nfIdx), wb.getFontAt(nfIdx)); | |||||
assertNotSame(wb.getFontAt(0), wb.getFontAt(nfIdx)); | |||||
// Find it now | |||||
assertNotNull( | |||||
wb.findFont( | |||||
true, (short) 123, (short) (22 * 20), | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
) | |||||
); | |||||
assertSame(nf, | |||||
wb.findFont( | |||||
true, (short) 123, (short) (22 * 20), | |||||
"Thingy", false, true, (short) 2, (byte) 2 | |||||
) | |||||
); | |||||
} | |||||
} | } | ||||
} | } |
wb.close(); | wb.close(); | ||||
} | } | ||||
public void baseTestSharedFormulas(String sampleFile) throws IOException { | |||||
Workbook wb = _testDataProvider.openSampleWorkbook(sampleFile); | |||||
Sheet sheet = wb.getSheetAt(0); | |||||
@Test | |||||
public void testSharedFormulas() throws IOException { | |||||
String fileName = "shared_formulas.xls" + (getClass().getName().contains("xssf") ? "x" : ""); | |||||
try (Workbook wb = _testDataProvider.openSampleWorkbook(fileName)) { | |||||
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | |||||
Cell cell; | |||||
Sheet sheet = wb.getSheetAt(0); | |||||
cell = sheet.getRow(1).getCell(0); | |||||
assertEquals("B2", cell.getCellFormula()); | |||||
assertEquals("ProductionOrderConfirmation", evaluator.evaluate(cell).getStringValue()); | |||||
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | |||||
Cell cell; | |||||
cell = sheet.getRow(2).getCell(0); | |||||
assertEquals("B3", cell.getCellFormula()); | |||||
assertEquals("RequiredAcceptanceDate", evaluator.evaluate(cell).getStringValue()); | |||||
cell = sheet.getRow(1).getCell(0); | |||||
assertEquals("B2", cell.getCellFormula()); | |||||
assertEquals("ProductionOrderConfirmation", evaluator.evaluate(cell).getStringValue()); | |||||
cell = sheet.getRow(3).getCell(0); | |||||
assertEquals("B4", cell.getCellFormula()); | |||||
assertEquals("Header", evaluator.evaluate(cell).getStringValue()); | |||||
cell = sheet.getRow(2).getCell(0); | |||||
assertEquals("B3", cell.getCellFormula()); | |||||
assertEquals("RequiredAcceptanceDate", evaluator.evaluate(cell).getStringValue()); | |||||
cell = sheet.getRow(4).getCell(0); | |||||
assertEquals("B5", cell.getCellFormula()); | |||||
assertEquals("UniqueDocumentNumberID", evaluator.evaluate(cell).getStringValue()); | |||||
cell = sheet.getRow(3).getCell(0); | |||||
assertEquals("B4", cell.getCellFormula()); | |||||
assertEquals("Header", evaluator.evaluate(cell).getStringValue()); | |||||
wb.close(); | |||||
cell = sheet.getRow(4).getCell(0); | |||||
assertEquals("B5", cell.getCellFormula()); | |||||
assertEquals("UniqueDocumentNumberID", evaluator.evaluate(cell).getStringValue()); | |||||
} | |||||
} | } | ||||
/** | /** |
_testDataProvider = testDataProvider; | _testDataProvider = testDataProvider; | ||||
} | } | ||||
public void baseTestResize(Picture input, Picture compare, double scaleX, double scaleY) { | |||||
input.resize(scaleX, scaleY); | |||||
ClientAnchor inpCA = input.getClientAnchor(); | |||||
ClientAnchor cmpCA = compare.getClientAnchor(); | |||||
protected abstract Picture getPictureShape(Drawing<?> pat, int picIdx); | |||||
Dimension inpDim = ImageUtils.getDimensionFromAnchor(input); | |||||
Dimension cmpDim = ImageUtils.getDimensionFromAnchor(compare); | |||||
@Test | |||||
public void resize() throws IOException { | |||||
String fileName = "resize_compare.xls" + (getClass().getName().contains("xssf") ? "x" : ""); | |||||
double scaleX = 2; | |||||
double scaleY = 2; | |||||
try (Workbook wb = _testDataProvider.openSampleWorkbook(fileName)) { | |||||
Sheet sh = wb.getSheetAt(0); | |||||
Drawing<?> pat = sh.createDrawingPatriarch(); | |||||
Picture input = getPictureShape(pat, 0); | |||||
Picture compare = getPictureShape(pat, 1); | |||||
input.resize(scaleX, scaleY); | |||||
double emuPX = Units.EMU_PER_PIXEL; | |||||
ClientAnchor inpCA = input.getClientAnchor(); | |||||
ClientAnchor cmpCA = compare.getClientAnchor(); | |||||
assertEquals(inpDim.getHeight(), cmpDim.getHeight(), emuPX*6, "the image height differs"); | |||||
assertEquals(inpDim.getWidth(), cmpDim.getWidth(), emuPX*6, "the image width differs"); | |||||
assertEquals(inpCA.getCol1(), cmpCA.getCol1(), "the starting column differs"); | |||||
assertEquals(inpCA.getDx1(), cmpCA.getDx1(), 1, "the column x-offset differs"); | |||||
assertEquals(inpCA.getDy1(), cmpCA.getDy1(), 1, "the column y-offset differs"); | |||||
assertEquals(inpCA.getCol2(), cmpCA.getCol2(), "the ending columns differs"); | |||||
// can't compare row heights because of variable test heights | |||||
double origDy1 = inpCA.getDy1(); | |||||
double origDx1 = inpCA.getDx1(); | |||||
input.resize(); | |||||
inpDim = ImageUtils.getDimensionFromAnchor(input); | |||||
Dimension inpDim = ImageUtils.getDimensionFromAnchor(input); | |||||
Dimension cmpDim = ImageUtils.getDimensionFromAnchor(compare); | |||||
Dimension imgDim = input.getImageDimension(); | |||||
double emuPX = Units.EMU_PER_PIXEL; | |||||
assertEquals(imgDim.getHeight(), inpDim.getHeight()/emuPX, 1, "the image height differs"); | |||||
assertEquals(imgDim.getWidth(), inpDim.getWidth()/emuPX, 1, "the image width differs"); | |||||
assertEquals(inpDim.getHeight(), cmpDim.getHeight(), emuPX * 6, "the image height differs"); | |||||
assertEquals(inpDim.getWidth(), cmpDim.getWidth(), emuPX * 6, "the image width differs"); | |||||
assertEquals(inpCA.getCol1(), cmpCA.getCol1(), "the starting column differs"); | |||||
assertEquals(inpCA.getDx1(), cmpCA.getDx1(), 1, "the column x-offset differs"); | |||||
assertEquals(inpCA.getDy1(), origDy1, 1, "the column y-offset differs - image has moved"); | |||||
assertEquals(inpCA.getDx1(), origDx1, 1, "the column x-offset differs - image has moved"); | |||||
assertEquals(inpCA.getDy1(), cmpCA.getDy1(), 1, "the column y-offset differs"); | |||||
assertEquals(inpCA.getCol2(), cmpCA.getCol2(), "the ending columns differs"); | |||||
// can't compare row heights because of variable test heights | |||||
input.resize(); | |||||
inpDim = ImageUtils.getDimensionFromAnchor(input); | |||||
Dimension imgDim = input.getImageDimension(); | |||||
assertEquals(imgDim.getHeight(), inpDim.getHeight() / emuPX, 1, "the image height differs"); | |||||
assertEquals(imgDim.getWidth(), inpDim.getWidth() / emuPX, 1, "the image width differs"); | |||||
} | |||||
} | } | ||||
wb2.close(); | wb2.close(); | ||||
} | } | ||||
protected void baseTestRowBounds(int maxRowNum) throws IOException { | |||||
@Test | |||||
public void testRowBounds() throws IOException { | |||||
int maxRowNum = _testDataProvider.getSpreadsheetVersion().getLastRowIndex(); | |||||
try (Workbook workbook = _testDataProvider.createWorkbook()) { | try (Workbook workbook = _testDataProvider.createWorkbook()) { | ||||
Sheet sheet = workbook.createSheet(); | Sheet sheet = workbook.createSheet(); | ||||
//Test low row bound | //Test low row bound | ||||
} | } | ||||
} | } | ||||
protected void baseTestCellBounds(int maxCellNum) throws IOException { | |||||
@Test | |||||
protected void testCellBounds() throws IOException { | |||||
int maxCellNum = _testDataProvider.getSpreadsheetVersion().getLastColumnIndex(); | |||||
try (Workbook wb1 = _testDataProvider.createWorkbook()) { | try (Workbook wb1 = _testDataProvider.createWorkbook()) { | ||||
Sheet sheet = wb1.createSheet(); | Sheet sheet = wb1.createSheet(); | ||||
} | } | ||||
} | } | ||||
public void baseTestGetSetMargin(double[] defaultMargins) throws IOException { | |||||
@Test | |||||
public void testGetSetMargin() throws IOException { | |||||
double[] defaultMargins = (getClass().getName().contains("xssf")) | |||||
? new double[]{0.7, 0.7, 0.75, 0.75, 0.3, 0.3} | |||||
: new double[]{0.75, 0.75, 1.0, 1.0, 0.3, 0.3}; | |||||
double marginLeft = defaultMargins[0]; | double marginLeft = defaultMargins[0]; | ||||
double marginRight = defaultMargins[1]; | double marginRight = defaultMargins[1]; | ||||
double marginTop = defaultMargins[2]; | double marginTop = defaultMargins[2]; |
} | } | ||||
} | } | ||||
protected void changeSheetNameWithSharedFormulas(String sampleFile) throws IOException { | |||||
@Test | |||||
public void changeSheetNameWithSharedFormulas() throws IOException { | |||||
String sampleFile = "shared_formulas.xls" + (getClass().getName().contains("xssf") ? "x" : ""); | |||||
try (Workbook wb = _testDataProvider.openSampleWorkbook(sampleFile)) { | try (Workbook wb = _testDataProvider.openSampleWorkbook(sampleFile)) { | ||||
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); |