Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ on GitHub.
* New `InvocationInterceptor` extension API (see
<<../user-guide/index.adoc#extensions-intercepting-invocations, User Guide>> for
details).
* Added support for method URIs, e.g. `method:org.junit.Foo#bar()`, to `DynamicContainer`
and `DynamicTest` factory methods.


[[release-notes-5.5.0-RC1-junit-vintage]]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2015-2019 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 v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.jupiter.engine.descriptor;

import java.net.URI;

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

/**
* Jupiter internal support for creating {@link MethodSource} from {@link URI}.
*
* @since 5.5
* @see MethodSource
* @see MethodSelector
*/
class MethodSourceSupport {

static final String METHOD_SCHEME = "method";

/**
* Create a new {@code MethodSource} from the supplied {@link URI}.
*
* <p>The supplied {@link URI} should be of the form {@code method:<FQMN>}
* where FQMN is the fully qualified method name. See
* {@link DiscoverySelectors#selectMethod(String)} for the supported formats.
*
* <p></p>The {@link URI#getSchemeSpecificPart() scheme-specific part}
* component of the {@code URI} will be used as fully qualified class name.
* The {@linkplain URI#getFragment() fragment} component of the {@code URI}
* will be used to retrieve the method name and method parameter types.
*
* @param uri the {@code URI} for the method; never {@code null}
* @return a new {@code MethodSource}; never {@code null}
* @since 5.5
* @see #METHOD_SCHEME
* @see DiscoverySelectors#selectMethod(String)
*/
static MethodSource from(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 schemeSpecificPart = Preconditions.notNull(uri.getSchemeSpecificPart(),
() -> "Invalid method URI (scheme-specific part must not be null). Please consult the Javadoc of "
+ DiscoverySelectors.class.getName()
+ "#selectMethod(String) for details on the supported formats.");
String fragment = Preconditions.notNull(uri.getFragment(),
() -> "Invalid method URI (fragment must not be null). Please consult the Javadoc of "
+ DiscoverySelectors.class.getName()
+ "#selectMethod(String) for details on the supported formats.");

String fullyQualifiedMethodName = schemeSpecificPart + "#" + fragment;
String[] methodSpec = ReflectionUtils.parseFullyQualifiedMethodName(fullyQualifiedMethodName);
return MethodSource.from(methodSpec[0], methodSpec[1], methodSpec[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.MethodSourceSupport.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);
}
if (METHOD_SCHEME.equals(uri.getScheme())) {
return MethodSourceSupport.from(uri);
}
return UriSource.from(uri);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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.MethodSource;
import org.junit.platform.engine.support.descriptor.UriSource;
import org.junit.platform.engine.support.hierarchical.Node;
import org.junit.platform.engine.support.hierarchical.OpenTest4JAwareThrowableCollector;
Expand Down Expand Up @@ -108,6 +109,18 @@ void defaultUriSourceFromUri() {
assertThat(source.getUri()).isEqualTo(uri);
}

@Test
void methodSourceFromUri() {
URI uri = URI.create("method:org.junit.Foo#bar(java.lang.String,%20java.lang.String[])");
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