DATACMNS-764 - Java 8 polishing.

Use Streamable in configuration APIs for more efficient, lazy traversal of source lists. Introduced SelectionSet.of(…) to move away from constructors. Avoid the use of null in SelectionSet.

Original pull request: #201.
This commit is contained in:
Oliver Gierke
2017-06-09 13:46:17 +02:00
parent eec63cb11d
commit 9462a5ba47
9 changed files with 118 additions and 132 deletions

View File

@@ -19,7 +19,6 @@ import static org.assertj.core.api.Assertions.*;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
@@ -32,6 +31,7 @@ import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.data.util.Streamable;
/**
* Unit tests for {@link AnnotationRepositoryConfigurationSource}.
@@ -65,7 +65,7 @@ public class AnnotationRepositoryConfigurationSourceUnitTests {
@Test // DATACMNS-47
public void evaluatesExcludeFiltersCorrectly() {
Collection<BeanDefinition> candidates = source.getCandidates(new DefaultResourceLoader());
Streamable<BeanDefinition> candidates = source.getCandidates(new DefaultResourceLoader());
assertThat(candidates).hasSize(1);
BeanDefinition candidate = candidates.iterator().next();

View File

@@ -1,8 +1,25 @@
/*
* Copyright 2017 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
*
* http://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.data.repository.config;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.util.Optional;
import org.junit.Test;
@@ -10,53 +27,53 @@ import org.junit.Test;
* Unit tests for {@link SelectionSet}
*
* @author Jens Schauder
* @author Oliver Gierke
*/
public class SelectionSetUnitTests {
@Test // DATACMNS-764
public void returnsUniqueResult() {
assertEquals("single value", new SelectionSet<>(singleton("single value")).uniqueResult());
assertThat(SelectionSet.of(singleton("single value")).uniqueResult()).hasValue("single value");
}
@Test // DATACMNS-764
public void emptyCollectionReturnsNull() {
assertNull(new SelectionSet<Object>(emptySet()).uniqueResult());
assertThat(SelectionSet.of(emptySet()).uniqueResult()).isEmpty();
}
@Test(expected = IllegalStateException.class) // DATACMNS-764
public void multipleElementsThrowException() {
new SelectionSet<>(asList("one", "two")).uniqueResult();
SelectionSet.of(asList("one", "two")).uniqueResult();
}
@Test(expected = NullPointerException.class) // DATACMNS-764
public void throwsCustomExceptionWhenConfigured() {
new SelectionSet<>(asList("one", "two"), c -> {
SelectionSet.of(asList("one", "two"), c -> {
throw new NullPointerException();
}).uniqueResult();
}
@Test // DATACMNS-764
public void usesFallbackWhenConfigured() {
String value = new SelectionSet<>(asList("one", "two"), c -> String.valueOf(c.size())).uniqueResult();
assertEquals("2", value);
assertThat(SelectionSet.of(asList("one", "two"), c -> Optional.of(String.valueOf(c.size()))).uniqueResult())
.hasValue("2");
}
@Test // DATACMNS-764
public void returnsUniqueResultAfterFilter() {
SelectionSet<String> selection = new SelectionSet<>(asList("one", "two", "three")).filterIfNecessary(s -> s.contains("w"));
SelectionSet<String> selection = SelectionSet.of(asList("one", "two", "three"))
.filterIfNecessary(s -> s.contains("w"));
assertEquals("two", selection.uniqueResult());
assertThat(selection.uniqueResult()).hasValue("two");
}
@Test // DATACMNS-764
public void ignoresFilterWhenResultIsAlreadyUnique() {
SelectionSet<String> selection = new SelectionSet<>(asList("one")).filterIfNecessary(s -> s.contains("w"));
SelectionSet<String> selection = SelectionSet.of(asList("one")).filterIfNecessary(s -> s.contains("w"));
assertEquals("one", selection.uniqueResult());
assertThat(selection.uniqueResult()).hasValue("one");
}
}