Polish contribution

This commit is contained in:
Mahmoud Ben Hassine
2020-09-21 13:14:39 +02:00
parent 52ce019e60
commit 51a1ecddad
3 changed files with 34 additions and 19 deletions

View File

@@ -73,6 +73,10 @@ public class JsonItemReader<T> extends AbstractItemCountingItemStreamItemReader<
this.jsonObjectReader = jsonObjectReader;
setExecutionContextName(ClassUtils.getShortName(JsonItemReader.class));
}
/**
* Create a new {@link JsonItemReader} instance.
*/
public JsonItemReader(){
setExecutionContextName(ClassUtils.getShortName(JsonItemReader.class));
}
@@ -108,6 +112,8 @@ public class JsonItemReader<T> extends AbstractItemCountingItemStreamItemReader<
@Override
protected void doOpen() throws Exception {
Assert.notNull(this.resource, "The resource must not be null.");
Assert.notNull(this.jsonObjectReader, "The json object reader must not be null.");
if (!this.resource.exists()) {
if (this.strict) {
throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2018-2020 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,6 +16,9 @@
package org.springframework.batch.item.json.builder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.json.JsonObjectReader;
import org.springframework.core.io.Resource;
@@ -32,6 +35,8 @@ import org.springframework.util.StringUtils;
*/
public class JsonItemReaderBuilder<T> {
protected Log logger = LogFactory.getLog(getClass());
private JsonObjectReader<T> jsonObjectReader;
private Resource resource;
@@ -144,6 +149,11 @@ public class JsonItemReaderBuilder<T> {
Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true.");
}
if (this.resource == null) {
logger.debug("The resource is null. This is only a valid scenario when " +
"injecting it later as in when using the MultiResourceItemReader");
}
JsonItemReader<T> reader = new JsonItemReader<>();
reader.setResource(this.resource);
reader.setJsonObjectReader(this.jsonObjectReader);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2018-2020 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.
@@ -60,7 +60,6 @@ public class JsonItemReaderBuilderTest {
try {
new JsonItemReaderBuilder<String>()
.jsonObjectReader(this.jsonObjectReader)
.resource(this.resource)
.build();
fail("A name is required when saveState is set to true.");
}
@@ -91,24 +90,24 @@ public class JsonItemReaderBuilderTest {
Object executionContext = getField(itemReader, "executionContextUserSupport");
Assert.assertEquals("jsonItemReader", getField(executionContext, "name"));
}
@Test
public void shouldBuildJsonItemReaderWhenResourceIsNotProvided(){
JsonItemReader<String> itemReader = new JsonItemReaderBuilder<String>()
.jsonObjectReader(this.jsonObjectReader)
.saveState(true)
.strict(true)
.name("jsonItemReader")
.maxItemCount(100)
.currentItemCount(50)
.build();
Assert.assertEquals(this.jsonObjectReader, getField(itemReader, "jsonObjectReader"));
Assert.assertEquals(100, getField(itemReader, "maxItemCount"));
Assert.assertEquals(50, getField(itemReader, "currentItemCount"));
Assert.assertTrue((Boolean) getField(itemReader, "saveState"));
Assert.assertTrue((Boolean) getField(itemReader, "strict"));
Object executionContext = getField(itemReader, "executionContextUserSupport");
Assert.assertEquals("jsonItemReader", getField(executionContext, "name"));
JsonItemReader<String> itemReader = new JsonItemReaderBuilder<String>()
.jsonObjectReader(this.jsonObjectReader)
.saveState(true)
.strict(true)
.name("jsonItemReader")
.maxItemCount(100)
.currentItemCount(50)
.build();
Assert.assertEquals(this.jsonObjectReader, getField(itemReader, "jsonObjectReader"));
Assert.assertEquals(100, getField(itemReader, "maxItemCount"));
Assert.assertEquals(50, getField(itemReader, "currentItemCount"));
Assert.assertTrue((Boolean) getField(itemReader, "saveState"));
Assert.assertTrue((Boolean) getField(itemReader, "strict"));
Object executionContext = getField(itemReader, "executionContextUserSupport");
Assert.assertEquals("jsonItemReader", getField(executionContext, "name"));
}
}