Replace anonymous types with lambda expressions or method references

This commit is contained in:
Mahmoud Ben Hassine
2023-07-04 18:49:31 +02:00
parent 07154909a1
commit 96f8d6bd35
34 changed files with 405 additions and 695 deletions

View File

@@ -61,9 +61,9 @@ class InlineDataSourceDefinitionTests {
@Bean
public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new JobBuilder("job", jobRepository)
.start(new StepBuilder("step", jobRepository).tasklet((contribution, chunkContext) -> {
return RepeatStatus.FINISHED;
}, transactionManager).build())
.start(new StepBuilder("step", jobRepository)
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED, transactionManager)
.build())
.build();
}

View File

@@ -122,9 +122,7 @@ class DefaultBatchConfigurationTests {
@Bean
public Step myStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
Tasklet myTasklet = (contribution, chunkContext) -> {
return RepeatStatus.FINISHED;
};
Tasklet myTasklet = (contribution, chunkContext) -> RepeatStatus.FINISHED;
return new StepBuilder("myStep", jobRepository).tasklet(myTasklet, transactionManager).build();
}

View File

@@ -230,7 +230,7 @@ class MulticasterBatchListenerTests {
@Test
void testBeforeReadFails() {
error = true;
Exception exception = assertThrows(StepListenerFailedException.class, () -> multicast.beforeRead());
Exception exception = assertThrows(StepListenerFailedException.class, multicast::beforeRead);
String message = exception.getCause().getMessage();
assertEquals("listener error", message, "Wrong message: " + message);
assertEquals(1, count);
@@ -457,7 +457,7 @@ class MulticasterBatchListenerTests {
StepListener listener = StepListenerFactoryBean.getListener(new AnnotationBasedStepListener());
multicast.register(listener);
Exception exception = assertThrows(StepListenerFailedException.class, () -> multicast.beforeRead());
Exception exception = assertThrows(StepListenerFailedException.class, multicast::beforeRead);
Throwable cause = exception.getCause();
String message = cause.getMessage();
assertTrue(cause instanceof IllegalStateException);

View File

@@ -41,7 +41,6 @@ import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.core.step.AbstractStep;
import org.springframework.batch.core.step.factory.FaultTolerantStepFactoryBean;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
@@ -90,12 +89,7 @@ class FaultTolerantStepFactoryBeanRetryTests {
JobExecution jobExecution;
private ItemWriter<String> writer = new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> data) throws Exception {
processed.addAll(data.getItems());
}
};
private ItemWriter<String> writer = data -> processed.addAll(data.getItems());
@SuppressWarnings("unchecked")
@BeforeEach
@@ -153,16 +147,13 @@ class FaultTolerantStepFactoryBeanRetryTests {
factory.setJobRepository(repository);
factory.setTransactionManager(new ResourcelessTransactionManager());
ItemWriter<Integer> failingWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends Integer> data) throws Exception {
int count = 0;
for (Integer item : data) {
if (count++ == 2) {
throw new Exception("Planned failure in writer");
}
written.add(item);
ItemWriter<Integer> failingWriter = data -> {
int count = 0;
for (Integer item : data) {
if (count++ == 2) {
throw new Exception("Planned failure in writer");
}
written.add(item);
}
};
@@ -203,16 +194,13 @@ class FaultTolerantStepFactoryBeanRetryTests {
void testProcessAllItemsWhenErrorInWriter() throws Exception {
final int RETRY_LIMIT = 3;
final List<String> ITEM_LIST = Arrays.asList("a", "b", "c");
ItemWriter<String> failingWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> data) throws Exception {
int count = 0;
for (String item : data) {
if (count++ == 2) {
throw new Exception("Planned failure in writer");
}
written.add(item);
ItemWriter<String> failingWriter = data -> {
int count = 0;
for (String item : data) {
if (count++ == 2) {
throw new Exception("Planned failure in writer");
}
written.add(item);
}
};
@@ -249,16 +237,13 @@ class FaultTolerantStepFactoryBeanRetryTests {
@Test
void testNoItemsReprocessedWhenErrorInWriterAndProcessorNotTransactional() throws Exception {
ItemWriter<String> failingWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> data) throws Exception {
int count = 0;
for (String item : data) {
if (count++ == 2) {
throw new Exception("Planned failure in writer");
}
written.add(item);
ItemWriter<String> failingWriter = data -> {
int count = 0;
for (String item : data) {
if (count++ == 2) {
throw new Exception("Planned failure in writer");
}
written.add(item);
}
};
@@ -359,14 +344,11 @@ class FaultTolerantStepFactoryBeanRetryTests {
reader.setName("foo");
factory.setItemReader(reader);
factory.setStreams(new ItemStream[] { reader });
factory.setItemWriter(new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> chunk) throws Exception {
if (fail && chunk.getItems().contains("e")) {
throw new RuntimeException("Planned failure");
}
processed.addAll(chunk.getItems());
factory.setItemWriter(chunk -> {
if (fail && chunk.getItems().contains("e")) {
throw new RuntimeException("Planned failure");
}
processed.addAll(chunk.getItems());
});
factory.setRetryLimit(0);
Step step = factory.getObject();
@@ -446,15 +428,12 @@ class FaultTolerantStepFactoryBeanRetryTests {
}
};
ItemWriter<String> itemWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> chunk) throws Exception {
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
if (chunk.getItems().contains("b") || chunk.getItems().contains("d")) {
throw new RuntimeException("Write error - planned but recoverable.");
}
ItemWriter<String> itemWriter = chunk -> {
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
if (chunk.getItems().contains("b") || chunk.getItems().contains("d")) {
throw new RuntimeException("Write error - planned but recoverable.");
}
};
factory.setItemReader(provider);
@@ -504,15 +483,12 @@ class FaultTolerantStepFactoryBeanRetryTests {
}
};
ItemWriter<String> itemWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> chunk) throws Exception {
logger.debug("Write Called! Item: [" + chunk + "]");
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
if (chunk.getItems().contains("b") || chunk.getItems().contains("d")) {
throw new RuntimeException("Write error - planned but recoverable.");
}
ItemWriter<String> itemWriter = chunk -> {
logger.debug("Write Called! Item: [" + chunk + "]");
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
if (chunk.getItems().contains("b") || chunk.getItems().contains("d")) {
throw new RuntimeException("Write error - planned but recoverable.");
}
};
factory.setItemReader(provider);
@@ -556,14 +532,11 @@ class FaultTolerantStepFactoryBeanRetryTests {
return item;
}
};
ItemWriter<String> itemWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> chunk) throws Exception {
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but retryable.");
}
ItemWriter<String> itemWriter = chunk -> {
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but retryable.");
};
factory.setItemReader(provider);
factory.setItemWriter(itemWriter);
@@ -608,14 +581,11 @@ class FaultTolerantStepFactoryBeanRetryTests {
return item;
}
};
ItemWriter<String> itemWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> chunk) throws Exception {
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but not skippable.");
}
ItemWriter<String> itemWriter = chunk -> {
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but not skippable.");
};
factory.setItemReader(provider);
factory.setItemWriter(itemWriter);
@@ -655,14 +625,11 @@ class FaultTolerantStepFactoryBeanRetryTests {
return item;
}
};
ItemWriter<String> itemWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> chunk) throws Exception {
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but retryable.");
}
ItemWriter<String> itemWriter = chunk -> {
processed.addAll(chunk.getItems());
written.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but retryable.");
};
factory.setItemReader(provider);
factory.setItemWriter(itemWriter);
@@ -707,13 +674,10 @@ class FaultTolerantStepFactoryBeanRetryTests {
return item;
}
};
ItemWriter<String> itemWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> chunk) throws Exception {
processed.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but retryable.");
}
ItemWriter<String> itemWriter = chunk -> {
processed.addAll(chunk.getItems());
logger.debug("Write Called! Item: [" + chunk.getItems() + "]");
throw new RuntimeException("Write error - planned but retryable.");
};
factory.setItemReader(provider);
factory.setItemWriter(itemWriter);

View File

@@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.aopalliance.intercept.Joinpoint;
import org.aopalliance.intercept.MethodInterceptor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -975,7 +976,7 @@ public class FaultTolerantStepFactoryBeanTests {
ProxyFactory proxy = new ProxyFactory();
proxy.setTarget(reader);
proxy.setInterfaces(new Class<?>[] { ItemReader.class, ItemStream.class });
proxy.addAdvice((MethodInterceptor) invocation -> invocation.proceed());
proxy.addAdvice((MethodInterceptor) Joinpoint::proceed);
Object advised = proxy.getProxy();
factory.setItemReader((ItemReader<? extends String>) advised);

View File

@@ -46,7 +46,7 @@ class DefaultJobParametersExtractorJobParametersTests {
@BeforeEach
void setUp() {
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(String.class, LocalDate.class, source -> LocalDate.parse(source));
conversionService.addConverter(String.class, LocalDate.class, LocalDate::parse);
this.jobParametersConverter.setConversionService(conversionService);
this.extractor.setJobParametersConverter(this.jobParametersConverter);
}

View File

@@ -33,7 +33,6 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.JobRepositorySupport;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemStreamSupport;
@@ -58,15 +57,12 @@ class AsyncTaskletStepTests {
private int throttleLimit = 20;
ItemWriter<String> itemWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends String> data) throws Exception {
// Thread.sleep(100L);
logger.info("Items: " + data);
processed.addAll(data.getItems());
if (data.getItems().contains("fail")) {
throw new RuntimeException("Planned");
}
ItemWriter<String> itemWriter = data -> {
// Thread.sleep(100L);
logger.info("Items: " + data);
processed.addAll(data.getItems());
if (data.getItems().contains("fail")) {
throw new RuntimeException("Planned");
}
};

View File

@@ -156,26 +156,23 @@ class JdbcJobRepositoryTests {
}
private JobExecution doConcurrentStart() throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
try {
JobExecution execution = repository.createJobExecution(job.getName(), new JobParameters());
try {
JobExecution execution = repository.createJobExecution(job.getName(), new JobParameters());
// simulate running execution
execution.setStartTime(LocalDateTime.now());
repository.update(execution);
cacheJobIds(execution);
list.add(execution);
Thread.sleep(1000);
}
catch (Exception e) {
list.add(e);
}
// simulate running execution
execution.setStartTime(LocalDateTime.now());
repository.update(execution);
cacheJobIds(execution);
list.add(execution);
Thread.sleep(1000);
}
catch (Exception e) {
list.add(e);
}
}).start();
Thread.sleep(400);

View File

@@ -47,7 +47,6 @@ import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.io.ClassPathResource;
@@ -174,23 +173,15 @@ class MySQLJdbcJobRepositoryIntegrationTests {
public ConfigurableConversionService conversionService() {
DefaultConversionService conversionService = new DefaultConversionService();
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
conversionService.addConverter(String.class, Date.class, new Converter<>() {
@Override
public Date convert(String source) {
try {
return dateFormat.parse(source);
}
catch (ParseException e) {
throw new RuntimeException(e);
}
}
});
conversionService.addConverter(Date.class, String.class, new Converter<>() {
@Override
public String convert(Date source) {
return dateFormat.format(source);
conversionService.addConverter(String.class, Date.class, source -> {
try {
return dateFormat.parse(source);
}
catch (ParseException e) {
throw new RuntimeException(e);
}
});
conversionService.addConverter(Date.class, String.class, dateFormat::format);
return conversionService;
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.batch.core.test.step;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -44,7 +42,6 @@ import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -213,12 +210,8 @@ class FaultTolerantStepFactoryBeanIntegrationTests {
}
public List<String> getCommitted() {
return jdbcTemplate.query("SELECT MESSAGE from ERROR_LOG where STEP_NAME='written'", new RowMapper<>() {
@Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString(1);
}
});
return jdbcTemplate.query("SELECT MESSAGE from ERROR_LOG where STEP_NAME='written'",
(rs, rowNum) -> rs.getString(1));
}
public void clear() {
@@ -259,12 +252,8 @@ class FaultTolerantStepFactoryBeanIntegrationTests {
}
public List<String> getCommitted() {
return jdbcTemplate.query("SELECT MESSAGE from ERROR_LOG where STEP_NAME='processed'", new RowMapper<>() {
@Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString(1);
}
});
return jdbcTemplate.query("SELECT MESSAGE from ERROR_LOG where STEP_NAME='processed'",
(rs, rowNum) -> rs.getString(1));
}
public void clear() {

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.batch.core.test.step;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -47,7 +45,6 @@ import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -239,12 +236,8 @@ class FaultTolerantStepFactoryBeanRollbackIntegrationTests {
}
public List<String> getCommitted() {
return jdbcTemplate.query("SELECT MESSAGE from ERROR_LOG where STEP_NAME='written'", new RowMapper<>() {
@Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString(1);
}
});
return jdbcTemplate.query("SELECT MESSAGE from ERROR_LOG where STEP_NAME='written'",
(rs, rowNum) -> rs.getString(1));
}
public void clear() {
@@ -292,12 +285,8 @@ class FaultTolerantStepFactoryBeanRollbackIntegrationTests {
}
public List<String> getCommitted() {
return jdbcTemplate.query("SELECT MESSAGE from ERROR_LOG where STEP_NAME='processed'", new RowMapper<>() {
@Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString(1);
}
});
return jdbcTemplate.query("SELECT MESSAGE from ERROR_LOG where STEP_NAME='processed'",
(rs, rowNum) -> rs.getString(1));
}
public void clear() {

View File

@@ -209,22 +209,16 @@ class FaultTolerantStepIntegrationTests {
// Given
ListItemReader<Integer> itemReader = new ListItemReader<>(Arrays.asList(1, 2, 3));
ItemProcessor<Integer, Integer> itemProcessor = new ItemProcessor<>() {
@Override
public Integer process(Integer item) throws Exception {
if (item.equals(2)) {
throw new Exception("Error during process item " + item);
}
return item;
ItemProcessor<Integer, Integer> itemProcessor = item -> {
if (item.equals(2)) {
throw new Exception("Error during process item " + item);
}
return item;
};
ItemWriter<Integer> itemWriter = new ItemWriter<>() {
@Override
public void write(Chunk<? extends Integer> chunk) throws Exception {
if (chunk.getItems().contains(3)) {
throw new Exception("Error during write");
}
ItemWriter<Integer> itemWriter = chunk -> {
if (chunk.getItems().contains(3)) {
throw new Exception("Error during write");
}
};