Skip to content

Commit 2b7d5b1

Browse files
committed
Use Java 9 collection factory methods
1 parent 5c42adb commit 2b7d5b1

28 files changed

+279
-310
lines changed

platform-tests/src/test/java/org/junit/platform/commons/util/AnnotationUtilsTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
package org.junit.platform.commons.util;
1212

13-
import static java.util.Arrays.asList;
1413
import static java.util.stream.Collectors.toList;
1514
import static org.assertj.core.api.Assertions.assertThat;
1615
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -239,7 +238,7 @@ void findRepeatableAnnotationsWithComposedTagBeforeContainer() {
239238
}
240239

241240
private void assertTagsFound(Class<?> clazz, String... tags) {
242-
assertEquals(asList(tags),
241+
assertEquals(List.of(tags),
243242
findRepeatableAnnotations(clazz, Tag.class).stream().map(Tag::value).collect(toList()),
244243
() -> "Tags found for class " + clazz.getName());
245244
}
@@ -297,7 +296,7 @@ void findInheritedRepeatableAnnotationsWithMultipleComposedAnnotationsOnSupercla
297296
}
298297

299298
private void assertExtensionsFound(Class<?> clazz, String... tags) {
300-
assertEquals(asList(tags),
299+
assertEquals(List.of(tags),
301300
findRepeatableAnnotations(clazz, ExtendWith.class).stream().map(ExtendWith::value).collect(toList()),
302301
() -> "Extensions found for class " + clazz.getName());
303302
}
@@ -490,22 +489,22 @@ void findPublicAnnotatedFieldsForDirectlyAnnotatedFieldOfWrongFieldType() {
490489
void findPublicAnnotatedFieldsForDirectlyAnnotatedField() {
491490
var fields = findPublicAnnotatedFields(getClass(), String.class, Annotation1.class);
492491
assertNotNull(fields);
493-
assertIterableEquals(asList("directlyAnnotatedField"), asNames(fields));
492+
assertIterableEquals(List.of("directlyAnnotatedField"), asNames(fields));
494493
}
495494

496495
@Test
497496
void findPublicAnnotatedFieldsForMetaAnnotatedField() {
498497
var fields = findPublicAnnotatedFields(getClass(), Number.class, Annotation1.class);
499498
assertNotNull(fields);
500499
assertEquals(1, fields.size());
501-
assertIterableEquals(asList("metaAnnotatedField"), asNames(fields));
500+
assertIterableEquals(List.of("metaAnnotatedField"), asNames(fields));
502501
}
503502

504503
@Test
505504
void findPublicAnnotatedFieldsForDirectlyAnnotatedFieldInInterface() {
506505
var fields = findPublicAnnotatedFields(InterfaceWithAnnotatedFields.class, String.class, Annotation1.class);
507506
assertNotNull(fields);
508-
assertIterableEquals(asList("foo"), asNames(fields));
507+
assertIterableEquals(List.of("foo"), asNames(fields));
509508
}
510509

511510
@Test

platform-tests/src/test/java/org/junit/platform/commons/util/ClassNamePatternFilterUtilsTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
package org.junit.platform.commons.util;
1212

13-
import static java.util.Arrays.asList;
1413
import static org.assertj.core.api.Assertions.assertThat;
1514

1615
import java.util.List;
@@ -47,7 +46,7 @@ class ClassNamePatternFilterUtilsTests {
4746
//@formatter:on
4847
@ParameterizedTest
4948
void alwaysEnabledConditions(String pattern) {
50-
List<? extends ExecutionCondition> executionConditions = asList(new AExecutionConditionClass(),
49+
List<? extends ExecutionCondition> executionConditions = List.of(new AExecutionConditionClass(),
5150
new BExecutionConditionClass());
5251
assertThat(executionConditions).filteredOn(
5352
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isNotEmpty();
@@ -64,7 +63,7 @@ void alwaysEnabledConditions(String pattern) {
6463
//@formatter:on
6564
@ParameterizedTest
6665
void alwaysDisabledConditions(String pattern) {
67-
List<? extends ExecutionCondition> executionConditions = asList(new AExecutionConditionClass(),
66+
List<? extends ExecutionCondition> executionConditions = List.of(new AExecutionConditionClass(),
6867
new BExecutionConditionClass());
6968
assertThat(executionConditions).filteredOn(
7069
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isEmpty();
@@ -81,7 +80,7 @@ void alwaysDisabledConditions(String pattern) {
8180
//@formatter:on
8281
@ParameterizedTest
8382
void alwaysEnabledListeners(String pattern) {
84-
List<? extends TestExecutionListener> executionConditions = asList(new ATestExecutionListenerClass(),
83+
List<? extends TestExecutionListener> executionConditions = List.of(new ATestExecutionListenerClass(),
8584
new BTestExecutionListenerClass());
8685
assertThat(executionConditions).filteredOn(
8786
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isNotEmpty();
@@ -98,7 +97,7 @@ void alwaysEnabledListeners(String pattern) {
9897
//@formatter:on
9998
@ParameterizedTest
10099
void alwaysDisabledListeners(String pattern) {
101-
List<? extends TestExecutionListener> executionConditions = asList(new ATestExecutionListenerClass(),
100+
List<? extends TestExecutionListener> executionConditions = List.of(new ATestExecutionListenerClass(),
102101
new BTestExecutionListenerClass());
103102
assertThat(executionConditions).filteredOn(
104103
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isEmpty();
@@ -115,7 +114,7 @@ void alwaysDisabledListeners(String pattern) {
115114
//@formatter:on
116115
@ParameterizedTest
117116
void alwaysEnabledClass(String pattern) {
118-
var executionConditions = asList(new AVanillaEmpty(), new BVanillaEmpty());
117+
var executionConditions = List.of(new AVanillaEmpty(), new BVanillaEmpty());
119118
assertThat(executionConditions).filteredOn(
120119
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isNotEmpty();
121120
}
@@ -131,7 +130,7 @@ void alwaysEnabledClass(String pattern) {
131130
//@formatter:on
132131
@ParameterizedTest
133132
void alwaysDisabledClass(String pattern) {
134-
var executionConditions = asList(new AVanillaEmpty(), new BVanillaEmpty());
133+
var executionConditions = List.of(new AVanillaEmpty(), new BVanillaEmpty());
135134
assertThat(executionConditions).filteredOn(
136135
ClassNamePatternFilterUtils.excludeMatchingClasses(pattern)).isEmpty();
137136
}

platform-tests/src/test/java/org/junit/platform/commons/util/CollectionUtilsTests.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
package org.junit.platform.commons.util;
1212

13-
import static java.util.Arrays.asList;
14-
import static java.util.Collections.emptySet;
15-
import static java.util.Collections.singleton;
1613
import static java.util.stream.Collectors.toList;
1714
import static org.assertj.core.api.Assertions.assertThat;
1815
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -25,6 +22,8 @@
2522
import java.lang.reflect.Array;
2623
import java.util.ArrayList;
2724
import java.util.Collection;
25+
import java.util.List;
26+
import java.util.Set;
2827
import java.util.concurrent.atomic.AtomicBoolean;
2928
import java.util.stream.DoubleStream;
3029
import java.util.stream.IntStream;
@@ -52,21 +51,21 @@ void getOnlyElementWithNullCollection() {
5251
@Test
5352
void getOnlyElementWithEmptyCollection() {
5453
var exception = assertThrows(PreconditionViolationException.class,
55-
() -> CollectionUtils.getOnlyElement(emptySet()));
54+
() -> CollectionUtils.getOnlyElement(Set.of()));
5655
assertEquals("collection must contain exactly one element: []", exception.getMessage());
5756
}
5857

5958
@Test
6059
void getOnlyElementWithSingleElementCollection() {
6160
var expected = new Object();
62-
var actual = CollectionUtils.getOnlyElement(singleton(expected));
61+
var actual = CollectionUtils.getOnlyElement(Set.of(expected));
6362
assertSame(expected, actual);
6463
}
6564

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

@@ -159,7 +158,7 @@ public Stream<String> stream() {
159158
@SuppressWarnings("unchecked")
160159
void toStreamWithIterable() {
161160

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

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

@@ -169,7 +168,7 @@ void toStreamWithIterable() {
169168
@Test
170169
@SuppressWarnings("unchecked")
171170
void toStreamWithIterator() {
172-
var input = asList("foo", "bar").iterator();
171+
var input = List.of("foo", "bar").iterator();
173172

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

platform-tests/src/test/java/org/junit/platform/commons/util/PackageUtilsTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import static org.junit.jupiter.api.Assertions.assertTrue;
1717
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
1818

19-
import java.util.Arrays;
2019
import java.util.List;
2120
import java.util.function.Function;
2221

@@ -91,13 +90,15 @@ void getAttributeFromDefaultPackageMemberIsEmpty() throws Exception {
9190

9291
@TestFactory
9392
List<DynamicTest> attributesFromValueWrapperClassArePresent() {
94-
return Arrays.asList(dynamicTest("getName", isPresent(Package::getName)),
93+
return List.of( //
94+
dynamicTest("getName", isPresent(Package::getName)),
9595
dynamicTest("getImplementationTitle", isPresent(Package::getImplementationTitle)),
9696
dynamicTest("getImplementationVendor", isPresent(Package::getImplementationVendor)),
9797
dynamicTest("getImplementationVersion", isPresent(Package::getImplementationVersion)),
9898
dynamicTest("getSpecificationTitle", isPresent(Package::getSpecificationTitle)),
9999
dynamicTest("getSpecificationVendor", isPresent(Package::getSpecificationVendor)),
100-
dynamicTest("getSpecificationVersion", isPresent(Package::getSpecificationVersion)));
100+
dynamicTest("getSpecificationVersion", isPresent(Package::getSpecificationVersion)) //
101+
);
101102
}
102103

103104
private Executable isPresent(Function<Package, String> function) {

platform-tests/src/test/java/org/junit/platform/commons/util/PreconditionsTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
package org.junit.platform.commons.util;
1212

13-
import static java.util.Collections.emptyList;
1413
import static java.util.Collections.singletonList;
1514
import static org.junit.jupiter.api.Assertions.assertEquals;
1615
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -21,7 +20,6 @@
2120
import static org.junit.platform.commons.util.Preconditions.notEmpty;
2221
import static org.junit.platform.commons.util.Preconditions.notNull;
2322

24-
import java.util.Arrays;
2523
import java.util.Collection;
2624
import java.util.List;
2725

@@ -70,7 +68,7 @@ void notEmptyPassesForNonEmptyArray() {
7068

7169
@Test
7270
void notEmptyPassesForNonEmptyCollection() {
73-
Collection<String> collection = Arrays.asList("a", "b", "c");
71+
Collection<String> collection = List.of("a", "b", "c");
7472
var nonEmptyCollection = notEmpty(collection, () -> "should not fail");
7573
assertSame(collection, nonEmptyCollection);
7674
}
@@ -117,7 +115,7 @@ void notEmptyThrowsForEmptyArray() {
117115
void notEmptyThrowsForEmptyCollection() {
118116
var message = "collection is empty";
119117

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

122120
assertEquals(message, exception.getMessage());
123121
}
@@ -134,10 +132,10 @@ void containsNoNullElementsPassesForArrayThatIsNullOrEmpty() {
134132
@Test
135133
void containsNoNullElementsPassesForCollectionThatIsNullOrEmpty() {
136134
containsNoNullElements((List<?>) null, "collection is null");
137-
containsNoNullElements(emptyList(), "collection is empty");
135+
containsNoNullElements(List.of(), "collection is empty");
138136

139137
containsNoNullElements((List<?>) null, () -> "collection is null");
140-
containsNoNullElements(emptyList(), () -> "collection is empty");
138+
containsNoNullElements(List.of(), () -> "collection is empty");
141139
}
142140

143141
@Test
@@ -149,7 +147,7 @@ void containsNoNullElementsPassesForArrayContainingNonNullElements() {
149147

150148
@Test
151149
void containsNoNullElementsPassesForCollectionContainingNonNullElements() {
152-
Collection<String> input = Arrays.asList("a", "b", "c");
150+
var input = List.of("a", "b", "c");
153151
var output = containsNoNullElements(input, "message");
154152
assertSame(input, output);
155153

platform-tests/src/test/java/org/junit/platform/console/ConsoleDetailsTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

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

231230
var expectedLines = Files.readAllLines(path, UTF_8);
232-
var actualLines = asList(result.out.split("\\R"));
231+
var actualLines = List.of(result.out.split("\\R"));
233232

234233
assertLinesMatch(expectedLines, actualLines);
235234
}

0 commit comments

Comments
 (0)