Skip to content

Commit

Permalink
Change Resource to WritableResource in file-based item writers
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine committed Apr 13, 2022
1 parent 0b49e08 commit 1d65031
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 38 deletions.
Expand Up @@ -19,15 +19,15 @@
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamWriter;
import org.springframework.batch.item.ItemWriter;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;

/**
* Interface for {@link ItemWriter}s that implement {@link ItemStream} and write
* output to {@link Resource}.
* output to {@link WritableResource}.
*
* @author Robert Kasanicky
*/
public interface ResourceAwareItemWriterItemStream<T> extends ItemStreamWriter<T> {

void setResource(Resource resource);
void setResource(WritableResource resource);
}
Expand Up @@ -32,6 +32,7 @@
import org.springframework.batch.item.file.transform.FormatterLineAggregator;
import org.springframework.batch.item.file.transform.LineAggregator;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.util.Assert;

/**
Expand All @@ -48,7 +49,7 @@ public class FlatFileItemWriterBuilder<T> {

protected Log logger = LogFactory.getLog(getClass());

private Resource resource;
private WritableResource resource;

private boolean forceSync = false;

Expand Down Expand Up @@ -112,9 +113,9 @@ public FlatFileItemWriterBuilder<T> name(String name) {
*
* @param resource the output of the writer.
* @return The current instance of the builder.
* @see FlatFileItemWriter#setResource(Resource)
* @see FlatFileItemWriter#setResource(WritableResource)
*/
public FlatFileItemWriterBuilder<T> resource(Resource resource) {
public FlatFileItemWriterBuilder<T> resource(WritableResource resource) {
this.resource = resource;

return this;
Expand Down
Expand Up @@ -21,6 +21,7 @@

import org.springframework.batch.item.support.AbstractFileItemWriter;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

Expand Down Expand Up @@ -61,7 +62,7 @@ public class JsonFileItemWriter<T> extends AbstractFileItemWriter<T> {
* @param resource to write json data to
* @param jsonObjectMarshaller used to marshal object into json representation
*/
public JsonFileItemWriter(Resource resource, JsonObjectMarshaller<T> jsonObjectMarshaller) {
public JsonFileItemWriter(WritableResource resource, JsonObjectMarshaller<T> jsonObjectMarshaller) {
Assert.notNull(resource, "resource must not be null");
Assert.notNull(jsonObjectMarshaller, "json object marshaller must not be null");
setResource(resource);
Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.springframework.batch.item.json.JsonFileItemWriter;
import org.springframework.batch.item.json.JsonObjectMarshaller;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.util.Assert;

/**
Expand All @@ -32,7 +33,7 @@
*/
public class JsonFileItemWriterBuilder<T> {

private Resource resource;
private WritableResource resource;
private JsonObjectMarshaller<T> jsonObjectMarshaller;
private FlatFileHeaderCallback headerCallback;
private FlatFileFooterCallback footerCallback;
Expand Down Expand Up @@ -119,13 +120,13 @@ public JsonFileItemWriterBuilder<T> jsonObjectMarshaller(JsonObjectMarshaller<T>
}

/**
* The {@link Resource} to be used as output.
* The {@link WritableResource} to be used as output.
*
* @param resource the output of the writer.
* @return The current instance of the builder.
* @see JsonFileItemWriter#setResource(Resource)
* @see JsonFileItemWriter#setResource(WritableResource)
*/
public JsonFileItemWriterBuilder<T> resource(Resource resource) {
public JsonFileItemWriterBuilder<T> resource(WritableResource resource) {
this.resource = resource;

return this;
Expand Down
Expand Up @@ -42,6 +42,7 @@
import org.springframework.batch.support.transaction.TransactionAwareBufferedWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -81,7 +82,7 @@ public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWr

private static final String RESTART_DATA_NAME = "current.count";

private Resource resource;
private WritableResource resource;

protected OutputState state = null;

Expand Down Expand Up @@ -133,7 +134,7 @@ public void setLineSeparator(String lineSeparator) {
* @param resource the resource to be written to
*/
@Override
public void setResource(Resource resource) {
public void setResource(WritableResource resource) {
this.resource = resource;
}

Expand Down
Expand Up @@ -52,6 +52,7 @@
import org.springframework.batch.support.transaction.TransactionAwareBufferedWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
Expand Down Expand Up @@ -103,7 +104,7 @@ public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T> impl
private static final String WRITE_STATISTICS_NAME = "record.count";

// file system resource
private Resource resource;
private WritableResource resource;

// xml marshaller
private Marshaller marshaller;
Expand Down Expand Up @@ -177,7 +178,7 @@ public StaxEventItemWriter() {
* @param resource the output file
*/
@Override
public void setResource(Resource resource) {
public void setResource(WritableResource resource) {
this.resource = resource;
}

Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.springframework.batch.item.xml.StaxEventItemWriter;
import org.springframework.batch.item.xml.StaxWriterCallback;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.oxm.Marshaller;
import org.springframework.util.Assert;

Expand All @@ -34,7 +35,7 @@
*/
public class StaxEventItemWriterBuilder<T> {

private Resource resource;
private WritableResource resource;

private Marshaller marshaller;

Expand Down Expand Up @@ -80,13 +81,13 @@ public StaxEventItemWriterBuilder<T> name(String name) {
}

/**
* The {@link Resource} to be used as output.
* The {@link WritableResource} to be used as output.
*
* @param resource the output from the writer
* @return the current instance of the builder.
* @see StaxEventItemWriter#setResource(Resource)
* @see StaxEventItemWriter#setResource(WritableResource)
*/
public StaxEventItemWriterBuilder<T> resource(Resource resource) {
public StaxEventItemWriterBuilder<T> resource(WritableResource resource) {
this.resource = resource;

return this;
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
Expand Down Expand Up @@ -838,7 +839,7 @@ public String aggregate(String item) {
* If append=true a new output file should still be created on the first run (not restart).
*/
public void testAppendToNotYetExistingFile() throws Exception {
Resource toBeCreated = new FileSystemResource("target/FlatFileItemWriterTests.out");
WritableResource toBeCreated = new FileSystemResource("target/FlatFileItemWriterTests.out");

outputFile = toBeCreated.getFile(); //enable easy content reading and auto-delete the file

Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.test.util.ReflectionTestUtils;

import static org.junit.Assert.assertEquals;
Expand All @@ -55,7 +56,7 @@ public void testMissingLineAggregator() {

@Test(expected = IllegalStateException.class)
public void testMultipleLineAggregators() throws IOException {
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));

new FlatFileItemWriterBuilder<Foo>()
.name("itemWriter")
Expand All @@ -72,7 +73,7 @@ public void testMultipleLineAggregators() throws IOException {
@Test
public void test() throws Exception {

Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));

FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
Expand All @@ -98,7 +99,7 @@ public void test() throws Exception {
@Test
public void testDelimitedOutputWithDefaultDelimiter() throws Exception {

Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));

FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
Expand All @@ -125,7 +126,7 @@ public void testDelimitedOutputWithDefaultDelimiter() throws Exception {
@Test
public void testDelimitedOutputWithEmptyDelimiter() throws Exception {

Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));

FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
Expand Down Expand Up @@ -153,7 +154,7 @@ public void testDelimitedOutputWithEmptyDelimiter() throws Exception {
@Test
public void testDelimitedOutputWithDefaultFieldExtractor() throws Exception {

Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));

FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
Expand Down Expand Up @@ -181,7 +182,7 @@ public void testDelimitedOutputWithDefaultFieldExtractor() throws Exception {
@Test
public void testDelimitedOutputWithCustomFieldExtractor() throws Exception {

Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));

FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
Expand Down Expand Up @@ -209,7 +210,7 @@ public void testDelimitedOutputWithCustomFieldExtractor() throws Exception {
@Test
public void testFormattedOutputWithDefaultFieldExtractor() throws Exception {

Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));

FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
Expand Down Expand Up @@ -237,7 +238,7 @@ public void testFormattedOutputWithDefaultFieldExtractor() throws Exception {
@Test
public void testFormattedOutputWithCustomFieldExtractor() throws Exception {

Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));

FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
Expand Down Expand Up @@ -265,7 +266,7 @@ public void testFormattedOutputWithCustomFieldExtractor() throws Exception {
@Test
public void testFlags() throws Exception {

Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));

String encoding = Charset.defaultCharset().name();

Expand All @@ -287,7 +288,7 @@ public void testFlags() throws Exception {
@Test
public void testFlagsWithEncoding() throws Exception {

Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
String encoding = "UTF-8";
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
.name("foo")
Expand Down
Expand Up @@ -30,14 +30,15 @@
import org.springframework.batch.item.ExecutionContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;

/**
* @author Mahmoud Ben Hassine
*/
@RunWith(MockitoJUnitRunner.class)
public class JsonFileItemWriterTests {

private Resource resource;
private WritableResource resource;
@Mock
private JsonObjectMarshaller<String> jsonObjectMarshaller;

Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.springframework.batch.item.json.JsonObjectMarshaller;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.test.util.ReflectionTestUtils;

import static org.junit.Assert.assertEquals;
Expand All @@ -41,7 +42,7 @@
*/
public class JsonFileItemWriterBuilderTests {

private Resource resource;
private WritableResource resource;
private JsonObjectMarshaller<String> jsonObjectMarshaller;

@Before
Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.oxm.Marshaller;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
Expand All @@ -53,7 +54,7 @@ public abstract class AbstractStaxEventWriterItemWriterTests {

protected StaxEventItemWriter<Trade> writer = new StaxEventItemWriter<>();

private Resource resource;
private WritableResource resource;

private File outputFile;

Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.transaction.TransactionStatus;
Expand All @@ -55,7 +56,7 @@ public class Jaxb2NamespaceMarshallingTests {

private StaxEventItemWriter<QualifiedTrade> writer = new StaxEventItemWriter<>();

private Resource resource;
private WritableResource resource;

private File outputFile;

Expand Down
Expand Up @@ -35,6 +35,7 @@
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
Expand Down Expand Up @@ -65,7 +66,7 @@ public class StaxEventItemWriterTests {
private StaxEventItemWriter<Object> writer;

// output file
private Resource resource;
private WritableResource resource;

private ExecutionContext executionContext;

Expand Down Expand Up @@ -479,7 +480,7 @@ public void write(XMLEventWriter writer) throws IOException {

@Test
public void testNonExistantResource() throws Exception {
Resource doesntExist = mock(Resource.class);
WritableResource doesntExist = mock(WritableResource.class);
when(doesntExist.getFile()).thenReturn(File.createTempFile("arbitrary", null));
when(doesntExist.exists()).thenReturn(false);

Expand Down

0 comments on commit 1d65031

Please sign in to comment.