Use lambdas and method references where appropriate
This commit is contained in:
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -28,7 +26,6 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.item.Chunk;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.jdbc.core.PreparedStatementCallback;
|
||||
@@ -192,16 +189,12 @@ public class JdbcBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
|
||||
}
|
||||
else {
|
||||
updateCounts = namedParameterJdbcTemplate.getJdbcOperations()
|
||||
.execute(sql, new PreparedStatementCallback<>() {
|
||||
@Override
|
||||
public int[] doInPreparedStatement(PreparedStatement ps)
|
||||
throws SQLException, DataAccessException {
|
||||
for (T item : chunk) {
|
||||
itemPreparedStatementSetter.setValues(item, ps);
|
||||
ps.addBatch();
|
||||
}
|
||||
return ps.executeBatch();
|
||||
.execute(sql, (PreparedStatementCallback<int[]>) ps -> {
|
||||
for (T item : chunk) {
|
||||
itemPreparedStatementSetter.setValues(item, ps);
|
||||
ps.addBatch();
|
||||
}
|
||||
return ps.executeBatch();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
import java.beans.PropertyEditorSupport;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* Property editor implementation which parses string and creates array of ranges. Ranges
|
||||
@@ -120,12 +119,7 @@ public class RangeArrayPropertyEditor extends PropertyEditorSupport {
|
||||
}
|
||||
|
||||
// sort array of Ranges
|
||||
Arrays.sort(c, new Comparator<>() {
|
||||
@Override
|
||||
public int compare(Integer r1, Integer r2) {
|
||||
return ranges[r1].getMin() - ranges[r2].getMin();
|
||||
}
|
||||
});
|
||||
Arrays.sort(c, (r1, r2) -> ranges[r1].getMin() - ranges[r2].getMin());
|
||||
|
||||
// set max values for all unbound ranges (except last range)
|
||||
for (int i = 0; i < c.length - 1; i++) {
|
||||
|
||||
@@ -481,12 +481,8 @@ public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T>
|
||||
try {
|
||||
final FileChannel channel = fileChannel;
|
||||
if (transactional) {
|
||||
TransactionAwareBufferedWriter writer = new TransactionAwareBufferedWriter(channel, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
closeStream();
|
||||
}
|
||||
});
|
||||
TransactionAwareBufferedWriter writer = new TransactionAwareBufferedWriter(channel,
|
||||
() -> closeStream());
|
||||
|
||||
writer.setEncoding(encoding);
|
||||
writer.setForceSync(forceSync);
|
||||
|
||||
@@ -20,8 +20,6 @@ import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.springframework.aop.ProxyMethodInvocation;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.batch.repeat.RepeatCallback;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.RepeatException;
|
||||
import org.springframework.batch.repeat.RepeatOperations;
|
||||
import org.springframework.batch.repeat.support.RepeatTemplate;
|
||||
@@ -73,45 +71,40 @@ public class RepeatOperationsInterceptor implements MethodInterceptor {
|
||||
}
|
||||
|
||||
try {
|
||||
repeatOperations.iterate(new RepeatCallback() {
|
||||
repeatOperations.iterate(context -> {
|
||||
try {
|
||||
|
||||
@Override
|
||||
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
|
||||
try {
|
||||
|
||||
MethodInvocation clone = invocation;
|
||||
if (invocation instanceof ProxyMethodInvocation) {
|
||||
clone = ((ProxyMethodInvocation) invocation).invocableClone();
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException(
|
||||
"MethodInvocation of the wrong type detected - this should not happen with Spring AOP, so please raise an issue if you see this exception");
|
||||
}
|
||||
|
||||
Object value = clone.proceed();
|
||||
if (voidReturnType) {
|
||||
return RepeatStatus.CONTINUABLE;
|
||||
}
|
||||
if (!isComplete(value)) {
|
||||
// Save the last result
|
||||
result.setValue(value);
|
||||
return RepeatStatus.CONTINUABLE;
|
||||
}
|
||||
else {
|
||||
result.setFinalValue(value);
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
MethodInvocation clone = invocation;
|
||||
if (invocation instanceof ProxyMethodInvocation) {
|
||||
clone = ((ProxyMethodInvocation) invocation).invocableClone();
|
||||
}
|
||||
catch (Throwable e) {
|
||||
if (e instanceof Exception) {
|
||||
throw (Exception) e;
|
||||
}
|
||||
else {
|
||||
throw new RepeatOperationsInterceptorException("Unexpected error in batch interceptor", e);
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException(
|
||||
"MethodInvocation of the wrong type detected - this should not happen with Spring AOP, so please raise an issue if you see this exception");
|
||||
}
|
||||
|
||||
Object value = clone.proceed();
|
||||
if (voidReturnType) {
|
||||
return RepeatStatus.CONTINUABLE;
|
||||
}
|
||||
if (!isComplete(value)) {
|
||||
// Save the last result
|
||||
result.setValue(value);
|
||||
return RepeatStatus.CONTINUABLE;
|
||||
}
|
||||
else {
|
||||
result.setFinalValue(value);
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
}
|
||||
catch (Throwable e) {
|
||||
if (e instanceof Exception) {
|
||||
throw (Exception) e;
|
||||
}
|
||||
else {
|
||||
throw new RepeatOperationsInterceptorException("Unexpected error in batch interceptor", e);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
catch (Throwable t) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
@@ -34,6 +34,7 @@ import org.springframework.util.ReflectionUtils;
|
||||
* Class that contains the specified annotation type.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
public class AnnotationMethodResolver implements MethodResolver {
|
||||
|
||||
@@ -85,15 +86,12 @@ public class AnnotationMethodResolver implements MethodResolver {
|
||||
public Method findMethod(final Class<?> clazz) {
|
||||
Assert.notNull(clazz, "class must not be null");
|
||||
final AtomicReference<Method> annotatedMethod = new AtomicReference<>();
|
||||
ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
|
||||
@Override
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType);
|
||||
if (annotation != null) {
|
||||
Assert.isNull(annotatedMethod.get(), "found more than one method on target class [" + clazz
|
||||
+ "] with the annotation type [" + annotationType + "]");
|
||||
annotatedMethod.set(method);
|
||||
}
|
||||
ReflectionUtils.doWithMethods(clazz, method -> {
|
||||
Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType);
|
||||
if (annotation != null) {
|
||||
Assert.isNull(annotatedMethod.get(), "found more than one method on target class [" + clazz
|
||||
+ "] with the annotation type [" + annotationType + "]");
|
||||
annotatedMethod.set(method);
|
||||
}
|
||||
});
|
||||
return annotatedMethod.get();
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
@@ -47,15 +45,12 @@ public class CompositeKeyFooDao extends JdbcDaoSupport implements FooDao {
|
||||
Map<?, ?> keys = (Map<?, ?>) key;
|
||||
Object[] args = keys.values().toArray();
|
||||
|
||||
RowMapper<Foo> fooMapper = new RowMapper<>() {
|
||||
@Override
|
||||
public Foo mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
RowMapper<Foo> fooMapper = (rs, rowNum) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
};
|
||||
|
||||
return getJdbcTemplate().query("SELECT ID, NAME, VALUE from T_FOOS where ID = ? and VALUE = ?", fooMapper, args)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2009-2022 the original author or authors.
|
||||
* Copyright 2009-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.
|
||||
@@ -41,7 +41,6 @@ import org.springframework.jdbc.datasource.DataSourceUtils;
|
||||
import org.springframework.jdbc.datasource.SmartDataSource;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@@ -191,35 +190,23 @@ class ExtendedConnectionDataSourceProxyTests {
|
||||
|
||||
Connection connection = DataSourceUtils.getConnection(csds);
|
||||
csds.startCloseSuppression(connection);
|
||||
tt.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
template.queryForList("select baz from bar");
|
||||
template.queryForList("select foo from bar");
|
||||
return null;
|
||||
}
|
||||
tt.execute((TransactionCallback<Void>) status -> {
|
||||
template.queryForList("select baz from bar");
|
||||
template.queryForList("select foo from bar");
|
||||
return null;
|
||||
});
|
||||
tt.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
template.queryForList("select ham from foo");
|
||||
tt2.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
template.queryForList("select 1 from eggs");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
template.queryForList("select more, ham from foo");
|
||||
tt.execute((TransactionCallback<Void>) status -> {
|
||||
template.queryForList("select ham from foo");
|
||||
tt2.execute((TransactionCallback<Void>) status1 -> {
|
||||
template.queryForList("select 1 from eggs");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
template.queryForList("select more, ham from foo");
|
||||
return null;
|
||||
});
|
||||
tt.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
template.queryForList("select spam from ham");
|
||||
return null;
|
||||
}
|
||||
tt.execute((TransactionCallback<Void>) status -> {
|
||||
template.queryForList("select spam from ham");
|
||||
return null;
|
||||
});
|
||||
csds.stopCloseSuppression(connection);
|
||||
DataSourceUtils.releaseConnection(connection, csds);
|
||||
|
||||
@@ -70,12 +70,7 @@ class JdbcBatchItemWriterClassicTests {
|
||||
};
|
||||
writer.setSql("SQL");
|
||||
writer.setJdbcTemplate(new NamedParameterJdbcTemplate(jdbcTemplate));
|
||||
writer.setItemPreparedStatementSetter(new ItemPreparedStatementSetter<>() {
|
||||
@Override
|
||||
public void setValues(String item, PreparedStatement ps) throws SQLException {
|
||||
list.add(item);
|
||||
}
|
||||
});
|
||||
writer.setItemPreparedStatementSetter((item, ps) -> list.add(item));
|
||||
writer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@@ -128,24 +123,16 @@ class JdbcBatchItemWriterClassicTests {
|
||||
@Test
|
||||
void testWriteAndFlushWithFailure() throws Exception {
|
||||
final RuntimeException ex = new RuntimeException("bar");
|
||||
writer.setItemPreparedStatementSetter(new ItemPreparedStatementSetter<>() {
|
||||
@Override
|
||||
public void setValues(String item, PreparedStatement ps) throws SQLException {
|
||||
list.add(item);
|
||||
throw ex;
|
||||
}
|
||||
writer.setItemPreparedStatementSetter((item, ps) -> {
|
||||
list.add(item);
|
||||
throw ex;
|
||||
});
|
||||
ps.addBatch();
|
||||
when(ps.executeBatch()).thenReturn(new int[] { 123 });
|
||||
Exception exception = assertThrows(RuntimeException.class, () -> writer.write(Chunk.of("foo")));
|
||||
assertEquals("bar", exception.getMessage());
|
||||
assertEquals(2, list.size());
|
||||
writer.setItemPreparedStatementSetter(new ItemPreparedStatementSetter<>() {
|
||||
@Override
|
||||
public void setValues(String item, PreparedStatement ps) throws SQLException {
|
||||
list.add(item);
|
||||
}
|
||||
});
|
||||
writer.setItemPreparedStatementSetter((item, ps) -> list.add(item));
|
||||
writer.write(Chunk.of("foo"));
|
||||
assertEquals(4, list.size());
|
||||
assertTrue(list.contains("SQL"));
|
||||
|
||||
@@ -148,12 +148,7 @@ public class JdbcBatchItemWriterNamedParameterTests {
|
||||
|
||||
mapWriter.setSql(sql);
|
||||
mapWriter.setJdbcTemplate(namedParameterJdbcOperations);
|
||||
mapWriter.setItemSqlParameterSourceProvider(new ItemSqlParameterSourceProvider<>() {
|
||||
@Override
|
||||
public SqlParameterSource createSqlParameterSource(Map<String, Object> item) {
|
||||
return new MapSqlParameterSource(item);
|
||||
}
|
||||
});
|
||||
mapWriter.setItemSqlParameterSourceProvider(item -> new MapSqlParameterSource(item));
|
||||
mapWriter.afterPropertiesSet();
|
||||
|
||||
ArgumentCaptor<SqlParameterSource[]> captor = ArgumentCaptor.forClass(SqlParameterSource[].class);
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.jdbc.support.JdbcTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@@ -59,13 +58,10 @@ class JdbcCursorItemReaderConfigTests {
|
||||
reader.setUseSharedExtendedConnection(true);
|
||||
reader.setSql("select foo from bar");
|
||||
final ExecutionContext ec = new ExecutionContext();
|
||||
tt.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
reader.open(ec);
|
||||
reader.close();
|
||||
return null;
|
||||
}
|
||||
tt.execute((TransactionCallback<Void>) status -> {
|
||||
reader.open(ec);
|
||||
reader.close();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -90,13 +86,10 @@ class JdbcCursorItemReaderConfigTests {
|
||||
reader.setDataSource(ds);
|
||||
reader.setSql("select foo from bar");
|
||||
final ExecutionContext ec = new ExecutionContext();
|
||||
tt.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
reader.open(ec);
|
||||
reader.close();
|
||||
return null;
|
||||
}
|
||||
tt.execute((TransactionCallback<Void>) status -> {
|
||||
reader.open(ec);
|
||||
reader.close();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -18,15 +18,12 @@ package org.springframework.batch.item.database;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletionService;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorCompletionService;
|
||||
@@ -45,7 +42,6 @@ import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
import org.springframework.test.jdbc.JdbcTestUtils;
|
||||
|
||||
@@ -119,22 +115,19 @@ class JdbcPagingItemReaderAsyncTests {
|
||||
CompletionService<List<Foo>> completionService = new ExecutorCompletionService<>(
|
||||
Executors.newFixedThreadPool(THREAD_COUNT));
|
||||
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||
completionService.submit(new Callable<>() {
|
||||
@Override
|
||||
public List<Foo> call() throws Exception {
|
||||
List<Foo> list = new ArrayList<>();
|
||||
Foo next = null;
|
||||
do {
|
||||
next = reader.read();
|
||||
Thread.sleep(10L);
|
||||
logger.debug("Reading item: " + next);
|
||||
if (next != null) {
|
||||
list.add(next);
|
||||
}
|
||||
completionService.submit(() -> {
|
||||
List<Foo> list = new ArrayList<>();
|
||||
Foo next = null;
|
||||
do {
|
||||
next = reader.read();
|
||||
Thread.sleep(10L);
|
||||
logger.debug("Reading item: " + next);
|
||||
if (next != null) {
|
||||
list.add(next);
|
||||
}
|
||||
while (next != null);
|
||||
return list;
|
||||
}
|
||||
while (next != null);
|
||||
return list;
|
||||
});
|
||||
}
|
||||
int count = 0;
|
||||
@@ -162,15 +155,12 @@ class JdbcPagingItemReaderAsyncTests {
|
||||
sortKeys.put("ID", Order.ASCENDING);
|
||||
queryProvider.setSortKeys(sortKeys);
|
||||
reader.setQueryProvider(queryProvider);
|
||||
reader.setRowMapper(new RowMapper<>() {
|
||||
@Override
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
reader.setRowMapper((rs, i) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
});
|
||||
reader.setPageSize(PAGE_SIZE);
|
||||
reader.afterPropertiesSet();
|
||||
|
||||
@@ -15,15 +15,12 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
/**
|
||||
@@ -50,15 +47,12 @@ class JdbcPagingItemReaderClassicParameterTests extends AbstractJdbcPagingItemRe
|
||||
queryProvider.setSortKeys(sortKeys);
|
||||
reader.setParameterValues(Collections.<String, Object>singletonMap("limit", 2));
|
||||
reader.setQueryProvider(queryProvider);
|
||||
reader.setRowMapper(new RowMapper<>() {
|
||||
@Override
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
reader.setRowMapper((rs, i) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
});
|
||||
reader.setPageSize(3);
|
||||
reader.afterPropertiesSet();
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -28,7 +26,6 @@ import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
/**
|
||||
@@ -55,15 +52,12 @@ public class JdbcPagingItemReaderCommonTests extends AbstractItemStreamItemReade
|
||||
sortKeys.put("ID", Order.ASCENDING);
|
||||
queryProvider.setSortKeys(sortKeys);
|
||||
reader.setQueryProvider(queryProvider);
|
||||
reader.setRowMapper(new RowMapper<>() {
|
||||
@Override
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
reader.setRowMapper((rs, i) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
});
|
||||
reader.setPageSize(3);
|
||||
reader.afterPropertiesSet();
|
||||
|
||||
@@ -15,15 +15,12 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
/**
|
||||
* Tests for {@link JpaPagingItemReader}.
|
||||
@@ -46,15 +43,12 @@ public class JdbcPagingItemReaderIntegrationTests extends AbstractGenericDataSou
|
||||
sortKeys.put("ID", Order.ASCENDING);
|
||||
queryProvider.setSortKeys(sortKeys);
|
||||
inputSource.setQueryProvider(queryProvider);
|
||||
inputSource.setRowMapper(new RowMapper<>() {
|
||||
@Override
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
inputSource.setRowMapper((rs, i) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
});
|
||||
inputSource.setPageSize(3);
|
||||
inputSource.afterPropertiesSet();
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
@@ -25,7 +23,6 @@ import org.junit.jupiter.api.Disabled;
|
||||
|
||||
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
/**
|
||||
@@ -55,15 +52,12 @@ class JdbcPagingItemReaderNamedParameterTests extends AbstractJdbcPagingItemRead
|
||||
queryProvider.setSortKeys(sortKeys);
|
||||
reader.setParameterValues(Collections.<String, Object>singletonMap("limit", 2));
|
||||
reader.setQueryProvider(queryProvider);
|
||||
reader.setRowMapper(new RowMapper<>() {
|
||||
@Override
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
reader.setRowMapper((rs, i) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
});
|
||||
reader.setPageSize(3);
|
||||
reader.afterPropertiesSet();
|
||||
|
||||
@@ -15,15 +15,12 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
/**
|
||||
* Tests for {@link JpaPagingItemReader} with sort key not equal to ID.
|
||||
@@ -47,15 +44,12 @@ public class JdbcPagingItemReaderOrderIntegrationTests extends AbstractGenericDa
|
||||
sortKeys.put("NAME", Order.DESCENDING);
|
||||
queryProvider.setSortKeys(sortKeys);
|
||||
inputSource.setQueryProvider(queryProvider);
|
||||
inputSource.setRowMapper(new RowMapper<>() {
|
||||
@Override
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
inputSource.setRowMapper((rs, i) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
});
|
||||
inputSource.setPageSize(3);
|
||||
inputSource.afterPropertiesSet();
|
||||
|
||||
@@ -20,8 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -42,7 +40,6 @@ import org.springframework.batch.item.database.support.SqlPagingQueryProviderFac
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
import org.springframework.test.jdbc.JdbcTestUtils;
|
||||
|
||||
@@ -157,15 +154,12 @@ class JdbcPagingRestartIntegrationTests {
|
||||
sortKeys.put("VALUE", Order.ASCENDING);
|
||||
factory.setSortKeys(sortKeys);
|
||||
reader.setQueryProvider(factory.getObject());
|
||||
reader.setRowMapper(new RowMapper<>() {
|
||||
@Override
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
reader.setRowMapper((rs, i) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
});
|
||||
reader.setPageSize(pageSize);
|
||||
reader.afterPropertiesSet();
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletionService;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorCompletionService;
|
||||
@@ -110,22 +109,19 @@ class JpaPagingItemReaderAsyncTests {
|
||||
CompletionService<List<Foo>> completionService = new ExecutorCompletionService<>(
|
||||
Executors.newFixedThreadPool(THREAD_COUNT));
|
||||
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||
completionService.submit(new Callable<>() {
|
||||
@Override
|
||||
public List<Foo> call() throws Exception {
|
||||
List<Foo> list = new ArrayList<>();
|
||||
Foo next = null;
|
||||
do {
|
||||
next = reader.read();
|
||||
Thread.sleep(10L);
|
||||
logger.debug("Reading item: " + next);
|
||||
if (next != null) {
|
||||
list.add(next);
|
||||
}
|
||||
completionService.submit(() -> {
|
||||
List<Foo> list = new ArrayList<>();
|
||||
Foo next = null;
|
||||
do {
|
||||
next = reader.read();
|
||||
Thread.sleep(10L);
|
||||
logger.debug("Reading item: " + next);
|
||||
if (next != null) {
|
||||
list.add(next);
|
||||
}
|
||||
while (next != null);
|
||||
return list;
|
||||
}
|
||||
while (next != null);
|
||||
return list;
|
||||
});
|
||||
}
|
||||
int count = 0;
|
||||
|
||||
@@ -15,9 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.support.JdbcDaoSupport;
|
||||
@@ -27,15 +24,12 @@ public class SingleKeyFooDao extends JdbcDaoSupport implements FooDao {
|
||||
@Override
|
||||
public Foo getFoo(Object key) {
|
||||
|
||||
RowMapper<Foo> fooMapper = new RowMapper<>() {
|
||||
@Override
|
||||
public Foo mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
}
|
||||
RowMapper<Foo> fooMapper = (rs, rowNum) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setId(rs.getInt(1));
|
||||
foo.setName(rs.getString(2));
|
||||
foo.setValue(rs.getInt(3));
|
||||
return foo;
|
||||
};
|
||||
|
||||
return getJdbcTemplate().query("SELECT ID, NAME, VALUE from T_FOOS where ID = ?", fooMapper, key).get(0);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2022 the original author or authors.
|
||||
* Copyright 2010-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.item.database;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.hsqldb.types.Types;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
@@ -25,7 +22,6 @@ import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ReaderNotOpenException;
|
||||
import org.springframework.batch.item.sample.Foo;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.jdbc.core.PreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.SqlParameter;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -68,12 +64,9 @@ class StoredProcedureItemReaderCommonTests extends AbstractDatabaseItemStreamIte
|
||||
reader.setProcedureName("read_some_foos");
|
||||
reader.setParameters(new SqlParameter[] { new SqlParameter("from_id", Types.NUMERIC),
|
||||
new SqlParameter("to_id", Types.NUMERIC) });
|
||||
reader.setPreparedStatementSetter(new PreparedStatementSetter() {
|
||||
@Override
|
||||
public void setValues(PreparedStatement ps) throws SQLException {
|
||||
ps.setInt(1, 1000);
|
||||
ps.setInt(2, 1001);
|
||||
}
|
||||
reader.setPreparedStatementSetter(ps -> {
|
||||
ps.setInt(1, 1000);
|
||||
ps.setInt(2, 1001);
|
||||
});
|
||||
reader.setRowMapper(new FooRowMapper());
|
||||
reader.setVerifyCursorPosition(false);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2022 the original author or authors.
|
||||
* Copyright 2010-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.
|
||||
@@ -21,20 +21,16 @@ import static org.mockito.Mockito.when;
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.hsqldb.types.Types;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.jdbc.core.PreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.SqlParameter;
|
||||
import org.springframework.jdbc.support.JdbcTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@@ -66,13 +62,10 @@ class StoredprocedureItemReaderConfigTests {
|
||||
reader.setUseSharedExtendedConnection(true);
|
||||
reader.setProcedureName("foo_bar");
|
||||
final ExecutionContext ec = new ExecutionContext();
|
||||
tt.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
reader.open(ec);
|
||||
reader.close();
|
||||
return null;
|
||||
}
|
||||
tt.execute((TransactionCallback<Void>) status -> {
|
||||
reader.open(ec);
|
||||
reader.close();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -101,13 +94,10 @@ class StoredprocedureItemReaderConfigTests {
|
||||
reader.setDataSource(ds);
|
||||
reader.setProcedureName("foo_bar");
|
||||
final ExecutionContext ec = new ExecutionContext();
|
||||
tt.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
reader.open(ec);
|
||||
reader.close();
|
||||
return null;
|
||||
}
|
||||
tt.execute((TransactionCallback<Void>) status -> {
|
||||
reader.open(ec);
|
||||
reader.close();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -137,20 +127,14 @@ class StoredprocedureItemReaderConfigTests {
|
||||
reader.setProcedureName("foo_bar");
|
||||
reader.setParameters(
|
||||
new SqlParameter[] { new SqlParameter("foo", Types.VARCHAR), new SqlParameter("bar", Types.OTHER) });
|
||||
reader.setPreparedStatementSetter(new PreparedStatementSetter() {
|
||||
@Override
|
||||
public void setValues(PreparedStatement ps) throws SQLException {
|
||||
}
|
||||
reader.setPreparedStatementSetter(ps -> {
|
||||
});
|
||||
reader.setRefCursorPosition(3);
|
||||
final ExecutionContext ec = new ExecutionContext();
|
||||
tt.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
reader.open(ec);
|
||||
reader.close();
|
||||
return null;
|
||||
}
|
||||
tt.execute((TransactionCallback<Void>) status -> {
|
||||
reader.open(ec);
|
||||
reader.close();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database.builder;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Arrays;
|
||||
import javax.sql.DataSource;
|
||||
@@ -33,7 +31,6 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.jdbc.core.PreparedStatementSetter;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
|
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
@@ -49,6 +46,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
* @author Drummond Dawson
|
||||
* @author Ankur Trapasiya
|
||||
* @author Parikshit Dutta
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
class JdbcCursorItemReaderBuilderTests {
|
||||
|
||||
@@ -207,12 +205,7 @@ class JdbcCursorItemReaderBuilderTests {
|
||||
JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>().dataSource(this.dataSource)
|
||||
.name("fooReader")
|
||||
.sql("SELECT * FROM FOO WHERE FIRST > ? ORDER BY FIRST")
|
||||
.preparedStatementSetter(new PreparedStatementSetter() {
|
||||
@Override
|
||||
public void setValues(PreparedStatement ps) throws SQLException {
|
||||
ps.setInt(1, 3);
|
||||
}
|
||||
})
|
||||
.preparedStatementSetter(ps -> ps.setInt(1, 3))
|
||||
.rowMapper((rs, rowNum) -> {
|
||||
Foo foo = new Foo();
|
||||
|
||||
|
||||
@@ -34,13 +34,10 @@ public class FlatFileItemReaderCommonTests extends AbstractItemStreamItemReaderT
|
||||
FlatFileItemReader<Foo> tested = new FlatFileItemReader<>();
|
||||
Resource resource = new ByteArrayResource(FOOS.getBytes());
|
||||
tested.setResource(resource);
|
||||
tested.setLineMapper(new LineMapper<>() {
|
||||
@Override
|
||||
public Foo mapLine(String line, int lineNumber) {
|
||||
Foo foo = new Foo();
|
||||
foo.setValue(Integer.parseInt(line.trim()));
|
||||
return foo;
|
||||
}
|
||||
tested.setLineMapper((line, lineNumber) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setValue(Integer.parseInt(line.trim()));
|
||||
return foo;
|
||||
});
|
||||
|
||||
tested.setSaveState(true);
|
||||
|
||||
@@ -440,14 +440,11 @@ class FlatFileItemReaderTests {
|
||||
*/
|
||||
@Test
|
||||
void testMappingExceptionWrapping() throws Exception {
|
||||
LineMapper<String> exceptionLineMapper = new LineMapper<>() {
|
||||
@Override
|
||||
public String mapLine(String line, int lineNumber) throws Exception {
|
||||
if (lineNumber == 2) {
|
||||
throw new Exception("Couldn't map line 2");
|
||||
}
|
||||
return line;
|
||||
LineMapper<String> exceptionLineMapper = (line, lineNumber) -> {
|
||||
if (lineNumber == 2) {
|
||||
throw new Exception("Couldn't map line 2");
|
||||
}
|
||||
return line;
|
||||
};
|
||||
reader.setLineMapper(exceptionLineMapper);
|
||||
reader.afterPropertiesSet();
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
@@ -32,13 +31,11 @@ import org.springframework.batch.item.Chunk;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStreamException;
|
||||
import org.springframework.batch.item.UnexpectedInputException;
|
||||
import org.springframework.batch.item.file.transform.LineAggregator;
|
||||
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.WritableResource;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -244,12 +241,7 @@ class FlatFileItemWriterTests {
|
||||
*/
|
||||
@Test
|
||||
void testWriteWithConverter() throws Exception {
|
||||
writer.setLineAggregator(new LineAggregator<>() {
|
||||
@Override
|
||||
public String aggregate(String item) {
|
||||
return "FOO:" + item;
|
||||
}
|
||||
});
|
||||
writer.setLineAggregator(item -> "FOO:" + item);
|
||||
String data = "string";
|
||||
writer.open(executionContext);
|
||||
writer.write(Chunk.of(data));
|
||||
@@ -264,12 +256,7 @@ class FlatFileItemWriterTests {
|
||||
*/
|
||||
@Test
|
||||
void testWriteWithConverterAndString() throws Exception {
|
||||
writer.setLineAggregator(new LineAggregator<>() {
|
||||
@Override
|
||||
public String aggregate(String item) {
|
||||
return "FOO:" + item;
|
||||
}
|
||||
});
|
||||
writer.setLineAggregator(item -> "FOO:" + item);
|
||||
writer.open(executionContext);
|
||||
writer.write(Chunk.of(TEST_STRING));
|
||||
String lineFromFile = readLine();
|
||||
@@ -300,14 +287,7 @@ class FlatFileItemWriterTests {
|
||||
@Test
|
||||
void testRestart() throws Exception {
|
||||
|
||||
writer.setFooterCallback(new FlatFileFooterCallback() {
|
||||
|
||||
@Override
|
||||
public void writeFooter(Writer writer) throws IOException {
|
||||
writer.write("footer");
|
||||
}
|
||||
|
||||
});
|
||||
writer.setFooterCallback(writer -> writer.write("footer"));
|
||||
|
||||
writer.open(executionContext);
|
||||
// write some lines
|
||||
@@ -356,19 +336,16 @@ class FlatFileItemWriterTests {
|
||||
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
|
||||
|
||||
writer.open(executionContext);
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(Chunk.of(TEST_STRING));
|
||||
assertEquals(expectedInTransaction, readLine());
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write(Chunk.of(TEST_STRING));
|
||||
assertEquals(expectedInTransaction, readLine());
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
writer.close();
|
||||
}
|
||||
@@ -376,35 +353,25 @@ class FlatFileItemWriterTests {
|
||||
@Test
|
||||
void testTransactionalRestart() throws Exception {
|
||||
|
||||
writer.setFooterCallback(new FlatFileFooterCallback() {
|
||||
|
||||
@Override
|
||||
public void writeFooter(Writer writer) throws IOException {
|
||||
writer.write("footer");
|
||||
}
|
||||
|
||||
});
|
||||
writer.setFooterCallback(writer -> writer.write("footer"));
|
||||
|
||||
writer.open(executionContext);
|
||||
|
||||
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
// write some lines
|
||||
writer.write(Chunk.of(new String[] { "testLine1", "testLine2", "testLine3" }));
|
||||
// write more lines
|
||||
writer.write(Chunk.of(new String[] { "testLine4", "testLine5" }));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
// write some lines
|
||||
writer.write(Chunk.of(new String[] { "testLine1", "testLine2", "testLine3" }));
|
||||
// write more lines
|
||||
writer.write(Chunk.of(new String[] { "testLine4", "testLine5" }));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
});
|
||||
// close template
|
||||
writer.close();
|
||||
@@ -412,20 +379,17 @@ class FlatFileItemWriterTests {
|
||||
// init with correct data
|
||||
writer.open(executionContext);
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
// write more lines
|
||||
writer.write(Chunk.of(new String[] { "testLine6", "testLine7", "testLine8" }));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
// write more lines
|
||||
writer.write(Chunk.of(new String[] { "testLine6", "testLine7", "testLine8" }));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
});
|
||||
// close template
|
||||
writer.close();
|
||||
@@ -456,35 +420,25 @@ class FlatFileItemWriterTests {
|
||||
|
||||
private void testTransactionalRestartWithMultiByteCharacter(String encoding) throws Exception {
|
||||
writer.setEncoding(encoding);
|
||||
writer.setFooterCallback(new FlatFileFooterCallback() {
|
||||
|
||||
@Override
|
||||
public void writeFooter(Writer writer) throws IOException {
|
||||
writer.write("footer");
|
||||
}
|
||||
|
||||
});
|
||||
writer.setFooterCallback(writer -> writer.write("footer"));
|
||||
|
||||
writer.open(executionContext);
|
||||
|
||||
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
// write some lines
|
||||
writer.write(Chunk.of(new String[] { "téstLine1", "téstLine2", "téstLine3" }));
|
||||
// write more lines
|
||||
writer.write(Chunk.of(new String[] { "téstLine4", "téstLine5" }));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
// write some lines
|
||||
writer.write(Chunk.of(new String[] { "téstLine1", "téstLine2", "téstLine3" }));
|
||||
// write more lines
|
||||
writer.write(Chunk.of(new String[] { "téstLine4", "téstLine5" }));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
});
|
||||
// close template
|
||||
writer.close();
|
||||
@@ -492,20 +446,17 @@ class FlatFileItemWriterTests {
|
||||
// init with correct data
|
||||
writer.open(executionContext);
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
// write more lines
|
||||
writer.write(Chunk.of(new String[] { "téstLine6", "téstLine7", "téstLine8" }));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
// write more lines
|
||||
writer.write(Chunk.of(new String[] { "téstLine6", "téstLine7", "téstLine8" }));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
});
|
||||
// close template
|
||||
writer.close();
|
||||
@@ -587,14 +538,7 @@ class FlatFileItemWriterTests {
|
||||
|
||||
@Test
|
||||
void testWriteFooter() throws Exception {
|
||||
writer.setFooterCallback(new FlatFileFooterCallback() {
|
||||
|
||||
@Override
|
||||
public void writeFooter(Writer writer) throws IOException {
|
||||
writer.write("a\nb");
|
||||
}
|
||||
|
||||
});
|
||||
writer.setFooterCallback(writer -> writer.write("a\nb"));
|
||||
writer.open(executionContext);
|
||||
writer.write(Chunk.of(TEST_STRING));
|
||||
writer.close();
|
||||
@@ -605,14 +549,7 @@ class FlatFileItemWriterTests {
|
||||
|
||||
@Test
|
||||
void testWriteHeader() throws Exception {
|
||||
writer.setHeaderCallback(new FlatFileHeaderCallback() {
|
||||
|
||||
@Override
|
||||
public void writeHeader(Writer writer) throws IOException {
|
||||
writer.write("a\nb");
|
||||
}
|
||||
|
||||
});
|
||||
writer.setHeaderCallback(writer -> writer.write("a\nb"));
|
||||
writer.open(executionContext);
|
||||
writer.write(Chunk.of(TEST_STRING));
|
||||
writer.close();
|
||||
@@ -626,13 +563,7 @@ class FlatFileItemWriterTests {
|
||||
|
||||
@Test
|
||||
void testWriteWithAppendAfterHeaders() throws Exception {
|
||||
writer.setHeaderCallback(new FlatFileHeaderCallback() {
|
||||
@Override
|
||||
public void writeHeader(Writer writer) throws IOException {
|
||||
writer.write("a\nb");
|
||||
}
|
||||
|
||||
});
|
||||
writer.setHeaderCallback(writer -> writer.write("a\nb"));
|
||||
writer.setAppendAllowed(true);
|
||||
writer.open(executionContext);
|
||||
writer.write(Chunk.of("test1"));
|
||||
@@ -651,14 +582,7 @@ class FlatFileItemWriterTests {
|
||||
|
||||
@Test
|
||||
void testWriteHeaderAndDeleteOnExit() {
|
||||
writer.setHeaderCallback(new FlatFileHeaderCallback() {
|
||||
|
||||
@Override
|
||||
public void writeHeader(Writer writer) throws IOException {
|
||||
writer.write("a\nb");
|
||||
}
|
||||
|
||||
});
|
||||
writer.setHeaderCallback(writer -> writer.write("a\nb"));
|
||||
writer.setShouldDeleteIfEmpty(true);
|
||||
writer.open(executionContext);
|
||||
assertTrue(outputFile.exists());
|
||||
@@ -681,14 +605,7 @@ class FlatFileItemWriterTests {
|
||||
|
||||
@Test
|
||||
void testWriteHeaderAndDeleteOnExitReopen() throws Exception {
|
||||
writer.setHeaderCallback(new FlatFileHeaderCallback() {
|
||||
|
||||
@Override
|
||||
public void writeHeader(Writer writer) throws IOException {
|
||||
writer.write("a\nb");
|
||||
}
|
||||
|
||||
});
|
||||
writer.setHeaderCallback(writer -> writer.write("a\nb"));
|
||||
writer.setShouldDeleteIfEmpty(true);
|
||||
writer.open(executionContext);
|
||||
writer.update(executionContext);
|
||||
@@ -718,14 +635,7 @@ class FlatFileItemWriterTests {
|
||||
|
||||
@Test
|
||||
void testWriteHeaderAfterRestartOnFirstChunk() throws Exception {
|
||||
writer.setHeaderCallback(new FlatFileHeaderCallback() {
|
||||
|
||||
@Override
|
||||
public void writeHeader(Writer writer) throws IOException {
|
||||
writer.write("a\nb");
|
||||
}
|
||||
|
||||
});
|
||||
writer.setHeaderCallback(writer -> writer.write("a\nb"));
|
||||
writer.open(executionContext);
|
||||
writer.write(Chunk.of(TEST_STRING));
|
||||
writer.close();
|
||||
@@ -744,14 +654,7 @@ class FlatFileItemWriterTests {
|
||||
|
||||
@Test
|
||||
void testWriteHeaderAfterRestartOnSecondChunk() throws Exception {
|
||||
writer.setHeaderCallback(new FlatFileHeaderCallback() {
|
||||
|
||||
@Override
|
||||
public void writeHeader(Writer writer) throws IOException {
|
||||
writer.write("a\nb");
|
||||
}
|
||||
|
||||
});
|
||||
writer.setHeaderCallback(writer -> writer.write("a\nb"));
|
||||
writer.open(executionContext);
|
||||
writer.write(Chunk.of(TEST_STRING));
|
||||
writer.update(executionContext);
|
||||
@@ -783,15 +686,11 @@ class FlatFileItemWriterTests {
|
||||
*/
|
||||
void testLineAggregatorFailure() throws Exception {
|
||||
|
||||
writer.setLineAggregator(new LineAggregator<>() {
|
||||
|
||||
@Override
|
||||
public String aggregate(String item) {
|
||||
if (item.equals("2")) {
|
||||
throw new RuntimeException("aggregation failed on " + item);
|
||||
}
|
||||
return item;
|
||||
writer.setLineAggregator(item -> {
|
||||
if (item.equals("2")) {
|
||||
throw new RuntimeException("aggregation failed on " + item);
|
||||
}
|
||||
return item;
|
||||
});
|
||||
Chunk<String> items = Chunk.of("1", "2", "3");
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.item.file;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.springframework.batch.item.AbstractItemStreamItemReaderTests;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
@@ -32,15 +30,10 @@ class MultiResourceItemReaderFlatFileTests extends AbstractItemStreamItemReaderT
|
||||
MultiResourceItemReader<Foo> multiReader = new MultiResourceItemReader<>();
|
||||
FlatFileItemReader<Foo> fileReader = new FlatFileItemReader<>();
|
||||
|
||||
fileReader.setLineMapper(new LineMapper<>() {
|
||||
|
||||
@Override
|
||||
public Foo mapLine(String line, int lineNumber) throws Exception {
|
||||
Foo foo = new Foo();
|
||||
foo.setValue(Integer.parseInt(line));
|
||||
return foo;
|
||||
}
|
||||
|
||||
fileReader.setLineMapper((line, lineNumber) -> {
|
||||
Foo foo = new Foo();
|
||||
foo.setValue(Integer.parseInt(line));
|
||||
return foo;
|
||||
});
|
||||
fileReader.setSaveState(true);
|
||||
|
||||
@@ -53,12 +46,8 @@ class MultiResourceItemReaderFlatFileTests extends AbstractItemStreamItemReaderT
|
||||
|
||||
multiReader.setResources(new Resource[] { r1, r2, r3, r4 });
|
||||
multiReader.setSaveState(true);
|
||||
multiReader.setComparator(new Comparator<>() {
|
||||
@Override
|
||||
public int compare(Resource arg0, Resource arg1) {
|
||||
return 0; // preserve original ordering
|
||||
}
|
||||
|
||||
multiReader.setComparator((arg0, arg1) -> {
|
||||
return 0; // preserve original ordering
|
||||
});
|
||||
|
||||
return multiReader;
|
||||
|
||||
@@ -68,11 +68,8 @@ class MultiResourceItemReaderIntegrationTests {
|
||||
itemReader.setLineMapper(new PassThroughLineMapper());
|
||||
|
||||
tested.setDelegate(itemReader);
|
||||
tested.setComparator(new Comparator<>() {
|
||||
@Override
|
||||
public int compare(Resource o1, Resource o2) {
|
||||
return 0; // do not change ordering
|
||||
}
|
||||
tested.setComparator((o1, o2) -> {
|
||||
return 0; // do not change ordering
|
||||
});
|
||||
tested.setResources(new Resource[] { r1, r2, r3, r4, r5 });
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ResourceAware;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import java.util.Comparator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -58,11 +57,8 @@ class MultiResourceItemReaderResourceAwareTests {
|
||||
itemReader.setLineMapper(new FooLineMapper());
|
||||
|
||||
tested.setDelegate(itemReader);
|
||||
tested.setComparator(new Comparator<>() {
|
||||
@Override
|
||||
public int compare(Resource o1, Resource o2) {
|
||||
return 0; // do not change ordering
|
||||
}
|
||||
tested.setComparator((o1, o2) -> {
|
||||
return 0; // do not change ordering
|
||||
});
|
||||
tested.setResources(new Resource[] { r1, r2, r3, r4, r5 });
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.batch.item.file;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Comparator;
|
||||
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.events.Attribute;
|
||||
@@ -81,11 +80,8 @@ class MultiResourceItemReaderXmlTests extends AbstractItemStreamItemReaderTests
|
||||
multiReader.setDelegate(reader);
|
||||
multiReader.setResources(new Resource[] { r1, r2, r3, r4 });
|
||||
multiReader.setSaveState(true);
|
||||
multiReader.setComparator(new Comparator<>() {
|
||||
@Override
|
||||
public int compare(Resource arg0, Resource arg1) {
|
||||
return 0; // preserve original ordering
|
||||
}
|
||||
multiReader.setComparator((arg0, arg1) -> {
|
||||
return 0; // preserve original ordering
|
||||
});
|
||||
|
||||
return multiReader;
|
||||
|
||||
@@ -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.
|
||||
@@ -16,8 +16,6 @@
|
||||
package org.springframework.batch.item.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -117,12 +115,7 @@ public class MultiResourceItemWriterFlatFileTests extends AbstractMultiResourceI
|
||||
@Test
|
||||
void testMultiResourceWriteScenarioWithFooter() throws Exception {
|
||||
|
||||
delegate.setFooterCallback(new FlatFileFooterCallback() {
|
||||
@Override
|
||||
public void writeFooter(Writer writer) throws IOException {
|
||||
writer.write("f");
|
||||
}
|
||||
});
|
||||
delegate.setFooterCallback(writer -> writer.write("f"));
|
||||
super.setUp(delegate);
|
||||
tested.open(executionContext);
|
||||
|
||||
@@ -145,12 +138,7 @@ public class MultiResourceItemWriterFlatFileTests extends AbstractMultiResourceI
|
||||
@Test
|
||||
void testTransactionalMultiResourceWriteScenarioWithFooter() throws Exception {
|
||||
|
||||
delegate.setFooterCallback(new FlatFileFooterCallback() {
|
||||
@Override
|
||||
public void writeFooter(Writer writer) throws IOException {
|
||||
writer.write("f");
|
||||
}
|
||||
});
|
||||
delegate.setFooterCallback(writer -> writer.write("f"));
|
||||
super.setUp(delegate);
|
||||
tested.open(executionContext);
|
||||
|
||||
@@ -206,12 +194,7 @@ public class MultiResourceItemWriterFlatFileTests extends AbstractMultiResourceI
|
||||
@Test
|
||||
void testRestartWithFooter() throws Exception {
|
||||
|
||||
delegate.setFooterCallback(new FlatFileFooterCallback() {
|
||||
@Override
|
||||
public void writeFooter(Writer writer) throws IOException {
|
||||
writer.write("f");
|
||||
}
|
||||
});
|
||||
delegate.setFooterCallback(writer -> writer.write("f"));
|
||||
|
||||
super.setUp(delegate);
|
||||
tested.open(executionContext);
|
||||
@@ -244,12 +227,7 @@ public class MultiResourceItemWriterFlatFileTests extends AbstractMultiResourceI
|
||||
@Test
|
||||
void testTransactionalRestartWithFooter() throws Exception {
|
||||
|
||||
delegate.setFooterCallback(new FlatFileFooterCallback() {
|
||||
@Override
|
||||
public void writeFooter(Writer writer) throws IOException {
|
||||
writer.write("f");
|
||||
}
|
||||
});
|
||||
delegate.setFooterCallback(writer -> writer.write("f"));
|
||||
super.setUp(delegate);
|
||||
tested.open(executionContext);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2022 the original author or authors.
|
||||
* Copyright 2017-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.
|
||||
@@ -48,12 +48,7 @@ class MultiResourceItemWriterBuilderTests {
|
||||
|
||||
private File file;
|
||||
|
||||
private final ResourceSuffixCreator suffixCreator = new ResourceSuffixCreator() {
|
||||
@Override
|
||||
public String getSuffix(int index) {
|
||||
return "A" + index;
|
||||
}
|
||||
};
|
||||
private final ResourceSuffixCreator suffixCreator = index -> "A" + index;
|
||||
|
||||
private final ExecutionContext executionContext = new ExecutionContext();
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
@@ -44,15 +43,12 @@ class BeanWrapperFieldSetMapperConcurrentTests {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(5);
|
||||
Collection<Future<Boolean>> results = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Future<Boolean> result = executorService.submit(new Callable<>() {
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
GreenBean bean = mapper.mapFieldSet(lineTokenizer.tokenize("blue,green"));
|
||||
assertEquals("green", bean.getGreen());
|
||||
}
|
||||
return true;
|
||||
Future<Boolean> result = executorService.submit(() -> {
|
||||
for (int i1 = 0; i1 < 10; i1++) {
|
||||
GreenBean bean = mapper.mapFieldSet(lineTokenizer.tokenize("blue,green"));
|
||||
assertEquals("green", bean.getGreen());
|
||||
}
|
||||
return true;
|
||||
});
|
||||
results.add(result);
|
||||
}
|
||||
|
||||
@@ -26,10 +26,8 @@ import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.batch.item.file.transform.DefaultFieldSet;
|
||||
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
|
||||
import org.springframework.batch.item.file.transform.FieldSet;
|
||||
import org.springframework.batch.item.file.transform.LineTokenizer;
|
||||
import org.springframework.batch.item.file.transform.Name;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
@@ -51,33 +49,13 @@ class PatternMatchingCompositeLineMapperTests {
|
||||
@Test
|
||||
void testKeyFound() throws Exception {
|
||||
Map<String, LineTokenizer> tokenizers = new HashMap<>();
|
||||
tokenizers.put("foo*", new LineTokenizer() {
|
||||
@Override
|
||||
public FieldSet tokenize(@Nullable String line) {
|
||||
return new DefaultFieldSet(new String[] { "a", "b" });
|
||||
}
|
||||
});
|
||||
tokenizers.put("bar*", new LineTokenizer() {
|
||||
@Override
|
||||
public FieldSet tokenize(@Nullable String line) {
|
||||
return new DefaultFieldSet(new String[] { "c", "d" });
|
||||
}
|
||||
});
|
||||
tokenizers.put("foo*", line -> new DefaultFieldSet(new String[] { "a", "b" }));
|
||||
tokenizers.put("bar*", line -> new DefaultFieldSet(new String[] { "c", "d" }));
|
||||
mapper.setTokenizers(tokenizers);
|
||||
|
||||
Map<String, FieldSetMapper<Name>> fieldSetMappers = new HashMap<>();
|
||||
fieldSetMappers.put("foo*", new FieldSetMapper<>() {
|
||||
@Override
|
||||
public Name mapFieldSet(FieldSet fs) {
|
||||
return new Name(fs.readString(0), fs.readString(1), 0);
|
||||
}
|
||||
});
|
||||
fieldSetMappers.put("bar*", new FieldSetMapper<>() {
|
||||
@Override
|
||||
public Name mapFieldSet(FieldSet fs) {
|
||||
return new Name(fs.readString(1), fs.readString(0), 0);
|
||||
}
|
||||
});
|
||||
fieldSetMappers.put("foo*", fs -> new Name(fs.readString(0), fs.readString(1), 0));
|
||||
fieldSetMappers.put("bar*", fs -> new Name(fs.readString(1), fs.readString(0), 0));
|
||||
mapper.setFieldSetMappers(fieldSetMappers);
|
||||
|
||||
Name name = mapper.mapLine("bar", 1);
|
||||
@@ -87,27 +65,12 @@ class PatternMatchingCompositeLineMapperTests {
|
||||
@Test
|
||||
void testMapperKeyNotFound() {
|
||||
Map<String, LineTokenizer> tokenizers = new HashMap<>();
|
||||
tokenizers.put("foo*", new LineTokenizer() {
|
||||
@Override
|
||||
public FieldSet tokenize(@Nullable String line) {
|
||||
return new DefaultFieldSet(new String[] { "a", "b" });
|
||||
}
|
||||
});
|
||||
tokenizers.put("bar*", new LineTokenizer() {
|
||||
@Override
|
||||
public FieldSet tokenize(@Nullable String line) {
|
||||
return new DefaultFieldSet(new String[] { "c", "d" });
|
||||
}
|
||||
});
|
||||
tokenizers.put("foo*", line -> new DefaultFieldSet(new String[] { "a", "b" }));
|
||||
tokenizers.put("bar*", line -> new DefaultFieldSet(new String[] { "c", "d" }));
|
||||
mapper.setTokenizers(tokenizers);
|
||||
|
||||
Map<String, FieldSetMapper<Name>> fieldSetMappers = new HashMap<>();
|
||||
fieldSetMappers.put("foo*", new FieldSetMapper<>() {
|
||||
@Override
|
||||
public Name mapFieldSet(FieldSet fs) {
|
||||
return new Name(fs.readString(0), fs.readString(1), 0);
|
||||
}
|
||||
});
|
||||
fieldSetMappers.put("foo*", fs -> new Name(fs.readString(0), fs.readString(1), 0));
|
||||
mapper.setFieldSetMappers(fieldSetMappers);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> mapper.mapLine("bar", 1));
|
||||
|
||||
@@ -33,12 +33,7 @@ class FormatterLineAggregatorTests {
|
||||
// object under test
|
||||
private FormatterLineAggregator<String[]> aggregator;
|
||||
|
||||
private final FieldExtractor<String[]> defaultFieldExtractor = new FieldExtractor<>() {
|
||||
@Override
|
||||
public Object[] extract(String[] item) {
|
||||
return item;
|
||||
}
|
||||
};
|
||||
private final FieldExtractor<String[]> defaultFieldExtractor = item -> item;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
|
||||
@@ -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.
|
||||
@@ -25,7 +25,6 @@ import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* @author Ben Hale
|
||||
@@ -45,12 +44,7 @@ class PatternMatchingCompositeLineTokenizerTests {
|
||||
void testEmptyKeyMatchesAnyLine() throws Exception {
|
||||
Map<String, LineTokenizer> map = new HashMap<>();
|
||||
map.put("*", new DelimitedLineTokenizer());
|
||||
map.put("foo", new LineTokenizer() {
|
||||
@Override
|
||||
public FieldSet tokenize(@Nullable String line) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
map.put("foo", line -> null);
|
||||
tokenizer.setTokenizers(map);
|
||||
tokenizer.afterPropertiesSet();
|
||||
FieldSet fields = tokenizer.tokenize("abc");
|
||||
@@ -61,12 +55,7 @@ class PatternMatchingCompositeLineTokenizerTests {
|
||||
void testEmptyKeyDoesNotMatchWhenAlternativeAvailable() throws Exception {
|
||||
|
||||
Map<String, LineTokenizer> map = new LinkedHashMap<>();
|
||||
map.put("*", new LineTokenizer() {
|
||||
@Override
|
||||
public FieldSet tokenize(@Nullable String line) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
map.put("*", line -> null);
|
||||
map.put("foo*", new DelimitedLineTokenizer());
|
||||
tokenizer.setTokenizers(map);
|
||||
tokenizer.afterPropertiesSet();
|
||||
@@ -83,12 +72,8 @@ class PatternMatchingCompositeLineTokenizerTests {
|
||||
|
||||
@Test
|
||||
void testMatchWithPrefix() throws Exception {
|
||||
tokenizer.setTokenizers(Collections.singletonMap("foo*", (LineTokenizer) new LineTokenizer() {
|
||||
@Override
|
||||
public FieldSet tokenize(@Nullable String line) {
|
||||
return new DefaultFieldSet(new String[] { line });
|
||||
}
|
||||
}));
|
||||
tokenizer.setTokenizers(
|
||||
Collections.singletonMap("foo*", (LineTokenizer) line -> new DefaultFieldSet(new String[] { line })));
|
||||
tokenizer.afterPropertiesSet();
|
||||
FieldSet fields = tokenizer.tokenize("foo bar");
|
||||
assertEquals(1, fields.getFieldCount());
|
||||
|
||||
@@ -36,12 +36,7 @@ class RecursiveCollectionItemTransformerTests {
|
||||
|
||||
@Test
|
||||
void testSetDelegateAndPassInString() {
|
||||
aggregator.setDelegate(new LineAggregator<>() {
|
||||
@Override
|
||||
public String aggregate(String item) {
|
||||
return "bar";
|
||||
}
|
||||
});
|
||||
aggregator.setDelegate(item -> "bar");
|
||||
assertEquals("bar", aggregator.aggregate(Collections.singleton("foo")));
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -24,8 +24,6 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.batch.item.Chunk;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.MailMessage;
|
||||
import org.springframework.mail.MailSendException;
|
||||
import org.springframework.mail.MailSender;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
@@ -99,12 +97,7 @@ class SimpleMailMessageItemWriterTests {
|
||||
void testCustomErrorHandler() {
|
||||
|
||||
final AtomicReference<String> content = new AtomicReference<>();
|
||||
writer.setMailErrorHandler(new MailErrorHandler() {
|
||||
@Override
|
||||
public void handle(MailMessage message, Exception exception) throws MailException {
|
||||
content.set(exception.getMessage());
|
||||
}
|
||||
});
|
||||
writer.setMailErrorHandler((message, exception) -> content.set(exception.getMessage()));
|
||||
|
||||
SimpleMailMessage foo = new SimpleMailMessage();
|
||||
SimpleMailMessage bar = new SimpleMailMessage();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2022 the original author or authors.
|
||||
* Copyright 2017-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.
|
||||
@@ -25,10 +25,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.batch.item.Chunk;
|
||||
import org.springframework.batch.item.mail.MailErrorHandler;
|
||||
import org.springframework.batch.item.mail.SimpleMailMessageItemWriter;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.MailMessage;
|
||||
import org.springframework.mail.MailSendException;
|
||||
import org.springframework.mail.MailSender;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
@@ -92,12 +89,7 @@ class SimpleMailMessageItemWriterBuilderTests {
|
||||
void testCustomErrorHandler() {
|
||||
final AtomicReference<String> content = new AtomicReference<>();
|
||||
SimpleMailMessageItemWriter writer = new SimpleMailMessageItemWriterBuilder()
|
||||
.mailErrorHandler(new MailErrorHandler() {
|
||||
@Override
|
||||
public void handle(MailMessage message, Exception exception) throws MailException {
|
||||
content.set(exception.getMessage());
|
||||
}
|
||||
})
|
||||
.mailErrorHandler((message, exception) -> content.set(exception.getMessage()))
|
||||
.mailSender(this.mailSender)
|
||||
.build();
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -27,9 +27,6 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.batch.item.Chunk;
|
||||
import org.springframework.batch.item.mail.MailErrorHandler;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.MailMessage;
|
||||
import org.springframework.mail.MailSendException;
|
||||
import org.springframework.mail.MailSender;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
@@ -99,12 +96,7 @@ class MimeMessageItemWriterTests {
|
||||
void testCustomErrorHandler() {
|
||||
|
||||
final AtomicReference<String> content = new AtomicReference<>();
|
||||
writer.setMailErrorHandler(new MailErrorHandler() {
|
||||
@Override
|
||||
public void handle(MailMessage message, Exception exception) throws MailException {
|
||||
content.set(exception.getMessage());
|
||||
}
|
||||
});
|
||||
writer.setMailErrorHandler((message, exception) -> content.set(exception.getMessage()));
|
||||
|
||||
MimeMessage foo = new MimeMessage(session);
|
||||
MimeMessage bar = new MimeMessage(session);
|
||||
|
||||
@@ -45,18 +45,8 @@ class ClassifierCompositeItemWriterTests {
|
||||
@Test
|
||||
void testWrite() throws Exception {
|
||||
Map<String, ItemWriter<String>> map = new HashMap<>();
|
||||
ItemWriter<String> fooWriter = new ItemWriter<>() {
|
||||
@Override
|
||||
public void write(Chunk<? extends String> chunk) throws Exception {
|
||||
foos.addAll(chunk.getItems());
|
||||
}
|
||||
};
|
||||
ItemWriter<String> defaultWriter = new ItemWriter<>() {
|
||||
@Override
|
||||
public void write(Chunk<? extends String> chunk) throws Exception {
|
||||
defaults.addAll(chunk.getItems());
|
||||
}
|
||||
};
|
||||
ItemWriter<String> fooWriter = chunk -> foos.addAll(chunk.getItems());
|
||||
ItemWriter<String> defaultWriter = chunk -> defaults.addAll(chunk.getItems());
|
||||
map.put("foo", fooWriter);
|
||||
map.put("*", defaultWriter);
|
||||
writer.setClassifier(new PatternMatchingClassifier(map));
|
||||
|
||||
@@ -43,18 +43,8 @@ class ClassifierCompositeItemWriterBuilderTests {
|
||||
@Test
|
||||
void testWrite() throws Exception {
|
||||
Map<String, ItemWriter<? super String>> map = new HashMap<>();
|
||||
ItemWriter<String> fooWriter = new ItemWriter<>() {
|
||||
@Override
|
||||
public void write(Chunk<? extends String> chunk) throws Exception {
|
||||
foos.addAll(chunk.getItems());
|
||||
}
|
||||
};
|
||||
ItemWriter<String> defaultWriter = new ItemWriter<>() {
|
||||
@Override
|
||||
public void write(Chunk<? extends String> chunk) throws Exception {
|
||||
defaults.addAll(chunk.getItems());
|
||||
}
|
||||
};
|
||||
ItemWriter<String> fooWriter = chunk -> foos.addAll(chunk.getItems());
|
||||
ItemWriter<String> defaultWriter = chunk -> defaults.addAll(chunk.getItems());
|
||||
map.put("foo", fooWriter);
|
||||
map.put("*", defaultWriter);
|
||||
ClassifierCompositeItemWriter<String> writer = new ClassifierCompositeItemWriterBuilder<String>()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2022 the original author or authors.
|
||||
* Copyright 2010-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.
|
||||
@@ -37,7 +37,6 @@ 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;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -71,9 +70,8 @@ abstract class AbstractStaxEventWriterItemWriterTests {
|
||||
StopWatch stopWatch = new StopWatch(getClass().getSimpleName());
|
||||
stopWatch.start();
|
||||
for (int i = 0; i < MAX_WRITE; i++) {
|
||||
new TransactionTemplate(new ResourcelessTransactionManager()).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
new TransactionTemplate(new ResourcelessTransactionManager())
|
||||
.execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write(objects);
|
||||
}
|
||||
@@ -84,8 +82,7 @@ abstract class AbstractStaxEventWriterItemWriterTests {
|
||||
throw new IllegalStateException("Exception encountered on write", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
writer.close();
|
||||
stopWatch.stop();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2022 the original author or authors.
|
||||
* Copyright 2010-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.
|
||||
@@ -39,7 +39,6 @@ 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;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -75,9 +74,8 @@ class Jaxb2NamespaceMarshallingTests {
|
||||
StopWatch stopWatch = new StopWatch(getClass().getSimpleName());
|
||||
stopWatch.start();
|
||||
for (int i = 0; i < MAX_WRITE; i++) {
|
||||
new TransactionTemplate(new ResourcelessTransactionManager()).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
new TransactionTemplate(new ResourcelessTransactionManager())
|
||||
.execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write(objects);
|
||||
}
|
||||
@@ -88,8 +86,7 @@ class Jaxb2NamespaceMarshallingTests {
|
||||
throw new IllegalStateException("Exception encountered on write", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
writer.close();
|
||||
stopWatch.stop();
|
||||
|
||||
@@ -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.
|
||||
@@ -20,7 +20,6 @@ import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.xml.stream.XMLEventFactory;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.transform.Result;
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
@@ -40,7 +39,6 @@ import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -224,39 +222,33 @@ class StaxEventItemWriterTests {
|
||||
|
||||
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
// write item
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
// write item
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
});
|
||||
writer.close();
|
||||
|
||||
// create new writer from saved restart data and continue writing
|
||||
writer = createItemWriter();
|
||||
writer.open(executionContext);
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
});
|
||||
writer.close();
|
||||
|
||||
@@ -285,20 +277,17 @@ class StaxEventItemWriterTests {
|
||||
|
||||
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
// write item
|
||||
writer.write(itemsMultiByte);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
// write item
|
||||
writer.write(itemsMultiByte);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
});
|
||||
writer.close();
|
||||
|
||||
@@ -306,19 +295,16 @@ class StaxEventItemWriterTests {
|
||||
writer = createItemWriter();
|
||||
writer.setEncoding(encoding);
|
||||
writer.open(executionContext);
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(itemsMultiByte);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write(itemsMultiByte);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
});
|
||||
writer.close();
|
||||
|
||||
@@ -336,17 +322,14 @@ class StaxEventItemWriterTests {
|
||||
|
||||
writer.open(executionContext);
|
||||
try {
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Could not write data", e);
|
||||
}
|
||||
throw new UnexpectedInputException("Could not write data");
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Could not write data", e);
|
||||
}
|
||||
throw new UnexpectedInputException("Could not write data");
|
||||
});
|
||||
}
|
||||
catch (UnexpectedInputException e) {
|
||||
@@ -358,20 +341,17 @@ class StaxEventItemWriterTests {
|
||||
|
||||
// create new writer from saved restart data and continue writing
|
||||
writer = createItemWriter();
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
writer.open(executionContext);
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
writer.open(executionContext);
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UnexpectedInputException("Could not write data", e);
|
||||
}
|
||||
// get restart data
|
||||
writer.update(executionContext);
|
||||
return null;
|
||||
});
|
||||
writer.close();
|
||||
|
||||
@@ -389,19 +369,14 @@ class StaxEventItemWriterTests {
|
||||
@Test
|
||||
void testWriteWithHeader() throws Exception {
|
||||
|
||||
writer.setHeaderCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setHeaderCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -435,35 +410,25 @@ class StaxEventItemWriterTests {
|
||||
*/
|
||||
@Test
|
||||
void testOpenAndClose() throws Exception {
|
||||
writer.setHeaderCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setHeaderCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
writer.setFooterCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "footer"));
|
||||
writer.add(factory.createEndElement("", "", "footer"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setFooterCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "footer"));
|
||||
writer.add(factory.createEndElement("", "", "footer"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -524,35 +489,25 @@ class StaxEventItemWriterTests {
|
||||
@Test
|
||||
void testDeleteIfEmptyNoRecordsWrittenHeaderAndFooter() throws Exception {
|
||||
writer.setShouldDeleteIfEmpty(true);
|
||||
writer.setHeaderCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setHeaderCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
writer.setFooterCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "footer"));
|
||||
writer.add(factory.createEndElement("", "", "footer"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setFooterCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "footer"));
|
||||
writer.add(factory.createEndElement("", "", "footer"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -608,35 +563,25 @@ class StaxEventItemWriterTests {
|
||||
@Test
|
||||
void testDeleteIfEmptyNoRecordsWrittenHeaderAndFooterRestartAfterDelete() throws Exception {
|
||||
writer.setShouldDeleteIfEmpty(true);
|
||||
writer.setHeaderCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setHeaderCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
writer.setFooterCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "footer"));
|
||||
writer.add(factory.createEndElement("", "", "footer"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setFooterCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "footer"));
|
||||
writer.add(factory.createEndElement("", "", "footer"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -888,32 +833,22 @@ class StaxEventItemWriterTests {
|
||||
|
||||
private void initWriterForSimpleCallbackTests() throws Exception {
|
||||
writer = createItemWriter();
|
||||
writer.setHeaderCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("ns", "https://www.springframework.org/test", "group"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
writer.setHeaderCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("ns", "https://www.springframework.org/test", "group"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
writer.setFooterCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createEndElement("ns", "https://www.springframework.org/test", "group"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setFooterCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createEndElement("ns", "https://www.springframework.org/test", "group"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -925,46 +860,36 @@ class StaxEventItemWriterTests {
|
||||
// header- and footer elements
|
||||
private void initWriterForComplexCallbackTests() throws Exception {
|
||||
writer = createItemWriter();
|
||||
writer.setHeaderCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "preHeader"));
|
||||
writer.add(factory.createCharacters("PRE-HEADER"));
|
||||
writer.add(factory.createEndElement("", "", "preHeader"));
|
||||
writer.add(factory.createStartElement("ns", "https://www.springframework.org/test", "group"));
|
||||
writer.add(factory.createStartElement("", "", "subGroup"));
|
||||
writer.add(factory.createStartElement("", "", "postHeader"));
|
||||
writer.add(factory.createCharacters("POST-HEADER"));
|
||||
writer.add(factory.createEndElement("", "", "postHeader"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
writer.setHeaderCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "preHeader"));
|
||||
writer.add(factory.createCharacters("PRE-HEADER"));
|
||||
writer.add(factory.createEndElement("", "", "preHeader"));
|
||||
writer.add(factory.createStartElement("ns", "https://www.springframework.org/test", "group"));
|
||||
writer.add(factory.createStartElement("", "", "subGroup"));
|
||||
writer.add(factory.createStartElement("", "", "postHeader"));
|
||||
writer.add(factory.createCharacters("POST-HEADER"));
|
||||
writer.add(factory.createEndElement("", "", "postHeader"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
writer.setFooterCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "preFooter"));
|
||||
writer.add(factory.createCharacters("PRE-FOOTER"));
|
||||
writer.add(factory.createEndElement("", "", "preFooter"));
|
||||
writer.add(factory.createEndElement("", "", "subGroup"));
|
||||
writer.add(factory.createEndElement("ns", "https://www.springframework.org/test", "group"));
|
||||
writer.add(factory.createStartElement("", "", "postFooter"));
|
||||
writer.add(factory.createCharacters("POST-FOOTER"));
|
||||
writer.add(factory.createEndElement("", "", "postFooter"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setFooterCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "preFooter"));
|
||||
writer.add(factory.createCharacters("PRE-FOOTER"));
|
||||
writer.add(factory.createEndElement("", "", "preFooter"));
|
||||
writer.add(factory.createEndElement("", "", "subGroup"));
|
||||
writer.add(factory.createEndElement("ns", "https://www.springframework.org/test", "group"));
|
||||
writer.add(factory.createStartElement("", "", "postFooter"));
|
||||
writer.add(factory.createCharacters("POST-FOOTER"));
|
||||
writer.add(factory.createEndElement("", "", "postFooter"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@@ -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.
|
||||
@@ -19,7 +19,6 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.stream.XMLEventFactory;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.transform.Result;
|
||||
|
||||
@@ -35,7 +34,6 @@ import org.springframework.core.io.WritableResource;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -86,17 +84,14 @@ class TransactionalStaxEventItemWriterTests {
|
||||
@Test
|
||||
void testWriteAndFlush() throws Exception {
|
||||
writer.open(executionContext);
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
writer.close();
|
||||
String content = outputFileContent();
|
||||
@@ -108,19 +103,14 @@ class TransactionalStaxEventItemWriterTests {
|
||||
*/
|
||||
@Test
|
||||
void testWriteWithHeaderAfterRollback() throws Exception {
|
||||
writer.setHeaderCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setHeaderCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -137,17 +127,14 @@ class TransactionalStaxEventItemWriterTests {
|
||||
}));
|
||||
writer.close();
|
||||
writer.open(executionContext);
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
writer.close();
|
||||
String content = outputFileContent();
|
||||
@@ -160,34 +147,26 @@ class TransactionalStaxEventItemWriterTests {
|
||||
*/
|
||||
@Test
|
||||
void testWriteWithHeaderAfterFlushAndRollback() throws Exception {
|
||||
writer.setHeaderCallback(new StaxWriterCallback() {
|
||||
|
||||
@Override
|
||||
public void write(XMLEventWriter writer) throws IOException {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
writer.setHeaderCallback(writer -> {
|
||||
XMLEventFactory factory = XMLEventFactory.newInstance();
|
||||
try {
|
||||
writer.add(factory.createStartElement("", "", "header"));
|
||||
writer.add(factory.createEndElement("", "", "header"));
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
writer.open(executionContext);
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
writer.update(executionContext);
|
||||
writer.close();
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Mahmoud Ben Hassine
|
||||
*
|
||||
*/
|
||||
class DirectPollerTests {
|
||||
@@ -38,17 +39,12 @@ class DirectPollerTests {
|
||||
@Test
|
||||
void testSimpleSingleThreaded() throws Exception {
|
||||
|
||||
Callable<String> callback = new Callable<>() {
|
||||
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
Set<String> executions = new HashSet<>(repository);
|
||||
if (executions.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return executions.iterator().next();
|
||||
Callable<String> callback = () -> {
|
||||
Set<String> executions = new HashSet<>(repository);
|
||||
if (executions.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return executions.iterator().next();
|
||||
};
|
||||
|
||||
sleepAndCreateStringInBackground(500L);
|
||||
@@ -63,17 +59,12 @@ class DirectPollerTests {
|
||||
@Test
|
||||
void testTimeUnit() throws Exception {
|
||||
|
||||
Callable<String> callback = new Callable<>() {
|
||||
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
Set<String> executions = new HashSet<>(repository);
|
||||
if (executions.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return executions.iterator().next();
|
||||
Callable<String> callback = () -> {
|
||||
Set<String> executions = new HashSet<>(repository);
|
||||
if (executions.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return executions.iterator().next();
|
||||
};
|
||||
|
||||
sleepAndCreateStringInBackground(500L);
|
||||
@@ -88,17 +79,12 @@ class DirectPollerTests {
|
||||
@Test
|
||||
void testWithError() {
|
||||
|
||||
Callable<String> callback = new Callable<>() {
|
||||
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
Set<String> executions = new HashSet<>(repository);
|
||||
if (executions.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
throw new RuntimeException("Expected");
|
||||
Callable<String> callback = () -> {
|
||||
Set<String> executions = new HashSet<>(repository);
|
||||
if (executions.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new RuntimeException("Expected");
|
||||
};
|
||||
|
||||
Poller<String> poller = new DirectPoller<>(100L);
|
||||
@@ -111,16 +97,13 @@ class DirectPollerTests {
|
||||
}
|
||||
|
||||
private void sleepAndCreateStringInBackground(final long duration) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(duration);
|
||||
repository.add("foo");
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Unexpected");
|
||||
}
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(duration);
|
||||
repository.add("foo");
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Unexpected");
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -18,8 +18,6 @@ package org.springframework.batch.repeat.callback;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.batch.repeat.RepeatCallback;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.support.RepeatTemplate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -31,12 +29,9 @@ class NestedRepeatCallbackTests {
|
||||
|
||||
@Test
|
||||
void testExecute() throws Exception {
|
||||
NestedRepeatCallback callback = new NestedRepeatCallback(new RepeatTemplate(), new RepeatCallback() {
|
||||
@Override
|
||||
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
|
||||
count++;
|
||||
return RepeatStatus.continueIf(count <= 1);
|
||||
}
|
||||
NestedRepeatCallback callback = new NestedRepeatCallback(new RepeatTemplate(), context -> {
|
||||
count++;
|
||||
return RepeatStatus.continueIf(count <= 1);
|
||||
});
|
||||
RepeatStatus result = callback.doInIteration(null);
|
||||
assertEquals(2, count);
|
||||
|
||||
@@ -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.
|
||||
@@ -20,7 +20,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -37,17 +36,8 @@ class CompositeExceptionHandlerTests {
|
||||
@Test
|
||||
void testDelegation() throws Throwable {
|
||||
final List<String> list = new ArrayList<>();
|
||||
handler.setHandlers(new ExceptionHandler[] { new ExceptionHandler() {
|
||||
@Override
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws RuntimeException {
|
||||
list.add("1");
|
||||
}
|
||||
}, new ExceptionHandler() {
|
||||
@Override
|
||||
public void handleException(RepeatContext context, Throwable throwable) throws RuntimeException {
|
||||
list.add("2");
|
||||
}
|
||||
} });
|
||||
handler.setHandlers(new ExceptionHandler[] { (context, throwable) -> list.add("1"),
|
||||
(context, throwable) -> list.add("2") });
|
||||
handler.handleException(null, new RuntimeException());
|
||||
assertEquals(2, list.size());
|
||||
assertEquals("1", list.get(0));
|
||||
|
||||
@@ -28,7 +28,6 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.batch.repeat.RepeatCallback;
|
||||
import org.springframework.batch.repeat.RepeatException;
|
||||
import org.springframework.batch.repeat.RepeatOperations;
|
||||
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
|
||||
@@ -75,18 +74,15 @@ class RepeatOperationsInterceptorTests {
|
||||
@Test
|
||||
void testSetTemplate() throws Exception {
|
||||
final List<Object> calls = new ArrayList<>();
|
||||
interceptor.setRepeatOperations(new RepeatOperations() {
|
||||
@Override
|
||||
public RepeatStatus iterate(RepeatCallback callback) {
|
||||
try {
|
||||
Object result = callback.doInIteration(null);
|
||||
calls.add(result);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RepeatException("Encountered exception in repeat.", e);
|
||||
}
|
||||
return RepeatStatus.CONTINUABLE;
|
||||
interceptor.setRepeatOperations(callback -> {
|
||||
try {
|
||||
Object result = callback.doInIteration(null);
|
||||
calls.add(result);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RepeatException("Encountered exception in repeat.", e);
|
||||
}
|
||||
return RepeatStatus.CONTINUABLE;
|
||||
});
|
||||
((Advised) service).addAdvice(interceptor);
|
||||
service.service();
|
||||
@@ -96,12 +92,9 @@ class RepeatOperationsInterceptorTests {
|
||||
@Test
|
||||
void testCallbackNotExecuted() {
|
||||
final List<Object> calls = new ArrayList<>();
|
||||
interceptor.setRepeatOperations(new RepeatOperations() {
|
||||
@Override
|
||||
public RepeatStatus iterate(RepeatCallback callback) {
|
||||
calls.add(null);
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
interceptor.setRepeatOperations(callback -> {
|
||||
calls.add(null);
|
||||
return RepeatStatus.FINISHED;
|
||||
});
|
||||
((Advised) service).addAdvice(interceptor);
|
||||
Exception exception = assertThrows(IllegalStateException.class, service::service);
|
||||
@@ -161,12 +154,9 @@ class RepeatOperationsInterceptorTests {
|
||||
void testInterceptorChainWithRetry() throws Exception {
|
||||
((Advised) service).addAdvice(interceptor);
|
||||
final List<Object> list = new ArrayList<>();
|
||||
((Advised) service).addAdvice(new MethodInterceptor() {
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
list.add("chain");
|
||||
return invocation.proceed();
|
||||
}
|
||||
((Advised) service).addAdvice((MethodInterceptor) invocation -> {
|
||||
list.add("chain");
|
||||
return invocation.proceed();
|
||||
});
|
||||
RepeatTemplate template = new RepeatTemplate();
|
||||
template.setCompletionPolicy(new SimpleCompletionPolicy(2));
|
||||
|
||||
@@ -245,15 +245,12 @@ class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
|
||||
void testNestedSession() {
|
||||
RepeatTemplate outer = getRepeatTemplate();
|
||||
RepeatTemplate inner = getRepeatTemplate();
|
||||
outer.iterate(new NestedRepeatCallback(inner, new RepeatCallback() {
|
||||
@Override
|
||||
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
|
||||
count++;
|
||||
assertNotNull(context);
|
||||
assertNotSame(context, context.getParent(), "Nested batch should have new session");
|
||||
assertSame(context, RepeatSynchronizationManager.getContext());
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
outer.iterate(new NestedRepeatCallback(inner, context -> {
|
||||
count++;
|
||||
assertNotNull(context);
|
||||
assertNotSame(context, context.getParent(), "Nested batch should have new session");
|
||||
assertSame(context, RepeatSynchronizationManager.getContext());
|
||||
return RepeatStatus.FINISHED;
|
||||
}) {
|
||||
@Override
|
||||
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
|
||||
@@ -269,14 +266,11 @@ class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
|
||||
void testNestedSessionTerminatesBeforeIteration() {
|
||||
RepeatTemplate outer = getRepeatTemplate();
|
||||
RepeatTemplate inner = getRepeatTemplate();
|
||||
outer.iterate(new NestedRepeatCallback(inner, new RepeatCallback() {
|
||||
@Override
|
||||
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
|
||||
count++;
|
||||
assertEquals(2, count);
|
||||
fail("Nested batch should not have been executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
outer.iterate(new NestedRepeatCallback(inner, context -> {
|
||||
count++;
|
||||
assertEquals(2, count);
|
||||
fail("Nested batch should not have been executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
}) {
|
||||
@Override
|
||||
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
|
||||
@@ -293,15 +287,12 @@ class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
|
||||
RepeatTemplate outer = getRepeatTemplate();
|
||||
outer.setCompletionPolicy(new SimpleCompletionPolicy(2));
|
||||
RepeatTemplate inner = getRepeatTemplate();
|
||||
outer.iterate(new NestedRepeatCallback(inner, new RepeatCallback() {
|
||||
@Override
|
||||
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
|
||||
count++;
|
||||
assertNotNull(context);
|
||||
assertNotSame(context, context.getParent(), "Nested batch should have new session");
|
||||
assertSame(context, RepeatSynchronizationManager.getContext());
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
outer.iterate(new NestedRepeatCallback(inner, context -> {
|
||||
count++;
|
||||
assertNotNull(context);
|
||||
assertNotSame(context, context.getParent(), "Nested batch should have new session");
|
||||
assertSame(context, RepeatSynchronizationManager.getContext());
|
||||
return RepeatStatus.FINISHED;
|
||||
}) {
|
||||
@Override
|
||||
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
|
||||
|
||||
@@ -108,15 +108,12 @@ class TaskExecutorRepeatTemplateAsynchronousTests extends AbstractTradeBatchTest
|
||||
RepeatTemplate outer = getRepeatTemplate();
|
||||
RepeatTemplate inner = new RepeatTemplate();
|
||||
|
||||
outer.iterate(new NestedRepeatCallback(inner, new RepeatCallback() {
|
||||
@Override
|
||||
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
|
||||
count++;
|
||||
assertNotNull(context);
|
||||
assertNotSame(context, context.getParent(), "Nested batch should have new session");
|
||||
assertSame(context, RepeatSynchronizationManager.getContext());
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
outer.iterate(new NestedRepeatCallback(inner, context -> {
|
||||
count++;
|
||||
assertNotNull(context);
|
||||
assertNotSame(context, context.getParent(), "Nested batch should have new session");
|
||||
assertSame(context, RepeatSynchronizationManager.getContext());
|
||||
return RepeatStatus.FINISHED;
|
||||
}) {
|
||||
@Override
|
||||
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
|
||||
|
||||
@@ -25,7 +25,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletionService;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorCompletionService;
|
||||
@@ -41,7 +40,6 @@ import org.junit.jupiter.api.condition.DisabledOnOs;
|
||||
import org.junit.jupiter.api.condition.OS;
|
||||
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -110,12 +108,7 @@ class ConcurrentTransactionAwareProxyTests {
|
||||
@Test
|
||||
void testTransactionalContains() {
|
||||
final Map<Long, Map<String, String>> map = TransactionAwareProxyFactory.createAppendOnlyTransactionalMap();
|
||||
boolean result = new TransactionTemplate(transactionManager).execute(new TransactionCallback<>() {
|
||||
@Override
|
||||
public Boolean doInTransaction(TransactionStatus status) {
|
||||
return map.containsKey("foo");
|
||||
}
|
||||
});
|
||||
boolean result = new TransactionTemplate(transactionManager).execute(status -> map.containsKey("foo"));
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@@ -124,17 +117,14 @@ class ConcurrentTransactionAwareProxyTests {
|
||||
for (int i = 0; i < outerMax; i++) {
|
||||
|
||||
final int count = i;
|
||||
completionService.submit(new Callable<>() {
|
||||
@Override
|
||||
public List<String> call() throws Exception {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i = 0; i < innerMax; i++) {
|
||||
String value = count + "bar" + i;
|
||||
saveInSetAndAssert(set, value);
|
||||
list.add(value);
|
||||
}
|
||||
return list;
|
||||
completionService.submit(() -> {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i1 = 0; i1 < innerMax; i1++) {
|
||||
String value = count + "bar" + i1;
|
||||
saveInSetAndAssert(set, value);
|
||||
list.add(value);
|
||||
}
|
||||
return list;
|
||||
});
|
||||
|
||||
}
|
||||
@@ -152,24 +142,21 @@ class ConcurrentTransactionAwareProxyTests {
|
||||
|
||||
for (int i = 0; i < outerMax; i++) {
|
||||
|
||||
completionService.submit(new Callable<>() {
|
||||
@Override
|
||||
public List<String> call() throws Exception {
|
||||
List<String> result = new ArrayList<>();
|
||||
for (int i = 0; i < innerMax; i++) {
|
||||
String value = "bar" + i;
|
||||
saveInListAndAssert(list, value);
|
||||
result.add(value);
|
||||
// Need to slow it down to allow threads to interleave
|
||||
Thread.sleep(10L);
|
||||
if (mutate) {
|
||||
list.remove(value);
|
||||
list.add(value);
|
||||
}
|
||||
completionService.submit(() -> {
|
||||
List<String> result = new ArrayList<>();
|
||||
for (int i1 = 0; i1 < innerMax; i1++) {
|
||||
String value = "bar" + i1;
|
||||
saveInListAndAssert(list, value);
|
||||
result.add(value);
|
||||
// Need to slow it down to allow threads to interleave
|
||||
Thread.sleep(10L);
|
||||
if (mutate) {
|
||||
list.remove(value);
|
||||
list.add(value);
|
||||
}
|
||||
logger.info("Added: " + innerMax + " values");
|
||||
return result;
|
||||
}
|
||||
logger.info("Added: " + innerMax + " values");
|
||||
return result;
|
||||
});
|
||||
|
||||
}
|
||||
@@ -192,16 +179,13 @@ class ConcurrentTransactionAwareProxyTests {
|
||||
for (int j = 0; j < numberOfKeys; j++) {
|
||||
final long id = j * 1000 + 123L + i;
|
||||
|
||||
completionService.submit(new Callable<>() {
|
||||
@Override
|
||||
public List<String> call() throws Exception {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i = 0; i < innerMax; i++) {
|
||||
String value = "bar" + i;
|
||||
list.add(saveInMapAndAssert(map, id, value).get("foo"));
|
||||
}
|
||||
return list;
|
||||
completionService.submit(() -> {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i1 = 0; i1 < innerMax; i1++) {
|
||||
String value = "bar" + i1;
|
||||
list.add(saveInMapAndAssert(map, id, value).get("foo"));
|
||||
}
|
||||
return list;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -215,12 +199,9 @@ class ConcurrentTransactionAwareProxyTests {
|
||||
|
||||
private String saveInSetAndAssert(final Set<String> set, final String value) {
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
set.add(value);
|
||||
return null;
|
||||
}
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
set.add(value);
|
||||
return null;
|
||||
});
|
||||
|
||||
Assert.state(set.contains(value), "Lost update: value=" + value);
|
||||
@@ -231,12 +212,9 @@ class ConcurrentTransactionAwareProxyTests {
|
||||
|
||||
private String saveInListAndAssert(final List<String> list, final String value) {
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
list.add(value);
|
||||
return null;
|
||||
}
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
list.add(value);
|
||||
return null;
|
||||
});
|
||||
|
||||
Assert.state(list.contains(value), "Lost update: value=" + value);
|
||||
@@ -248,15 +226,12 @@ class ConcurrentTransactionAwareProxyTests {
|
||||
private Map<String, String> saveInMapAndAssert(final Map<Long, Map<String, String>> map, final Long id,
|
||||
final String value) {
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
if (!map.containsKey(id)) {
|
||||
map.put(id, new HashMap<>());
|
||||
}
|
||||
map.get(id).put("foo", value);
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
if (!map.containsKey(id)) {
|
||||
map.put(id, new HashMap<>());
|
||||
}
|
||||
map.get(id).put("foo", value);
|
||||
return null;
|
||||
});
|
||||
|
||||
Map<String, String> result = map.get(id);
|
||||
|
||||
@@ -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.
|
||||
@@ -26,7 +26,6 @@ import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@@ -66,36 +65,27 @@ class TransactionAwareListFactoryTests {
|
||||
|
||||
@Test
|
||||
void testTransactionalAdd() {
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
testAdd();
|
||||
return null;
|
||||
}
|
||||
transactionTemplate.execute((TransactionCallback<Void>) status -> {
|
||||
testAdd();
|
||||
return null;
|
||||
});
|
||||
assertEquals(4, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTransactionalRemove() {
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
testRemove();
|
||||
return null;
|
||||
}
|
||||
transactionTemplate.execute((TransactionCallback<Void>) status -> {
|
||||
testRemove();
|
||||
return null;
|
||||
});
|
||||
assertEquals(2, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTransactionalClear() {
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
testClear();
|
||||
return null;
|
||||
}
|
||||
transactionTemplate.execute((TransactionCallback<Void>) status -> {
|
||||
testClear();
|
||||
return null;
|
||||
});
|
||||
assertEquals(0, list.size());
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -21,7 +21,6 @@ import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@@ -84,60 +83,45 @@ class TransactionAwareMapFactoryTests {
|
||||
|
||||
@Test
|
||||
void testTransactionalAdd() {
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
testAdd();
|
||||
return null;
|
||||
}
|
||||
transactionTemplate.execute((TransactionCallback<Void>) status -> {
|
||||
testAdd();
|
||||
return null;
|
||||
});
|
||||
assertEquals(4, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTransactionalEmpty() {
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
testEmpty();
|
||||
return null;
|
||||
}
|
||||
transactionTemplate.execute((TransactionCallback<Void>) status -> {
|
||||
testEmpty();
|
||||
return null;
|
||||
});
|
||||
assertEquals(4, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTransactionalValues() {
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
testValues();
|
||||
return null;
|
||||
}
|
||||
transactionTemplate.execute((TransactionCallback<Void>) status -> {
|
||||
testValues();
|
||||
return null;
|
||||
});
|
||||
assertEquals(4, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTransactionalRemove() {
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
testRemove();
|
||||
return null;
|
||||
}
|
||||
transactionTemplate.execute((TransactionCallback<Void>) status -> {
|
||||
testRemove();
|
||||
return null;
|
||||
});
|
||||
assertEquals(2, map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTransactionalClear() {
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
testClear();
|
||||
return null;
|
||||
}
|
||||
transactionTemplate.execute((TransactionCallback<Void>) status -> {
|
||||
testClear();
|
||||
return null;
|
||||
});
|
||||
assertEquals(0, map.size());
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ import org.springframework.core.io.Resource;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.support.JdbcTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -125,38 +124,32 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
|
||||
TransactionTemplate transactionTemplate = new TransactionTemplate(new JdbcTransactionManager(dataSource));
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
String[] scripts;
|
||||
try {
|
||||
scripts = StringUtils.delimitedListToStringArray(
|
||||
stripComments(IOUtils.readLines(scriptResource.getInputStream(), "UTF-8")), ";");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
|
||||
}
|
||||
for (String s : scripts) {
|
||||
String script = s.trim();
|
||||
if (StringUtils.hasText(script)) {
|
||||
try {
|
||||
jdbcTemplate.execute(script);
|
||||
transactionTemplate.execute((TransactionCallback<Void>) status -> {
|
||||
String[] scripts;
|
||||
try {
|
||||
scripts = StringUtils.delimitedListToStringArray(
|
||||
stripComments(IOUtils.readLines(scriptResource.getInputStream(), "UTF-8")), ";");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
|
||||
}
|
||||
for (String s : scripts) {
|
||||
String script = s.trim();
|
||||
if (StringUtils.hasText(script)) {
|
||||
try {
|
||||
jdbcTemplate.execute(script);
|
||||
}
|
||||
catch (DataAccessException e) {
|
||||
if (ignoreFailedDrop && script.toLowerCase().startsWith("drop")) {
|
||||
logger.debug("DROP script failed (ignoring): " + script);
|
||||
}
|
||||
catch (DataAccessException e) {
|
||||
if (ignoreFailedDrop && script.toLowerCase().startsWith("drop")) {
|
||||
logger.debug("DROP script failed (ignoring): " + script);
|
||||
}
|
||||
else {
|
||||
throw e;
|
||||
}
|
||||
else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user