Skip to content

Commit

Permalink
Use Java 9 collection factory methods
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Feb 1, 2021
1 parent 5c42adb commit 2b7d5b1
Show file tree
Hide file tree
Showing 28 changed files with 279 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

package org.junit.platform.commons.util;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -239,7 +238,7 @@ void findRepeatableAnnotationsWithComposedTagBeforeContainer() {
}

private void assertTagsFound(Class<?> clazz, String... tags) {
assertEquals(asList(tags),
assertEquals(List.of(tags),
findRepeatableAnnotations(clazz, Tag.class).stream().map(Tag::value).collect(toList()),
() -> "Tags found for class " + clazz.getName());
}
Expand Down Expand Up @@ -297,7 +296,7 @@ void findInheritedRepeatableAnnotationsWithMultipleComposedAnnotationsOnSupercla
}

private void assertExtensionsFound(Class<?> clazz, String... tags) {
assertEquals(asList(tags),
assertEquals(List.of(tags),
findRepeatableAnnotations(clazz, ExtendWith.class).stream().map(ExtendWith::value).collect(toList()),
() -> "Extensions found for class " + clazz.getName());
}
Expand Down Expand Up @@ -490,22 +489,22 @@ void findPublicAnnotatedFieldsForDirectlyAnnotatedFieldOfWrongFieldType() {
void findPublicAnnotatedFieldsForDirectlyAnnotatedField() {
var fields = findPublicAnnotatedFields(getClass(), String.class, Annotation1.class);
assertNotNull(fields);
assertIterableEquals(asList("directlyAnnotatedField"), asNames(fields));
assertIterableEquals(List.of("directlyAnnotatedField"), asNames(fields));
}

@Test
void findPublicAnnotatedFieldsForMetaAnnotatedField() {
var fields = findPublicAnnotatedFields(getClass(), Number.class, Annotation1.class);
assertNotNull(fields);
assertEquals(1, fields.size());
assertIterableEquals(asList("metaAnnotatedField"), asNames(fields));
assertIterableEquals(List.of("metaAnnotatedField"), asNames(fields));
}

@Test
void findPublicAnnotatedFieldsForDirectlyAnnotatedFieldInInterface() {
var fields = findPublicAnnotatedFields(InterfaceWithAnnotatedFields.class, String.class, Annotation1.class);
assertNotNull(fields);
assertIterableEquals(asList("foo"), asNames(fields));
assertIterableEquals(List.of("foo"), asNames(fields));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

package org.junit.platform.commons.util;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
Expand Down Expand Up @@ -47,7 +46,7 @@ class ClassNamePatternFilterUtilsTests {
//@formatter:on
@ParameterizedTest
void alwaysEnabledConditions(String pattern) {
List<? extends ExecutionCondition> executionConditions = asList(new AExecutionConditionClass(),
List<? extends ExecutionCondition> executionConditions = List.of(new AExecutionConditionClass(),
new BExecutionConditionClass());
assertThat(executionConditions).filteredOn(
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isNotEmpty();
Expand All @@ -64,7 +63,7 @@ void alwaysEnabledConditions(String pattern) {
//@formatter:on
@ParameterizedTest
void alwaysDisabledConditions(String pattern) {
List<? extends ExecutionCondition> executionConditions = asList(new AExecutionConditionClass(),
List<? extends ExecutionCondition> executionConditions = List.of(new AExecutionConditionClass(),
new BExecutionConditionClass());
assertThat(executionConditions).filteredOn(
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isEmpty();
Expand All @@ -81,7 +80,7 @@ void alwaysDisabledConditions(String pattern) {
//@formatter:on
@ParameterizedTest
void alwaysEnabledListeners(String pattern) {
List<? extends TestExecutionListener> executionConditions = asList(new ATestExecutionListenerClass(),
List<? extends TestExecutionListener> executionConditions = List.of(new ATestExecutionListenerClass(),
new BTestExecutionListenerClass());
assertThat(executionConditions).filteredOn(
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isNotEmpty();
Expand All @@ -98,7 +97,7 @@ void alwaysEnabledListeners(String pattern) {
//@formatter:on
@ParameterizedTest
void alwaysDisabledListeners(String pattern) {
List<? extends TestExecutionListener> executionConditions = asList(new ATestExecutionListenerClass(),
List<? extends TestExecutionListener> executionConditions = List.of(new ATestExecutionListenerClass(),
new BTestExecutionListenerClass());
assertThat(executionConditions).filteredOn(
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isEmpty();
Expand All @@ -115,7 +114,7 @@ void alwaysDisabledListeners(String pattern) {
//@formatter:on
@ParameterizedTest
void alwaysEnabledClass(String pattern) {
var executionConditions = asList(new AVanillaEmpty(), new BVanillaEmpty());
var executionConditions = List.of(new AVanillaEmpty(), new BVanillaEmpty());
assertThat(executionConditions).filteredOn(
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isNotEmpty();
}
Expand All @@ -131,7 +130,7 @@ void alwaysEnabledClass(String pattern) {
//@formatter:on
@ParameterizedTest
void alwaysDisabledClass(String pattern) {
var executionConditions = asList(new AVanillaEmpty(), new BVanillaEmpty());
var executionConditions = List.of(new AVanillaEmpty(), new BVanillaEmpty());
assertThat(executionConditions).filteredOn(
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

package org.junit.platform.commons.util;

import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -25,6 +22,8 @@
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -52,21 +51,21 @@ void getOnlyElementWithNullCollection() {
@Test
void getOnlyElementWithEmptyCollection() {
var exception = assertThrows(PreconditionViolationException.class,
() -> CollectionUtils.getOnlyElement(emptySet()));
() -> CollectionUtils.getOnlyElement(Set.of()));
assertEquals("collection must contain exactly one element: []", exception.getMessage());
}

@Test
void getOnlyElementWithSingleElementCollection() {
var expected = new Object();
var actual = CollectionUtils.getOnlyElement(singleton(expected));
var actual = CollectionUtils.getOnlyElement(Set.of(expected));
assertSame(expected, actual);
}

@Test
void getOnlyElementWithMultiElementCollection() {
var exception = assertThrows(PreconditionViolationException.class,
() -> CollectionUtils.getOnlyElement(asList("foo", "bar")));
() -> CollectionUtils.getOnlyElement(List.of("foo", "bar")));
assertEquals("collection must contain exactly one element: [foo, bar]", exception.getMessage());
}

Expand Down Expand Up @@ -159,7 +158,7 @@ public Stream<String> stream() {
@SuppressWarnings("unchecked")
void toStreamWithIterable() {

Iterable<String> input = () -> asList("foo", "bar").iterator();
Iterable<String> input = () -> List.of("foo", "bar").iterator();

var result = (Stream<String>) CollectionUtils.toStream(input);

Expand All @@ -169,7 +168,7 @@ void toStreamWithIterable() {
@Test
@SuppressWarnings("unchecked")
void toStreamWithIterator() {
var input = asList("foo", "bar").iterator();
var input = List.of("foo", "bar").iterator();

var result = (Stream<String>) CollectionUtils.toStream(input);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

Expand Down Expand Up @@ -91,13 +90,15 @@ void getAttributeFromDefaultPackageMemberIsEmpty() throws Exception {

@TestFactory
List<DynamicTest> attributesFromValueWrapperClassArePresent() {
return Arrays.asList(dynamicTest("getName", isPresent(Package::getName)),
return List.of( //
dynamicTest("getName", isPresent(Package::getName)),
dynamicTest("getImplementationTitle", isPresent(Package::getImplementationTitle)),
dynamicTest("getImplementationVendor", isPresent(Package::getImplementationVendor)),
dynamicTest("getImplementationVersion", isPresent(Package::getImplementationVersion)),
dynamicTest("getSpecificationTitle", isPresent(Package::getSpecificationTitle)),
dynamicTest("getSpecificationVendor", isPresent(Package::getSpecificationVendor)),
dynamicTest("getSpecificationVersion", isPresent(Package::getSpecificationVersion)));
dynamicTest("getSpecificationVersion", isPresent(Package::getSpecificationVersion)) //
);
}

private Executable isPresent(Function<Package, String> function) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

package org.junit.platform.commons.util;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
Expand All @@ -21,7 +20,6 @@
import static org.junit.platform.commons.util.Preconditions.notEmpty;
import static org.junit.platform.commons.util.Preconditions.notNull;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

Expand Down Expand Up @@ -70,7 +68,7 @@ void notEmptyPassesForNonEmptyArray() {

@Test
void notEmptyPassesForNonEmptyCollection() {
Collection<String> collection = Arrays.asList("a", "b", "c");
Collection<String> collection = List.of("a", "b", "c");
var nonEmptyCollection = notEmpty(collection, () -> "should not fail");
assertSame(collection, nonEmptyCollection);
}
Expand Down Expand Up @@ -117,7 +115,7 @@ void notEmptyThrowsForEmptyArray() {
void notEmptyThrowsForEmptyCollection() {
var message = "collection is empty";

var exception = assertThrows(PreconditionViolationException.class, () -> notEmpty(emptyList(), message));
var exception = assertThrows(PreconditionViolationException.class, () -> notEmpty(List.of(), message));

assertEquals(message, exception.getMessage());
}
Expand All @@ -134,10 +132,10 @@ void containsNoNullElementsPassesForArrayThatIsNullOrEmpty() {
@Test
void containsNoNullElementsPassesForCollectionThatIsNullOrEmpty() {
containsNoNullElements((List<?>) null, "collection is null");
containsNoNullElements(emptyList(), "collection is empty");
containsNoNullElements(List.of(), "collection is empty");

containsNoNullElements((List<?>) null, () -> "collection is null");
containsNoNullElements(emptyList(), () -> "collection is empty");
containsNoNullElements(List.of(), () -> "collection is empty");
}

@Test
Expand All @@ -149,7 +147,7 @@ void containsNoNullElementsPassesForArrayContainingNonNullElements() {

@Test
void containsNoNullElementsPassesForCollectionContainingNonNullElements() {
Collection<String> input = Arrays.asList("a", "b", "c");
var input = List.of("a", "b", "c");
var output = containsNoNullElements(input, "message");
assertSame(input, output);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
Expand Down Expand Up @@ -229,7 +228,7 @@ public void execute() throws Throwable {
assumeTrue(Files.isReadable(path), "can not read: " + path);

var expectedLines = Files.readAllLines(path, UTF_8);
var actualLines = asList(result.out.split("\\R"));
var actualLines = List.of(result.out.split("\\R"));

assertLinesMatch(expectedLines, actualLines);
}
Expand Down
Loading

0 comments on commit 2b7d5b1

Please sign in to comment.