Skip to content

Commit a1c3efb

Browse files
committed
Polish ManagedList[Tests] and ManagedSet[Tests]
1 parent 6efe3ae commit a1c3efb

File tree

4 files changed

+43
-38
lines changed

4 files changed

+43
-38
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedList.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.beans.factory.support;
1818

1919
import java.util.ArrayList;
20-
import java.util.Arrays;
20+
import java.util.Collections;
2121
import java.util.List;
2222

2323
import org.springframework.beans.BeanMetadataElement;
@@ -31,6 +31,8 @@
3131
* @author Rod Johnson
3232
* @author Rob Harrop
3333
* @author Juergen Hoeller
34+
* @author Stephane Nicoll
35+
* @author Sam Brannen
3436
* @since 27.05.2003
3537
* @param <E> the element type
3638
*/
@@ -55,16 +57,16 @@ public ManagedList(int initialCapacity) {
5557

5658

5759
/**
58-
* Return a new instance containing an arbitrary number of elements.
60+
* Create a new instance containing an arbitrary number of elements.
5961
* @param elements the elements to be contained in the list
6062
* @param <E> the {@code List}'s element type
61-
* @return a {@code List} containing the specified elements
63+
* @return a {@code ManagedList} containing the specified elements
6264
* @since 5.3.16
6365
*/
64-
@SuppressWarnings("unchecked")
66+
@SafeVarargs
6567
public static <E> ManagedList<E> of(E... elements) {
6668
ManagedList<E> list = new ManagedList<>();
67-
list.addAll(Arrays.asList(elements));
69+
Collections.addAll(list, elements);
6870
return list;
6971
}
7072

spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedSet.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.beans.factory.support;
1818

19-
import java.util.Arrays;
19+
import java.util.Collections;
2020
import java.util.LinkedHashSet;
2121
import java.util.Set;
2222

@@ -30,6 +30,8 @@
3030
*
3131
* @author Juergen Hoeller
3232
* @author Rob Harrop
33+
* @author Stephane Nicoll
34+
* @author Sam Brannen
3335
* @since 21.01.2004
3436
* @param <E> the element type
3537
*/
@@ -54,16 +56,16 @@ public ManagedSet(int initialCapacity) {
5456

5557

5658
/**
57-
* Return a new instance containing an arbitrary number of elements.
59+
* Create a new instance containing an arbitrary number of elements.
5860
* @param elements the elements to be contained in the set
5961
* @param <E> the {@code Set}'s element type
60-
* @return a {@code Set} containing the specified elements
62+
* @return a {@code ManagedSet} containing the specified elements
6163
* @since 5.3.16
6264
*/
63-
@SuppressWarnings("unchecked")
65+
@SafeVarargs
6466
public static <E> ManagedSet<E> of(E... elements) {
6567
ManagedSet<E> set = new ManagedSet<>();
66-
set.addAll(Arrays.asList(elements));
68+
Collections.addAll(set, elements);
6769
return set;
6870
}
6971

spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,61 @@
2525
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2626

2727
/**
28+
* Unit tests for {@link ManagedList}.
29+
*
2830
* @author Rick Evans
2931
* @author Juergen Hoeller
3032
* @author Sam Brannen
3133
*/
3234
@SuppressWarnings({ "rawtypes", "unchecked" })
33-
public class ManagedListTests {
35+
class ManagedListTests {
3436

3537
@Test
36-
public void mergeSunnyDay() {
38+
void mergeSunnyDay() {
3739
ManagedList parent = ManagedList.of("one", "two");
3840
ManagedList child = ManagedList.of("three");
3941
child.setMergeEnabled(true);
4042
List mergedList = child.merge(parent);
41-
assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3);
43+
assertThat(mergedList).as("merge() obviously did not work.").containsExactly("one", "two", "three");
4244
}
4345

4446
@Test
45-
public void mergeWithNullParent() {
47+
void mergeWithNullParent() {
4648
ManagedList child = ManagedList.of("one");
4749
child.setMergeEnabled(true);
4850
assertThat(child.merge(null)).isSameAs(child);
4951
}
5052

5153
@Test
52-
public void mergeNotAllowedWhenMergeNotEnabled() {
54+
void mergeNotAllowedWhenMergeNotEnabled() {
5355
ManagedList child = new ManagedList();
54-
assertThatIllegalStateException().isThrownBy(() ->
55-
child.merge(null));
56+
assertThatIllegalStateException().isThrownBy(() -> child.merge(null));
5657
}
5758

5859
@Test
59-
public void mergeWithNonCompatibleParentType() {
60+
void mergeWithIncompatibleParentType() {
6061
ManagedList child = ManagedList.of("one");
6162
child.setMergeEnabled(true);
62-
assertThatIllegalArgumentException().isThrownBy(() ->
63-
child.merge("hello"));
63+
assertThatIllegalArgumentException().isThrownBy(() -> child.merge("hello"));
6464
}
6565

6666
@Test
67-
public void mergeEmptyChild() {
67+
void mergeEmptyChild() {
6868
ManagedList parent = ManagedList.of("one", "two");
6969
ManagedList child = new ManagedList();
7070
child.setMergeEnabled(true);
7171
List mergedList = child.merge(parent);
72-
assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(2);
72+
assertThat(mergedList).as("merge() obviously did not work.").containsExactly("one", "two");
7373
}
7474

7575
@Test
76-
public void mergeChildValuesOverrideTheParents() {
76+
void mergedChildValuesDoNotOverrideTheParents() {
7777
// doesn't make much sense in the context of a list...
7878
ManagedList parent = ManagedList.of("one", "two");
7979
ManagedList child = ManagedList.of("one");
8080
child.setMergeEnabled(true);
8181
List mergedList = child.merge(parent);
82-
assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3);
82+
assertThat(mergedList).as("merge() obviously did not work.").containsExactly("one", "two", "one");
8383
}
8484

8585
}

spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,62 @@
2525
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2626

2727
/**
28+
* Unit tests for {@link ManagedSet}.
29+
*
2830
* @author Rick Evans
2931
* @author Juergen Hoeller
3032
* @author Sam Brannen
3133
*/
3234
@SuppressWarnings({ "rawtypes", "unchecked" })
33-
public class ManagedSetTests {
35+
class ManagedSetTests {
3436

3537
@Test
36-
public void mergeSunnyDay() {
38+
void mergeSunnyDay() {
3739
ManagedSet parent = ManagedSet.of("one", "two");
3840
ManagedSet child = ManagedSet.of("three");
3941
child.add("three");
42+
child.add("four");
4043
child.setMergeEnabled(true);
4144
Set mergedSet = child.merge(parent);
42-
assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(3);
45+
assertThat(mergedSet).as("merge() obviously did not work.").containsExactly("one", "two", "three", "four");
4346
}
4447

4548
@Test
46-
public void mergeWithNullParent() {
49+
void mergeWithNullParent() {
4750
ManagedSet child = ManagedSet.of("one");
4851
child.setMergeEnabled(true);
4952
assertThat(child.merge(null)).isSameAs(child);
5053
}
5154

5255
@Test
53-
public void mergeNotAllowedWhenMergeNotEnabled() {
54-
assertThatIllegalStateException().isThrownBy(() ->
55-
new ManagedSet().merge(null));
56+
void mergeNotAllowedWhenMergeNotEnabled() {
57+
assertThatIllegalStateException().isThrownBy(() -> new ManagedSet().merge(null));
5658
}
5759

5860
@Test
59-
public void mergeWithNonCompatibleParentType() {
61+
void mergeWithNonCompatibleParentType() {
6062
ManagedSet child = ManagedSet.of("one");
6163
child.setMergeEnabled(true);
62-
assertThatIllegalArgumentException().isThrownBy(() ->
63-
child.merge("hello"));
64+
assertThatIllegalArgumentException().isThrownBy(() -> child.merge("hello"));
6465
}
6566

6667
@Test
67-
public void mergeEmptyChild() {
68+
void mergeEmptyChild() {
6869
ManagedSet parent = ManagedSet.of("one", "two");
6970
ManagedSet child = new ManagedSet();
7071
child.setMergeEnabled(true);
7172
Set mergedSet = child.merge(parent);
72-
assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(2);
73+
assertThat(mergedSet).as("merge() obviously did not work.").containsExactly("one", "two");
7374
}
7475

7576
@Test
76-
public void mergeChildValuesOverrideTheParents() {
77+
void mergeChildValuesOverrideTheParents() {
7778
// asserts that the set contract is not violated during a merge() operation...
7879
ManagedSet parent = ManagedSet.of("one", "two");
7980
ManagedSet child = ManagedSet.of("one");
8081
child.setMergeEnabled(true);
8182
Set mergedSet = child.merge(parent);
82-
assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(2);
83+
assertThat(mergedSet).as("merge() obviously did not work.").containsExactly("one", "two");
8384
}
8485

8586
}

0 commit comments

Comments
 (0)