Skip to content

Commit

Permalink
Introduce support for converting from a URI of the form method:<FQMN>…
Browse files Browse the repository at this point in the history
… to a MethodSource

Introduce support for converting from a URI of the form method:<FQMN> to a MethodSource, where FQMN is the fully qualified method name. See the Javadoc for DiscoverySelectors.selectMethod(String) for the supported formats for a FQMN. Closes junit-teamgh-1850
  • Loading branch information
ielatif authored and ISSAM EL-ATIF committed May 23, 2019
1 parent 36e44cf commit d2fbebf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.junit.jupiter.engine.descriptor;

import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.engine.support.descriptor.MethodSource;

import java.net.URI;
import java.util.Optional;

class MethodSourceUtils {

static final String METHOD_SCHEME = "method";

static MethodSource fromUri(URI uri) {
Preconditions.notNull(uri, "URI must not be null");
Preconditions.condition(METHOD_SCHEME.equals(uri.getScheme()),
() -> "URI [" + uri + "] must have [" + METHOD_SCHEME + "] scheme");
String fragment = Optional.ofNullable(uri.getFragment()).orElseThrow(() -> new IllegalArgumentException("Invalid method URI"));
String fullyQualifiedMethodName = uri.getSchemeSpecificPart() + "#" + fragment;
String[] methodSpecs = ReflectionUtils.parseFullyQualifiedMethodName(fullyQualifiedMethodName);
return MethodSource.from(methodSpecs[0], methodSpecs[1], methodSpecs[2]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.junit.jupiter.engine.descriptor;

import static org.apiguardian.api.API.Status.INTERNAL;
import static org.junit.jupiter.engine.descriptor.MethodSourceUtils.METHOD_SCHEME;
import static org.junit.platform.engine.support.descriptor.ClasspathResourceSource.CLASSPATH_SCHEME;

import java.lang.reflect.Method;
Expand Down Expand Up @@ -165,7 +166,13 @@ static Optional<JupiterTestDescriptor> createDynamicDescriptor(JupiterTestDescri
*/
static TestSource fromUri(URI uri) {
Preconditions.notNull(uri, "URI must not be null");
return CLASSPATH_SCHEME.equals(uri.getScheme()) ? ClasspathResourceSource.from(uri) : UriSource.from(uri);
if (CLASSPATH_SCHEME.equals(uri.getScheme())) {
return ClasspathResourceSource.from(uri);
} else if (METHOD_SCHEME.equals(uri.getScheme())) {
return MethodSourceUtils.fromUri(uri);
} else {
return UriSource.from(uri);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Optional;
import java.util.stream.Stream;

Expand All @@ -31,13 +33,10 @@
import org.junit.jupiter.engine.config.JupiterConfiguration;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
import org.junit.jupiter.engine.extension.ExtensionRegistry;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.ClasspathResourceSource;
import org.junit.platform.engine.support.descriptor.DirectorySource;
import org.junit.platform.engine.support.descriptor.FilePosition;
import org.junit.platform.engine.support.descriptor.FileSource;
import org.junit.platform.engine.support.descriptor.UriSource;
import org.junit.platform.engine.support.descriptor.*;
import org.junit.platform.engine.support.hierarchical.Node;
import org.junit.platform.engine.support.hierarchical.OpenTest4JAwareThrowableCollector;

Expand Down Expand Up @@ -108,6 +107,19 @@ void defaultUriSourceFromUri() {
assertThat(source.getUri()).isEqualTo(uri);
}

@Test
void methodSourceFromUri() {
String methodUri = "method:org.junit.Foo#bar(java.lang.String,%20java.lang.String[])";
URI uri = URI.create(methodUri);
TestSource testSource = TestFactoryTestDescriptor.fromUri(uri);

assertThat(testSource).isInstanceOf(MethodSource.class);
assertThat(testSource.getClass().getSimpleName()).isEqualTo("MethodSource");
MethodSource source = (MethodSource) testSource;
assertThat(source.getClassName()).isEqualTo("org.junit.Foo");
assertThat(source.getMethodName()).isEqualTo("bar");
assertThat(source.getMethodParameterTypes()).isEqualTo("java.lang.String, java.lang.String[]");
}
}

@Nested
Expand Down

0 comments on commit d2fbebf

Please sign in to comment.