Skip to content

Commit

Permalink
Upgrade to JUnit 5.5.0-RC1 and test MethodSource URIs
Browse files Browse the repository at this point in the history
JUnit 5.5.0-RC1 adds support for MethodSource URIs.
See junit-team/junit5#1850
  • Loading branch information
Ken D committed Jun 18, 2019
1 parent 2c474e4 commit 2d5a2a1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ subprojects {
"api"("org.slf4j:slf4j-api:1.7.26")
//"testImplementation"("org.slf4j:slf4j-api:1.7.26")
"testImplementation"("ch.qos.logback:logback-classic:1.2.3")
"testImplementation"("org.junit.jupiter:junit-jupiter-api:5.4.2")
"testImplementation"("org.junit.jupiter:junit-jupiter-engine:5.4.2")
"testImplementation"("org.junit.jupiter:junit-jupiter-api:5.5.0-RC1")
"testImplementation"("org.junit.jupiter:junit-jupiter-engine:5.5.0-RC1")
"testImplementation"("org.assertj:assertj-core:3.11.1")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ else if (source instanceof ClasspathResourceSource) {
return fromClassName(className);
}
else if (source instanceof MethodSource) {
// MethodSource methodSource = (MethodSource) source;
// return fromMethod(methodSource.getClassName(), methodSource.getMethodName(),
// methodSource.getMethodParameterTypes());
return null;
MethodSource methodSource = (MethodSource) source;
return fromMethod(methodSource.getClassName(), methodSource.getMethodName(),
methodSource.getMethodParameterTypes());
}
else {
// TODO! log a warning.
Expand All @@ -62,7 +61,6 @@ else if (source instanceof MethodSource) {
}
}

// See https://github.com/junit-team/junit5/issues/1850
public static URI fromMethod(Method method) {

String methodParameterTypes = null;
Expand All @@ -87,24 +85,25 @@ public static URI fromMethod(Method method) {
}

private static URI fromMethod(String className, String methodName, String methodParameterTypes) {
String path = pathFromClassName(className) + "#" + methodName;
String path = pathFromClassName(className);
String fragment = methodName;
if (methodParameterTypes != null) {
path += methodParameterTypes;
fragment += methodParameterTypes;
}
return createUri("method", path, null);
return createUri("method", path, null, fragment);
}

public static URI fromClassName(String className) {
return createUri("classpath", pathFromClassName(className), null);
return createUri("classpath", pathFromClassName(className), null, null);
}

private static String pathFromClassName(String className) {
return "/" + className.replace('.', '/');
}

private static URI createUri(String scheme, String path, String query) {
private static URI createUri(String scheme, String path, String query, String fragment) {
try {
return new URI(scheme, null, path, query, null);
return new URI(scheme, null, path, query, fragment);
}
catch (URISyntaxException e) {
throw new IllegalStateException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2019 Ken Dobson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.co.magictractor.zillions.suite;

import static org.assertj.core.api.Assertions.assertThat;

import java.lang.reflect.Method;
import java.net.URI;

import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.ReflectionUtils;

public class SourceUriSupportTest {

@Test
public void testFromMethodWithNoParams() throws ReflectiveOperationException, SecurityException {
Method method = getClass().getMethod("testFromMethodWithNoParams");
URI uri = SourceUriSupport.fromMethod(method);
checkMethodUri(uri, method, "");
}

@Test
public void testFromMethodWithSimpleParams() throws ReflectiveOperationException, SecurityException {
Method method = getClass().getDeclaredMethod("checkMethodUri", URI.class, Method.class, String.class);
URI uri = SourceUriSupport.fromMethod(method);
checkMethodUri(uri, method, "(java.net.URI,java.lang.reflect.Method,java.lang.String)");
}

@Test
public void testFromMethodWithArrayParams() throws ReflectiveOperationException, SecurityException {
Method method = getClass().getDeclaredMethod("dummy", String[].class, int[][].class);
URI uri = SourceUriSupport.fromMethod(method);
checkMethodUri(uri, method, "([Ljava.lang.String;,[[I)");
}

private void checkMethodUri(URI uri, Method expectedMethod, String expectedParamsString) {
assertThat(uri.getScheme()).isEqualTo("method");
assertThat(uri.getPath()).isEqualTo("/" + getClass().getName().replace('.', '/'));
assertThat(uri.getFragment()).isEqualTo(expectedMethod.getName() + expectedParamsString);
assertThat(uri.getQuery()).isNull();

String[] methodParts = ReflectionUtils.parseFullyQualifiedMethodName(uri.toString().substring(7));
Method methodFromSource = ReflectionUtils.findMethod(getClass(), methodParts[1], methodParts[2]).get();
assertThat(methodFromSource).isEqualTo(expectedMethod);
}

public void dummy(String[] array1, int[][] array2) {
// Do nothing. Just used for testing MethodSource URIs.
}

}

0 comments on commit 2d5a2a1

Please sign in to comment.