Skip to content

Commit

Permalink
Hide implementation details
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Jan 31, 2023
1 parent 872146d commit 700c2d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
package org.junit.platform.launcher;

import static org.apiguardian.api.API.Status.EXPERIMENTAL;
import static org.apiguardian.api.API.Status.INTERNAL;

import java.util.List;

import org.apiguardian.api.API;

Expand Down Expand Up @@ -58,18 +55,6 @@
@API(status = EXPERIMENTAL, since = "1.10")
public interface LauncherInterceptor {

LauncherInterceptor NOOP = new LauncherInterceptor() {
@Override
public <T> T intercept(Invocation<T> invocation) {
return invocation.proceed();
}

@Override
public void close() {
// do nothing
}
};

/**
* Intercept the supplied invocation.
*
Expand Down Expand Up @@ -97,28 +82,4 @@ interface Invocation<T> {
T proceed();
}

@API(status = INTERNAL, since = "1.10")
static LauncherInterceptor composite(List<LauncherInterceptor> interceptors) {
if (interceptors.isEmpty()) {
return NOOP;
}
return interceptors.stream() //
.skip(1) //
.reduce(interceptors.get(0), (a, b) -> new LauncherInterceptor() {
@Override
public void close() {
try {
a.close();
}
finally {
b.close();
}
}

@Override
public <T> T intercept(Invocation<T> invocation) {
return a.intercept(() -> b.intercept(invocation));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,27 @@
*/
class DefaultLauncherSession implements LauncherSession {

private static final LauncherInterceptor NOOP_INTERCEPTOR = new LauncherInterceptor() {
@Override
public <T> T intercept(Invocation<T> invocation) {
return invocation.proceed();
}

@Override
public void close() {
// do nothing
}
};

private final LauncherInterceptor interceptor;
private final LauncherSessionListener listener;
private final DelegatingLauncher launcher;

DefaultLauncherSession(List<LauncherInterceptor> interceptors, Supplier<LauncherSessionListener> listenerSupplier,
Supplier<Launcher> launcherSupplier) {
interceptor = LauncherInterceptor.composite(interceptors);
interceptor = composite(interceptors);
Launcher launcher;
if (interceptor == LauncherInterceptor.NOOP) {
if (interceptor == NOOP_INTERCEPTOR) {
this.listener = listenerSupplier.get();
launcher = launcherSupplier.get();
}
Expand Down Expand Up @@ -98,4 +110,28 @@ public void execute(TestPlan testPlan, TestExecutionListener... listeners) {
throw new PreconditionViolationException("Launcher session has already been closed");
}
}

private static LauncherInterceptor composite(List<LauncherInterceptor> interceptors) {
if (interceptors.isEmpty()) {
return NOOP_INTERCEPTOR;
}
return interceptors.stream() //
.skip(1) //
.reduce(interceptors.get(0), (a, b) -> new LauncherInterceptor() {
@Override
public void close() {
try {
a.close();
}
finally {
b.close();
}
}

@Override
public <T> T intercept(Invocation<T> invocation) {
return a.intercept(() -> b.intercept(invocation));
}
});
}
}

0 comments on commit 700c2d4

Please sign in to comment.