Remove double brace initialization

Remove double brace collection initialization as
it is considered bad practice.
This commit is contained in:
Philippe Marschall
2021-03-13 20:40:09 +01:00
committed by Mahmoud Ben Hassine
parent f15edd414a
commit 3d0cb90590
8 changed files with 38 additions and 101 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.item.data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -201,12 +202,9 @@ public class MongoItemReaderTests {
assertEquals("{ $natural : 1}", query.getHint());
}
@SuppressWarnings("serial")
@Test
public void testQueryWithParameters() {
reader.setParameterValues(new ArrayList<Object>(){{
add("foo");
}});
reader.setParameterValues(Collections.singletonList("foo"));
reader.setQuery("{ name : ?0 }");
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
@@ -222,12 +220,9 @@ public class MongoItemReaderTests {
assertEquals("{\"name\": -1}", query.getSortObject().toJson());
}
@SuppressWarnings("serial")
@Test
public void testQueryWithCollection() {
reader.setParameterValues(new ArrayList<Object>(){{
add("foo");
}});
reader.setParameterValues(Collections.singletonList("foo"));
reader.setQuery("{ name : ?0 }");
reader.setCollection("collection");

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.item.data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -60,7 +61,6 @@ import static org.mockito.ArgumentMatchers.eq;
* @author Parikshit Dutta
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class MongoItemWriterTests {
@Rule
@@ -106,10 +106,7 @@ public class MongoItemWriterTests {
@Test
public void testWriteNoTransactionNoCollection() throws Exception {
List<Item> items = new ArrayList<Item>() {{
add(new Item("Foo"));
add(new Item("Bar"));
}};
List<Item> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
writer.write(items);
@@ -119,10 +116,7 @@ public class MongoItemWriterTests {
@Test
public void testWriteNoTransactionWithCollection() throws Exception {
List<Object> items = new ArrayList<Object>() {{
add(new Item("Foo"));
add(new Item("Bar"));
}};
List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
writer.setCollection("collection");
@@ -142,10 +136,7 @@ public class MongoItemWriterTests {
@Test
public void testWriteTransactionNoCollection() throws Exception {
final List<Object> items = new ArrayList<Object>() {{
add(new Item("Foo"));
add(new Item("Bar"));
}};
final List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
try {
@@ -163,10 +154,7 @@ public class MongoItemWriterTests {
@Test
public void testWriteTransactionWithCollection() throws Exception {
final List<Object> items = new ArrayList<Object>() {{
add(new Item("Foo"));
add(new Item("Bar"));
}};
final List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
writer.setCollection("collection");
@@ -186,10 +174,7 @@ public class MongoItemWriterTests {
@Test
public void testWriteTransactionFails() throws Exception {
final List<Object> items = new ArrayList<Object>() {{
add(new Item("Foo"));
add(new Item("Bar"));
}};
final List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
writer.setCollection("collection");
@@ -218,10 +203,7 @@ public class MongoItemWriterTests {
*/
@Test
public void testWriteTransactionReadOnly() throws Exception {
final List<Object> items = new ArrayList<Object>() {{
add(new Item("Foo"));
add(new Item("Bar"));
}};
final List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
writer.setCollection("collection");
@@ -247,10 +229,7 @@ public class MongoItemWriterTests {
@Test
public void testRemoveNoObjectIdNoCollection() throws Exception {
writer.setDelete(true);
List<Object> items = new ArrayList<Object>() {{
add(new Item("Foo"));
add(new Item("Bar"));
}};
List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
writer.write(items);
@@ -261,10 +240,7 @@ public class MongoItemWriterTests {
@Test
public void testRemoveNoObjectIdWithCollection() throws Exception {
writer.setDelete(true);
List<Object> items = new ArrayList<Object>() {{
add(new Item("Foo"));
add(new Item("Bar"));
}};
List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
writer.setCollection("collection");
writer.write(items);
@@ -276,10 +252,7 @@ public class MongoItemWriterTests {
@Test
public void testRemoveNoTransactionNoCollection() throws Exception {
writer.setDelete(true);
List<Object> items = new ArrayList<Object>() {{
add(new Item(1));
add(new Item(2));
}};
List<Object> items = Arrays.asList(new Item(1), new Item(2));
writer.write(items);
@@ -290,10 +263,7 @@ public class MongoItemWriterTests {
@Test
public void testRemoveNoTransactionWithCollection() throws Exception {
writer.setDelete(true);
List<Object> items = new ArrayList<Object>() {{
add(new Item(1));
add(new Item(2));
}};
List<Object> items = Arrays.asList(new Item(1), new Item(2));
writer.setCollection("collection");

View File

@@ -16,7 +16,6 @@
package org.springframework.batch.item.database;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.hamcrest.BaseMatcher;
@@ -131,7 +130,7 @@ public class JdbcBatchItemWriterNamedParameterTests {
writer.write(Collections.singletonList(new Foo("bar")));
}
@SuppressWarnings({ "rawtypes", "serial", "unchecked" })
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testWriteAndFlushMap() throws Exception {
JdbcBatchItemWriter<Map<String, Object>> mapWriter = new JdbcBatchItemWriter<>();
@@ -145,14 +144,13 @@ public class JdbcBatchItemWriterNamedParameterTests {
when(namedParameterJdbcOperations.batchUpdate(eq(sql),
captor.capture()))
.thenReturn(new int[] {1});
mapWriter.write(Collections.singletonList(new HashMap<String, Object>() {{put("foo", "bar");}}));
mapWriter.write(Collections.singletonList(Collections.singletonMap("foo", "bar")));
assertEquals(1, captor.getValue().length);
Map<String, Object> results = captor.getValue()[0];
assertEquals("bar", results.get("foo"));
}
@SuppressWarnings( "serial" )
@Test
public void testWriteAndFlushMapWithItemSqlParameterSourceProvider() throws Exception {
JdbcBatchItemWriter<Map<String, Object>> mapWriter = new JdbcBatchItemWriter<>();
@@ -172,7 +170,7 @@ public class JdbcBatchItemWriterNamedParameterTests {
when(namedParameterJdbcOperations.batchUpdate(any(String.class),
captor.capture()))
.thenReturn(new int[] {1});
mapWriter.write(Collections.singletonList(new HashMap<String, Object>() {{put("foo", "bar");}}));
mapWriter.write(Collections.singletonList(Collections.singletonMap("foo", "bar")));
assertEquals(1, captor.getValue().length);
SqlParameterSource results = captor.getValue()[0];

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2013 the original author or authors.
* Copyright 2008-2021 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.
@@ -22,6 +22,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Before;
@@ -41,15 +42,13 @@ public class CompositeItemProcessorTests {
private ItemProcessor<Object, Object> processor1;
private ItemProcessor<Object, Object> processor2;
@SuppressWarnings({ "unchecked", "serial" })
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
processor1 = mock(ItemProcessor.class);
processor2 = mock(ItemProcessor.class);
composite.setDelegates(new ArrayList<ItemProcessor<Object,Object>>() {{
add(processor1); add(processor2);
}});
composite.setDelegates(Arrays.asList(processor1, processor2));
composite.afterPropertiesSet();
}
@@ -76,14 +75,13 @@ public class CompositeItemProcessorTests {
* Test that the CompositeItemProcessor can work with generic types for the ItemProcessor delegates.
*/
@Test
@SuppressWarnings({"unchecked", "serial"})
@SuppressWarnings("unchecked")
public void testItemProcessorGenerics() throws Exception {
CompositeItemProcessor<String, String> composite = new CompositeItemProcessor<>();
final ItemProcessor<String, Integer> processor1 = mock(ItemProcessor.class);
final ItemProcessor<Integer, String> processor2 = mock(ItemProcessor.class);
composite.setDelegates(new ArrayList<ItemProcessor<?,?>>() {{
add(processor1); add(processor2);
}});
composite.setDelegates(Arrays.asList(processor1, processor2));
composite.afterPropertiesSet();
when(processor1.process("input")).thenReturn(5);

View File

@@ -16,7 +16,7 @@
package org.springframework.batch.jsr.item;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.batch.api.chunk.ItemReader;
@@ -112,16 +112,11 @@ public class ItemReaderAdapterTests {
}
@Test
@SuppressWarnings("serial")
public void testCheckpointChange() throws Exception {
ItemReaderAdapter<String> adapter = new ItemReaderAdapter<>(new ItemReader() {
private CheckpointContainer container = null;
private List<String> items = new ArrayList<String>() {{
add("foo");
add("bar");
add("baz");
}};
private List<String> items = Arrays.asList("foo", "bar", "baz");
@Override
public Object readItem() throws Exception {