Skip to content

Commit

Permalink
Use Java 17 ServiceLoader#stream in AbstractContainerGroupAssert
Browse files Browse the repository at this point in the history
  • Loading branch information
ascopes committed Jan 9, 2025
1 parent a4f1680 commit 6c8241c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static org.assertj.core.api.Assertions.assertThat;

import io.github.ascopes.jct.containers.ContainerGroup;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractListAssert;
import org.assertj.core.api.ObjectAssert;
Expand Down Expand Up @@ -74,9 +74,9 @@ public LocationAssert location() {
requireNonNull(type, "type must not be null");
isNotNull();

var items = new ArrayList<T>();
actual.getServiceLoader(type).iterator().forEachRemaining(items::add);

var items = actual.getServiceLoader(type)
.stream()
.map(ServiceLoader.Provider::get);
return assertThat(items);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ protected TraceDiagnosticListAssert newAbstractIterableAssert(
}

private Predicate<@Nullable TraceDiagnostic<? extends JavaFileObject>> kindIsOneOf(
Iterable<Kind> kinds) {
Iterable<Kind> kinds
) {
var kindsSet = new LinkedHashSet<Kind>();
kinds.forEach(kindsSet::add);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ protected A toAssert(@Nullable E value, String description) {
protected TypeAwareListAssert<@Nullable E, A> newAbstractIterableAssert(
Iterable<? extends @Nullable E> iterable
) {
var list = StreamSupport
.stream(iterable.spliterator(), false)
.toList();

var list = StreamSupport.stream(iterable.spliterator(), false).toList();
return new TypeAwareListAssert<>(list, assertFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.github.ascopes.jct.containers.ContainerGroup;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Stream;
import org.assertj.core.api.AbstractListAssert;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -117,7 +118,8 @@ void servicesReturnsListAssertionAcrossTheAvailableServices() {
var impl3 = mock(SomeServiceType.class);

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

var containerGroup = mock(ContainerGroup.class);
when(containerGroup.getServiceLoader(any())).then(ctx -> serviceLoader);
Expand Down Expand Up @@ -148,4 +150,22 @@ static final class AssertionImpl
super(containerGroup, AssertionImpl.class);
}
}

static class ServiceLoaderProvider<T> implements ServiceLoader.Provider<T> {
private final T service;

ServiceLoaderProvider(T service) {
this.service = service;
}

@Override
public T get() {
return service;
}

@Override
public Class<? extends T> type() {
return (Class<T>) service.getClass();
}
}
}

0 comments on commit 6c8241c

Please sign in to comment.