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; package org.springframework.beans.factory.support;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Collections;
import java.util.List; import java.util.List;
import org.springframework.beans.BeanMetadataElement; import org.springframework.beans.BeanMetadataElement;
@ -31,6 +31,8 @@ import org.springframework.lang.Nullable;
* @author Rod Johnson * @author Rod Johnson
* @author Rob Harrop * @author Rob Harrop
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Stephane Nicoll
* @author Sam Brannen
* @since 27.05.2003 * @since 27.05.2003
* @param <E> the element type * @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 elements the elements to be contained in the list
* @param <E> the {@code List}'s element type * @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 * @since 5.3.16
*/ */
@SuppressWarnings("unchecked") @SafeVarargs
public static <E> ManagedList<E> of(E... elements) { public static <E> ManagedList<E> of(E... elements) {
ManagedList<E> list = new ManagedList<>(); ManagedList<E> list = new ManagedList<>();
list.addAll(Arrays.asList(elements)); Collections.addAll(list, elements);
return list; return list;
} }

View File

@ -16,7 +16,7 @@
package org.springframework.beans.factory.support; package org.springframework.beans.factory.support;
import java.util.Arrays; import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -30,6 +30,8 @@ import org.springframework.lang.Nullable;
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rob Harrop * @author Rob Harrop
* @author Stephane Nicoll
* @author Sam Brannen
* @since 21.01.2004 * @since 21.01.2004
* @param <E> the element type * @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 elements the elements to be contained in the set
* @param <E> the {@code Set}'s element type * @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 * @since 5.3.16
*/ */
@SuppressWarnings("unchecked") @SafeVarargs
public static <E> ManagedSet<E> of(E... elements) { public static <E> ManagedSet<E> of(E... elements) {
ManagedSet<E> set = new ManagedSet<>(); ManagedSet<E> set = new ManagedSet<>();
set.addAll(Arrays.asList(elements)); Collections.addAll(set, elements);
return set; return set;
} }

View File

@ -25,61 +25,61 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/** /**
* Unit tests for {@link ManagedList}.
*
* @author Rick Evans * @author Rick Evans
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
*/ */
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
public class ManagedListTests { class ManagedListTests {
@Test @Test
public void mergeSunnyDay() { void mergeSunnyDay() {
ManagedList parent = ManagedList.of("one", "two"); ManagedList parent = ManagedList.of("one", "two");
ManagedList child = ManagedList.of("three"); ManagedList child = ManagedList.of("three");
child.setMergeEnabled(true); child.setMergeEnabled(true);
List mergedList = child.merge(parent); 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 @Test
public void mergeWithNullParent() { void mergeWithNullParent() {
ManagedList child = ManagedList.of("one"); ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true); child.setMergeEnabled(true);
assertThat(child.merge(null)).isSameAs(child); assertThat(child.merge(null)).isSameAs(child);
} }
@Test @Test
public void mergeNotAllowedWhenMergeNotEnabled() { void mergeNotAllowedWhenMergeNotEnabled() {
ManagedList child = new ManagedList(); ManagedList child = new ManagedList();
assertThatIllegalStateException().isThrownBy(() -> assertThatIllegalStateException().isThrownBy(() -> child.merge(null));
child.merge(null));
} }
@Test @Test
public void mergeWithNonCompatibleParentType() { void mergeWithIncompatibleParentType() {
ManagedList child = ManagedList.of("one"); ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true); child.setMergeEnabled(true);
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() -> child.merge("hello"));
child.merge("hello"));
} }
@Test @Test
public void mergeEmptyChild() { void mergeEmptyChild() {
ManagedList parent = ManagedList.of("one", "two"); ManagedList parent = ManagedList.of("one", "two");
ManagedList child = new ManagedList(); ManagedList child = new ManagedList();
child.setMergeEnabled(true); child.setMergeEnabled(true);
List mergedList = child.merge(parent); 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 @Test
public void mergeChildValuesOverrideTheParents() { void mergedChildValuesDoNotOverrideTheParents() {
// doesn't make much sense in the context of a list... // doesn't make much sense in the context of a list...
ManagedList parent = ManagedList.of("one", "two"); ManagedList parent = ManagedList.of("one", "two");
ManagedList child = ManagedList.of("one"); ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true); child.setMergeEnabled(true);
List mergedList = child.merge(parent); 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; import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/** /**
* Unit tests for {@link ManagedSet}.
*
* @author Rick Evans * @author Rick Evans
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
*/ */
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
public class ManagedSetTests { class ManagedSetTests {
@Test @Test
public void mergeSunnyDay() { void mergeSunnyDay() {
ManagedSet parent = ManagedSet.of("one", "two"); ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = ManagedSet.of("three"); ManagedSet child = ManagedSet.of("three");
child.add("three"); child.add("three");
child.add("four");
child.setMergeEnabled(true); child.setMergeEnabled(true);
Set mergedSet = child.merge(parent); 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 @Test
public void mergeWithNullParent() { void mergeWithNullParent() {
ManagedSet child = ManagedSet.of("one"); ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true); child.setMergeEnabled(true);
assertThat(child.merge(null)).isSameAs(child); assertThat(child.merge(null)).isSameAs(child);
} }
@Test @Test
public void mergeNotAllowedWhenMergeNotEnabled() { void mergeNotAllowedWhenMergeNotEnabled() {
assertThatIllegalStateException().isThrownBy(() -> assertThatIllegalStateException().isThrownBy(() -> new ManagedSet().merge(null));
new ManagedSet().merge(null));
} }
@Test @Test
public void mergeWithNonCompatibleParentType() { void mergeWithNonCompatibleParentType() {
ManagedSet child = ManagedSet.of("one"); ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true); child.setMergeEnabled(true);
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() -> child.merge("hello"));
child.merge("hello"));
} }
@Test @Test
public void mergeEmptyChild() { void mergeEmptyChild() {
ManagedSet parent = ManagedSet.of("one", "two"); ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = new ManagedSet(); ManagedSet child = new ManagedSet();
child.setMergeEnabled(true); child.setMergeEnabled(true);
Set mergedSet = child.merge(parent); 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 @Test
public void mergeChildValuesOverrideTheParents() { void mergeChildValuesOverrideTheParents() {
// asserts that the set contract is not violated during a merge() operation... // asserts that the set contract is not violated during a merge() operation...
ManagedSet parent = ManagedSet.of("one", "two"); ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = ManagedSet.of("one"); ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true); child.setMergeEnabled(true);
Set mergedSet = child.merge(parent); 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");
} }
} }