Skip to content

Commit

Permalink
Fixed remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Nov 5, 2015
1 parent 09b5cf3 commit 266a037
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 63 deletions.
Expand Up @@ -61,17 +61,18 @@ private void resolveUniqueId(UniqueId uniqueId) {
resolveClass((Class<?>) uniqueId.getJavaElement(), uniqueId.getUniqueId(), root, true);
}
if (uniqueId.getJavaElement() instanceof Method) {
resolveMethod((Method) uniqueId.getJavaElement(), uniqueId.getUniqueId(), root, true);
resolveMethod((Method) uniqueId.getJavaElement(), uniqueId.getJavaContainer(), uniqueId.getUniqueId(), root,
true);
}
}

private void resolveMethod(Method method, String uniqueId, AbstractTestDescriptor parent, boolean withChildren) {
private void resolveMethod(Method method, Class<?> testClass, String uniqueId, AbstractTestDescriptor parent,
boolean withChildren) {
if (!methodTester.accept(method)) {
throwCannotResolveMethodException(method);
}
Class<?> enclosingClass = method.getDeclaringClass();
UniqueId parentId = UniqueId.fromClass(enclosingClass, root);
parent = resolveClass(enclosingClass, parentId.getUniqueId(), parent, false);
UniqueId parentId = UniqueId.fromClass(testClass, root);
parent = resolveClass(testClass, parentId.getUniqueId(), parent, false);

MethodTestDescriptor descriptor = getOrCreateMethodDescriptor(method, uniqueId);
parent.addChild(descriptor);
Expand Down
Expand Up @@ -20,7 +20,6 @@

import lombok.Value;

import org.junit.gen5.commons.util.AnnotationUtils;
import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.commons.util.ReflectionUtils;
import org.junit.gen5.engine.TestDescriptor;
Expand Down Expand Up @@ -52,9 +51,7 @@ public static UniqueId fromUniqueId(String uniqueId, TestDescriptor engineDescri
Preconditions.condition(parts.remove(0).equals(engineDescriptor.getUniqueId()),
"uniqueId must start with engineId");

AnnotatedElement element = findElement(uniqueId, parts);

return new UniqueId(uniqueId, element);
return createElement(uniqueId, parts);

}

Expand All @@ -70,40 +67,44 @@ public static UniqueId fromClassName(String className, TestDescriptor engineDesc
public static UniqueId fromClass(Class<?> clazz, TestDescriptor engineDescriptor) {
Preconditions.notNull(clazz, "clazz must not be null");
String uniqueId = engineDescriptor.getUniqueId() + ":" + clazz.getName();
return new UniqueId(uniqueId, clazz);
return new UniqueId(uniqueId, clazz, null);
}

public static UniqueId fromMethod(Method testMethod, Class<?> clazz, TestDescriptor engineDescriptor) {
String uniqueId = fromClass(clazz, engineDescriptor).getUniqueId() + "#" + testMethod.getName() + "()";
return new UniqueId(uniqueId, testMethod);
return new UniqueId(uniqueId, testMethod, clazz);
}

private static AnnotatedElement findElement(String uniqueId, List<String> parts) {
AnnotatedElement current = null;
private static UniqueId createElement(String uniqueId, List<String> parts) {
AnnotatedElement currentJavaElement = null;
Class<?> currentJavaContainer = null;
String head = parts.remove(0);
while (true) {
switch (head.charAt(0)) {
case ':':
current = findTopLevelClass(head);
currentJavaElement = findTopLevelClass(head);
break;
case '$':
current = findNestedClass(head, (Class<?>) current);
currentJavaContainer = (Class<?>) currentJavaElement;
currentJavaElement = findNestedClass(head, (Class<?>) currentJavaElement);
break;
case '#':
current = findMethod(head, (Class<?>) current);
currentJavaContainer = (Class<?>) currentJavaElement;
currentJavaElement = findMethod(head, (Class<?>) currentJavaElement);
break;
default:
current = null;
currentJavaContainer = null;
currentJavaElement = null;
}

if (current == null) {
if (currentJavaElement == null) {
throwCannotResolveUniqueIdException(uniqueId, head);
}
if (parts.isEmpty())
break;
head = parts.remove(0);
}
return current;
return new UniqueId(uniqueId, currentJavaElement, currentJavaContainer);
}

private static Method findMethod(String methodSpecPart, Class<?> clazz) {
Expand Down Expand Up @@ -155,10 +156,12 @@ private static void throwCannotResolveUniqueIdException(String fullUniqueId, Str

private final String uniqueId;
private final AnnotatedElement javaElement;
private final Class<?> javaContainer;

private UniqueId(String uniqueId, AnnotatedElement javaElement) {
private UniqueId(String uniqueId, AnnotatedElement javaElement, Class<?> javaContainer) {
this.uniqueId = uniqueId;
this.javaElement = javaElement;
this.javaContainer = javaContainer;
}

}
Expand Up @@ -36,52 +36,39 @@ public class MethodTestDescriptorTests {
public void constructFromMethod() throws Exception {
Class<?> testClass = getClass();
Method testMethod = testClass.getDeclaredMethod("test");
ClassTestDescriptor parent = new ClassTestDescriptor("a class id", testClass);
MethodTestDescriptor descriptor = new MethodTestDescriptor("a method id", testMethod);
parent.addChild(descriptor);

assertEquals("a method id", descriptor.getUniqueId());
assertEquals(testClass, ((ClassTestDescriptor) descriptor.getParent()).getTestClass());
assertEquals(testMethod, descriptor.getTestMethod());
assertEquals("test", descriptor.getDisplayName(), "display name:");
}

// @org.junit.Test
// public void constructFromMethodWithCustomDisplayName() throws Exception {
// Class<?> testClass = getClass();
// Method testMethod = testClass.getDeclaredMethod("foo");
// ClassTestDescriptor parent = new ClassTestDescriptor(testClass, ENGINE_DESCRIPTOR);
// MethodTestDescriptor descriptor = new MethodTestDescriptor(testMethod, parent);
//
// assertEquals(testClass, descriptor.getParent().getTestClass());
// assertEquals(testMethod, descriptor.getTestMethod());
// assertEquals("custom test name", descriptor.getDisplayName(), "display name:");
// }
//
// @org.junit.Test
// public void constructFromMethodWithCustomDisplayNameInCustomTestAnnotation() throws Exception {
// Class<?> testClass = getClass();
// Method testMethod = testClass.getDeclaredMethod("customTestAnnotation");
// ClassTestDescriptor parent = new ClassTestDescriptor(testClass, ENGINE_DESCRIPTOR);
// MethodTestDescriptor descriptor = new MethodTestDescriptor(testMethod, parent);
//
// assertEquals(testClass, descriptor.getParent().getTestClass());
// assertEquals(testMethod, descriptor.getTestMethod());
// assertEquals("custom name", descriptor.getDisplayName(), "display name:");
// }
//
// @org.junit.Test
// public void constructFromMethodWithParameters() throws Exception {
// Class<?> testClass = getClass();
// Method testMethod = testClass.getDeclaredMethod("test", String.class, BigDecimal.class);
// ClassTestDescriptor parent = new ClassTestDescriptor(testClass, ENGINE_DESCRIPTOR);
// MethodTestDescriptor descriptor = new MethodTestDescriptor(testMethod, parent);
//
// assertEquals(TEST_METHOD_STRING_BIGDECIMAL_UID, descriptor.getUniqueId());
// assertEquals(testClass, descriptor.getParent().getTestClass());
// assertEquals(testMethod, descriptor.getTestMethod());
// assertEquals("test", descriptor.getDisplayName(), "display name:");
// }
@org.junit.Test
public void constructFromMethodWithCustomDisplayName() throws Exception {
Method testMethod = getClass().getDeclaredMethod("foo");
MethodTestDescriptor descriptor = new MethodTestDescriptor("any id", testMethod);

assertEquals(testMethod, descriptor.getTestMethod());
assertEquals("custom test name", descriptor.getDisplayName(), "display name:");
}

@org.junit.Test
public void constructFromMethodWithCustomDisplayNameInCustomTestAnnotation() throws Exception {
Method testMethod = getClass().getDeclaredMethod("customTestAnnotation");
MethodTestDescriptor descriptor = new MethodTestDescriptor("any id", testMethod);

assertEquals(testMethod, descriptor.getTestMethod());
assertEquals("custom name", descriptor.getDisplayName(), "display name:");
}

@org.junit.Test
public void constructFromMethodWithParameters() throws Exception {
Method testMethod = getClass().getDeclaredMethod("test", String.class, BigDecimal.class);
MethodTestDescriptor descriptor = new MethodTestDescriptor("any id", testMethod);

assertEquals(testMethod, descriptor.getTestMethod());
assertEquals("test", descriptor.getDisplayName(), "display name:");
}

void test() {
}
Expand Down
Expand Up @@ -11,6 +11,7 @@
package org.junit.gen5.engine.junit5;

import java.lang.reflect.Method;
import java.math.BigDecimal;

import org.junit.Assert;
import org.junit.gen5.api.Test;
Expand Down Expand Up @@ -85,13 +86,17 @@ public void fromMethod() throws NoSuchMethodException {
UniqueId uniqueId = UniqueId.fromMethod(testMethod, ATestClass.class, engineDescriptor);
Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.ATestClass#test1()", uniqueId.getUniqueId());
Assert.assertSame(testMethod, uniqueId.getJavaElement());
Assert.assertSame(ATestClass.class, uniqueId.getJavaContainer());
}

@org.junit.Test
public void fromMethodInSubclass() throws NoSuchMethodException {
Method testMethod = ATestClass.class.getDeclaredMethod("test1", new Class[0]);
//Todo: Yet to implement
// @org.junit.Test
public void fromMethodWithParameters() throws NoSuchMethodException {
Method testMethod = BTestClass.class.getDeclaredMethod("test4", String.class, BigDecimal.class);
UniqueId uniqueId = UniqueId.fromMethod(testMethod, BTestClass.class, engineDescriptor);
Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.BTestClass#test1()", uniqueId.getUniqueId());
Assert.assertEquals(
"junit5:org.junit.gen5.engine.junit5.BTestClass#test4(java.lang.String, java.math.BigDecimal)",
uniqueId.getUniqueId());
Assert.assertSame(testMethod, uniqueId.getJavaElement());
}

Expand Down Expand Up @@ -123,4 +128,9 @@ void test3() {

class BTestClass extends ATestClass {

@Test
void test4(String aString, BigDecimal aBigDecimal) {

}

}

0 comments on commit 266a037

Please sign in to comment.