Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced TestDescriptorResolver for JUnit5TestEngine
- Loading branch information
Showing
7 changed files
with
279 additions
and
51 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
65 changes: 65 additions & 0 deletions
65
...t5-engine/src/main/java/org/junit/gen5/engine/junit5/ClassNameTestDescriptorResolver.java
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,65 @@ | |||
/* | |||
* Copyright 2015 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.gen5.engine.junit5; | |||
|
|||
import static java.util.stream.Collectors.toList; | |||
import static org.junit.gen5.commons.util.ReflectionUtils.loadClass; | |||
|
|||
import java.util.Arrays; | |||
import java.util.LinkedList; | |||
import java.util.List; | |||
|
|||
import org.junit.gen5.api.Test; | |||
import org.junit.gen5.engine.ClassNameSpecification; | |||
import org.junit.gen5.engine.EngineDescriptor; | |||
import org.junit.gen5.engine.TestDescriptor; | |||
|
|||
/** | |||
* @author Stefan Bechtold | |||
* @since 5.0 | |||
*/ | |||
public class ClassNameTestDescriptorResolver | |||
implements TestDescriptorResolver<ClassNameSpecification, JavaClassTestDescriptor> { | |||
|
|||
@Override | |||
public JavaClassTestDescriptor resolve(TestDescriptor parent, ClassNameSpecification element) { | |||
Class<?> clazz = loadClass(element.getClassName()); | |||
return new JavaClassTestDescriptor(clazz, parent); | |||
} | |||
|
|||
@Override | |||
public List<TestDescriptor> resolveChildren(JavaClassTestDescriptor parent, ClassNameSpecification element) { | |||
if (parent.getUniqueId().endsWith(element.getClassName())) { | |||
// TODO fetch children resolvers from according to type | |||
List<TestDescriptor> children = new LinkedList<>(); | |||
|
|||
children.addAll(Arrays.stream(parent.getTestClass().getDeclaredMethods()).filter( | |||
method -> method.isAnnotationPresent(Test.class)).map( | |||
method -> new JavaMethodTestDescriptor(method, parent)).collect(toList())); | |||
|
|||
List<JavaClassTestDescriptor> groups = Arrays.stream(parent.getTestClass().getDeclaredClasses()).filter( | |||
Class::isMemberClass).map(clazz -> new JavaClassTestDescriptor(clazz, parent)).collect(toList()); | |||
children.addAll(groups); | |||
|
|||
groups.forEach(group -> children.addAll(resolveChildren(group, element))); | |||
|
|||
return children; | |||
} | |||
else { | |||
JavaClassTestDescriptor child = resolve(parent, element); | |||
List<TestDescriptor> children = resolveChildren(child, element); | |||
List<TestDescriptor> result = new LinkedList<>(); | |||
result.add(child); | |||
result.addAll(children); | |||
return result; | |||
} | |||
} | |||
} |
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
27 changes: 27 additions & 0 deletions
27
junit5-engine/src/main/java/org/junit/gen5/engine/junit5/TestDescriptorResolver.java
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
/* | |||
* Copyright 2015 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.gen5.engine.junit5; | |||
|
|||
import java.util.List; | |||
|
|||
import org.junit.gen5.engine.TestDescriptor; | |||
import org.junit.gen5.engine.TestPlanSpecificationElement; | |||
|
|||
/** | |||
* @author Stefan Bechtold | |||
* @since 5.0 | |||
*/ | |||
interface TestDescriptorResolver<SPECIFICATION_ELEMENT extends TestPlanSpecificationElement, TEST_DESCRIPTOR extends TestDescriptor> { | |||
|
|||
TEST_DESCRIPTOR resolve(TestDescriptor parent, SPECIFICATION_ELEMENT element); | |||
|
|||
List<TestDescriptor> resolveChildren(TEST_DESCRIPTOR parent, SPECIFICATION_ELEMENT element); | |||
} |
39 changes: 39 additions & 0 deletions
39
junit5-engine/src/main/java/org/junit/gen5/engine/junit5/TestDescriptorResolverRegistry.java
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
/* | |||
* Copyright 2015 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.gen5.engine.junit5; | |||
|
|||
import java.util.HashMap; | |||
import java.util.Map; | |||
|
|||
import org.junit.gen5.engine.TestPlanSpecificationElement; | |||
|
|||
/** | |||
* @author Stefan Bechtold | |||
* @since 5.0 | |||
*/ | |||
class TestDescriptorResolverRegistry { | |||
|
|||
private final Map<Class<? extends TestPlanSpecificationElement>, TestDescriptorResolver> resolvers = new HashMap<>(); | |||
|
|||
public TestDescriptorResolver forType(Class<? extends TestPlanSpecificationElement> type) { | |||
if (resolvers.containsKey(type)) { | |||
return resolvers.get(type); | |||
} | |||
else { | |||
throw new UnsupportedOperationException( | |||
"There is no specification resolver registered for type: " + type.getName()); | |||
} | |||
} | |||
|
|||
public void addResolver(Class<? extends TestPlanSpecificationElement> element, TestDescriptorResolver resolver) { | |||
resolvers.put(element, resolver); | |||
} | |||
} |
109 changes: 109 additions & 0 deletions
109
junit5-engine/src/main/java/org/junit/gen5/engine/junit5/UniqueIdTestDescriptorResolver.java
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,109 @@ | |||
/* | |||
* Copyright 2015 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.gen5.engine.junit5; | |||
|
|||
import java.lang.reflect.Method; | |||
import java.util.ArrayList; | |||
import java.util.Collections; | |||
import java.util.LinkedList; | |||
import java.util.List; | |||
import java.util.regex.Matcher; | |||
import java.util.regex.Pattern; | |||
|
|||
import org.junit.gen5.commons.util.Preconditions; | |||
import org.junit.gen5.commons.util.ReflectionUtils; | |||
import org.junit.gen5.engine.EngineDescriptor; | |||
import org.junit.gen5.engine.TestDescriptor; | |||
import org.junit.gen5.engine.UniqueIdSpecification; | |||
|
|||
/** | |||
* <p>The pattern of the unique ID takes the form of | |||
* <code>{fully qualified class name}#{method name}({comma separated list | |||
* of method parameter types})</code>, where each method parameter type is | |||
* a fully qualified class name or a primitive type. For example, | |||
* {@code org.example.MyTests#test()} references the {@code test()} method | |||
* in the {@code org.example.MyTests} class that does not accept parameters. | |||
* Similarly, {@code org.example.MyTests#test(java.lang.String, java.math.BigDecimal)} | |||
* references the {@code test()} method in the {@code org.example.MyTests} | |||
* class that requires a {@code String} and {@code BigDecimal} as parameters. | |||
* | |||
* @author Sam Brannen | |||
* @author Stefan Bechtold | |||
* @since 5.0 | |||
*/ | |||
public class UniqueIdTestDescriptorResolver implements TestDescriptorResolver<UniqueIdSpecification, TestDescriptor> { | |||
|
|||
// The following pattern only supports descriptors for test methods. | |||
// TODO Support descriptors for test classes. | |||
// TODO Decide if we want to support descriptors for packages. | |||
private static final Pattern UID_PATTERN = Pattern.compile("^(?:([^:]+):)?(?:([^#]+)#)?(?:([^(]+)\\(([^)]*)\\))?$"); | |||
|
|||
@Override | |||
public TestDescriptor resolve(TestDescriptor parent, UniqueIdSpecification element) { | |||
String uid = element.getUniqueId(); | |||
Preconditions.notEmpty(uid, "UniqueID must not be empty"); | |||
Preconditions.condition(uid.startsWith(parent.getUniqueId()), | |||
String.format("UniqueID '%s' must start with UniqueID of parent '%s'!", uid, parent.getUniqueId())); | |||
|
|||
Matcher matcher = UID_PATTERN.matcher(uid); | |||
Preconditions.condition(matcher.matches(), () -> String.format("Given UniqueID '%s' was not recognised!", uid)); | |||
|
|||
String className = matcher.group(2); | |||
String methodName = matcher.group(3); | |||
String methodParameters = matcher.group(4); | |||
|
|||
Class<?> clazz = ReflectionUtils.loadClass(className); | |||
if (parent instanceof EngineDescriptor) { | |||
return new JavaClassTestDescriptor(clazz, parent); | |||
} | |||
else if (parent instanceof JavaClassTestDescriptor) { | |||
JavaClassTestDescriptor group = (JavaClassTestDescriptor) parent; | |||
|
|||
try { | |||
List<Class<?>> paramTypeList = new ArrayList<>(); | |||
for (String type : methodParameters.split(",")) { | |||
type = type.trim(); | |||
if (!type.isEmpty()) { | |||
paramTypeList.add(ReflectionUtils.loadClass(type)); | |||
} | |||
} | |||
|
|||
Class<?>[] parameterTypes = paramTypeList.toArray(new Class<?>[paramTypeList.size()]); | |||
JavaClassTestDescriptor testClassGroup = group; | |||
Method method = testClassGroup.getTestClass().getDeclaredMethod(methodName, parameterTypes); | |||
return new JavaMethodTestDescriptor(method, testClassGroup); | |||
} | |||
catch (NoSuchMethodException e) { | |||
throw new IllegalStateException("Failed to get method with name '" + methodName + "'.", e); | |||
} | |||
} | |||
else { | |||
throw new IllegalStateException( | |||
String.format("Given UniqueID '%s' could not be completely resolved!", uid)); | |||
} | |||
} | |||
|
|||
@Override | |||
public List<TestDescriptor> resolveChildren(TestDescriptor parent, UniqueIdSpecification element) { | |||
if (element.getUniqueId().equals(parent.getUniqueId())) { | |||
return Collections.emptyList(); | |||
} | |||
else { | |||
TestDescriptor child = resolve(parent, element); | |||
List<TestDescriptor> children = resolveChildren(child, element); | |||
|
|||
List<TestDescriptor> result = new LinkedList<>(); | |||
result.add(child); | |||
result.addAll(children); | |||
return result; | |||
} | |||
} | |||
} |
Oops, something went wrong.