Skip to content

Commit 809f660

Browse files
committed
Use Java 17 ServiceLoader#stream in AbstractContainerGroupAssert
1 parent 851fe7e commit 809f660

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

Diff for: java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/AbstractContainerGroupAssert.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020

2121
import io.github.ascopes.jct.containers.ContainerGroup;
22-
import java.util.ArrayList;
2322
import java.util.List;
23+
import java.util.ServiceLoader;
2424
import org.assertj.core.api.AbstractAssert;
2525
import org.assertj.core.api.AbstractListAssert;
2626
import org.assertj.core.api.ObjectAssert;
@@ -74,9 +74,9 @@ public LocationAssert location() {
7474
requireNonNull(type, "type must not be null");
7575
isNotNull();
7676

77-
var items = new ArrayList<T>();
78-
actual.getServiceLoader(type).iterator().forEachRemaining(items::add);
79-
77+
var items = actual.getServiceLoader(type)
78+
.stream()
79+
.map(ServiceLoader.Provider::get);
8080
return assertThat(items);
8181
}
8282
}

Diff for: java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/TraceDiagnosticListAssert.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ protected TraceDiagnosticListAssert newAbstractIterableAssert(
353353
}
354354

355355
private Predicate<@Nullable TraceDiagnostic<? extends JavaFileObject>> kindIsOneOf(
356-
Iterable<Kind> kinds) {
356+
Iterable<Kind> kinds
357+
) {
357358
var kindsSet = new LinkedHashSet<Kind>();
358359
kinds.forEach(kindsSet::add);
359360

Diff for: java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/TypeAwareListAssert.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ protected A toAssert(@Nullable E value, String description) {
5656
protected TypeAwareListAssert<@Nullable E, A> newAbstractIterableAssert(
5757
Iterable<? extends @Nullable E> iterable
5858
) {
59-
var list = StreamSupport
60-
.stream(iterable.spliterator(), false)
61-
.toList();
62-
59+
var list = StreamSupport.stream(iterable.spliterator(), false).toList();
6360
return new TypeAwareListAssert<>(list, assertFactory);
6461
}
6562
}

Diff for: java-compiler-testing/src/test/java/io/github/ascopes/jct/assertions/AbstractContainerGroupAssertTest.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.github.ascopes.jct.containers.ContainerGroup;
2828
import java.util.List;
2929
import java.util.ServiceLoader;
30+
import java.util.stream.Stream;
3031
import org.assertj.core.api.AbstractListAssert;
3132
import org.jspecify.annotations.Nullable;
3233
import org.junit.jupiter.api.DisplayName;
@@ -117,7 +118,8 @@ void servicesReturnsListAssertionAcrossTheAvailableServices() {
117118
var impl3 = mock(SomeServiceType.class);
118119

119120
ServiceLoader<SomeServiceType> serviceLoader = mock();
120-
when(serviceLoader.iterator()).then(ctx -> List.of(impl1, impl2, impl3).iterator());
121+
when(serviceLoader.stream()).then(ctx -> Stream.of(impl1, impl2, impl3)
122+
.map(ServiceLoaderProvider::new));
121123

122124
var containerGroup = mock(ContainerGroup.class);
123125
when(containerGroup.getServiceLoader(any())).then(ctx -> serviceLoader);
@@ -148,4 +150,22 @@ static final class AssertionImpl
148150
super(containerGroup, AssertionImpl.class);
149151
}
150152
}
153+
154+
static class ServiceLoaderProvider<T> implements ServiceLoader.Provider<T> {
155+
private final T service;
156+
157+
ServiceLoaderProvider(T service) {
158+
this.service = service;
159+
}
160+
161+
@Override
162+
public T get() {
163+
return service;
164+
}
165+
166+
@Override
167+
public Class<? extends T> type() {
168+
return (Class<T>) service.getClass();
169+
}
170+
}
151171
}

0 commit comments

Comments
 (0)