Skip to content

Adds DiscoveryCallbacks #577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.jupiter.api.extension;

import org.junit.platform.commons.meta.API;

/**
* @author swm16
*
*/
@FunctionalInterface
@API(API.Usage.Experimental)
public interface AfterDiscoveryCallback extends Extension {

void afterDiscovery(Object testDescriptor) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.jupiter.engine;

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

import org.junit.jupiter.api.extension.AfterDiscoveryCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.engine.extension.ExtensionRegistry;
import org.junit.platform.commons.util.ExceptionUtils;
import org.junit.platform.engine.TestDescriptor;

public class AfterDiscoveryCallbackInvoker {

static final Class<? extends Extension> AFTER_DISCOVERY_CALLBACK_TYPE = AfterDiscoveryCallback.class;
static final List<Class<? extends Extension>> EXTENSION_TYPES = Arrays.asList(AFTER_DISCOVERY_CALLBACK_TYPE);

void invokeAfterDiscoveryCallbacks(TestDescriptor engineDescriptor) {
TestDescriptor.Visitor visitor = (testDescriptor) -> {
ExtensionRegistry registry = getExtensionRegistry(testDescriptor);
registry.stream(AfterDiscoveryCallback.class).forEach(
(extension) -> executeAndMaskThrowable(() -> extension.afterDiscovery((testDescriptor))));
};
engineDescriptor.accept(visitor);
}

protected void executeAndMaskThrowable(Executable executable) {
try {
executable.execute();
}
catch (Throwable throwable) {
ExceptionUtils.throwAsUncheckedException(throwable);
}
}

ExtensionRegistry getExtensionRegistry(TestDescriptor testDescriptor) {
ExtensionRegistry defaultRegistry = ExtensionRegistry.createRegistryWithDefaultExtensions();
return defaultRegistry;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,18 @@ private void resolveDiscoveryRequest(EngineDiscoveryRequest discoveryRequest,
DiscoverySelectorResolver resolver = new DiscoverySelectorResolver();
resolver.resolveSelectors(discoveryRequest, engineDescriptor);
applyDiscoveryFilters(discoveryRequest, engineDescriptor);
invokeAfterDiscoveryCallbacks(engineDescriptor);
}

private void applyDiscoveryFilters(EngineDiscoveryRequest discoveryRequest,
JupiterEngineDescriptor engineDescriptor) {
new DiscoveryFilterApplier().applyAllFilters(discoveryRequest, engineDescriptor);
}

private void invokeAfterDiscoveryCallbacks(JupiterEngineDescriptor engineDescriptor) {
new AfterDiscoveryCallbackInvoker().invokeAfterDiscoveryCallbacks(engineDescriptor);
}

@Override
protected JupiterEngineExecutionContext createExecutionContext(ExecutionRequest request) {
return new JupiterEngineExecutionContext(request.getEngineExecutionListener(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.jupiter.engine.extension;

import org.junit.jupiter.api.extension.AfterDiscoveryCallback;
import org.junit.platform.engine.TestDescriptor;

/**
* @author swm16
*
*/
public class DiscoveryReportExtension implements AfterDiscoveryCallback {

/* (non-Javadoc)
* @see org.junit.jupiter.api.extension.AfterDiscoveryCallback#afterDiscovery(java.lang.Object)
*/
@Override
public void afterDiscovery(Object object) {
if (object instanceof TestDescriptor) {
TestDescriptor testDescriptor = (TestDescriptor) object;
System.out.println("DiscoveryReportExtension.afterDiscovery(): UniqueId - " + testDescriptor.getUniqueId());
System.out.println(
"DiscoveryReportExtension.afterDiscovery(): DisplayName - " + testDescriptor.getDisplayName());
}
else {
System.out.println("afterDiscovery(): ERROR - That wasn't a TestDescriptor");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public class ExtensionRegistry {
private static final Logger LOG = Logger.getLogger(ExtensionRegistry.class.getName());

private static final List<Extension> DEFAULT_EXTENSIONS = Collections.unmodifiableList(
Arrays.asList(new DisabledCondition(), new TestInfoParameterResolver(), new TestReporterParameterResolver()));
Arrays.asList(new DisabledCondition(), new TestInfoParameterResolver(), new TestReporterParameterResolver(),
new DiscoveryReportExtension()));

/**
* Factory for creating and populating a new root registry with the default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ExtensionRegistryTests {
void newRegistryWithoutParentHasDefaultExtensions() {
List<Extension> extensions = registry.getExtensions(Extension.class);

assertEquals(3, extensions.size());
assertEquals(4, extensions.size());
assertExtensionRegistered(registry, DisabledCondition.class);
assertExtensionRegistered(registry, TestInfoParameterResolver.class);
assertExtensionRegistered(registry, TestReporterParameterResolver.class);
Expand Down