Polish ManagedList[Tests] and ManagedSet[Tests]

This commit is contained in:
Sam Brannen 2022-05-17 14:51:27 +02:00
parent 6efe3aee34
commit a1c3efbb5f
4 changed files with 43 additions and 38 deletions

View File

@ -17,7 +17,7 @@
package org.springframework.beans.factory.support;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.BeanMetadataElement;
@ -31,6 +31,8 @@ import org.springframework.lang.Nullable;
* @author Rod Johnson
* @author Rob Harrop
* @author Juergen Hoeller
* @author Stephane Nicoll
* @author Sam Brannen
* @since 27.05.2003
* @param <E> the element type
*/
@ -55,16 +57,16 @@ public class ManagedList<E> extends ArrayList<E> implements Mergeable, BeanMetad
/**
* Return a new instance containing an arbitrary number of elements.
* Create a new instance containing an arbitrary number of elements.
* @param elements the elements to be contained in the list
* @param <E> the {@code List}'s element type
* @return a {@code List} containing the specified elements
* @return a {@code ManagedList} containing the specified elements
* @since 5.3.16
*/
@SuppressWarnings("unchecked")
@SafeVarargs
public static <E> ManagedList<E> of(E... elements) {
ManagedList<E> list = new ManagedList<>();
list.addAll(Arrays.asList(elements));
Collections.addAll(list, elements);
return list;
}

View File

@ -16,7 +16,7 @@
package org.springframework.beans.factory.support;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
@ -30,6 +30,8 @@ import org.springframework.lang.Nullable;
*
* @author Juergen Hoeller
* @author Rob Harrop
* @author Stephane Nicoll
* @author Sam Brannen
* @since 21.01.2004
* @param <E> the element type
*/
@ -54,16 +56,16 @@ public class ManagedSet<E> extends LinkedHashSet<E> implements Mergeable, BeanMe
/**
* Return a new instance containing an arbitrary number of elements.
* Create a new instance containing an arbitrary number of elements.
* @param elements the elements to be contained in the set
* @param <E> the {@code Set}'s element type
* @return a {@code Set} containing the specified elements
* @return a {@code ManagedSet} containing the specified elements
* @since 5.3.16
*/
@SuppressWarnings("unchecked")
@SafeVarargs
public static <E> ManagedSet<E> of(E... elements) {
ManagedSet<E> set = new ManagedSet<>();
set.addAll(Arrays.asList(elements));
Collections.addAll(set, elements);
return set;
}

View File

@ -25,61 +25,61 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Unit tests for {@link ManagedList}.
*
* @author Rick Evans
* @author Juergen Hoeller
* @author Sam Brannen
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ManagedListTests {
class ManagedListTests {
@Test
public void mergeSunnyDay() {
void mergeSunnyDay() {
ManagedList parent = ManagedList.of("one", "two");
ManagedList child = ManagedList.of("three");
child.setMergeEnabled(true);
List mergedList = child.merge(parent);
assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3);
assertThat(mergedList).as("merge() obviously did not work.").containsExactly("one", "two", "three");
}
@Test
public void mergeWithNullParent() {
void mergeWithNullParent() {
ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true);
assertThat(child.merge(null)).isSameAs(child);
}
@Test
public void mergeNotAllowedWhenMergeNotEnabled() {
void mergeNotAllowedWhenMergeNotEnabled() {
ManagedList child = new ManagedList();
assertThatIllegalStateException().isThrownBy(() ->
child.merge(null));
assertThatIllegalStateException().isThrownBy(() -> child.merge(null));
}
@Test
public void mergeWithNonCompatibleParentType() {
void mergeWithIncompatibleParentType() {
ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true);
assertThatIllegalArgumentException().isThrownBy(() ->
child.merge("hello"));
assertThatIllegalArgumentException().isThrownBy(() -> child.merge("hello"));
}
@Test
public void mergeEmptyChild() {
void mergeEmptyChild() {
ManagedList parent = ManagedList.of("one", "two");
ManagedList child = new ManagedList();
child.setMergeEnabled(true);
List mergedList = child.merge(parent);
assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(2);
assertThat(mergedList).as("merge() obviously did not work.").containsExactly("one", "two");
}
@Test
public void mergeChildValuesOverrideTheParents() {
void mergedChildValuesDoNotOverrideTheParents() {
// doesn't make much sense in the context of a list...
ManagedList parent = ManagedList.of("one", "two");
ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true);
List mergedList = child.merge(parent);
assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3);
assertThat(mergedList).as("merge() obviously did not work.").containsExactly("one", "two", "one");
}
}

View File

@ -25,61 +25,62 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Unit tests for {@link ManagedSet}.
*
* @author Rick Evans
* @author Juergen Hoeller
* @author Sam Brannen
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ManagedSetTests {
class ManagedSetTests {
@Test
public void mergeSunnyDay() {
void mergeSunnyDay() {
ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = ManagedSet.of("three");
child.add("three");
child.add("four");
child.setMergeEnabled(true);
Set mergedSet = child.merge(parent);
assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(3);
assertThat(mergedSet).as("merge() obviously did not work.").containsExactly("one", "two", "three", "four");
}
@Test
public void mergeWithNullParent() {
void mergeWithNullParent() {
ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true);
assertThat(child.merge(null)).isSameAs(child);
}
@Test
public void mergeNotAllowedWhenMergeNotEnabled() {
assertThatIllegalStateException().isThrownBy(() ->
new ManagedSet().merge(null));
void mergeNotAllowedWhenMergeNotEnabled() {
assertThatIllegalStateException().isThrownBy(() -> new ManagedSet().merge(null));
}
@Test
public void mergeWithNonCompatibleParentType() {
void mergeWithNonCompatibleParentType() {
ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true);
assertThatIllegalArgumentException().isThrownBy(() ->
child.merge("hello"));
assertThatIllegalArgumentException().isThrownBy(() -> child.merge("hello"));
}
@Test
public void mergeEmptyChild() {
void mergeEmptyChild() {
ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = new ManagedSet();
child.setMergeEnabled(true);
Set mergedSet = child.merge(parent);
assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(2);
assertThat(mergedSet).as("merge() obviously did not work.").containsExactly("one", "two");
}
@Test
public void mergeChildValuesOverrideTheParents() {
void mergeChildValuesOverrideTheParents() {
// asserts that the set contract is not violated during a merge() operation...
ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true);
Set mergedSet = child.merge(parent);
assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(2);
assertThat(mergedSet).as("merge() obviously did not work.").containsExactly("one", "two");
}
}