Add convenience factory method for Managed[List|Set|Map]

Closes gh-28026
This commit is contained in:
Stephane Nicoll
2022-02-10 12:37:19 +01:00
parent 7139a877f4
commit d2c7dfb79e
9 changed files with 92 additions and 74 deletions

View File

@@ -2298,9 +2298,7 @@ class DefaultListableBeanFactoryTests {
@Test
void prototypeWithArrayConversionForConstructor() {
List<String> list = new ManagedList<>();
list.add("myName");
list.add("myBeanName");
List<String> list = ManagedList.of("myName", "myBeanName");
RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bd.getConstructorArgumentValues().addGenericArgumentValue(list);
@@ -2316,9 +2314,7 @@ class DefaultListableBeanFactoryTests {
@Test
void prototypeWithArrayConversionForFactoryMethod() {
List<String> list = new ManagedList<>();
list.add("myName");
list.add("myBeanName");
List<String> list = ManagedList.of("myName", "myBeanName");
RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bd.setFactoryMethodName("create");

View File

@@ -16,6 +16,7 @@
package org.springframework.beans.factory.config;
import java.util.AbstractMap.SimpleEntry;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -357,22 +358,18 @@ public class PropertyResourceConfigurerTests {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("stringArray", new String[] {"${os.name}", "${age}"});
List<Object> friends = new ManagedList<>();
friends.add("na${age}me");
friends.add(new RuntimeBeanReference("${ref}"));
List<Object> friends = ManagedList.of("na${age}me", new RuntimeBeanReference("${ref}"));
pvs.add("friends", friends);
Set<Object> someSet = new ManagedSet<>();
someSet.add("na${age}me");
someSet.add(new RuntimeBeanReference("${ref}"));
someSet.add(new TypedStringValue("${age}", Integer.class));
Set<Object> someSet = ManagedSet.of("na${age}me",
new RuntimeBeanReference("${ref}"), new TypedStringValue("${age}", Integer.class));
pvs.add("someSet", someSet);
Map<Object, Object> someMap = new ManagedMap<>();
someMap.put(new TypedStringValue("key${age}"), new TypedStringValue("${age}"));
someMap.put(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}"));
someMap.put("key1", new RuntimeBeanReference("${ref}"));
someMap.put("key2", "${age}name");
Map<Object, Object> someMap = ManagedMap.ofEntries(
new SimpleEntry<>(new TypedStringValue("key${age}"), new TypedStringValue("${age}")),
new SimpleEntry<>(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}")),
new SimpleEntry<>("key1", new RuntimeBeanReference("${ref}")),
new SimpleEntry<>("key2", "${age}name"));
MutablePropertyValues innerPvs = new MutablePropertyValues();
innerPvs.add("country", "${os.name}");
RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@@ -34,11 +34,8 @@ public class ManagedListTests {
@Test
public void mergeSunnyDay() {
ManagedList parent = new ManagedList();
parent.add("one");
parent.add("two");
ManagedList child = new ManagedList();
child.add("three");
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);
@@ -46,8 +43,7 @@ public class ManagedListTests {
@Test
public void mergeWithNullParent() {
ManagedList child = new ManagedList();
child.add("one");
ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true);
assertThat(child.merge(null)).isSameAs(child);
}
@@ -61,8 +57,7 @@ public class ManagedListTests {
@Test
public void mergeWithNonCompatibleParentType() {
ManagedList child = new ManagedList();
child.add("one");
ManagedList child = ManagedList.of("one");
child.setMergeEnabled(true);
assertThatIllegalArgumentException().isThrownBy(() ->
child.merge("hello"));
@@ -70,9 +65,7 @@ public class ManagedListTests {
@Test
public void mergeEmptyChild() {
ManagedList parent = new ManagedList();
parent.add("one");
parent.add("two");
ManagedList parent = ManagedList.of("one", "two");
ManagedList child = new ManagedList();
child.setMergeEnabled(true);
List mergedList = child.merge(parent);
@@ -82,11 +75,8 @@ public class ManagedListTests {
@Test
public void mergeChildValuesOverrideTheParents() {
// doesn't make much sense in the context of a list...
ManagedList parent = new ManagedList();
parent.add("one");
parent.add("two");
ManagedList child = new ManagedList();
child.add("one");
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);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.beans.factory.support;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map;
import org.junit.jupiter.api.Test;
@@ -34,11 +35,9 @@ public class ManagedMapTests {
@Test
public void mergeSunnyDay() {
ManagedMap parent = new ManagedMap();
parent.put("one", "one");
parent.put("two", "two");
ManagedMap child = new ManagedMap();
child.put("three", "three");
ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"),
new SimpleEntry<>("two", "two"));
ManagedMap child = ManagedMap.ofEntries(new SimpleEntry<>("tree", "three"));
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
assertThat(mergedMap.size()).as("merge() obviously did not work.").isEqualTo(3);
@@ -67,9 +66,8 @@ public class ManagedMapTests {
@Test
public void mergeEmptyChild() {
ManagedMap parent = new ManagedMap();
parent.put("one", "one");
parent.put("two", "two");
ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"),
new SimpleEntry<>("two", "two"));
ManagedMap child = new ManagedMap();
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
@@ -78,11 +76,9 @@ public class ManagedMapTests {
@Test
public void mergeChildValuesOverrideTheParents() {
ManagedMap parent = new ManagedMap();
parent.put("one", "one");
parent.put("two", "two");
ManagedMap child = new ManagedMap();
child.put("one", "fork");
ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"),
new SimpleEntry<>("two", "two"));
ManagedMap child = ManagedMap.ofEntries(new SimpleEntry<>("one", "fork"));
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
// child value for 'one' must override parent value...

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@@ -34,10 +34,8 @@ public class ManagedSetTests {
@Test
public void mergeSunnyDay() {
ManagedSet parent = new ManagedSet();
parent.add("one");
parent.add("two");
ManagedSet child = new ManagedSet();
ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = ManagedSet.of("three");
child.add("three");
child.setMergeEnabled(true);
Set mergedSet = child.merge(parent);
@@ -46,8 +44,7 @@ public class ManagedSetTests {
@Test
public void mergeWithNullParent() {
ManagedSet child = new ManagedSet();
child.add("one");
ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true);
assertThat(child.merge(null)).isSameAs(child);
}
@@ -60,8 +57,7 @@ public class ManagedSetTests {
@Test
public void mergeWithNonCompatibleParentType() {
ManagedSet child = new ManagedSet();
child.add("one");
ManagedSet child = ManagedSet.of("one");
child.setMergeEnabled(true);
assertThatIllegalArgumentException().isThrownBy(() ->
child.merge("hello"));
@@ -69,9 +65,7 @@ public class ManagedSetTests {
@Test
public void mergeEmptyChild() {
ManagedSet parent = new ManagedSet();
parent.add("one");
parent.add("two");
ManagedSet parent = ManagedSet.of("one", "two");
ManagedSet child = new ManagedSet();
child.setMergeEnabled(true);
Set mergedSet = child.merge(parent);
@@ -81,11 +75,8 @@ public class ManagedSetTests {
@Test
public void mergeChildValuesOverrideTheParents() {
// asserts that the set contract is not violated during a merge() operation...
ManagedSet parent = new ManagedSet();
parent.add("one");
parent.add("two");
ManagedSet child = new ManagedSet();
child.add("one");
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);