FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
+
+ wb.close();
}
/**
FileOutputStream out = new FileOutputStream("calendar-"+year+".xlsx");
wb.write(out);
out.close();
+
+ wb.close();
}
/**
package org.apache.poi.xssf.usermodel;
+import static org.junit.Assert.*;
+
import java.io.IOException;
import org.apache.poi.ss.usermodel.BaseTestDataFormat;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.junit.Test;
/**
* Tests for {@link XSSFDataFormat}
/**
* [Bug 49928] formatCellValue returns incorrect value for \u00a3 formatted cells
*/
- public void test49928() {
+ @Test
+ public void test49928() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49928.xlsx");
doTest49928Core(wb);
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
*/
- public void test58532() {
+ @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)
*/
+ @Test
public void test58778() throws IOException {
- XSSFWorkbook wb = new XSSFWorkbook();
- Cell cell = wb.createSheet("bug58778").createRow(0).createCell(0);
+ XSSFWorkbook wb1 = new XSSFWorkbook();
+ Cell cell = wb1.createSheet("bug58778").createRow(0).createCell(0);
cell.setCellValue(5.25);
- CellStyle style = wb.createCellStyle();
+ CellStyle style = wb1.createCellStyle();
- XSSFDataFormat dataFormat = wb.createDataFormat();
+ XSSFDataFormat dataFormat = wb1.createDataFormat();
short poundFmtIdx = 6;
dataFormat.putFormat(poundFmtIdx, poundFmt);
cell.setCellStyle(style);
// Cell should appear as "<poundsymbol>5"
- wb = XSSFTestDataSamples.writeOutCloseAndReadBack(wb);
- cell = wb.getSheet("bug58778").getRow(0).getCell(0);
- assertEquals(5.25, cell.getNumericCellValue());
+ 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());
// 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
- wb.close();
+ wb2.close();
+ wb1.close();
}
}
package org.apache.poi.hssf.usermodel;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.ss.usermodel.*;
+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.CellStyle;
+import org.apache.poi.ss.usermodel.DataFormat;
+import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.junit.Test;
/**
* Tests for {@link HSSFDataFormat}
/**
* [Bug 49928] formatCellValue returns incorrect value for \u00a3 formatted cells
*/
- public void test49928(){
+ @Test
+ public void test49928() throws IOException {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49928.xls");
doTest49928Core(wb);
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
*/
- public void test58532() {
+ @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
*/
- public void test51378(){
+ @Test
+ 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);
}
}
}
+ wb.close();
}
}
public class BaseTestSlideShowFactory {
private static final POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
+ @SuppressWarnings("resource")
protected static void testFactoryFromFile(String file) throws Exception {
SlideShow<?,?> ss;
// from file
assertCloseDoesNotModifyFile(file, ss);
}
+ @SuppressWarnings("resource")
protected static void testFactoryFromStream(String file) throws Exception {
SlideShow<?,?> ss;
// from stream
assertCloseDoesNotModifyFile(file, ss);
}
+ @SuppressWarnings("resource")
protected static void testFactoryFromNative(String file) throws Exception {
SlideShow<?,?> ss;
// from NPOIFS
}
}
+ @SuppressWarnings("resource")
protected static void testFactoryFromProtectedFile(String protectedFile, String password) throws Exception {
SlideShow<?,?> ss;
// from protected file
assertCloseDoesNotModifyFile(protectedFile, ss);
}
+ @SuppressWarnings("resource")
protected static void testFactoryFromProtectedStream(String protectedFile, String password) throws Exception {
SlideShow<?,?> ss;
// from protected stream
assertCloseDoesNotModifyFile(protectedFile, ss);
}
+ @SuppressWarnings("resource")
protected static void testFactoryFromProtectedNative(String protectedFile, String password) throws Exception {
SlideShow<?,?> ss;
// Encryption layer is a BIFF8 binary format that can be read by NPOIFSFileSystem,
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.util.Units;
import org.junit.Test;
public final void find() throws IOException {
Workbook book = _testDataProvider.createWorkbook();
Sheet sheet = book.createSheet();
- assertNull(sheet.getCellComment(0, 0));
+ assertNull(sheet.getCellComment(new CellAddress(0, 0)));
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
- assertNull(sheet.getCellComment(0, 0));
+ assertNull(sheet.getCellComment(new CellAddress(0, 0)));
assertNull(cell.getCellComment());
book.close();
}
CreationHelper factory = wb1.getCreationHelper();
Sheet sheet = wb1.createSheet();
- assertNull(sheet.getCellComment(cellRow, cellColumn));
+ assertNull(sheet.getCellComment(new CellAddress(cellRow, cellColumn)));
Cell cell = sheet.createRow(cellRow).createCell(cellColumn);
cell.setCellValue(factory.createRichTextString(cellText));
assertNull(cell.getCellComment());
- assertNull(sheet.getCellComment(cellRow, cellColumn));
+ assertNull(sheet.getCellComment(new CellAddress(cellRow, cellColumn)));
Drawing patr = sheet.createDrawingPatriarch();
ClientAnchor anchor = factory.createClientAnchor();
comment.setAuthor(commentAuthor);
cell.setCellComment(comment);
assertNotNull(cell.getCellComment());
- assertNotNull(sheet.getCellComment(cellRow, cellColumn));
+ assertNotNull(sheet.getCellComment(new CellAddress(cellRow, cellColumn)));
//verify our settings
assertEquals(commentAuthor, comment.getAuthor());
cell = row.getCell(0);
comment = cell.getCellComment();
assertNull("Cells in the first column are not commented", comment);
- assertNull(sheet.getCellComment(rownum, 0));
+ assertNull(sheet.getCellComment(new CellAddress(rownum, 0)));
}
for (int rownum = 0; rownum < 3; rownum++) {
cell = row.getCell(1);
comment = cell.getCellComment();
assertNotNull("Cells in the second column have comments", comment);
- assertNotNull("Cells in the second column have comments", sheet.getCellComment(rownum, 1));
+ assertNotNull("Cells in the second column have comments", sheet.getCellComment(new CellAddress(rownum, 1)));
assertEquals("Yegor Kozlov", comment.getAuthor());
assertFalse("cells in the second column have not empyy notes",
package org.apache.poi.ss.usermodel;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.apache.poi.ss.ITestDataProvider;
+import org.junit.Test;
/**
* Tests of implementation of {@link DataFormat}
*
*/
-public abstract class BaseTestDataFormat extends TestCase {
+public abstract class BaseTestDataFormat {
private final ITestDataProvider _testDataProvider;
protected BaseTestDataFormat(ITestDataProvider testDataProvider) {
_testDataProvider = testDataProvider;
}
-
+
public void assertNotBuiltInFormat(String customFmt) {
//check it is not in built-in formats
assertEquals(-1, BuiltinFormats.getBuiltinFormat(customFmt));
}
- public final void testBuiltinFormats() {
+ @Test
+ public final void testBuiltinFormats() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
DataFormat df = wb.createDataFormat();
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
*/
- public abstract void test49928();
- protected final String poundFmt = "\"\u00a3\"#,##0;[Red]\\-\"\u00a3\"#,##0";
+ @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();
Cell cell = sheet.getRow(0).getCell(0);
CellStyle style = cell.getCellStyle();
- String poundFmt = "\"\u00a3\"#,##0;[Red]\\-\"\u00a3\"#,##0";
- // not expected normally, id of a custom format should be greater
+ // not expected normally, id of a custom format should be greater
// than BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX
short poundFmtIdx = 6;
assertEquals(poundFmtIdx, dataFormat.getFormat(poundFmt));
assertEquals(poundFmt, dataFormat.getFormat(poundFmtIdx));
}
-
+
+ @Test
public void testReadbackFormat() throws IOException {
readbackFormat("built-in format", "0.00");
readbackFormat("overridden built-in format", poundFmt);
-
+
String customFormat = "#0.00 AM/PM";
assertNotBuiltInFormat(customFormat);
readbackFormat("custom format", customFormat);
}
-
+
private void readbackFormat(String msg, String fmt) throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
try {
wb.close();
}
}
-
- public abstract void test58532();
+
+ @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("Wrong formatting of " + value + " for row " + rn,
expWhole, fmt.formatCellValue(r.getCell(1), eval));
assertEquals("Wrong formatting of " + value + " for row " + rn,
exp3dp, fmt.formatCellValue(r.getCell(3), eval));
}
}
-
+
/**
- * Localised accountancy formats
+ * Localized accountancy formats
*/
- public final void test58536() {
+ @Test
+ 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]* \"-\"??_-;_-@_-";
-
+
CellStyle cs = wb.createCellStyle();
cs.setDataFormat(fmt.getFormat(formatUK));
-
+
Cell pve = r.createCell(0);
pve.setCellValue(12345);
pve.setCellStyle(cs);
-
+
Cell nve = r.createCell(1);
nve.setCellValue(-12345);
nve.setCellStyle(cs);
-
+
Cell zero = r.createCell(2);
zero.setCellValue(0);
zero.setCellStyle(cs);
-
- assertEquals(pound+" 12,345", formatter.formatCellValue(pve));
+
+ 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));
+ //assertEquals(pound+" - ", formatter.formatCellValue(zero));
+
+ wb.close();
}
-
+
/**
* Using a single quote (') instead of a comma (,) as
- * a number separator, eg 1000 -> 1'000
+ * a number separator, eg 1000 -> 1'000
*/
- public final void test55265() {
+ @Test
+ 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));
+
+ 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();
}
}
assertEquals(3, sheet.getLastRowNum());
// Verify comments are in the position expected
- assertNotNull(sheet.getCellComment(0,0));
- assertNull(sheet.getCellComment(1,0));
- assertNotNull(sheet.getCellComment(2,0));
- assertNotNull(sheet.getCellComment(3,0));
+ assertNotNull(sheet.getCellComment(new CellAddress(0,0)));
+ assertNull(sheet.getCellComment(new CellAddress(1,0)));
+ assertNotNull(sheet.getCellComment(new CellAddress(2,0)));
+ assertNotNull(sheet.getCellComment(new CellAddress(3,0)));
- String comment1 = sheet.getCellComment(0,0).getString().getString();
+ String comment1 = sheet.getCellComment(new CellAddress(0,0)).getString().getString();
assertEquals(comment1,"comment top row1 (index0)\n");
- String comment3 = sheet.getCellComment(2,0).getString().getString();
+ String comment3 = sheet.getCellComment(new CellAddress(2,0)).getString().getString();
assertEquals(comment3,"comment top row3 (index2)\n");
- String comment4 = sheet.getCellComment(3,0).getString().getString();
+ String comment4 = sheet.getCellComment(new CellAddress(3,0)).getString().getString();
assertEquals(comment4,"comment top row4 (index3)\n");
//Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
// Test that comments were shifted as expected
assertEquals(4, sheet.getLastRowNum());
- assertNotNull(sheet.getCellComment(0,0));
- assertNull(sheet.getCellComment(1,0));
- assertNull(sheet.getCellComment(2,0));
- assertNotNull(sheet.getCellComment(3,0));
- assertNotNull(sheet.getCellComment(4,0));
+ assertNotNull(sheet.getCellComment(new CellAddress(0,0)));
+ assertNull(sheet.getCellComment(new CellAddress(1,0)));
+ assertNull(sheet.getCellComment(new CellAddress(2,0)));
+ assertNotNull(sheet.getCellComment(new CellAddress(3,0)));
+ assertNotNull(sheet.getCellComment(new CellAddress(4,0)));
- String comment1_shifted = sheet.getCellComment(0,0).getString().getString();
+ String comment1_shifted = sheet.getCellComment(new CellAddress(0,0)).getString().getString();
assertEquals(comment1,comment1_shifted);
- String comment3_shifted = sheet.getCellComment(3,0).getString().getString();
+ String comment3_shifted = sheet.getCellComment(new CellAddress(3,0)).getString().getString();
assertEquals(comment3,comment3_shifted);
- String comment4_shifted = sheet.getCellComment(4,0).getString().getString();
+ String comment4_shifted = sheet.getCellComment(new CellAddress(4,0)).getString().getString();
assertEquals(comment4,comment4_shifted);
// Write out and read back in again
assertEquals(4, sheet.getLastRowNum());
// Verify comments are in the position expected after the shift
- assertNotNull(sheet.getCellComment(0,0));
- assertNull(sheet.getCellComment(1,0));
- assertNull(sheet.getCellComment(2,0));
- assertNotNull(sheet.getCellComment(3,0));
- assertNotNull(sheet.getCellComment(4,0));
+ assertNotNull(sheet.getCellComment(new CellAddress(0,0)));
+ assertNull(sheet.getCellComment(new CellAddress(1,0)));
+ assertNull(sheet.getCellComment(new CellAddress(2,0)));
+ assertNotNull(sheet.getCellComment(new CellAddress(3,0)));
+ assertNotNull(sheet.getCellComment(new CellAddress(4,0)));
- comment1_shifted = sheet.getCellComment(0,0).getString().getString();
+ comment1_shifted = sheet.getCellComment(new CellAddress(0,0)).getString().getString();
assertEquals(comment1,comment1_shifted);
- comment3_shifted = sheet.getCellComment(3,0).getString().getString();
+ comment3_shifted = sheet.getCellComment(new CellAddress(3,0)).getString().getString();
assertEquals(comment3,comment3_shifted);
- comment4_shifted = sheet.getCellComment(4,0).getString().getString();
+ comment4_shifted = sheet.getCellComment(new CellAddress(4,0)).getString().getString();
assertEquals(comment4,comment4_shifted);
// Shifting back up again, now two rows
assertEquals(2, sheet.getLastRowNum());
// Verify comments are in the position expected
- assertNull("Had: " + (sheet.getCellComment(0,0) == null ? "null" : sheet.getCellComment(0,0).getString()),
- sheet.getCellComment(0,0));
- assertNotNull(sheet.getCellComment(1,0));
- assertNotNull(sheet.getCellComment(2,0));
+ assertNull("Had: " + (sheet.getCellComment(new CellAddress(0,0)) == null ? "null" : sheet.getCellComment(new CellAddress(0,0)).getString()),
+ sheet.getCellComment(new CellAddress(0,0)));
+ assertNotNull(sheet.getCellComment(new CellAddress(1,0)));
+ assertNotNull(sheet.getCellComment(new CellAddress(2,0)));
}
- comment1 = sheet.getCellComment(1,0).getString().getString();
+ comment1 = sheet.getCellComment(new CellAddress(1,0)).getString().getString();
assertEquals(comment1,"comment top row3 (index2)\n");
- String comment2 = sheet.getCellComment(2,0).getString().getString();
+ String comment2 = sheet.getCellComment(new CellAddress(2,0)).getString().getString();
assertEquals(comment2,"comment top row4 (index3)\n");
wb2.close();
expectedMergedRegions.add(C4_D8);
assertEquals(expectedMergedRegions, sheet.getMergedRegions());
+ wb.close();
}