Skip to content

Commit

Permalink
Removed ClassNameSpecification
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Nov 17, 2015
1 parent d094dcb commit c30424a
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 118 deletions.
Expand Up @@ -17,17 +17,17 @@


import javax.inject.Inject; import javax.inject.Inject;


import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.engine.TestPlanSpecification;
import org.junit.gen5.engine.TestPlanSpecificationElement;
import org.junit.gen5.launcher.Launcher;

import io.airlift.airline.Arguments; import io.airlift.airline.Arguments;
import io.airlift.airline.Command; import io.airlift.airline.Command;
import io.airlift.airline.Help; import io.airlift.airline.Help;
import io.airlift.airline.Option; import io.airlift.airline.Option;
import io.airlift.airline.model.CommandMetadata; import io.airlift.airline.model.CommandMetadata;


import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.engine.TestPlanSpecification;
import org.junit.gen5.engine.TestPlanSpecificationElement;
import org.junit.gen5.launcher.Launcher;

/** /**
* @author Stefan Bechtold * @author Stefan Bechtold
* @author Sam Brannen * @author Sam Brannen
Expand Down

This file was deleted.

Expand Up @@ -10,7 +10,6 @@


package org.junit.gen5.engine; package org.junit.gen5.engine;


import static java.util.Arrays.stream;
import static java.util.Collections.unmodifiableList; import static java.util.Collections.unmodifiableList;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;


Expand All @@ -24,6 +23,7 @@
import java.util.stream.Stream; import java.util.stream.Stream;


import org.junit.gen5.commons.util.Preconditions; import org.junit.gen5.commons.util.Preconditions;
import org.junit.gen5.commons.util.ReflectionUtils;


/** /**
* @author Sam Brannen * @author Sam Brannen
Expand All @@ -36,23 +36,25 @@ public static TestPlanSpecificationElement forPackage(String packageName) {
} }


public static List<TestPlanSpecificationElement> forPackages(Collection<String> packageNames) { public static List<TestPlanSpecificationElement> forPackages(Collection<String> packageNames) {
return packageNames.stream().map(PackageSpecification::new).collect(toList()); return packageNames.stream().map(packageName -> forPackage(packageName)).collect(toList());
} }


public static TestPlanSpecificationElement forClassName(String className) { public static TestPlanSpecificationElement forClassName(String className) {
return new ClassNameSpecification(className); Class<?> testClass = ReflectionUtils.loadClass(className).orElseThrow(
} () -> new IllegalArgumentException("Class " + className + " not found."));

return forClass(testClass);
public static List<TestPlanSpecificationElement> forClassNames(String... classNames) {
return forClassNames(stream(classNames));
} }


public static List<TestPlanSpecificationElement> forClassNames(Collection<String> classNames) { public static List<TestPlanSpecificationElement> forClassNames(Collection<String> classNames) {
return forClassNames(classNames.stream()); return forClassNames(classNames.stream());
} }


public static List<TestPlanSpecificationElement> forClassNames(Stream<String> classNames) { public static List<TestPlanSpecificationElement> forClassNames(Stream<String> classNames) {
return classNames.map(ClassNameSpecification::new).collect(toList()); return classNames.map(name -> forClassName(name)).collect(toList());
}

public static TestPlanSpecificationElement forClass(Class<?> testClass) {
return new ClassSpecification(testClass);
} }


public static TestPlanSpecificationElement forUniqueId(String uniqueId) { public static TestPlanSpecificationElement forUniqueId(String uniqueId) {
Expand Down
Expand Up @@ -12,9 +12,6 @@


public interface TestPlanSpecificationVisitor { public interface TestPlanSpecificationVisitor {


default void visitClassNameSpecification(String className) {
}

default void visitUniqueIdSpecification(String uniqueId) { default void visitUniqueIdSpecification(String uniqueId) {
} }


Expand Down
Expand Up @@ -12,7 +12,6 @@


import lombok.Data; import lombok.Data;


import org.junit.gen5.commons.util.ReflectionUtils;
import org.junit.gen5.engine.EngineDescriptor; import org.junit.gen5.engine.EngineDescriptor;
import org.junit.gen5.engine.TestPlanSpecificationVisitor; import org.junit.gen5.engine.TestPlanSpecificationVisitor;
import org.junit.internal.runners.ErrorReportingRunner; import org.junit.internal.runners.ErrorReportingRunner;
Expand All @@ -29,9 +28,7 @@ class JUnit4SpecificationResolver implements TestPlanSpecificationVisitor {
// TODO support more TestPlanSpecificationElements/visit methods // TODO support more TestPlanSpecificationElements/visit methods


@Override @Override
public void visitClassNameSpecification(String className) { public void visitClassSpecification(Class<?> testClass) {
Class<?> testClass = ReflectionUtils.loadClass(className).orElseThrow(
() -> new IllegalArgumentException("Class " + className + " not found."));


// TODO JL: Hack to break endless recursion if runner will lead to the // TODO JL: Hack to break endless recursion if runner will lead to the
// execution of JUnit5 test (eg. @RunWith(JUnit5.class)) // execution of JUnit5 test (eg. @RunWith(JUnit5.class))
Expand Down
Expand Up @@ -11,8 +11,7 @@
package org.junit.gen5.engine.junit4; package org.junit.gen5.engine.junit4;


import static java.util.function.Function.identity; import static java.util.function.Function.identity;
import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.*;
import static java.util.stream.Collectors.toMap;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
Expand Down
Expand Up @@ -20,7 +20,6 @@
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;


Expand Down Expand Up @@ -245,14 +244,14 @@ private void addOnlyIncludeEngineFilter(TestPlanSpecification plan) {
} }
} }


private Collection<TestPlanSpecificationElement> getPackagesSpecificationElements() { private List<TestPlanSpecificationElement> getPackagesSpecificationElements() {
String[] packages = getAnnotatedPackages(testClass); String[] packages = getAnnotatedPackages(testClass);
return stream(packages).map(TestPlanSpecification::forPackage).collect(toList()); return stream(packages).map(TestPlanSpecification::forPackage).collect(toList());
} }


private List<TestPlanSpecificationElement> getClassnameSpecificationElements() { private List<TestPlanSpecificationElement> getClassnameSpecificationElements() {
Class<?>[] testClasses = getAnnotatedClasses(testClass); Class<?>[] testClasses = getAnnotatedClasses(testClass);
return stream(testClasses).map(Class::getName).map(TestPlanSpecification::forClassName).collect(toList()); return stream(testClasses).map(TestPlanSpecification::forClass).collect(toList());
} }


private List<TestPlanSpecificationElement> getUniqueIdSpecificationElements() { private List<TestPlanSpecificationElement> getUniqueIdSpecificationElements() {
Expand Down
Expand Up @@ -18,10 +18,6 @@ static JUnit5Testable fromUniqueId(String uniqueId, String engineId) {
return new JUnit5TestableFactory().fromUniqueId(uniqueId, engineId); return new JUnit5TestableFactory().fromUniqueId(uniqueId, engineId);
} }


static JUnit5Testable fromClassName(String className, String engineId) {
return new JUnit5TestableFactory().fromClassName(className, engineId);
}

static JUnit5Testable fromClass(Class<?> clazz, String engineId) { static JUnit5Testable fromClass(Class<?> clazz, String engineId) {
return new JUnit5TestableFactory().fromClass(clazz, engineId); return new JUnit5TestableFactory().fromClass(clazz, engineId);
} }
Expand Down
Expand Up @@ -45,11 +45,6 @@ public SpecificationResolver(EngineDescriptor engineDescriptor) {
public void resolveElement(TestPlanSpecificationElement element) { public void resolveElement(TestPlanSpecificationElement element) {
element.accept(new TestPlanSpecificationVisitor() { element.accept(new TestPlanSpecificationVisitor() {


@Override
public void visitClassNameSpecification(String className) {
resolveClassNameSpecification(className);
}

@Override @Override
public void visitClassSpecification(Class<?> testClass) { public void visitClassSpecification(Class<?> testClass) {
resolveClassSpecification(testClass); resolveClassSpecification(testClass);
Expand Down Expand Up @@ -78,13 +73,7 @@ private void resolveClassSpecification(Class<?> testClass) {
resolveTestable(testable); resolveTestable(testable);
} }


private void resolveClassNameSpecification(String className) {
JUnit5Testable testable = JUnit5Testable.fromClassName(className, engineDescriptor.getUniqueId());
resolveTestable(testable);
}

private void resolveUniqueIdSpecification(String uniqueId) { private void resolveUniqueIdSpecification(String uniqueId) {

JUnit5Testable testable = JUnit5Testable.fromUniqueId(uniqueId, engineDescriptor.getUniqueId()); JUnit5Testable testable = JUnit5Testable.fromUniqueId(uniqueId, engineDescriptor.getUniqueId());
resolveTestable(testable); resolveTestable(testable);
} }
Expand All @@ -99,7 +88,7 @@ public void visitClass(String uniqueId, Class<?> testClass) {


@Override @Override
public void visitMethod(String uniqueId, Method method, Class<?> container) { public void visitMethod(String uniqueId, Method method, Class<?> container) {
resolveMethodTestable(method, container, uniqueId, engineDescriptor); resolveMethodTestable(method, container, uniqueId);
} }


@Override @Override
Expand All @@ -113,8 +102,7 @@ private void resolveTestable(JUnit5Testable testable) {
resolveTestable(testable, true); resolveTestable(testable, true);
} }


private void resolveMethodTestable(Method method, Class<?> testClass, String uniqueId, private void resolveMethodTestable(Method method, Class<?> testClass, String uniqueId) {
AbstractTestDescriptor parentDescriptor) {
JUnit5Testable parentTestable = JUnit5Testable.fromClass(testClass, engineDescriptor.getUniqueId()); JUnit5Testable parentTestable = JUnit5Testable.fromClass(testClass, engineDescriptor.getUniqueId());
TestDescriptor newParentDescriptor = resolveAndReturnParentTestable(parentTestable); TestDescriptor newParentDescriptor = resolveAndReturnParentTestable(parentTestable);
MethodTestDescriptor descriptor = getOrCreateMethodDescriptor(method, uniqueId); MethodTestDescriptor descriptor = getOrCreateMethodDescriptor(method, uniqueId);
Expand Down
Expand Up @@ -61,7 +61,7 @@ public void fromUniqueIdForMethod() throws NoSuchMethodException {
"junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass#test1()", engineDescriptor.getUniqueId()); "junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass#test1()", engineDescriptor.getUniqueId());
Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass#test1()", Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass#test1()",
testable.getUniqueId()); testable.getUniqueId());
Method testMethod = ATestClass.class.getDeclaredMethod("test1", new Class[0]); Method testMethod = ATestClass.class.getDeclaredMethod("test1");
Assert.assertEquals(testMethod, testable.getJavaMethod()); Assert.assertEquals(testMethod, testable.getJavaMethod());
} }


Expand All @@ -74,7 +74,7 @@ public void fromUniqueIdForMethodWithParameters() throws NoSuchMethodException {
Assert.assertEquals( Assert.assertEquals(
"junit5:org.junit.gen5.engine.junit5.descriptor.BTestClass#test4(java.lang.String, java.math.BigDecimal)", "junit5:org.junit.gen5.engine.junit5.descriptor.BTestClass#test4(java.lang.String, java.math.BigDecimal)",
testable.getUniqueId()); testable.getUniqueId());
Method testMethod = BTestClass.class.getDeclaredMethod("test4", new Class[] { String.class, BigDecimal.class }); Method testMethod = BTestClass.class.getDeclaredMethod("test4", String.class, BigDecimal.class);
Assert.assertEquals(testMethod, testable.getJavaMethod()); Assert.assertEquals(testMethod, testable.getJavaMethod());
} }


Expand All @@ -86,30 +86,29 @@ public void fromUniqueIdForMethodInNestedClass() throws NoSuchMethodException {
engineDescriptor.getUniqueId()); engineDescriptor.getUniqueId());
Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass$AnInnerTestClass#test2()", Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass$AnInnerTestClass#test2()",
testable.getUniqueId()); testable.getUniqueId());
Method testMethod = ATestClass.AnInnerTestClass.class.getDeclaredMethod("test2", new Class[0]); Method testMethod = ATestClass.AnInnerTestClass.class.getDeclaredMethod("test2");
Assert.assertEquals(testMethod, testable.getJavaMethod()); Assert.assertEquals(testMethod, testable.getJavaMethod());
} }


@org.junit.Test @org.junit.Test
public void fromClassName() throws NoSuchMethodException { public void fromClass() throws NoSuchMethodException {
JUnit5Class testable = (JUnit5Class) JUnit5Testable.fromClassName( JUnit5Class testable = (JUnit5Class) JUnit5Testable.fromClass(ATestClass.class, engineDescriptor.getUniqueId());
"org.junit.gen5.engine.junit5.descriptor.ATestClass", engineDescriptor.getUniqueId());
Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass", testable.getUniqueId()); Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass", testable.getUniqueId());
Assert.assertSame(ATestClass.class, testable.getJavaClass()); Assert.assertSame(ATestClass.class, testable.getJavaClass());
} }


@org.junit.Test @org.junit.Test
public void nestedClassFromClassName() throws NoSuchMethodException { public void nestedClassFromClass() throws NoSuchMethodException {
JUnit5Class testable = (JUnit5Class) JUnit5Testable.fromClassName( JUnit5Class testable = (JUnit5Class) JUnit5Testable.fromClass(ATestClass.AnInnerTestClass.class,
"org.junit.gen5.engine.junit5.descriptor.ATestClass$AnInnerTestClass", engineDescriptor.getUniqueId()); engineDescriptor.getUniqueId());
Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass$AnInnerTestClass", Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass$AnInnerTestClass",
testable.getUniqueId()); testable.getUniqueId());
Assert.assertSame(ATestClass.AnInnerTestClass.class, testable.getJavaClass()); Assert.assertSame(ATestClass.AnInnerTestClass.class, testable.getJavaClass());
} }


@org.junit.Test @org.junit.Test
public void fromMethod() throws NoSuchMethodException { public void fromMethod() throws NoSuchMethodException {
Method testMethod = ATestClass.class.getDeclaredMethod("test1", new Class[0]); Method testMethod = ATestClass.class.getDeclaredMethod("test1");
JUnit5Method testable = (JUnit5Method) JUnit5Testable.fromMethod(testMethod, ATestClass.class, JUnit5Method testable = (JUnit5Method) JUnit5Testable.fromMethod(testMethod, ATestClass.class,
engineDescriptor.getUniqueId()); engineDescriptor.getUniqueId());
Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass#test1()", Assert.assertEquals("junit5:org.junit.gen5.engine.junit5.descriptor.ATestClass#test1()",
Expand Down
Expand Up @@ -14,9 +14,9 @@


import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;

import org.junit.gen5.api.Context; import org.junit.gen5.api.Context;
import org.junit.gen5.api.Test; import org.junit.gen5.api.Test;
import org.junit.gen5.engine.ClassNameSpecification;
import org.junit.gen5.engine.ClassSpecification; import org.junit.gen5.engine.ClassSpecification;
import org.junit.gen5.engine.EngineDescriptor; import org.junit.gen5.engine.EngineDescriptor;
import org.junit.gen5.engine.PackageSpecification; import org.junit.gen5.engine.PackageSpecification;
Expand All @@ -30,8 +30,8 @@ public class SpecificationResolverTest {
private SpecificationResolver resolver = new SpecificationResolver(engineDescriptor); private SpecificationResolver resolver = new SpecificationResolver(engineDescriptor);


@org.junit.Test @org.junit.Test
public void testSingleClassNameResolution() { public void testSingleClassResolution() {
ClassNameSpecification specification = new ClassNameSpecification(MyTestClass.class.getName()); ClassSpecification specification = new ClassSpecification(MyTestClass.class);


resolver.resolveElement(specification); resolver.resolveElement(specification);


Expand All @@ -43,26 +43,9 @@ public void testSingleClassNameResolution() {
} }


@org.junit.Test @org.junit.Test
public void testClassNameResolutionOfNestedClass() { public void testTwoClassesResolution() {
ClassNameSpecification specification = new ClassNameSpecification( ClassSpecification specification1 = new ClassSpecification(MyTestClass.class);
OtherTestClass.NestedTestClass.class.getName()); ClassSpecification specification2 = new ClassSpecification(YourTestClass.class);

resolver.resolveElement(specification);

assertEquals(3, engineDescriptor.allChildren().size());
List<String> uniqueIds = engineDescriptor.allChildren().stream().map(d -> d.getUniqueId()).collect(
Collectors.toList());
assertTrue(uniqueIds.contains("junit5:org.junit.gen5.engine.junit5.descriptor.OtherTestClass$NestedTestClass"));
assertTrue(uniqueIds.contains(
"junit5:org.junit.gen5.engine.junit5.descriptor.OtherTestClass$NestedTestClass#test5()"));
assertTrue(uniqueIds.contains(
"junit5:org.junit.gen5.engine.junit5.descriptor.OtherTestClass$NestedTestClass#test6()"));
}

@org.junit.Test
public void testTwoClassNameResolution() {
ClassNameSpecification specification1 = new ClassNameSpecification(MyTestClass.class.getName());
ClassNameSpecification specification2 = new ClassNameSpecification(YourTestClass.class.getName());


resolver.resolveElement(specification1); resolver.resolveElement(specification1);
resolver.resolveElement(specification2); resolver.resolveElement(specification2);
Expand All @@ -78,19 +61,6 @@ public void testTwoClassNameResolution() {
assertTrue(uniqueIds.contains("junit5:org.junit.gen5.engine.junit5.descriptor.YourTestClass#test4()")); assertTrue(uniqueIds.contains("junit5:org.junit.gen5.engine.junit5.descriptor.YourTestClass#test4()"));
} }


@org.junit.Test
public void testSingleClassResolution() {
ClassSpecification specification = new ClassSpecification(MyTestClass.class);

resolver.resolveElement(specification);

assertEquals(3, engineDescriptor.allChildren().size());
List<String> uniqueIds = uniqueIds();
assertTrue(uniqueIds.contains("junit5:org.junit.gen5.engine.junit5.descriptor.MyTestClass"));
assertTrue(uniqueIds.contains("junit5:org.junit.gen5.engine.junit5.descriptor.MyTestClass#test1()"));
assertTrue(uniqueIds.contains("junit5:org.junit.gen5.engine.junit5.descriptor.MyTestClass#test2()"));
}

@org.junit.Test @org.junit.Test
public void testClassResolutionOfNestedClass() { public void testClassResolutionOfNestedClass() {
ClassSpecification specification = new ClassSpecification(OtherTestClass.NestedTestClass.class); ClassSpecification specification = new ClassSpecification(OtherTestClass.NestedTestClass.class);
Expand Down Expand Up @@ -260,8 +230,8 @@ public void testPackageResolution() {
} }


@org.junit.Test @org.junit.Test
public void testContextResolutionFromContainerClass() { public void testContextResolutionFromBaseClass() {
ClassNameSpecification specification = new ClassNameSpecification(TestCaseWithContexts.class.getName()); ClassSpecification specification = new ClassSpecification(TestCaseWithContexts.class);


resolver.resolveElement(specification); resolver.resolveElement(specification);


Expand All @@ -283,8 +253,7 @@ public void testContextResolutionFromContainerClass() {


@org.junit.Test @org.junit.Test
public void testContextResolutionFromContextClass() { public void testContextResolutionFromContextClass() {
ClassNameSpecification specification = new ClassNameSpecification( ClassSpecification specification = new ClassSpecification(TestCaseWithContexts.InnerContext.class);
TestCaseWithContexts.InnerContext.class.getName());


resolver.resolveElement(specification); resolver.resolveElement(specification);


Expand Down Expand Up @@ -325,7 +294,8 @@ public void testContextResolutionFromUniqueId() {


@org.junit.Test @org.junit.Test
public void testContextResolutionFromClass() { public void testContextResolutionFromClass() {
ClassSpecification specification = new ClassSpecification(TestCaseWithContexts.InnerContext.InnerInnerContext.class); ClassSpecification specification = new ClassSpecification(
TestCaseWithContexts.InnerContext.InnerInnerContext.class);


resolver.resolveElement(specification); resolver.resolveElement(specification);


Expand Down

0 comments on commit c30424a

Please sign in to comment.