Skip to content

Commit

Permalink
Add missing preconditions for selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed May 30, 2016
1 parent 36f3f42 commit 89bc5ed
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 32 deletions.
Expand Up @@ -14,6 +14,7 @@

import org.junit.gen5.commons.meta.API;
import org.junit.gen5.commons.util.PreconditionViolationException;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.commons.util.ReflectionUtils;
import org.junit.gen5.engine.DiscoverySelector;

Expand All @@ -23,11 +24,14 @@
@API(Experimental)
public class ClassSelector implements DiscoverySelector {

public static ClassSelector forClass(Class<?> testClass) {
return new ClassSelector(testClass);
public static ClassSelector forClass(Class<?> clazz) {
Preconditions.notNull(clazz, "Class must not be null");
return new ClassSelector(clazz);
}

public static ClassSelector forClassName(String className) {
Preconditions.notBlank(className, "className must not be null or empty");

return forClass(ReflectionUtils.loadClass(className).orElseThrow(
() -> new PreconditionViolationException("Could not resolve class with name: " + className)));
}
Expand All @@ -39,7 +43,7 @@ private ClassSelector(Class<?> testClass) {
}

public Class<?> getTestClass() {
return testClass;
return this.testClass;
}

}
Expand Up @@ -19,6 +19,7 @@
import java.util.stream.Collectors;

import org.junit.gen5.commons.meta.API;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.engine.DiscoverySelector;

/**
Expand All @@ -32,6 +33,8 @@ public static List<DiscoverySelector> forPath(String path) {
}

public static List<DiscoverySelector> forPaths(Set<File> paths) {
Preconditions.notNull(paths, "paths must not be null");

// @formatter:off
return paths.stream()
.filter(File::exists)
Expand Down
Expand Up @@ -15,6 +15,7 @@
import java.lang.reflect.Method;

import org.junit.gen5.commons.meta.API;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.commons.util.ReflectionUtils;
import org.junit.gen5.engine.DiscoverySelector;

Expand All @@ -24,43 +25,46 @@
@API(Experimental)
public class MethodSelector implements DiscoverySelector {

public static MethodSelector forMethod(String testClassName, String testMethodName) {
Class<?> testClass = getTestClass(testClassName);
Method testMethod = getTestMethod(testClass, testMethodName);
return forMethod(testClass, testMethod);
public static MethodSelector forMethod(String className, String methodName) {
Preconditions.notBlank(className, "Class name must not be null or empty");
Preconditions.notBlank(methodName, "Method name must not be null or empty");
return forMethod(getTestClass(className), getTestMethod(getTestClass(className), methodName));
}

public static MethodSelector forMethod(Class<?> testClass, String testMethodName) {
Method testMethod = getTestMethod(testClass, testMethodName);
return forMethod(testClass, testMethod);
public static MethodSelector forMethod(Class<?> clazz, String methodName) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notBlank(methodName, "Method name must not be null or empty");
return forMethod(clazz, getTestMethod(clazz, methodName));
}

public static MethodSelector forMethod(Class<?> testClass, Method testMethod) {
return new MethodSelector(testClass, testMethod);
public static MethodSelector forMethod(Class<?> clazz, Method method) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notNull(method, "Method must not be null");
return new MethodSelector(clazz, method);
}

private final Class<?> testClass;
private final Method testMethod;
private final Class<?> clazz;
private final Method method;

private MethodSelector(Class<?> testClass, Method testMethod) {
this.testClass = testClass;
this.testMethod = testMethod;
private MethodSelector(Class<?> clazz, Method method) {
this.clazz = clazz;
this.method = method;
}

public Class<?> getTestClass() {
return testClass;
return this.clazz;
}

public Method getTestMethod() {
return testMethod;
return this.method;
}

private static Class<?> getTestClass(String testClassName) {
return ReflectionUtils.loadClass(testClassName).get();
private static Class<?> getTestClass(String clazzName) {
return ReflectionUtils.loadClass(clazzName).get();
}

private static Method getTestMethod(Class<?> testClass, String testMethodName) {
return ReflectionUtils.findMethod(testClass, testMethodName).get();
private static Method getTestMethod(Class<?> clazz, String methodName) {
return ReflectionUtils.findMethod(clazz, methodName).get();
}

}
Expand Up @@ -36,17 +36,17 @@
public class NameBasedSelector {

public static DiscoverySelector forName(String name) {
Preconditions.notBlank(name, "name must not be blank or empty");
Preconditions.notBlank(name, "name must not be null or empty");

Optional<Class<?>> testClassOptional = ReflectionUtils.loadClass(name);
if (testClassOptional.isPresent()) {
return forClass(testClassOptional.get());
Optional<Class<?>> classOptional = ReflectionUtils.loadClass(name);
if (classOptional.isPresent()) {
return forClass(classOptional.get());
}

Optional<Method> testMethodOptional = ReflectionUtils.loadMethod(name);
if (testMethodOptional.isPresent()) {
Method testMethod = testMethodOptional.get();
return forMethod(testMethod.getDeclaringClass(), testMethod);
Optional<Method> methodOptional = ReflectionUtils.loadMethod(name);
if (methodOptional.isPresent()) {
Method method = methodOptional.get();
return forMethod(method.getDeclaringClass(), method);
}

if (ReflectionUtils.isPackage(name)) {
Expand Down
Expand Up @@ -13,6 +13,7 @@
import static org.junit.gen5.commons.meta.API.Usage.Experimental;

import org.junit.gen5.commons.meta.API;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.engine.DiscoverySelector;

/**
Expand All @@ -22,6 +23,7 @@
public class PackageSelector implements DiscoverySelector {

public static PackageSelector forPackageName(String packageName) {
Preconditions.notBlank(packageName, "Package name must not be null or empty");
return new PackageSelector(packageName);
}

Expand All @@ -32,7 +34,7 @@ private PackageSelector(String packageName) {
}

public String getPackageName() {
return packageName;
return this.packageName;
}

}
Expand Up @@ -13,6 +13,7 @@
import static org.junit.gen5.commons.meta.API.Usage.Experimental;

import org.junit.gen5.commons.meta.API;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.engine.DiscoverySelector;
import org.junit.gen5.engine.UniqueId;

Expand All @@ -23,10 +24,12 @@
public class UniqueIdSelector implements DiscoverySelector {

public static UniqueIdSelector forUniqueId(UniqueId uniqueId) {
Preconditions.notNull(uniqueId, "UniqueId must not be null");
return forUniqueId(uniqueId.toString());
}

public static UniqueIdSelector forUniqueId(String uniqueId) {
Preconditions.notBlank(uniqueId, "Unique ID must not be null or empty");
return new UniqueIdSelector(uniqueId);
}

Expand Down

0 comments on commit 89bc5ed

Please sign in to comment.