Skip to content

Commit daf0f98

Browse files
committed
Favor factory methods instead of double braces initialization
Signed-off-by: Stefano Cordio <[email protected]>
1 parent 2bd5b84 commit daf0f98

File tree

9 files changed

+32
-56
lines changed

9 files changed

+32
-56
lines changed

spring-batch-core/src/test/java/org/springframework/batch/core/listener/CompositeItemReadListenerTests.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import static org.mockito.Mockito.mock;
1919

20-
import java.util.ArrayList;
20+
import java.util.List;
2121

2222
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
@@ -67,11 +67,7 @@ void testOnReadError() {
6767

6868
@Test
6969
void testSetListeners() {
70-
compositeListener.setListeners(new ArrayList<>() {
71-
{
72-
add(listener);
73-
}
74-
});
70+
compositeListener.setListeners(List.of(listener));
7571
listener.beforeRead();
7672
compositeListener.beforeRead();
7773
}

spring-batch-core/src/test/java/org/springframework/batch/core/listener/CompositeItemWriteListenerTests.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.batch.core.listener;
1717

18-
import java.util.ArrayList;
18+
import java.util.List;
1919

2020
import org.junit.jupiter.api.BeforeEach;
2121
import org.junit.jupiter.api.Test;
@@ -69,11 +69,7 @@ void testOnWriteError() {
6969

7070
@Test
7171
void testSetListeners() {
72-
compositeListener.setListeners(new ArrayList<>() {
73-
{
74-
add(listener);
75-
}
76-
});
72+
compositeListener.setListeners(List.of(listener));
7773
Chunk<Object> item = Chunk.of(new Object());
7874
listener.beforeWrite(item);
7975
compositeListener.beforeWrite(item);

spring-batch-core/src/test/java/org/springframework/batch/core/step/tasklet/ConfigurableSystemProcessExitCodeMapperTests.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@ class ConfigurableSystemProcessExitCodeMapperTests {
3636
*/
3737
@Test
3838
void testMapping() {
39-
Map<Object, ExitStatus> mappings = new HashMap<>() {
40-
{
41-
put(0, ExitStatus.COMPLETED);
42-
put(1, ExitStatus.FAILED);
43-
put(2, ExitStatus.EXECUTING);
44-
put(3, ExitStatus.NOOP);
45-
put(4, ExitStatus.UNKNOWN);
46-
put(ConfigurableSystemProcessExitCodeMapper.ELSE_KEY, ExitStatus.UNKNOWN);
47-
}
48-
};
39+
Map<Object, ExitStatus> mappings = Map.of( //
40+
0, ExitStatus.COMPLETED, //
41+
1, ExitStatus.FAILED, //
42+
2, ExitStatus.EXECUTING, //
43+
3, ExitStatus.NOOP, //
44+
4, ExitStatus.UNKNOWN, //
45+
ConfigurableSystemProcessExitCodeMapper.ELSE_KEY, ExitStatus.UNKNOWN);
4946

5047
mapper.setMappings(mappings);
5148

spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/exception/SimpleLimitExceptionHandlerTests.java

+13-21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.ArrayList;
2525
import java.util.Collections;
2626
import java.util.List;
27+
import java.util.stream.Collectors;
28+
import java.util.stream.IntStream;
2729

2830
import org.junit.jupiter.api.BeforeEach;
2931
import org.junit.jupiter.api.Test;
@@ -152,19 +154,14 @@ void testExceptionNotThrownBelowLimit() throws Throwable {
152154
handler.setLimit(EXCEPTION_LIMIT);
153155
handler.afterPropertiesSet();
154156

155-
@SuppressWarnings("serial")
156-
List<Throwable> throwables = new ArrayList<>() {
157-
{
158-
for (int i = 0; i < (EXCEPTION_LIMIT); i++) {
159-
add(new RuntimeException("below exception limit"));
160-
}
161-
}
162-
};
157+
List<RuntimeException> exceptions = IntStream.range(0, EXCEPTION_LIMIT)
158+
.mapToObj(__ -> new RuntimeException("below exception limit"))
159+
.toList();
163160

164161
RepeatContextSupport context = new RepeatContextSupport(null);
165162

166-
for (Throwable throwable : throwables) {
167-
assertDoesNotThrow(() -> handler.handleException(context, throwable));
163+
for (RuntimeException exception : exceptions) {
164+
assertDoesNotThrow(() -> handler.handleException(context, exception));
168165
}
169166

170167
}
@@ -180,22 +177,17 @@ void testExceptionThrownAboveLimit() throws Throwable {
180177
handler.setLimit(EXCEPTION_LIMIT);
181178
handler.afterPropertiesSet();
182179

183-
@SuppressWarnings("serial")
184-
List<Throwable> throwables = new ArrayList<>() {
185-
{
186-
for (int i = 0; i < (EXCEPTION_LIMIT); i++) {
187-
add(new RuntimeException("below exception limit"));
188-
}
189-
}
190-
};
180+
List<RuntimeException> exceptions = IntStream.range(0, EXCEPTION_LIMIT)
181+
.mapToObj(__ -> new RuntimeException("below exception limit"))
182+
.collect(Collectors.toCollection(ArrayList::new));
191183

192-
throwables.add(new RuntimeException("above exception limit"));
184+
exceptions.add(new RuntimeException("above exception limit"));
193185

194186
RepeatContextSupport context = new RepeatContextSupport(null);
195187

196188
Exception expected = assertThrows(RuntimeException.class, () -> {
197-
for (Throwable throwable : throwables) {
198-
handler.handleException(context, throwable);
189+
for (Throwable exception : exceptions) {
190+
handler.handleException(context, exception);
199191
}
200192
});
201193
assertEquals("above exception limit", expected.getMessage());

spring-batch-infrastructure/src/test/java/org/springframework/batch/support/transaction/ConcurrentTransactionAwareProxyTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void testConcurrentTransactionalMap() {
108108
@Test
109109
void testTransactionalContains() {
110110
final Map<Long, Map<String, String>> map = TransactionAwareProxyFactory.createAppendOnlyTransactionalMap();
111-
boolean result = new TransactionTemplate(transactionManager).execute(status -> map.containsKey("foo"));
111+
boolean result = new TransactionTemplate(transactionManager).execute(status -> map.containsKey(0L));
112112
assertFalse(result);
113113
}
114114

spring-batch-samples/src/main/java/org/springframework/batch/samples/chunking/ManagerConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public ListItemReader<Integer> itemReader() {
110110
@Bean
111111
public TaskletStep managerStep() {
112112
return this.managerStepBuilderFactory.get("managerStep")
113-
.<Integer, Integer>chunk(3)
113+
.chunk(3)
114114
.reader(itemReader())
115115
.outputChannel(requests())
116116
.inputChannel(replies())

spring-batch-samples/src/test/java/org/springframework/batch/samples/compositewriter/CompositeItemWriterSampleFunctionalTests.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,12 @@ void testJobLaunch() throws Exception {
7474
}
7575

7676
private void checkOutputTable(int before) {
77-
final List<Trade> trades = new ArrayList<>() {
78-
{
79-
add(new Trade("UK21341EAH41", 211, new BigDecimal("31.11"), "customer1"));
80-
add(new Trade("UK21341EAH42", 212, new BigDecimal("32.11"), "customer2"));
81-
add(new Trade("UK21341EAH43", 213, new BigDecimal("33.11"), "customer3"));
82-
add(new Trade("UK21341EAH44", 214, new BigDecimal("34.11"), "customer4"));
83-
add(new Trade("UK21341EAH45", 215, new BigDecimal("35.11"), "customer5"));
84-
}
85-
};
77+
final List<Trade> trades = List.of( //
78+
new Trade("UK21341EAH41", 211, new BigDecimal("31.11"), "customer1"),
79+
new Trade("UK21341EAH42", 212, new BigDecimal("32.11"), "customer2"),
80+
new Trade("UK21341EAH43", 213, new BigDecimal("33.11"), "customer3"),
81+
new Trade("UK21341EAH44", 214, new BigDecimal("34.11"), "customer4"),
82+
new Trade("UK21341EAH45", 215, new BigDecimal("35.11"), "customer5"));
8683

8784
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");
8885

spring-batch-samples/src/test/java/org/springframework/batch/samples/partition/file/PartitionFileJobFunctionalTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ void testUpdateCredit() throws Exception {
8585
int itemCount = inputs.size();
8686
assertTrue(itemCount > 0, "No entries were available in the input");
8787

88-
inputs.iterator();
8988
for (int i = 0; i < itemCount; i++) {
9089
assertEquals(inputs.get(i).getCredit().add(CustomerCreditIncreaseProcessor.FIXED_AMOUNT).intValue(),
9190
outputs.get(i).getCredit().intValue());

spring-batch-samples/src/test/java/org/springframework/batch/samples/partition/jdbc/PartitionJdbcJobFunctionalTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ void testUpdateCredit() throws Exception {
8585
int itemCount = inputs.size();
8686
assertTrue(itemCount > 0, "Input from reader has no entries.");
8787

88-
inputs.iterator();
8988
for (int i = 0; i < itemCount; i++) {
9089
assertEquals(inputs.get(i).getCredit().add(CustomerCreditIncreaseProcessor.FIXED_AMOUNT).intValue(),
9190
outputs.get(i).getCredit().intValue());

0 commit comments

Comments
 (0)