This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discover and execute TestNG-based test methods
Execution of discovered methods is a best-effort solution. Should be transformed to use TestNG entry-point correctly.
- Loading branch information
Showing
5 changed files
with
172 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.testng.junit5; | ||
|
||
import static org.junit.platform.commons.util.ReflectionUtils.findAllClassesInClasspathRoot; | ||
import static org.junit.platform.commons.util.ReflectionUtils.findAllClassesInPackage; | ||
|
||
import java.util.Collection; | ||
import java.util.function.BiConsumer; | ||
import org.junit.platform.commons.util.ClassFilter; | ||
import org.junit.platform.engine.EngineDiscoveryRequest; | ||
import org.junit.platform.engine.discovery.ClassSelector; | ||
import org.junit.platform.engine.discovery.ClasspathRootSelector; | ||
import org.junit.platform.engine.discovery.PackageSelector; | ||
import org.junit.platform.engine.support.descriptor.EngineDescriptor; | ||
|
||
/** This should be in org.junit.platform.engine.support... */ | ||
class DiscoveryHelper { | ||
|
||
private final EngineDiscoveryRequest engineDiscoveryRequest; | ||
private final ClassFilter classFilter; | ||
|
||
DiscoveryHelper(EngineDiscoveryRequest engineDiscoveryRequest, ClassFilter classFilter) { | ||
this.engineDiscoveryRequest = engineDiscoveryRequest; | ||
this.classFilter = classFilter; | ||
} | ||
|
||
void discover(EngineDescriptor engine, BiConsumer<EngineDescriptor, Class<?>> handler) { | ||
// class-path root | ||
engineDiscoveryRequest | ||
.getSelectorsByType(ClasspathRootSelector.class) | ||
.stream() | ||
.map(ClasspathRootSelector::getClasspathRoot) | ||
.map(uri -> findAllClassesInClasspathRoot(uri, classFilter)) | ||
.flatMap(Collection::stream) | ||
.forEach(candidate -> handler.accept(engine, candidate)); | ||
|
||
// package | ||
engineDiscoveryRequest | ||
.getSelectorsByType(PackageSelector.class) | ||
.stream() | ||
.map(PackageSelector::getPackageName) | ||
.map(packageName -> findAllClassesInPackage(packageName, classFilter)) | ||
.flatMap(Collection::stream) | ||
.forEach(candidate -> handler.accept(engine, candidate)); | ||
|
||
// class | ||
engineDiscoveryRequest | ||
.getSelectorsByType(ClassSelector.class) | ||
.stream() | ||
.map(ClassSelector::getJavaClass) | ||
.filter(classFilter) | ||
.forEach(candidate -> handler.accept(engine, candidate)); | ||
|
||
// TODO Add missing selector types... | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.testng.junit5; | ||
|
||
import static org.junit.platform.commons.util.ReflectionUtils.isAbstract; | ||
import static org.junit.platform.commons.util.ReflectionUtils.isInnerClass; | ||
import static org.junit.platform.commons.util.ReflectionUtils.isPublic; | ||
|
||
import org.junit.platform.engine.TestSource; | ||
import org.junit.platform.engine.UniqueId; | ||
import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor; | ||
import org.junit.platform.engine.support.descriptor.ClassSource; | ||
|
||
public class NGClassDescriptor extends AbstractTestDescriptor { | ||
|
||
static boolean isCandidate(Class<?> candidate) { | ||
if (!isPublic(candidate)) { | ||
return false; | ||
} | ||
if (isAbstract(candidate)) { | ||
return false; | ||
} | ||
if (isInnerClass(candidate)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
static NGClassDescriptor newContainerDescriptor(UniqueId container, Class<?> candidate) { | ||
var id = container.append("testng-class", candidate.getTypeName()); | ||
return new NGClassDescriptor(id, candidate.getSimpleName(), ClassSource.from(candidate)); | ||
} | ||
|
||
private NGClassDescriptor(UniqueId uniqueId, String displayName, TestSource source) { | ||
super(uniqueId, displayName, source); | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.CONTAINER; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.testng.junit5; | ||
|
||
import java.lang.reflect.Method; | ||
import org.junit.platform.engine.UniqueId; | ||
import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor; | ||
import org.junit.platform.engine.support.descriptor.MethodSource; | ||
|
||
public class NGMethodDescriptor extends AbstractTestDescriptor { | ||
|
||
static NGMethodDescriptor newMethodDescriptor(UniqueId container, Method method) { | ||
var id = container.append("testng-method", method.getName()); | ||
return new NGMethodDescriptor(id, method); | ||
} | ||
|
||
private final Method method; | ||
|
||
private NGMethodDescriptor(UniqueId uniqueId, Method method) { | ||
super(uniqueId, method.getName(), MethodSource.from(method)); | ||
this.method = method; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.TEST; | ||
} | ||
|
||
public Method getMethod() { | ||
return method; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters