Test result read.

Fixed bug in PoiItemReader/PoiSheet which lead to ignoring empty columns.
This commit is contained in:
Marten Deinum
2014-08-28 10:10:25 +02:00
committed by Michael Minella
parent d799fdd561
commit 9f26c52f7a
5 changed files with 25 additions and 7 deletions

View File

@@ -74,6 +74,11 @@
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
</dependencies>
<build>

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.item.excel.poi;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.batch.item.excel.AbstractExcelItemReader;
@@ -49,6 +50,7 @@ public class PoiItemReader<T> extends AbstractExcelItemReader<T> {
@Override
protected void openExcelFile(final Resource resource) throws Exception {
this.workbook = WorkbookFactory.create(resource.getInputStream());
this.workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
}
}

View File

@@ -34,6 +34,8 @@ public class PoiSheet implements Sheet {
private final org.apache.poi.ss.usermodel.Sheet delegate;
private int numberOfColumns = -1;
/**
* Constructor which takes the delegate sheet.
*
@@ -71,7 +73,8 @@ public class PoiSheet implements Sheet {
final Row row = this.delegate.getRow(rowNumber);
final List<String> cells = new LinkedList<String>();
for (Cell cell : row) {
for (int i =0; i< getNumberOfColumns(); i++) {
Cell cell = row.getCell(i);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
cells.add(String.valueOf(cell.getNumericCellValue()));
@@ -107,10 +110,9 @@ public class PoiSheet implements Sheet {
*/
@Override
public int getNumberOfColumns() {
final String[] columns = this.getHeader();
if (columns != null) {
return columns.length;
if (numberOfColumns < 0) {
numberOfColumns = this.delegate.getRow(0).getLastCellNum();
}
return 0;
return numberOfColumns;
}
}

View File

@@ -23,6 +23,7 @@ import org.junit.Test;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.excel.mapping.PassThroughRowMapper;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StringUtils;
import static org.junit.Assert.assertEquals;
@@ -38,6 +39,8 @@ public abstract class AbstractExcelItemReaderTests {
protected AbstractExcelItemReader itemReader;
private ExecutionContext executionContext;
@Before
public void setup() throws Exception {
this.itemReader = createExcelItemReader();
@@ -52,7 +55,8 @@ public abstract class AbstractExcelItemReaderTests {
});
configureItemReader(this.itemReader);
this.itemReader.afterPropertiesSet();
this.itemReader.open(new ExecutionContext());
executionContext = new ExecutionContext();
this.itemReader.open(executionContext);
}
protected void configureItemReader(AbstractExcelItemReader itemReader) {
@@ -69,8 +73,13 @@ public abstract class AbstractExcelItemReaderTests {
String[] row = null;
do {
row = (String[]) this.itemReader.read();
this.logger.debug("Read: "+ StringUtils.arrayToCommaDelimitedString(row));
this.logger.debug("Read: " + StringUtils.arrayToCommaDelimitedString(row));
if (row != null) {
assertEquals(6, row.length);
}
} while (row != null);
int readCount = (Integer) ReflectionTestUtils.getField(this.itemReader, "currentItemCount" );
assertEquals(4320, readCount); // File contains 4321 lines, first is header 4321-1=4320 records read.
}
@Test(expected = IllegalArgumentException.class)