Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility features #174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.monitorjbl.xlsx.exceptions;

import com.monitorjbl.xlsx.StreamingReader;

/**
* Will be thrown by {@link StreamingReader.Builder#read(java.io.File)}, if a sheet is not found.
*
* @deprecated The POI API {@link org.apache.poi.ss.usermodel.Workbook#getSheet} is designed to return {@code null},
* if a sheet is not found. This class will be removed in a future release.
*/
@Deprecated
public class MissingSheetException extends RuntimeException {

public MissingSheetException() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbook.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.monitorjbl.xlsx.impl;

import com.monitorjbl.xlsx.exceptions.MissingSheetException;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.CellStyle;
Expand Down Expand Up @@ -28,7 +27,7 @@ public StreamingWorkbook(StreamingWorkbookReader reader) {

int findSheetByName(String name) {
for(int i = 0; i < reader.getSheetProperties().size(); i++) {
if(reader.getSheetProperties().get(i).get("name").equals(name)) {
if(reader.getSheetProperties().get(i).get("name").equalsIgnoreCase(name)) {
return i;
}
}
Expand Down Expand Up @@ -104,7 +103,8 @@ public Sheet getSheetAt(int index) {
public Sheet getSheet(String name) {
int index = getSheetIndex(name);
if(index == -1) {
throw new MissingSheetException("Sheet '" + name + "' does not exist");
// Must return null, if sheet is not found. See {@link org.apache.poi.ss.usermodel.Workbook#getSheet}
return null;
}
return reader.getSheets().get(index);
}
Expand Down
57 changes: 33 additions & 24 deletions src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package com.monitorjbl.xlsx;

import com.monitorjbl.xlsx.exceptions.MissingSheetException;
import static org.apache.poi.ss.usermodel.CellType.BOOLEAN;
import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
import static org.apache.poi.ss.usermodel.CellType.STRING;
import static org.apache.poi.ss.usermodel.Row.MissingCellPolicy.CREATE_NULL_AS_BLANK;
import static org.apache.poi.ss.usermodel.Row.MissingCellPolicy.RETURN_BLANK_AS_NULL;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.ss.usermodel.Cell;
Expand All @@ -26,22 +40,6 @@
import java.util.Locale;
import java.util.Map;

import static org.apache.poi.ss.usermodel.CellType.BOOLEAN;
import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
import static org.apache.poi.ss.usermodel.CellType.STRING;
import static org.apache.poi.ss.usermodel.Row.MissingCellPolicy.CREATE_NULL_AS_BLANK;
import static org.apache.poi.ss.usermodel.Row.MissingCellPolicy.RETURN_BLANK_AS_NULL;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

public class StreamingReaderTest {
@BeforeClass
public static void init() {
Expand Down Expand Up @@ -384,25 +382,36 @@ public void testSheetName_alpha() throws Exception {
}
}

@Test(expected = MissingSheetException.class)
@Test
public void testSheetName_ignoreCase() throws Exception {
try (
InputStream is = new FileInputStream(new File("src/test/resources/sheets.xlsx"));
Workbook wb = StreamingReader.builder().open(is)
)
{
Sheet sheet = wb.getSheet("sheetalpha");
assertThat(sheet, is(notNullValue()));
}
}

@Test
public void testSheetName_missingInStream() throws Exception {
try(
InputStream is = new FileInputStream(new File("src/test/resources/sheets.xlsx"));
Workbook wb = StreamingReader.builder().open(is);
Workbook wb = StreamingReader.builder().open(is)
) {
wb.getSheet("asdfasdfasdf");
fail("Should have failed");
Sheet sheet = wb.getSheet("asdfasdfasdf");
assertThat(sheet, is(nullValue()));
}
}

@Test
public void testSheetName_missingInFile() throws Exception {
File f = new File("src/test/resources/sheets.xlsx");
try(Workbook wb = StreamingReader.builder().open(f)) {
wb.getSheet("asdfasdfasdf");
fail("Should have failed");
} catch(MissingSheetException e) {
assertTrue(f.exists());
Sheet sheet = wb.getSheet("asdfasdfasdf");
assertThat(sheet, is(nullValue()));
}
}

Expand Down