fix some Eclipse warnings, unnecessary null-check and missing close() in tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1594201 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2014-05-13 11:31:18 +00:00
parent aa3a7ef63d
commit 8a1411bda1
2 changed files with 62 additions and 59 deletions

View File

@ -70,7 +70,8 @@ public class ExtractorFactory {
/** Should this thread prefer event based over usermodel based extractors? */ /** Should this thread prefer event based over usermodel based extractors? */
private static final ThreadLocal<Boolean> threadPreferEventExtractors = new ThreadLocal<Boolean>() { private static final ThreadLocal<Boolean> threadPreferEventExtractors = new ThreadLocal<Boolean>() {
protected Boolean initialValue() { return Boolean.FALSE; } @Override
protected Boolean initialValue() { return Boolean.FALSE; }
}; };
/** Should all threads prefer event based over usermodel based extractors? */ /** Should all threads prefer event based over usermodel based extractors? */
private static Boolean allPreferEventExtractors; private static Boolean allPreferEventExtractors;
@ -168,9 +169,9 @@ public class ExtractorFactory {
if(corePart.getContentType().equals(rel.getContentType())) { if(corePart.getContentType().equals(rel.getContentType())) {
if(getPreferEventExtractor()) { if(getPreferEventExtractor()) {
return new XSSFEventBasedExcelExtractor(pkg); return new XSSFEventBasedExcelExtractor(pkg);
} else {
return new XSSFExcelExtractor(pkg);
} }
return new XSSFExcelExtractor(pkg);
} }
} }
@ -317,10 +318,7 @@ public class ExtractorFactory {
} }
// Create the extractors // Create the extractors
if( if(dirs.size() == 0 && nonPOIFS.size() == 0){
(dirs == null || dirs.size() == 0) &&
(nonPOIFS == null || nonPOIFS.size() == 0)
){
return new POITextExtractor[0]; return new POITextExtractor[0];
} }

View File

@ -133,7 +133,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
b.cloneSheet(0); b.cloneSheet(0);
assertEquals(2, b.getNumberOfSheets()); assertEquals(2, b.getNumberOfSheets());
} }
@Test @Test
public void readWriteWithCharts() { public void readWriteWithCharts() {
HSSFWorkbook b; HSSFWorkbook b;
@ -561,7 +561,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertTrue(clsid1.equals(clsid2)); assertTrue(clsid1.equals(clsid2));
} }
/** /**
* Tests that we can work with both {@link POIFSFileSystem} * Tests that we can work with both {@link POIFSFileSystem}
* and {@link NPOIFSFileSystem} * and {@link NPOIFSFileSystem}
@ -594,7 +594,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
npoifsFileSystem.close(); npoifsFileSystem.close();
} }
} }
@Test @Test
public void wordDocEmbeddedInXls() throws IOException { public void wordDocEmbeddedInXls() throws IOException {
// Open the two filesystems // Open the two filesystems
@ -636,21 +636,26 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
@Test @Test
public void writeWorkbookFromNPOIFS() throws IOException { public void writeWorkbookFromNPOIFS() throws IOException {
InputStream is = HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"); InputStream is = HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls");
NPOIFSFileSystem fs = new NPOIFSFileSystem(is); try {
NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
// Start as NPOIFS try {
HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true); // Start as NPOIFS
assertEquals(3, wb.getNumberOfSheets()); HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true);
assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); assertEquals(3, wb.getNumberOfSheets());
assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
// Will switch to POIFS
wb = HSSFTestDataSamples.writeOutAndReadBack(wb); // Will switch to POIFS
assertEquals(3, wb.getNumberOfSheets()); wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue()); assertEquals(3, wb.getNumberOfSheets());
assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
fs.close(); } finally {
fs.close();
}
} finally {
is.close();
}
} }
@Test @Test
public void cellStylesLimit() { public void cellStylesLimit() {
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
@ -728,7 +733,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertEquals("'first sheet'!D1", cf.getRule(0).getFormula1()); assertEquals("'first sheet'!D1", cf.getRule(0).getFormula1());
assertEquals("'other sheet'!D1", cf.getRule(0).getFormula2()); assertEquals("'other sheet'!D1", cf.getRule(0).getFormula2());
} }
private boolean hasEntry(DirectoryNode dirNode, String entryName) { private boolean hasEntry(DirectoryNode dirNode, String entryName) {
try { try {
dirNode.getEntry(entryName); dirNode.getEntry(entryName);
@ -743,7 +748,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls"); HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls");
InternalWorkbook iwb = wb.getWorkbook(); InternalWorkbook iwb = wb.getWorkbook();
iwb.findDrawingGroup(); iwb.findDrawingGroup();
for(int pictureIndex=1; pictureIndex <= 4; pictureIndex++){ for(int pictureIndex=1; pictureIndex <= 4; pictureIndex++){
EscherBSERecord bse = iwb.getBSERecord(pictureIndex); EscherBSERecord bse = iwb.getBSERecord(pictureIndex);
assertEquals(1, bse.getRef()); assertEquals(1, bse.getRef());
@ -776,7 +781,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertTrue(e.getMessage(), e.getMessage().contains("does not contain a BIFF8")); assertTrue(e.getMessage(), e.getMessage().contains("does not contain a BIFF8"));
} }
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Test @Test
public void selectedSheetShort() { public void selectedSheetShort() {
@ -792,7 +797,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
confirmActiveSelected(sheet4, false); confirmActiveSelected(sheet4, false);
wb.setSelectedTab((short)1); wb.setSelectedTab((short)1);
// see Javadoc, in this case selected means "active" // see Javadoc, in this case selected means "active"
assertEquals(wb.getActiveSheetIndex(), wb.getSelectedTab()); assertEquals(wb.getActiveSheetIndex(), wb.getSelectedTab());
@ -810,7 +815,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
confirmActiveSelected(sheet2, true); confirmActiveSelected(sheet2, true);
confirmActiveSelected(sheet3, false); confirmActiveSelected(sheet3, false);
confirmActiveSelected(sheet4, false); confirmActiveSelected(sheet4, false);
assertEquals(0, wb.getFirstVisibleTab()); assertEquals(0, wb.getFirstVisibleTab());
wb.setDisplayedTab((short)2); wb.setDisplayedTab((short)2);
assertEquals(2, wb.getFirstVisibleTab()); assertEquals(2, wb.getFirstVisibleTab());
@ -829,7 +834,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertTrue(e.getMessage(), e.getMessage().contains("already contains a sheet of this name")); assertTrue(e.getMessage(), e.getMessage().contains("already contains a sheet of this name"));
} }
} }
@Test @Test
public void getSheetIndex() { public void getSheetIndex() {
HSSFWorkbook wb=new HSSFWorkbook(); HSSFWorkbook wb=new HSSFWorkbook();
@ -837,12 +842,12 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
HSSFSheet sheet2 = wb.createSheet("Sheet2"); HSSFSheet sheet2 = wb.createSheet("Sheet2");
HSSFSheet sheet3 = wb.createSheet("Sheet3"); HSSFSheet sheet3 = wb.createSheet("Sheet3");
HSSFSheet sheet4 = wb.createSheet("Sheet4"); HSSFSheet sheet4 = wb.createSheet("Sheet4");
assertEquals(0, wb.getSheetIndex(sheet1)); assertEquals(0, wb.getSheetIndex(sheet1));
assertEquals(1, wb.getSheetIndex(sheet2)); assertEquals(1, wb.getSheetIndex(sheet2));
assertEquals(2, wb.getSheetIndex(sheet3)); assertEquals(2, wb.getSheetIndex(sheet3));
assertEquals(3, wb.getSheetIndex(sheet4)); assertEquals(3, wb.getSheetIndex(sheet4));
// remove sheets // remove sheets
wb.removeSheetAt(0); wb.removeSheetAt(0);
wb.removeSheetAt(2); wb.removeSheetAt(2);
@ -860,52 +865,52 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
HSSFWorkbook wb=new HSSFWorkbook(); HSSFWorkbook wb=new HSSFWorkbook();
wb.createSheet("Sheet1"); wb.createSheet("Sheet1");
wb.createSheet("Sheet2"); wb.createSheet("Sheet2");
assertEquals(0, wb.getExternalSheetIndex(0)); assertEquals(0, wb.getExternalSheetIndex(0));
assertEquals(1, wb.getExternalSheetIndex(1)); assertEquals(1, wb.getExternalSheetIndex(1));
assertEquals("Sheet1", wb.findSheetNameFromExternSheet(0)); assertEquals("Sheet1", wb.findSheetNameFromExternSheet(0));
assertEquals("Sheet2", wb.findSheetNameFromExternSheet(1)); assertEquals("Sheet2", wb.findSheetNameFromExternSheet(1));
//assertEquals(null, wb.findSheetNameFromExternSheet(2)); //assertEquals(null, wb.findSheetNameFromExternSheet(2));
assertEquals(0, wb.getSheetIndexFromExternSheetIndex(0)); assertEquals(0, wb.getSheetIndexFromExternSheetIndex(0));
assertEquals(1, wb.getSheetIndexFromExternSheetIndex(1)); assertEquals(1, wb.getSheetIndexFromExternSheetIndex(1));
assertEquals(-1, wb.getSheetIndexFromExternSheetIndex(2)); assertEquals(-1, wb.getSheetIndexFromExternSheetIndex(2));
assertEquals(-1, wb.getSheetIndexFromExternSheetIndex(100)); assertEquals(-1, wb.getSheetIndexFromExternSheetIndex(100));
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Test @Test
public void sstString() { public void sstString() {
HSSFWorkbook wb=new HSSFWorkbook(); HSSFWorkbook wb=new HSSFWorkbook();
int sst = wb.addSSTString("somestring"); int sst = wb.addSSTString("somestring");
assertEquals("somestring", wb.getSSTString(sst)); assertEquals("somestring", wb.getSSTString(sst));
//assertNull(wb.getSSTString(sst+1)); //assertNull(wb.getSSTString(sst+1));
} }
@Test @Test
public void names() { public void names() {
HSSFWorkbook wb=new HSSFWorkbook(); HSSFWorkbook wb=new HSSFWorkbook();
try { try {
wb.getNameAt(0); wb.getNameAt(0);
fail("Fails without any defined names"); fail("Fails without any defined names");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
assertTrue(e.getMessage(), e.getMessage().contains("no defined names")); assertTrue(e.getMessage(), e.getMessage().contains("no defined names"));
} }
HSSFName name = wb.createName(); HSSFName name = wb.createName();
assertNotNull(name); assertNotNull(name);
assertNull(wb.getName("somename")); assertNull(wb.getName("somename"));
name.setNameName("myname"); name.setNameName("myname");
assertNotNull(wb.getName("myname")); assertNotNull(wb.getName("myname"));
assertEquals(0, wb.getNameIndex(name)); assertEquals(0, wb.getNameIndex(name));
assertEquals(0, wb.getNameIndex("myname")); assertEquals(0, wb.getNameIndex("myname"));
try { try {
wb.getNameAt(5); wb.getNameAt(5);
fail("Fails without any defined names"); fail("Fails without any defined names");
@ -920,7 +925,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertTrue(e.getMessage(), e.getMessage().contains("outside the allowable range")); assertTrue(e.getMessage(), e.getMessage().contains("outside the allowable range"));
} }
} }
@Test @Test
public void testMethods() { public void testMethods() {
HSSFWorkbook wb=new HSSFWorkbook(); HSSFWorkbook wb=new HSSFWorkbook();
@ -928,16 +933,16 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
//wb.dumpDrawingGroupRecords(true); //wb.dumpDrawingGroupRecords(true);
//wb.dumpDrawingGroupRecords(false); //wb.dumpDrawingGroupRecords(false);
} }
@Test @Test
public void writeProtection() { public void writeProtection() {
HSSFWorkbook wb=new HSSFWorkbook(); HSSFWorkbook wb=new HSSFWorkbook();
assertFalse(wb.isWriteProtected()); assertFalse(wb.isWriteProtected());
wb.writeProtectWorkbook("mypassword", "myuser"); wb.writeProtectWorkbook("mypassword", "myuser");
assertTrue(wb.isWriteProtected()); assertTrue(wb.isWriteProtected());
wb.unwriteProtectWorkbook(); wb.unwriteProtectWorkbook();
assertFalse(wb.isWriteProtected()); assertFalse(wb.isWriteProtected());
} }
@ -949,7 +954,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received"); assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received");
HSSFSheet sheet = wb.cloneSheet(0); HSSFSheet sheet = wb.cloneSheet(0);
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received", "Invoice (2)"); assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received", "Invoice (2)");
wb.setSheetName(wb.getSheetIndex(sheet), "copy"); wb.setSheetName(wb.getSheetIndex(sheet), "copy");
@ -961,16 +966,16 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertSheetOrder(wb, "copy", "Invoice", "Invoice1", "Digest", "Deferred", "Received"); assertSheetOrder(wb, "copy", "Invoice", "Invoice1", "Digest", "Deferred", "Received");
wb.removeSheetAt(0); wb.removeSheetAt(0);
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received"); assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received");
// check that the overall workbook serializes with its correct size // check that the overall workbook serializes with its correct size
int expected = wb.getWorkbook().getSize(); int expected = wb.getWorkbook().getSize();
int written = wb.getWorkbook().serialize(0, new byte[expected*2]); int written = wb.getWorkbook().serialize(0, new byte[expected*2]);
assertEquals("Did not have the expected size when writing the workbook: written: " + written + ", but expected: " + expected, assertEquals("Did not have the expected size when writing the workbook: written: " + written + ", but expected: " + expected,
expected, written); expected, written);
HSSFWorkbook read = HSSFTestDataSamples.writeOutAndReadBack(wb); HSSFWorkbook read = HSSFTestDataSamples.writeOutAndReadBack(wb);
assertSheetOrder(read, "Invoice", "Invoice1", "Digest", "Deferred", "Received"); assertSheetOrder(read, "Invoice", "Invoice1", "Digest", "Deferred", "Received");
} }
@ -982,7 +987,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received"); assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received");
HSSFSheet sheet = wb.cloneSheet(0); HSSFSheet sheet = wb.cloneSheet(0);
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received", "Invoice (2)"); assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received", "Invoice (2)");
wb.setSheetName(wb.getSheetIndex(sheet), "copy"); wb.setSheetName(wb.getSheetIndex(sheet), "copy");
@ -994,24 +999,24 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertSheetOrder(wb, "copy", "Invoice", "Invoice1", "Digest", "Deferred", "Received"); assertSheetOrder(wb, "copy", "Invoice", "Invoice1", "Digest", "Deferred", "Received");
wb.removeSheetAt(0); wb.removeSheetAt(0);
assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received"); assertSheetOrder(wb, "Invoice", "Invoice1", "Digest", "Deferred", "Received");
wb.removeSheetAt(1); wb.removeSheetAt(1);
assertSheetOrder(wb, "Invoice", "Digest", "Deferred", "Received"); assertSheetOrder(wb, "Invoice", "Digest", "Deferred", "Received");
wb.setSheetOrder("Digest", 3); wb.setSheetOrder("Digest", 3);
assertSheetOrder(wb, "Invoice", "Deferred", "Received", "Digest"); assertSheetOrder(wb, "Invoice", "Deferred", "Received", "Digest");
// check that the overall workbook serializes with its correct size // check that the overall workbook serializes with its correct size
int expected = wb.getWorkbook().getSize(); int expected = wb.getWorkbook().getSize();
int written = wb.getWorkbook().serialize(0, new byte[expected*2]); int written = wb.getWorkbook().serialize(0, new byte[expected*2]);
assertEquals("Did not have the expected size when writing the workbook: written: " + written + ", but expected: " + expected, assertEquals("Did not have the expected size when writing the workbook: written: " + written + ", but expected: " + expected,
expected, written); expected, written);
HSSFWorkbook read = HSSFTestDataSamples.writeOutAndReadBack(wb); HSSFWorkbook read = HSSFTestDataSamples.writeOutAndReadBack(wb);
assertSheetOrder(read, "Invoice", "Deferred", "Received", "Digest"); assertSheetOrder(read, "Invoice", "Deferred", "Received", "Digest");
} }