Customization of Apache POI helpers
Prior to this commit it was impossible to modify or configure the DataFormatter and FormulaEvaluator in use. With this commit we resolve this by introducing two interfaces. 1 DataFormatterCustomizer to customize/configure the the DataFormatter in use. 1 interface the FormulaEvaluatorFactory which is a factory used to create the FormulaEvaluator. The default DataFormatterCustomizer will configure the formatter to use the cached values from cells instead of evaluating the formulas. The default FormulaEvaluatorFactory in use will always return null instead of creating a FormulaEvaluator. Fixes: #126
This commit is contained in:
@@ -6,9 +6,9 @@ The `PoiItemReader` has the most features but is also the most memory intensive
|
||||
|
||||
To reduce the memory footprint the `StreamingXlsxItemReader` can be used, this will only keep the current row in memory and discard it afterward. Not everything is supported while streaming the XLSX file. It can be that formulas don't get evaluated or lead to an error.
|
||||
|
||||
NOTE: The `ItemReader` classess are **not threadsafe**. The API from https://poi.apache.org/help/faq.html#20[Apache POI] itself isn't threadsafe as well as the https://docs.spring.io/spring-batch/docs/current/api/org/springframework/batch/item/support/AbstractItemCountingItemStreamItemReader.html[`AbstractItemCountingItemStreamItemReader`] used as a base class for the `ItemReader` classes. Reading from multiple threads is therefore not supported. Using a multi-threaded processor/writer should work as long as you use a single thread for reading.
|
||||
WARNING: The `ItemReader` classess are **not threadsafe**. The API from https://poi.apache.org/help/faq.html#20[Apache POI] itself isn't threadsafe as well as the https://docs.spring.io/spring-batch/docs/current/api/org/springframework/batch/item/support/AbstractItemCountingItemStreamItemReader.html[`AbstractItemCountingItemStreamItemReader`] used as a base class for the `ItemReader` classes. Reading from multiple threads is therefore not supported. Using a multi-threaded processor/writer should work as long as you use a single thread for reading.
|
||||
|
||||
*Compatibility:* Spring Batch Excel is compatible with Spring Batch 4.3 and 5.0/5.1.
|
||||
*Compatibility:* Spring Batch Excel is compatible with Spring Batch 5.x.
|
||||
|
||||
== Configuration of `PoiItemReader`
|
||||
|
||||
@@ -34,10 +34,10 @@ Configuration of can be done in XML or Java Config.
|
||||
----
|
||||
@Bean
|
||||
@StepScope
|
||||
public PoiItemReader excelReader() {
|
||||
public PoiItemReader excelReader(RowMapper rowMapper) {
|
||||
PoiItemReader reader = new PoiItemReader();
|
||||
reader.setResource(new FileSystemResource("/path/to/your/excel/file"));
|
||||
reader.setRowMapper(rowMapper());
|
||||
reader.setRowMapper(rowMapper);
|
||||
return reader;
|
||||
}
|
||||
|
||||
@@ -82,7 +82,6 @@ public RowMapper rowMapper() {
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
== Configuration properties
|
||||
[cols="1,1,1,4"]
|
||||
.Properties for item readers
|
||||
@@ -99,8 +98,12 @@ public RowMapper rowMapper() {
|
||||
| `strict` | no | `true` | This controls wether or not an exception is thrown if the file doesn't exists or isn't readable, by default an exception will be thrown.
|
||||
| `datesAsIso` | no | `false` | Controls if dates need to be parsed as ISO or to use the format as specified in the excel sheet.
|
||||
| `userLocale` | no | `null` | Set the `java.util.Locale` to use when formatting dates when there is no explicit format set in the Excel document.
|
||||
| `dataFormatterCustomizer` | no | `DataFormatterCustomizer.DEFAULT` | To additionally configure the https://poi.apache.org/apidocs/dev/org/apache/poi/ss/usermodel/DataFormatter.html[`DataFormatter`] in use to format the data. The default will set the `useCachedValuesForFormulaCells` property to `true` to use cached values instead of evaluating the formulas.
|
||||
| `formulaEvaluatorFactory` | no | `FormulaEvaluatorFactory.NOOP` | A factory approach to create a `FormulaEvaluator` used by Apache POI to evaluate the formulas in the, the default implementation will return `null` as the default is to use the cached values.
|
||||
|===
|
||||
|
||||
== ColumnNameExtractors
|
||||
|
||||
- `StaticColumnNameExtractor` uses a preset list of column names.
|
||||
- `RowNumberColumnNameExtractor` (**the default**) reads a given row (default 0) to determine the column names of the current sheet
|
||||
|
||||
|
||||
@@ -74,6 +74,10 @@ public abstract class AbstractExcelItemReader<T> extends AbstractItemCountingIte
|
||||
|
||||
private DataFormatter dataFormatter;
|
||||
|
||||
private DataFormatterCustomizer dataFormatterCustomizer = DataFormatterCustomizer.DEFAULT;
|
||||
|
||||
private FormulaEvaluatorFactory formulaEvaluatorFactory = FormulaEvaluatorFactory.NOOP;
|
||||
|
||||
public AbstractExcelItemReader() {
|
||||
super();
|
||||
this.setName(ClassUtils.getShortName(this.getClass()));
|
||||
@@ -222,12 +226,15 @@ public abstract class AbstractExcelItemReader<T> extends AbstractItemCountingIte
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
Assert.notNull(this.rowMapper, "RowMapper must be set");
|
||||
Assert.notNull(this.dataFormatterCustomizer, "DataFormatterCustomizer must be set");
|
||||
Assert.notNull(this.formulaEvaluatorFactory, "FormulaEvaluatorFactory must be set");
|
||||
if (this.datesAsIso) {
|
||||
this.dataFormatter = (this.userLocale != null) ? new IsoFormattingDateDataFormatter(this.userLocale) : new IsoFormattingDateDataFormatter();
|
||||
}
|
||||
else {
|
||||
this.dataFormatter = (this.userLocale != null) ? new DataFormatter(this.userLocale) : new DataFormatter();
|
||||
}
|
||||
this.dataFormatterCustomizer.customize(this.dataFormatter);
|
||||
}
|
||||
|
||||
protected DataFormatter getDataFormatter() {
|
||||
@@ -330,4 +337,29 @@ public abstract class AbstractExcelItemReader<T> extends AbstractItemCountingIte
|
||||
public void setUserLocale(Locale userLocale) {
|
||||
this.userLocale = userLocale;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code DataFormatterCustomizer} to use to configure the {@code DataFormatter} used to format/read data.
|
||||
* The default used is the {@code DataFormatterCustomizer.DEFAULT} which will disable formula evaluating and return
|
||||
* the cached value for a cell.
|
||||
* @param dataFormatterCustomizer the {@code DataFormatterCustomizer} never {@code null}.
|
||||
*/
|
||||
public void setDataFormatterCustomizer(DataFormatterCustomizer dataFormatterCustomizer) {
|
||||
this.dataFormatterCustomizer = dataFormatterCustomizer;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code FormulaEvaluatorFactory} to use when a {@code FormulaEvaluator} is needed. The default used will
|
||||
* return {@code null} as the evaluator to use, this as by default the {@code DataFormatter} is configured to use
|
||||
* the cached value anyway.
|
||||
* @param formulaEvaluatorFactory the {@code FormulaEvaluatorFactory} to use, never {@code null}
|
||||
* @see FormulaEvaluatorFactory
|
||||
*/
|
||||
public void setFormulaEvaluatorFactory(FormulaEvaluatorFactory formulaEvaluatorFactory) {
|
||||
this.formulaEvaluatorFactory = formulaEvaluatorFactory;
|
||||
}
|
||||
|
||||
protected FormulaEvaluatorFactory getFormulaEvaluatorFactory() {
|
||||
return this.formulaEvaluatorFactory;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2011-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.extensions.excel;
|
||||
|
||||
import org.apache.poi.ss.usermodel.DataFormatter;
|
||||
|
||||
/**
|
||||
* Callback for customizing a given {@code DataFormatter}. Designed for use with a lambda expression or method reference.
|
||||
* @author Marten Deinum
|
||||
* @since 0.2.0
|
||||
*
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface DataFormatterCustomizer {
|
||||
|
||||
/** Noop {@code DataFormatterCustomizer}. **/
|
||||
DataFormatterCustomizer NOOP = (df) -> { };
|
||||
|
||||
/** The default {@code DataFormatterCustomizer}, setting the use of cached values. **/
|
||||
DataFormatterCustomizer DEFAULT = (df) -> df.setUseCachedValuesForFormulaCells(true);
|
||||
|
||||
void customize(DataFormatter dataFormatter);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2011-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.extensions.excel;
|
||||
|
||||
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Factory interface for creating a {@code FormulaEvaluator}, the default will return {@code null} as to re-use the
|
||||
* cached formula result in the workbooks.
|
||||
*
|
||||
* @author Marten Deinum
|
||||
* @since 0.2.0
|
||||
*
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface FormulaEvaluatorFactory {
|
||||
|
||||
/** Return {@code null} for the {@code FormulaEvaluator}, used by default. **/
|
||||
FormulaEvaluatorFactory NOOP = (wb) -> null;
|
||||
|
||||
/** Delegate the creation of the {@code FormulaEvaluator} to the workbook. **/
|
||||
FormulaEvaluatorFactory SIMPLE = (wb) -> wb.getCreationHelper().createFormulaEvaluator();
|
||||
|
||||
/**
|
||||
* Create the {@code FormulaEvaluator} for the given {@code Workbook}.
|
||||
* @param workbook the workbook
|
||||
* @return the {@code FormulaEvaluator}, can be {@code null}.
|
||||
*/
|
||||
@Nullable
|
||||
FormulaEvaluator create(Workbook workbook);
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public class PoiItemReader<T> extends AbstractExcelItemReader<T> {
|
||||
|
||||
@Override
|
||||
protected Sheet getSheet(final int sheet) {
|
||||
return new PoiSheet(this.workbook.getSheetAt(sheet), getDataFormatter());
|
||||
return new PoiSheet(this.workbook.getSheetAt(sheet), getDataFormatter(), getFormulaEvaluatorFactory());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.apache.poi.ss.usermodel.DataFormatter;
|
||||
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
|
||||
import org.springframework.batch.extensions.excel.FormulaEvaluatorFactory;
|
||||
import org.springframework.batch.extensions.excel.Sheet;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -41,6 +42,7 @@ class PoiSheet implements Sheet {
|
||||
private final org.apache.poi.ss.usermodel.Sheet delegate;
|
||||
private final int numberOfRows;
|
||||
private final String name;
|
||||
private final FormulaEvaluatorFactory formulaEvaluatorFactory;
|
||||
|
||||
private FormulaEvaluator evaluator;
|
||||
|
||||
@@ -48,13 +50,15 @@ class PoiSheet implements Sheet {
|
||||
* Constructor which takes the delegate sheet.
|
||||
* @param delegate the apache POI sheet
|
||||
* @param dataFormatter the {@code DataFormatter} to use.
|
||||
* @param formulaEvaluatorFactory the {@code FormulaEvaluatorFactory} to use.
|
||||
*/
|
||||
PoiSheet(final org.apache.poi.ss.usermodel.Sheet delegate, DataFormatter dataFormatter) {
|
||||
PoiSheet(final org.apache.poi.ss.usermodel.Sheet delegate, DataFormatter dataFormatter, FormulaEvaluatorFactory formulaEvaluatorFactory) {
|
||||
super();
|
||||
this.delegate = delegate;
|
||||
this.numberOfRows = this.delegate.getLastRowNum() + 1;
|
||||
this.name = this.delegate.getSheetName();
|
||||
this.dataFormatter = dataFormatter;
|
||||
this.formulaEvaluatorFactory = formulaEvaluatorFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,17 +112,18 @@ class PoiSheet implements Sheet {
|
||||
* Lazy getter for the {@code FormulaEvaluator}. Takes some time to create an
|
||||
* instance, so if not necessary don't create it.
|
||||
* @return the {@code FormulaEvaluator}
|
||||
* @see FormulaEvaluatorFactory
|
||||
*/
|
||||
private FormulaEvaluator getFormulaEvaluator() {
|
||||
if (this.evaluator == null) {
|
||||
this.evaluator = this.delegate.getWorkbook().getCreationHelper().createFormulaEvaluator();
|
||||
this.evaluator = this.formulaEvaluatorFactory.create(this.delegate.getWorkbook());
|
||||
}
|
||||
return this.evaluator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<String[]> iterator() {
|
||||
return new Iterator<String[]>() {
|
||||
return new Iterator<>() {
|
||||
private final Iterator<Row> delegateIter = PoiSheet.this.delegate.iterator();
|
||||
|
||||
@Override
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.batch.extensions.excel.streaming;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -38,6 +37,7 @@ import org.apache.poi.xssf.usermodel.XSSFComment;
|
||||
import org.xml.sax.Attributes;
|
||||
|
||||
import org.springframework.batch.extensions.excel.Sheet;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.StaxUtils;
|
||||
|
||||
@@ -161,7 +161,7 @@ class StreamingSheet implements Sheet {
|
||||
|
||||
@Override
|
||||
public Iterator<String[]> iterator() {
|
||||
return new Iterator<String[]>() {
|
||||
return new Iterator<>() {
|
||||
|
||||
private String[] currentRow;
|
||||
|
||||
@@ -233,9 +233,10 @@ class StreamingSheet implements Sheet {
|
||||
*/
|
||||
private static final class AttributesAdapter implements Attributes {
|
||||
|
||||
private final Map<String, String> attributes = new HashMap<>();
|
||||
private final Map<String, String> attributes;
|
||||
|
||||
private AttributesAdapter(XMLStreamReader delegate) {
|
||||
this.attributes = CollectionUtils.newHashMap(delegate.getAttributeCount());
|
||||
for (int i = 0; i < delegate.getAttributeCount(); i++) {
|
||||
String name = delegate.getAttributeLocalName(i);
|
||||
String value = delegate.getAttributeValue(i);
|
||||
|
||||
@@ -97,7 +97,10 @@ public class StreamingXlsxItemReader<T> extends AbstractExcelItemReader<T> {
|
||||
|
||||
@Override
|
||||
protected void doClose() throws Exception {
|
||||
this.pkg.revert();
|
||||
|
||||
if (this.pkg != null) {
|
||||
this.pkg.revert();
|
||||
}
|
||||
|
||||
for (StreamingSheet sheet : this.sheets) {
|
||||
sheet.close();
|
||||
|
||||
@@ -17,16 +17,23 @@
|
||||
package org.springframework.batch.extensions.excel;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import org.springframework.batch.extensions.excel.mapping.PassThroughRowMapper;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
@@ -37,35 +44,46 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
* @author Marten Deinum
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public abstract class AbstractExcelItemReaderTests {
|
||||
|
||||
protected static final Consumer<AbstractExcelItemReader<String[]>> NOOP = (reader) -> { };
|
||||
|
||||
private final DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||
|
||||
protected final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
protected AbstractExcelItemReader<String[]> itemReader;
|
||||
private AbstractExcelItemReader<String[]> itemReader;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws Exception {
|
||||
public void setup() {
|
||||
this.itemReader = createExcelItemReader();
|
||||
this.itemReader.setLinesToSkip(1); // First line is column names
|
||||
this.itemReader.setResource(new ClassPathResource("player.xls"));
|
||||
this.itemReader.setRowMapper(new PassThroughRowMapper());
|
||||
this.itemReader.setSkippedRowsCallback((rs) -> this.logger.info("Skipping: " + Arrays.toString(rs.getCurrentRow())));
|
||||
configureItemReader(this.itemReader);
|
||||
this.itemReader.setUserLocale(Locale.US); // Fixed Locale to prevent changes in different environments
|
||||
|
||||
}
|
||||
|
||||
protected void configureAndOpenItemReader(String resource, Consumer<AbstractExcelItemReader<String[]>> configurer) {
|
||||
this.itemReader.setResource(this.resourceLoader.getResource(resource));
|
||||
configurer.accept(this.itemReader);
|
||||
this.itemReader.afterPropertiesSet();
|
||||
|
||||
ExecutionContext executionContext = new ExecutionContext();
|
||||
this.itemReader.open(executionContext);
|
||||
}
|
||||
|
||||
protected void configureItemReader(AbstractExcelItemReader<String[]> itemReader) {
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void after() {
|
||||
this.itemReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readExcelFile() throws Exception {
|
||||
@ParameterizedTest
|
||||
@MethodSource("scenarios")
|
||||
public void readExcelFile(String resource, Consumer<AbstractExcelItemReader<String[]>> configurer) throws Exception {
|
||||
configureAndOpenItemReader(resource, configurer);
|
||||
assertThat(this.itemReader.getNumberOfSheets()).isEqualTo(3);
|
||||
String[] row;
|
||||
do {
|
||||
@@ -82,6 +100,7 @@ public abstract class AbstractExcelItemReaderTests {
|
||||
assertThat(readCount).isEqualTo(4321);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRequiredProperties() {
|
||||
assertThatThrownBy(() -> {
|
||||
@@ -92,4 +111,6 @@ public abstract class AbstractExcelItemReaderTests {
|
||||
|
||||
protected abstract AbstractExcelItemReader<String[]> createExcelItemReader();
|
||||
|
||||
protected abstract Stream<Arguments> scenarios();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,24 +16,27 @@
|
||||
|
||||
package org.springframework.batch.extensions.excel.poi;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReader;
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReaderTests;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Marten Deinum
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class PoiItemReaderXlsxWithBlankLinesTests extends AbstractExcelItemReaderTests {
|
||||
|
||||
@Override
|
||||
protected void configureItemReader(AbstractExcelItemReader<String[]> itemReader) {
|
||||
itemReader.setResource(new ClassPathResource("player_with_blank_lines.xlsx"));
|
||||
}
|
||||
public class PoiItemReaderTests extends AbstractExcelItemReaderTests {
|
||||
|
||||
@Override
|
||||
protected AbstractExcelItemReader<String[]> createExcelItemReader() {
|
||||
return new PoiItemReader<>();
|
||||
}
|
||||
|
||||
public Stream<Arguments> scenarios() {
|
||||
return Stream.of(
|
||||
Arguments.of("classpath:/player.xls", NOOP),
|
||||
Arguments.of("classpath:/player.xlsx", NOOP),
|
||||
Arguments.of("classpath:/player_with_blank_lines.xls", NOOP),
|
||||
Arguments.of("classpath:/player_with_blank_lines.xlsx", NOOP),
|
||||
Arguments.of("classpath:/player_with_password.xls", (Consumer<AbstractExcelItemReader<?>>) (reader) -> reader.setPassword("readme")));
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.extensions.excel.poi;
|
||||
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReader;
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReaderTests;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
/**
|
||||
* @author Marten Deinum
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class PoiItemReaderXlsPasswordTests extends AbstractExcelItemReaderTests {
|
||||
|
||||
@Override
|
||||
protected void configureItemReader(AbstractExcelItemReader<String[]> itemReader) {
|
||||
itemReader.setResource(new ClassPathResource("player_with_password.xls"));
|
||||
itemReader.setPassword("readme");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractExcelItemReader<String[]> createExcelItemReader() {
|
||||
return new PoiItemReader<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.extensions.excel.poi;
|
||||
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReader;
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReaderTests;
|
||||
|
||||
/**
|
||||
* @author Marten Deinum
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class PoiItemReaderXlsTests extends AbstractExcelItemReaderTests {
|
||||
|
||||
@Override
|
||||
protected AbstractExcelItemReader<String[]> createExcelItemReader() {
|
||||
return new PoiItemReader<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.extensions.excel.poi;
|
||||
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReader;
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReaderTests;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Marten Deinum
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class PoiItemReaderXlsWithBlankLinesTests extends AbstractExcelItemReaderTests {
|
||||
|
||||
@Override
|
||||
protected void configureItemReader(AbstractExcelItemReader<String[]> itemReader) {
|
||||
itemReader.setResource(new ClassPathResource("player_with_blank_lines.xls"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractExcelItemReader<String[]> createExcelItemReader() {
|
||||
return new PoiItemReader<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.extensions.excel.poi;
|
||||
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReader;
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReaderTests;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Marten Deinum
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class PoiItemReaderXlsxTests extends AbstractExcelItemReaderTests {
|
||||
|
||||
@Override
|
||||
protected void configureItemReader(AbstractExcelItemReader<String[]> itemReader) {
|
||||
itemReader.setResource(new ClassPathResource("player.xlsx"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractExcelItemReader<String[]> createExcelItemReader() {
|
||||
return new PoiItemReader<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,9 +16,12 @@
|
||||
|
||||
package org.springframework.batch.extensions.excel.streaming;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReader;
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReaderTests;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Marten Deinum
|
||||
@@ -26,14 +29,15 @@ import org.springframework.core.io.ClassPathResource;
|
||||
*/
|
||||
class StreamingXlsxItemReaderTests extends AbstractExcelItemReaderTests {
|
||||
|
||||
@Override
|
||||
protected void configureItemReader(AbstractExcelItemReader<String[]> itemReader) {
|
||||
itemReader.setResource(new ClassPathResource("player.xlsx"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractExcelItemReader<String[]> createExcelItemReader() {
|
||||
return new StreamingXlsxItemReader<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Stream<Arguments> scenarios() {
|
||||
return Stream.of(
|
||||
Arguments.of("classpath:/player.xlsx", NOOP),
|
||||
Arguments.of("classpath:/player_with_blank_lines.xlsx", NOOP));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.extensions.excel.streaming;
|
||||
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReader;
|
||||
import org.springframework.batch.extensions.excel.AbstractExcelItemReaderTests;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Marten Deinum
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class StreamingXlsxItemReaderWithBlankLinesTests extends AbstractExcelItemReaderTests {
|
||||
|
||||
@Override
|
||||
protected void configureItemReader(AbstractExcelItemReader<String[]> itemReader) {
|
||||
itemReader.setResource(new ClassPathResource("player_with_blank_lines.xlsx"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractExcelItemReader<String[]> createExcelItemReader() {
|
||||
return new StreamingXlsxItemReader<>();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user