Polish MutablePropertyValuesTests

This commit is contained in:
Sam Brannen 2023-12-05 11:43:57 +01:00
parent ceba4162bb
commit aa347e5fe6
2 changed files with 51 additions and 79 deletions

View File

@ -1,55 +0,0 @@
/*
* Copyright 2002-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans;
import java.util.HashMap;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Rod Johnson
* @author Chris Beams
*/
public abstract class AbstractPropertyValuesTests {
/**
* Must contain: forname=Tony surname=Blair age=50
*/
protected void doTestTony(PropertyValues pvs) {
assertThat(pvs.getPropertyValues()).as("Contains 3").hasSize(3);
assertThat(pvs.contains("forname")).as("Contains forname").isTrue();
assertThat(pvs.contains("surname")).as("Contains surname").isTrue();
assertThat(pvs.contains("age")).as("Contains age").isTrue();
assertThat(pvs.contains("tory")).as("Doesn't contain tory").isFalse();
PropertyValue[] ps = pvs.getPropertyValues();
Map<String, String> m = new HashMap<>();
m.put("forname", "Tony");
m.put("surname", "Blair");
m.put("age", "50");
for (PropertyValue element : ps) {
Object val = m.get(element.getName());
assertThat(val).as("Can't have unexpected value").isNotNull();
assertThat(val instanceof String).as("Val i string").isTrue();
assertThat(val.equals(element.getValue())).as("val matches expected").isTrue();
m.remove(element.getName());
}
assertThat(m).as("Map size is 0").isEmpty();
}
}

View File

@ -16,7 +16,9 @@
package org.springframework.beans; package org.springframework.beans;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -30,10 +32,10 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
public class MutablePropertyValuesTests extends AbstractPropertyValuesTests { class MutablePropertyValuesTests {
@Test @Test
public void testValid() { void valid() {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony")); pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair")); pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
@ -48,7 +50,7 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
} }
@Test @Test
public void testAddOrOverride() { void addOrOverride() {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony")); pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair")); pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
@ -56,25 +58,25 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
doTestTony(pvs); doTestTony(pvs);
PropertyValue addedPv = new PropertyValue("rod", "Rod"); PropertyValue addedPv = new PropertyValue("rod", "Rod");
pvs.addPropertyValue(addedPv); pvs.addPropertyValue(addedPv);
assertThat(pvs.getPropertyValue("rod").equals(addedPv)).isTrue(); assertThat(pvs.getPropertyValue("rod")).isEqualTo(addedPv);
PropertyValue changedPv = new PropertyValue("forname", "Greg"); PropertyValue changedPv = new PropertyValue("forname", "Greg");
pvs.addPropertyValue(changedPv); pvs.addPropertyValue(changedPv);
assertThat(pvs.getPropertyValue("forname").equals(changedPv)).isTrue(); assertThat(pvs.getPropertyValue("forname")).isEqualTo(changedPv);
} }
@Test @Test
public void testChangesOnEquals() { void changesOnEquals() {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony")); pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair")); pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
pvs.addPropertyValue(new PropertyValue("age", "50")); pvs.addPropertyValue(new PropertyValue("age", "50"));
MutablePropertyValues pvs2 = pvs; MutablePropertyValues pvs2 = pvs;
PropertyValues changes = pvs2.changesSince(pvs); PropertyValues changes = pvs2.changesSince(pvs);
assertThat(changes.getPropertyValues().length).as("changes are empty").isEqualTo(0); assertThat(changes.getPropertyValues()).as("changes are empty").isEmpty();
} }
@Test @Test
public void testChangeOfOneField() { void changeOfOneField() {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony")); pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair")); pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
@ -82,33 +84,31 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
MutablePropertyValues pvs2 = new MutablePropertyValues(pvs); MutablePropertyValues pvs2 = new MutablePropertyValues(pvs);
PropertyValues changes = pvs2.changesSince(pvs); PropertyValues changes = pvs2.changesSince(pvs);
assertThat(changes.getPropertyValues().length).as("changes are empty, not of length " + changes.getPropertyValues().length) assertThat(changes.getPropertyValues()).as("changes").isEmpty();
.isEqualTo(0);
pvs2.addPropertyValue(new PropertyValue("forname", "Gordon")); pvs2.addPropertyValue(new PropertyValue("forname", "Gordon"));
changes = pvs2.changesSince(pvs); changes = pvs2.changesSince(pvs);
assertThat(changes.getPropertyValues().length).as("1 change").isEqualTo(1); assertThat(changes.getPropertyValues()).as("1 change").hasSize(1);
PropertyValue fn = changes.getPropertyValue("forname"); PropertyValue fn = changes.getPropertyValue("forname");
assertThat(fn).as("change is forname").isNotNull(); assertThat(fn).as("change is forname").isNotNull();
assertThat(fn.getValue().equals("Gordon")).as("new value is gordon").isTrue(); assertThat(fn.getValue()).isEqualTo("Gordon");
MutablePropertyValues pvs3 = new MutablePropertyValues(pvs); MutablePropertyValues pvs3 = new MutablePropertyValues(pvs);
changes = pvs3.changesSince(pvs); changes = pvs3.changesSince(pvs);
assertThat(changes.getPropertyValues().length).as("changes are empty, not of length " + changes.getPropertyValues().length) assertThat(changes.getPropertyValues()).as("changes").isEmpty();
.isEqualTo(0);
// add new // add new
pvs3.addPropertyValue(new PropertyValue("foo", "bar")); pvs3.addPropertyValue(new PropertyValue("foo", "bar"));
pvs3.addPropertyValue(new PropertyValue("fi", "fum")); pvs3.addPropertyValue(new PropertyValue("fi", "fum"));
changes = pvs3.changesSince(pvs); changes = pvs3.changesSince(pvs);
assertThat(changes.getPropertyValues().length).as("2 change").isEqualTo(2); assertThat(changes.getPropertyValues()).as("2 changes").hasSize(2);
fn = changes.getPropertyValue("foo"); fn = changes.getPropertyValue("foo");
assertThat(fn).as("change in foo").isNotNull(); assertThat(fn).as("change in foo").isNotNull();
assertThat(fn.getValue().equals("bar")).as("new value is bar").isTrue(); assertThat(fn.getValue()).isEqualTo("bar");
} }
@Test @Test
public void iteratorContainsPropertyValue() { void iteratorContainsPropertyValue() {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("foo", "bar"); pvs.add("foo", "bar");
@ -122,28 +122,55 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
} }
@Test @Test
public void iteratorIsEmptyForEmptyValues() { void iteratorIsEmptyForEmptyValues() {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
Iterator<PropertyValue> it = pvs.iterator(); Iterator<PropertyValue> it = pvs.iterator();
assertThat(it.hasNext()).isFalse(); assertThat(it.hasNext()).isFalse();
} }
@Test @Test
public void streamContainsPropertyValue() { void streamContainsPropertyValue() {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("foo", "bar"); pvs.add("foo", "bar");
assertThat(pvs.stream()).isNotNull(); assertThat(pvs.stream()).isNotNull();
assertThat(pvs.stream().count()).isEqualTo(1L); assertThat(pvs.stream()).hasSize(1);
assertThat(pvs.stream().anyMatch(pv -> "foo".equals(pv.getName()) && "bar".equals(pv.getValue()))).isTrue(); assertThat(pvs.stream()).anyMatch(pv -> "foo".equals(pv.getName()) && "bar".equals(pv.getValue()));
assertThat(pvs.stream().anyMatch(pv -> "bar".equals(pv.getName()) && "foo".equals(pv.getValue()))).isFalse(); assertThat(pvs.stream()).noneMatch(pv -> "bar".equals(pv.getName()) && "foo".equals(pv.getValue()));
} }
@Test @Test
public void streamIsEmptyForEmptyValues() { void streamIsEmptyForEmptyValues() {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
assertThat(pvs.stream()).isNotNull(); assertThat(pvs.stream()).isNotNull();
assertThat(pvs.stream().count()).isEqualTo(0L); assertThat(pvs.stream()).isEmpty();
}
/**
* Must contain: forname=Tony surname=Blair age=50
*/
protected void doTestTony(PropertyValues pvs) {
PropertyValue[] propertyValues = pvs.getPropertyValues();
assertThat(propertyValues).hasSize(3);
assertThat(pvs.contains("forname")).as("Contains forname").isTrue();
assertThat(pvs.contains("surname")).as("Contains surname").isTrue();
assertThat(pvs.contains("age")).as("Contains age").isTrue();
assertThat(pvs.contains("tory")).as("Doesn't contain tory").isFalse();
Map<String, String> map = new HashMap<>();
map.put("forname", "Tony");
map.put("surname", "Blair");
map.put("age", "50");
for (PropertyValue element : propertyValues) {
Object val = map.get(element.getName());
assertThat(val).as("Can't have unexpected value").isNotNull();
assertThat(val).isInstanceOf(String.class);
assertThat(val).isEqualTo(element.getValue());
map.remove(element.getName());
}
assertThat(map).isEmpty();
} }
} }