From 7d184aa93f9a868b47ec2db27454c8b5cbe7a76f Mon Sep 17 00:00:00 2001 From: Philippe Marschall Date: Sat, 13 Mar 2021 20:40:09 +0100 Subject: [PATCH] Remove double brace initialization Remove double brace collection initialization as it is considered bad practice. --- .../support/BatchPropertyContext.java | 11 ++-- .../support/BatchPropertyContextTests.java | 15 ++---- .../core/step/builder/StepBuilderTests.java | 19 ++----- .../batch/item/data/MongoItemReaderTests.java | 13 ++--- .../batch/item/data/MongoItemWriterTests.java | 54 +++++-------------- ...dbcBatchItemWriterNamedParameterTests.java | 10 ++-- .../support/CompositeItemProcessorTests.java | 16 +++--- .../jsr/item/ItemReaderAdapterTests.java | 11 ++-- 8 files changed, 43 insertions(+), 106 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyContext.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyContext.java index 027e8932e4..e91116ecb8 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyContext.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-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. @@ -218,7 +218,6 @@ public Properties getStepArtifactProperties(String stepName, String artifactName * * @param properties the step artifact {@link Properties} to add */ - @SuppressWarnings("serial") public void setStepArtifactProperties(Map> properties) { Assert.notNull(properties, "Step artifact properties cannot be null"); @@ -232,12 +231,10 @@ public void setStepArtifactProperties(Map> prope Map artifactProperties = stepArtifactProperties.get(stepName); if (artifactProperties == null) { - stepArtifactProperties.put(stepName, new HashMap() {{ - put(artifactName, props); - }}); - } else { - artifactProperties.put(artifactName, props); + artifactProperties = new HashMap<>(); + stepArtifactProperties.put(stepName, artifactProperties); } + artifactProperties.put(artifactName, props); } } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyContextTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyContextTests.java index 8c47bd41ce..53aa509822 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyContextTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-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. @@ -17,6 +17,7 @@ import static org.junit.Assert.assertEquals; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -65,25 +66,19 @@ public void setUp() { stepArtifactProperties.setProperty("readerProperty1", "readerProperty1value"); stepArtifactProperties.setProperty("readerProperty2", "readerProperty2value"); - this.stepArtifactProperties.put("step1", new HashMap() {{ - put("reader", stepArtifactProperties); - }}); + this.stepArtifactProperties.put("step1", Collections.singletonMap("reader", stepArtifactProperties)); final Properties partitionProperties = new Properties(); partitionProperties.setProperty("writerProperty1", "writerProperty1valuePartition0"); partitionProperties.setProperty("writerProperty2", "writerProperty2valuePartition0"); - this.partitionProperties.put("step2:partition0", new HashMap() {{ - put("writer", partitionProperties); - }}); + this.partitionProperties.put("step2:partition0", Collections.singletonMap("writer", partitionProperties)); final Properties partitionStepProperties = new Properties(); partitionStepProperties.setProperty("writerProperty1Step", "writerProperty1"); partitionStepProperties.setProperty("writerProperty2Step", "writerProperty2"); - this.partitionProperties.put("step2", new HashMap() {{ - put("writer", partitionStepProperties); - }}); + this.partitionProperties.put("step2", Collections.singletonMap("writer", partitionStepProperties)); } @Test diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/StepBuilderTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/StepBuilderTests.java index 81f2f073d0..70ea44144b 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/StepBuilderTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/StepBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-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. @@ -15,7 +15,7 @@ */ package org.springframework.batch.core.step.builder; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.function.Function; @@ -58,7 +58,6 @@ * @author Mahmoud Ben Hassine * */ -@SuppressWarnings("serial") public class StepBuilderTests { @Test @@ -180,15 +179,10 @@ public void testItemListeners() throws Exception { jobRepository.add(execution); PlatformTransactionManager transactionManager = new ResourcelessTransactionManager(); - List items = new ArrayList() {{ - add("1"); - add("2"); - add("3"); - }}; + List items = Arrays.asList("1", "2", "3"); ItemReader reader = new ListItemReader<>(items); - @SuppressWarnings("unchecked") SimpleStepBuilder builder = new StepBuilder("step") .repository(jobRepository) .transactionManager(transactionManager) @@ -219,16 +213,11 @@ public void testFunctions() throws Exception { jobRepository.add(execution); PlatformTransactionManager transactionManager = new ResourcelessTransactionManager(); - List items = new ArrayList() {{ - add(1L); - add(2L); - add(3L); - }}; + List items = Arrays.asList(1L, 2L, 3L); ItemReader reader = new ListItemReader<>(items); ListItemWriter itemWriter = new ListItemWriter<>(); - @SuppressWarnings("unchecked") SimpleStepBuilder builder = new StepBuilder("step") .repository(jobRepository) .transactionManager(transactionManager) diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemReaderTests.java index c01f9018d0..71567b99ee 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemReaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2020 the original author or authors. + * Copyright 2013-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. @@ -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; @@ -198,12 +199,9 @@ public void testQueryWithHint() { assertEquals("{ $natural : 1}", query.getHint()); } - @SuppressWarnings("serial") @Test public void testQueryWithParameters() { - reader.setParameterValues(new ArrayList(){{ - add("foo"); - }}); + reader.setParameterValues(Collections.singletonList("foo")); reader.setQuery("{ name : ?0 }"); ArgumentCaptor queryContainer = ArgumentCaptor.forClass(Query.class); @@ -219,12 +217,9 @@ public void testQueryWithParameters() { assertEquals("{\"name\": -1}", query.getSortObject().toJson()); } - @SuppressWarnings("serial") @Test public void testQueryWithCollection() { - reader.setParameterValues(new ArrayList(){{ - add("foo"); - }}); + reader.setParameterValues(Collections.singletonList("foo")); reader.setQuery("{ name : ?0 }"); reader.setCollection("collection"); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemWriterTests.java index d2565ea214..b016feb573 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemWriterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2020 the original author or authors. + * Copyright 2013-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. @@ -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; @@ -57,7 +58,6 @@ * @author Parikshit Dutta * @author Mahmoud Ben Hassine */ -@SuppressWarnings("serial") public class MongoItemWriterTests { private MongoItemWriter writer; @@ -101,10 +101,7 @@ public void testAfterPropertiesSet() throws Exception { @Test public void testWriteNoTransactionNoCollection() throws Exception { - List items = new ArrayList() {{ - add(new Item("Foo")); - add(new Item("Bar")); - }}; + List items = Arrays.asList(new Item("Foo"), new Item("Bar")); writer.write(items); @@ -114,10 +111,7 @@ public void testWriteNoTransactionNoCollection() throws Exception { @Test public void testWriteNoTransactionWithCollection() throws Exception { - List items = new ArrayList() {{ - add(new Item("Foo")); - add(new Item("Bar")); - }}; + List items = Arrays.asList(new Item("Foo"), new Item("Bar")); writer.setCollection("collection"); @@ -137,10 +131,7 @@ public void testWriteNoTransactionNoItems() throws Exception { @Test public void testWriteTransactionNoCollection() throws Exception { - final List items = new ArrayList() {{ - add(new Item("Foo")); - add(new Item("Bar")); - }}; + final List items = Arrays.asList(new Item("Foo"), new Item("Bar")); new TransactionTemplate(transactionManager).execute((TransactionCallback) status -> { try { @@ -158,10 +149,7 @@ public void testWriteTransactionNoCollection() throws Exception { @Test public void testWriteTransactionWithCollection() throws Exception { - final List items = new ArrayList() {{ - add(new Item("Foo")); - add(new Item("Bar")); - }}; + final List items = Arrays.asList(new Item("Foo"), new Item("Bar")); writer.setCollection("collection"); @@ -181,10 +169,7 @@ public void testWriteTransactionWithCollection() throws Exception { @Test public void testWriteTransactionFails() throws Exception { - final List items = new ArrayList() {{ - add(new Item("Foo")); - add(new Item("Bar")); - }}; + final List items = Arrays.asList(new Item("Foo"), new Item("Bar")); writer.setCollection("collection"); @@ -213,10 +198,7 @@ public void testWriteTransactionFails() throws Exception { */ @Test public void testWriteTransactionReadOnly() throws Exception { - final List items = new ArrayList() {{ - add(new Item("Foo")); - add(new Item("Bar")); - }}; + final List items = Arrays.asList(new Item("Foo"), new Item("Bar")); writer.setCollection("collection"); @@ -242,10 +224,7 @@ public void testWriteTransactionReadOnly() throws Exception { @Test public void testRemoveNoObjectIdNoCollection() throws Exception { writer.setDelete(true); - List items = new ArrayList() {{ - add(new Item("Foo")); - add(new Item("Bar")); - }}; + List items = Arrays.asList(new Item("Foo"), new Item("Bar")); writer.write(items); @@ -256,10 +235,7 @@ public void testRemoveNoObjectIdNoCollection() throws Exception { @Test public void testRemoveNoObjectIdWithCollection() throws Exception { writer.setDelete(true); - List items = new ArrayList() {{ - add(new Item("Foo")); - add(new Item("Bar")); - }}; + List items = Arrays.asList(new Item("Foo"), new Item("Bar")); writer.setCollection("collection"); writer.write(items); @@ -271,10 +247,7 @@ public void testRemoveNoObjectIdWithCollection() throws Exception { @Test public void testRemoveNoTransactionNoCollection() throws Exception { writer.setDelete(true); - List items = new ArrayList() {{ - add(new Item(1)); - add(new Item(2)); - }}; + List items = Arrays.asList(new Item(1), new Item(2)); writer.write(items); @@ -285,10 +258,7 @@ public void testRemoveNoTransactionNoCollection() throws Exception { @Test public void testRemoveNoTransactionWithCollection() throws Exception { writer.setDelete(true); - List items = new ArrayList() {{ - add(new Item(1)); - add(new Item(2)); - }}; + List items = Arrays.asList(new Item(1), new Item(2)); writer.setCollection("collection"); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcBatchItemWriterNamedParameterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcBatchItemWriterNamedParameterTests.java index 7e14337c3e..af1cff6234 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcBatchItemWriterNamedParameterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcBatchItemWriterNamedParameterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2008 the original author or authors. + * Copyright 2006-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. @@ -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 void testWriteAndFlush() throws Exception { writer.write(Collections.singletonList(new Foo("bar"))); } - @SuppressWarnings({ "rawtypes", "serial", "unchecked" }) + @SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testWriteAndFlushMap() throws Exception { JdbcBatchItemWriter> mapWriter = new JdbcBatchItemWriter<>(); @@ -145,14 +144,13 @@ public void testWriteAndFlushMap() throws Exception { when(namedParameterJdbcOperations.batchUpdate(eq(sql), captor.capture())) .thenReturn(new int[] {1}); - mapWriter.write(Collections.singletonList(new HashMap() {{put("foo", "bar");}})); + mapWriter.write(Collections.singletonList(Collections.singletonMap("foo", "bar"))); assertEquals(1, captor.getValue().length); Map results = captor.getValue()[0]; assertEquals("bar", results.get("foo")); } - @SuppressWarnings({ "rawtypes", "serial", "unchecked" }) @Test public void testWriteAndFlushMapWithItemSqlParameterSourceProvider() throws Exception { JdbcBatchItemWriter> mapWriter = new JdbcBatchItemWriter<>(); @@ -172,7 +170,7 @@ public SqlParameterSource createSqlParameterSource(Map item) { when(namedParameterJdbcOperations.batchUpdate(any(String.class), captor.capture())) .thenReturn(new int[] {1}); - mapWriter.write(Collections.singletonList(new HashMap() {{put("foo", "bar");}})); + mapWriter.write(Collections.singletonList(Collections.singletonMap("foo", "bar"))); assertEquals(1, captor.getValue().length); SqlParameterSource results = captor.getValue()[0]; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemProcessorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemProcessorTests.java index 0dc6dcd5f1..a2a9af0f73 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemProcessorTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemProcessorTests.java @@ -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.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 processor1; private ItemProcessor processor2; - @SuppressWarnings({ "unchecked", "serial" }) + @SuppressWarnings("unchecked") @Before public void setUp() throws Exception { processor1 = mock(ItemProcessor.class); processor2 = mock(ItemProcessor.class); - composite.setDelegates(new ArrayList>() {{ - add(processor1); add(processor2); - }}); + composite.setDelegates(Arrays.asList(processor1, processor2)); composite.afterPropertiesSet(); } @@ -76,14 +75,13 @@ public void testTransform() throws Exception { * 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 composite = new CompositeItemProcessor<>(); final ItemProcessor processor1 = mock(ItemProcessor.class); final ItemProcessor processor2 = mock(ItemProcessor.class); - composite.setDelegates(new ArrayList>() {{ - add(processor1); add(processor2); - }}); + composite.setDelegates(Arrays.asList(processor1, processor2)); + composite.afterPropertiesSet(); when(processor1.process("input")).thenReturn(5); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/jsr/item/ItemReaderAdapterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/jsr/item/ItemReaderAdapterTests.java index a944c2975f..9d8046547d 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/jsr/item/ItemReaderAdapterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/jsr/item/ItemReaderAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2018 the original author or authors. + * Copyright 2013-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. @@ -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; @@ -110,16 +110,11 @@ public void testRead() throws Exception { } @Test - @SuppressWarnings("serial") public void testCheckpointChange() throws Exception { ItemReaderAdapter adapter = new ItemReaderAdapter<>(new ItemReader() { private CheckpointContainer container = null; - private List items = new ArrayList() {{ - add("foo"); - add("bar"); - add("baz"); - }}; + private List items = Arrays.asList("foo", "bar", "baz"); @Override public Object readItem() throws Exception {