Consistent Iterator/Stream support in PropertySources and PropertyValues

Issue: SPR-16894
This commit is contained in:
Juergen Hoeller
2018-07-05 23:59:56 +02:00
parent bb3061d112
commit d8c7270c00
7 changed files with 194 additions and 18 deletions

View File

@@ -30,7 +30,7 @@ public abstract class AbstractPropertyValuesTests {
/**
* Must contain: forname=Tony surname=Blair age=50
*/
protected void doTestTony(PropertyValues pvs) throws Exception {
protected void doTestTony(PropertyValues pvs) {
assertTrue("Contains 3", pvs.getPropertyValues().length == 3);
assertTrue("Contains forname", pvs.contains("forname"));
assertTrue("Contains surname", pvs.contains("surname"));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2018 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,8 +16,11 @@
package org.springframework.beans;
import java.util.Iterator;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
@@ -25,11 +28,12 @@ import static org.junit.Assert.*;
*
* @author Rod Johnson
* @author Chris Beams
* @author Juergen Hoeller
*/
public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
@Test
public void testValid() throws Exception {
public void testValid() {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
@@ -44,7 +48,7 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
}
@Test
public void testAddOrOverride() throws Exception {
public void testAddOrOverride() {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
@@ -59,7 +63,7 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
}
@Test
public void testChangesOnEquals() throws Exception {
public void testChangesOnEquals() {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
@@ -70,7 +74,7 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
}
@Test
public void testChangeOfOneField() throws Exception {
public void testChangeOfOneField() {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
@@ -103,4 +107,50 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
assertTrue("new value is bar", fn.getValue().equals("bar"));
}
@Test
public void iteratorContainsPropertyValue() {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("foo", "bar");
Iterator<PropertyValue> it = pvs.iterator();
assertTrue(it.hasNext());
PropertyValue pv = it.next();
assertEquals("foo", pv.getName());
assertEquals("bar", pv.getValue());
try {
it.remove();
fail("Should have thrown UnsupportedOperationException");
}
catch (UnsupportedOperationException ex) {
// expected
}
assertFalse(it.hasNext());
}
@Test
public void iteratorIsEmptyForEmptyValues() {
MutablePropertyValues pvs = new MutablePropertyValues();
Iterator<PropertyValue> it = pvs.iterator();
assertFalse(it.hasNext());
}
@Test
public void streamContainsPropertyValue() {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("foo", "bar");
assertThat(pvs.stream(), notNullValue());
assertThat(pvs.stream().count(), is(1L));
assertThat(pvs.stream().anyMatch(pv -> "foo".equals(pv.getName()) && "bar".equals(pv.getValue())), is(true));
assertThat(pvs.stream().anyMatch(pv -> "bar".equals(pv.getName()) && "foo".equals(pv.getValue())), is(false));
}
@Test
public void streamIsEmptyForEmptyValues() {
MutablePropertyValues pvs = new MutablePropertyValues();
assertThat(pvs.stream(), notNullValue());
assertThat(pvs.stream().count(), is(0L));
}
}