Use lambdas and method references where appropriate

This commit is contained in:
Mahmoud Ben Hassine
2023-06-12 16:43:25 +02:00
parent c5b4f1a777
commit 93d911e100
115 changed files with 1260 additions and 2500 deletions

View File

@@ -18,8 +18,6 @@ package org.springframework.batch.sample.common;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
@@ -37,7 +35,6 @@ import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -84,12 +81,7 @@ public class StagingItemReader<T>
"SELECT ID FROM BATCH_STAGING WHERE JOB_ID=? AND PROCESSED=? ORDER BY ID",
new RowMapper<>() {
@Override
public Long mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong(1);
}
},
(rs, rowNum) -> rs.getLong(1),
stepExecution.getJobExecution().getJobId(), StagingItemWriter.NEW);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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,8 +16,6 @@
package org.springframework.batch.sample;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -33,7 +31,6 @@ import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.jdbc.JdbcTestUtils;
@@ -90,13 +87,10 @@ class CustomerFilterJobFunctionalTests {
new Customer("customer6", 123456));
activeRow = 0;
jdbcTemplate.query(GET_CUSTOMERS, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
CustomerFilterJobFunctionalTests.Customer customer = customers.get(activeRow++);
assertEquals(customer.getName(), rs.getString(1));
assertEquals(customer.getCredit(), rs.getDouble(2), .01);
}
jdbcTemplate.query(GET_CUSTOMERS, rs -> {
Customer customer = customers.get(activeRow++);
assertEquals(customer.getName(), rs.getString(1));
assertEquals(customer.getCredit(), rs.getDouble(2), .01);
});
Map<String, Object> step1Execution = this.getStepExecution(jobExecution, "uploadCustomer");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
@@ -17,8 +17,6 @@
package org.springframework.batch.sample;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -34,7 +32,6 @@ import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.jdbc.JdbcTestUtils;
@@ -95,29 +92,23 @@ class TradeJobFunctionalTests {
new Trade("UK21341EAH48", 108, new BigDecimal("109.25"), "customer3"),
new Trade("UK21341EAH49", 854, new BigDecimal("123.39"), "customer4"));
jdbcTemplate.query(GET_TRADES, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
Trade trade = trades.get(activeRow++);
jdbcTemplate.query(GET_TRADES, rs -> {
Trade trade = trades.get(activeRow++);
assertEquals(trade.getIsin(), rs.getString(1));
assertEquals(trade.getQuantity(), rs.getLong(2));
assertEquals(trade.getPrice(), rs.getBigDecimal(3));
assertEquals(trade.getCustomer(), rs.getString(4));
}
assertEquals(trade.getIsin(), rs.getString(1));
assertEquals(trade.getQuantity(), rs.getLong(2));
assertEquals(trade.getPrice(), rs.getBigDecimal(3));
assertEquals(trade.getCustomer(), rs.getString(4));
});
assertEquals(activeRow, trades.size());
activeRow = 0;
jdbcTemplate.query(GET_CUSTOMERS, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
Customer customer = customers.get(activeRow++);
jdbcTemplate.query(GET_CUSTOMERS, rs -> {
Customer customer = customers.get(activeRow++);
assertEquals(customer.getName(), rs.getString(1));
assertEquals(customer.getCredit(), rs.getDouble(2), .01);
}
assertEquals(customer.getName(), rs.getString(1));
assertEquals(customer.getCredit(), rs.getDouble(2), .01);
});
assertEquals(customers.size(), activeRow);

View File

@@ -32,7 +32,6 @@ import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
@@ -100,17 +99,14 @@ class StagingItemReaderTests {
void testUpdateProcessIndicatorAfterCommit() {
TransactionTemplate txTemplate = new TransactionTemplate(transactionManager);
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
txTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus transactionStatus) {
try {
testReaderWithProcessorUpdatesProcessIndicator();
}
catch (Exception e) {
fail("Unexpected Exception: " + e);
}
return null;
txTemplate.execute((TransactionCallback<Void>) transactionStatus -> {
try {
testReaderWithProcessorUpdatesProcessIndicator();
}
catch (Exception e) {
fail("Unexpected Exception: " + e);
}
return null;
});
long id = jdbcTemplate.queryForObject("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?", Long.class, jobId);
String before = jdbcTemplate.queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?", String.class, id);
@@ -123,23 +119,20 @@ class StagingItemReaderTests {
TransactionTemplate txTemplate = new TransactionTemplate(transactionManager);
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
final Long idToUse = txTemplate.execute(new TransactionCallback<>() {
@Override
public Long doInTransaction(TransactionStatus transactionStatus) {
final Long idToUse = txTemplate.execute(transactionStatus -> {
long id = jdbcTemplate.queryForObject("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?", Long.class,
jobId);
String before = jdbcTemplate.queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
String.class, id);
assertEquals(StagingItemWriter.NEW, before);
long id = jdbcTemplate.queryForObject("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?", Long.class,
jobId);
String before = jdbcTemplate.queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?", String.class,
id);
assertEquals(StagingItemWriter.NEW, before);
ProcessIndicatorItemWrapper<String> wrapper = reader.read();
assertEquals("FOO", wrapper.getItem());
ProcessIndicatorItemWrapper<String> wrapper = reader.read();
assertEquals("FOO", wrapper.getItem());
transactionStatus.setRollbackOnly();
transactionStatus.setRollbackOnly();
return id;
}
return id;
});
String after = jdbcTemplate.queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?", String.class,

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
@@ -15,9 +15,6 @@
*/
package org.springframework.batch.sample.domain.football.internal;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeEach;
@@ -26,7 +23,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.batch.sample.domain.football.Player;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;
@@ -74,16 +70,13 @@ class JdbcPlayerDaoIntegrationTests {
@Transactional
void testSavePlayer() {
playerDao.savePlayer(player);
jdbcTemplate.query(GET_PLAYER, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
assertEquals(rs.getString("PLAYER_ID"), "AKFJDL00");
assertEquals(rs.getString("LAST_NAME"), "Doe");
assertEquals(rs.getString("FIRST_NAME"), "John");
assertEquals(rs.getString("POS"), "QB");
assertEquals(rs.getInt("YEAR_OF_BIRTH"), 1975);
assertEquals(rs.getInt("YEAR_DRAFTED"), 1998);
}
jdbcTemplate.query(GET_PLAYER, rs -> {
assertEquals(rs.getString("PLAYER_ID"), "AKFJDL00");
assertEquals(rs.getString("LAST_NAME"), "Doe");
assertEquals(rs.getString("FIRST_NAME"), "John");
assertEquals(rs.getString("POS"), "QB");
assertEquals(rs.getInt("YEAR_OF_BIRTH"), 1975);
assertEquals(rs.getInt("YEAR_DRAFTED"), 1998);
});
}

View File

@@ -16,9 +16,7 @@
package org.springframework.batch.sample.domain.multiline;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DefaultFieldSet;
import org.springframework.batch.item.file.transform.FieldSet;
import static org.junit.jupiter.api.Assertions.*;
@@ -57,12 +55,7 @@ class AggregateItemFieldSetMapperTests {
@Test
void testDelegate() throws Exception {
mapper.setDelegate(new FieldSetMapper<>() {
@Override
public String mapFieldSet(FieldSet fs) {
return "foo";
}
});
mapper.setDelegate(fs -> "foo");
assertEquals("foo", mapper.mapFieldSet(new DefaultFieldSet(new String[] { "FOO" })).getItem());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2022 the original author or authors.
* Copyright 2008-2023 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.
@@ -17,7 +17,6 @@ package org.springframework.batch.sample.domain.trade.internal;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.sample.domain.trade.CustomerDebit;
import org.springframework.batch.sample.domain.trade.CustomerDebitDao;
import org.springframework.batch.sample.domain.trade.Trade;
@@ -33,12 +32,9 @@ class CustomerUpdateProcessorTests {
trade.setCustomer("testCustomerName");
trade.setPrice(new BigDecimal("123.0"));
CustomerDebitDao dao = new CustomerDebitDao() {
@Override
public void write(CustomerDebit customerDebit) {
assertEquals("testCustomerName", customerDebit.getName());
assertEquals(new BigDecimal("123.0"), customerDebit.getDebit());
}
CustomerDebitDao dao = customerDebit -> {
assertEquals("testCustomerName", customerDebit.getName());
assertEquals(new BigDecimal("123.0"), customerDebit.getDebit());
};
CustomerUpdateWriter processor = new CustomerUpdateWriter();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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,8 +16,6 @@
package org.springframework.batch.sample.domain.trade.internal;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
@@ -27,7 +25,6 @@ import org.springframework.batch.sample.domain.trade.CustomerDebit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.annotation.Transactional;
@@ -57,11 +54,8 @@ class JdbcCustomerDebitDaoTests {
writer.write(customerDebit);
jdbcTemplate.query("SELECT name, credit FROM CUSTOMER WHERE name = 'testName'", new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
assertEquals(95, rs.getLong("credit"));
}
jdbcTemplate.query("SELECT name, credit FROM CUSTOMER WHERE name = 'testName'", rs -> {
assertEquals(95, rs.getLong("credit"));
});
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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,8 +16,6 @@
package org.springframework.batch.sample.domain.trade.internal;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
@@ -29,7 +27,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.annotation.Transactional;
@@ -69,13 +66,10 @@ class JdbcTradeWriterTests implements InitializingBean {
writer.writeTrade(trade);
jdbcTemplate.query("SELECT * FROM TRADE WHERE ISIN = '5647238492'", new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
assertEquals("testCustomer", rs.getString("CUSTOMER"));
assertEquals(new BigDecimal(Double.toString(99.69)), rs.getBigDecimal("PRICE"));
assertEquals(5, rs.getLong("QUANTITY"));
}
jdbcTemplate.query("SELECT * FROM TRADE WHERE ISIN = '5647238492'", rs -> {
assertEquals("testCustomer", rs.getString("CUSTOMER"));
assertEquals(new BigDecimal(Double.toString(99.69)), rs.getBigDecimal("PRICE"));
assertEquals(5, rs.getLong("QUANTITY"));
});
}

View File

@@ -17,7 +17,6 @@
package org.springframework.batch.sample.iosample;
import java.util.Date;
import java.util.concurrent.Callable;
import org.junit.jupiter.api.Test;
@@ -79,23 +78,20 @@ class TwoJobInstancesDelimitedFunctionalTests {
.toJobParameters();
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(jobParameters);
int count = StepScopeTestUtils.doInStepScope(stepExecution, new Callable<>() {
@Override
public Integer call() throws Exception {
int count = 0;
int count = StepScopeTestUtils.doInStepScope(stepExecution, () -> {
int count1 = 0;
readerStream.open(new ExecutionContext());
readerStream.open(new ExecutionContext());
try {
while (reader.read() != null) {
count++;
}
try {
while (reader.read() != null) {
count1++;
}
finally {
readerStream.close();
}
return count;
}
finally {
readerStream.close();
}
return count1;
});
assertEquals(expected, count);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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,8 +16,6 @@
package org.springframework.batch.sample.jmx;
import org.junit.jupiter.api.Test;
import org.springframework.jmx.export.notification.NotificationPublisher;
import org.springframework.jmx.export.notification.UnableToSendNotificationException;
import javax.management.Notification;
import java.util.ArrayList;
@@ -30,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* @author Dave Syer
* @author Thomas Risberg
* @author Glenn Renfro
* @author Mahmoud Ben Hassine
*
*/
class JobExecutionNotificationPublisherTests {
@@ -40,12 +39,7 @@ class JobExecutionNotificationPublisherTests {
void testRepeatOperationsOpenUsed() {
final List<Notification> list = new ArrayList<>();
publisher.setNotificationPublisher(new NotificationPublisher() {
@Override
public void sendNotification(Notification notification) throws UnableToSendNotificationException {
list.add(notification);
}
});
publisher.setNotificationPublisher(notification -> list.add(notification));
publisher.onApplicationEvent(new SimpleMessageApplicationEvent(this, "foo"));
assertEquals(1, list.size());

View File

@@ -38,6 +38,7 @@ import static org.junit.jupiter.api.Assertions.*;
/**
* @author Dave Syer
* @author Jinwoo Bae
* @author Mahmoud Ben Hassine
*
*/
class RemoteLauncherTests {
@@ -117,16 +118,13 @@ class RemoteLauncherTests {
static void setUp() throws Exception {
System.setProperty("com.sun.management.jmxremote", "");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
JobRegistryBackgroundJobRunner.main("adhoc-job-launcher-context.xml", "jobs/adhocLoopJob.xml");
}
catch (Exception e) {
logger.error(e);
errors.add(e);
}
Thread thread = new Thread(() -> {
try {
JobRegistryBackgroundJobRunner.main("adhoc-job-launcher-context.xml", "jobs/adhocLoopJob.xml");
}
catch (Exception e) {
logger.error(e);
errors.add(e);
}
});

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
@@ -29,11 +29,6 @@ import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersIncrementer;
import org.springframework.batch.core.JobParametersValidator;
import org.springframework.batch.core.configuration.JobLocator;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.lang.Nullable;
import java.io.Serializable;
@@ -47,6 +42,7 @@ import static org.mockito.Mockito.mock;
/**
* @author Dave Syer
* @author Glenn Renfro
* @author Mahmoud Ben Hassine
*
*/
class JobLauncherDetailsTests {
@@ -59,21 +55,14 @@ class JobLauncherDetailsTests {
@BeforeEach
public void setUp() throws Exception {
details.setJobLauncher(new JobLauncher() {
@Override
public JobExecution run(org.springframework.batch.core.Job job, JobParameters jobParameters)
throws JobExecutionAlreadyRunningException, JobRestartException {
list.add(jobParameters);
return null;
}
details.setJobLauncher((job, jobParameters) -> {
list.add(jobParameters);
return null;
});
details.setJobLocator(new JobLocator() {
@Override
public org.springframework.batch.core.Job getJob(@Nullable String name) throws NoSuchJobException {
list.add(name);
return new StubJob("foo");
}
details.setJobLocator(name -> {
list.add(name);
return new StubJob("foo");
});
}