Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted method finding during discovery into MethodFinder object.
- Loading branch information
Showing
2 changed files
with
71 additions
and
32 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
68 changes: 68 additions & 0 deletions
68
junit5-engine/src/main/java/org/junit/gen5/engine/junit5/discovery/MethodFinder.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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.gen5.engine.junit5.discovery; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.gen5.commons.util.PreconditionViolationException; | ||
import org.junit.gen5.commons.util.ReflectionUtils; | ||
import org.junit.gen5.engine.UniqueId; | ||
|
||
public class MethodFinder { | ||
|
||
public Optional<Method> findMethod(String methodSpecPart, Class<?> clazz) { | ||
try { | ||
|
||
// TODO Throw IAE when format wrong. Currently you get IndexOutOfBoundsException. | ||
int startParams = methodSpecPart.indexOf('('); | ||
String methodName = methodSpecPart.substring(0, startParams); | ||
int endParams = methodSpecPart.lastIndexOf(')'); | ||
String paramsPart = methodSpecPart.substring(startParams + 1, endParams); | ||
Class<?>[] parameterTypes = resolveParameterTypes(paramsPart); | ||
return findMethod(clazz, methodName, parameterTypes); | ||
} | ||
catch (RuntimeException rte) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private Class<?>[] resolveParameterTypes(String paramsPart) { | ||
if (paramsPart.isEmpty()) { | ||
return new Class<?>[0]; | ||
} | ||
|
||
// @formatter:off | ||
List<Class<?>> types = Arrays.stream(paramsPart.split(",")) | ||
.map(className -> loadRequiredParameterClass(className)) | ||
.collect(Collectors.toList()); | ||
// @formatter:on | ||
|
||
return types.toArray(new Class<?>[types.size()]); | ||
} | ||
|
||
private Optional<Method> findMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) { | ||
return ReflectionUtils.findMethod(clazz, methodName, parameterTypes); | ||
} | ||
|
||
private Class<?> loadRequiredParameterClass(String className) { | ||
return ReflectionUtils.loadClass(className).orElseThrow(() -> new RuntimeException("Not found: " + className)); | ||
} | ||
|
||
private static RuntimeException createCannotResolveUniqueIdException(UniqueId fullUniqueId, String uniqueIdPart) { | ||
return new PreconditionViolationException( | ||
String.format("Cannot resolve part '%s' of unique ID '%s'", uniqueIdPart, fullUniqueId.getUniqueString())); | ||
} | ||
|
||
} |