From d80db99d683dde7570981e090bc8bd7a63df8e86 Mon Sep 17 00:00:00 2001 From: Michiel Kalkman Date: Mon, 1 Jun 2020 22:06:34 +0200 Subject: [PATCH 1/6] issue #537 add failing unit tests --- .../JUnitCustomRunnerTestUnitFinderTest.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java b/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java index 48c84cbd2..224f5cf15 100644 --- a/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java +++ b/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java @@ -36,6 +36,7 @@ import org.junit.experimental.categories.Category; import org.junit.rules.ExternalResource; import org.junit.rules.TestRule; +import org.junit.runner.Description; import org.junit.runner.RunWith; import org.junit.runner.Runner; import org.junit.runners.BlockJUnit4ClassRunner; @@ -116,6 +117,55 @@ public CustomSuiteRunner(final Class klass, final RunnerBuilder rb) } + /** + * It is possible to override the getDescription() method in a (Parent)Runner, + * and this way it is even possibly to add a non-class name. + * + * This appears to be happening in org.apache.maven.plugins:maven-pmd-plugin:3.13.0. + * + * This fails in two ways: + * 1) if the Suite has no SuiteClasses, it is determined to be a + * test, after which its description is used to create a TestUnit + * but as the classname of the description is not an actual class + * a ClassNotFoundException is thrown and the entiry test results + * in "PitError: Coverage generation minion exited abnormall + * y!" errors. + * 2) if the Suite does have SuiteClasss, it is determined to NOT be a + * test, after which it is ignored entirely. + */ + public static class CustomDescriptionSuiteRunner extends Suite { + + CustomSuiteRunner customSuiteRunner; + + public CustomDescriptionSuiteRunner(final Class klass, final RunnerBuilder rb) + throws InitializationError { + super(klass, rb); + customSuiteRunner = new CustomSuiteRunner(klass, rb); + } + + @Override + public Description getDescription() { + Description description = Description.createSuiteDescription(super.getDescription().getClassName());; + final Description unit_tests = Description.createSuiteDescription("Unit Tests"); + description.addChild( unit_tests); + for (Description child : customSuiteRunner.getDescription().getChildren()) { + for( Description grandChild : child.getChildren()) { + unit_tests.addChild(grandChild); + } + } + + return description; + } + + private Description createChildrenDescriptions(Runner runner, String suiteName) { + Description suite = Description.createSuiteDescription(suiteName); + for (Description child : runner.getDescription().getChildren()) { + suite.addChild(child); + } + return suite; + } + } + public static class One { @Test public void one() { @@ -144,6 +194,28 @@ public void two() { @SuiteClasses({ One.class, Two.class }) public static class CustomSuite { + + } + + @RunWith(CustomDescriptionSuiteRunner.class) + @SuiteClasses({ }) + public static class EmptySuite { + } + + + @RunWith(CustomDescriptionSuiteRunner.class) + @SuiteClasses({ One.class }) + public static class CustomTest { + @Test + public void six() { + + } + } + + @Test + public void shouldHandleEmptySuite() { + final Collection actual = findWithTestee(EmptySuite.class); + assertTrue(actual.isEmpty()); } @Test @@ -152,6 +224,12 @@ public void shouldNotFindTestsInCustomSuite() { assertTrue(actual.isEmpty()); } + @Test + public void shouldNotFindTestsInCustomSuite2() { + final Collection actual = findWithTestee(CustomTest.class); + assertEquals(2, actual.size()); + } + public static class Three { @Test public void one() { From 8d38c909e0e959533c54d54a414cde48df4ef7b3 Mon Sep 17 00:00:00 2001 From: Michiel Kalkman Date: Mon, 1 Jun 2020 22:26:13 +0200 Subject: [PATCH 2/6] issue #537 fix failing unit tests --- .../JUnitCustomRunnerTestUnitFinder.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java b/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java index 009dd6ae8..9a44da6fe 100644 --- a/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java +++ b/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java @@ -46,6 +46,7 @@ import org.pitest.testapi.TestUnit; import org.pitest.testapi.TestUnitFinder; import org.pitest.util.IsolationUtils; +import org.pitest.util.PitError; public class JUnitCustomRunnerTestUnitFinder implements TestUnitFinder { @@ -201,10 +202,33 @@ private boolean isJUnitThreeSuiteMethodNotForOwnClass(final Runner runner, } private List splitIntoFilteredUnits(final Description description) { - return description.getChildren().stream() - .filter(isTest()) - .map(descriptionToTestUnit()) - .collect(Collectors.toList()); + final ArrayList allChildren = description.getChildren().stream() + .map(child -> { + if (isClazz(child)) { + return Collections.singletonList(child); + } else { + return child.getChildren(); + } + }).collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll); + + return allChildren.stream() + .filter(isTest()) + .map(descriptionToTestUnit()) + .collect(Collectors.toList()); + } + + private boolean isClazz(Description description1) { + try { + IsolationUtils.convertForClassLoader( + IsolationUtils.getContextClassLoader(), description1.getClassName()); + } catch (PitError pitError) { + if (pitError.getCause() instanceof ClassNotFoundException) { + return false; + } else { + throw pitError; + } + } + return true; } private Function descriptionToTestUnit() { From 692a4300accb73ddaf64e0ce956d042b0f538cd7 Mon Sep 17 00:00:00 2001 From: Michiel Kalkman Date: Mon, 1 Jun 2020 22:27:06 +0200 Subject: [PATCH 3/6] issue #537 fix failing unit tests --- .../JUnitCustomRunnerTestUnitFinderTest.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java b/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java index 224f5cf15..28fd24ce3 100644 --- a/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java +++ b/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java @@ -123,15 +123,13 @@ public CustomSuiteRunner(final Class klass, final RunnerBuilder rb) * * This appears to be happening in org.apache.maven.plugins:maven-pmd-plugin:3.13.0. * - * This fails in two ways: - * 1) if the Suite has no SuiteClasses, it is determined to be a - * test, after which its description is used to create a TestUnit - * but as the classname of the description is not an actual class - * a ClassNotFoundException is thrown and the entiry test results - * in "PitError: Coverage generation minion exited abnormall - * y!" errors. - * 2) if the Suite does have SuiteClasss, it is determined to NOT be a - * test, after which it is ignored entirely. + * This needs to be addressed in two ways: + * 1) if the Suite has no SuiteClasses, isTest() is + * true. Pitest must not fail if the the name in the + * Description throws a ClassNotFoundException + * 2) if the Suite has SuiteClasses, isTest() is false, + * Pitest must then use the child descriptions of the Suite + * to continue testing with. */ public static class CustomDescriptionSuiteRunner extends Suite { From 7f70532f096bfc881ead073803553c8995c1741b Mon Sep 17 00:00:00 2001 From: Michiel Kalkman Date: Mon, 1 Jun 2020 22:40:07 +0200 Subject: [PATCH 4/6] issue #537 fix failing unit tests --- .../junit/JUnitCustomRunnerTestUnitFinderTest.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java b/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java index 28fd24ce3..c3d757ce3 100644 --- a/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java +++ b/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java @@ -123,13 +123,11 @@ public CustomSuiteRunner(final Class klass, final RunnerBuilder rb) * * This appears to be happening in org.apache.maven.plugins:maven-pmd-plugin:3.13.0. * - * This needs to be addressed in two ways: - * 1) if the Suite has no SuiteClasses, isTest() is - * true. Pitest must not fail if the the name in the - * Description throws a ClassNotFoundException - * 2) if the Suite has SuiteClasses, isTest() is false, - * Pitest must then use the child descriptions of the Suite - * to continue testing with. + * This needs to be addressed as follows: + * if a ClassNotFoundException is thrown + * when creating a TestUnit because of non-class name, + * find its direct children to create test units + * from. */ public static class CustomDescriptionSuiteRunner extends Suite { From a35a091eff31fb2e85870e5a8a10226f14b21eb4 Mon Sep 17 00:00:00 2001 From: Michiel Kalkman Date: Mon, 1 Jun 2020 22:52:50 +0200 Subject: [PATCH 5/6] issue #537 fix failing unit tests --- .../org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java b/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java index c3d757ce3..a3e139ee9 100644 --- a/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java +++ b/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java @@ -202,10 +202,6 @@ public static class EmptySuite { @RunWith(CustomDescriptionSuiteRunner.class) @SuiteClasses({ One.class }) public static class CustomTest { - @Test - public void six() { - - } } @Test From 5bc605def4e1d6ce91ddbdacfe6d49e4260de44c Mon Sep 17 00:00:00 2001 From: Michiel Kalkman Date: Mon, 1 Jun 2020 22:56:38 +0200 Subject: [PATCH 6/6] issue #537 fix failing unit tests --- .../JUnitCustomRunnerTestUnitFinderTest.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java b/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java index a3e139ee9..29b6badb3 100644 --- a/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java +++ b/pitest/src/test/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinderTest.java @@ -195,31 +195,31 @@ public static class CustomSuite { @RunWith(CustomDescriptionSuiteRunner.class) @SuiteClasses({ }) - public static class EmptySuite { + public static class EmptyCustomDescriptionSuite { } @RunWith(CustomDescriptionSuiteRunner.class) @SuiteClasses({ One.class }) - public static class CustomTest { + public static class CustomDescriptionSuite { } @Test public void shouldHandleEmptySuite() { - final Collection actual = findWithTestee(EmptySuite.class); + final Collection actual = findWithTestee(EmptyCustomDescriptionSuite.class); assertTrue(actual.isEmpty()); } @Test - public void shouldNotFindTestsInCustomSuite() { - final Collection actual = findWithTestee(CustomSuite.class); - assertTrue(actual.isEmpty()); + public void shouldNotFindTestsInCustomDescriptionSuite() { + final Collection actual = findWithTestee(CustomDescriptionSuite.class); + assertEquals(2, actual.size()); } @Test - public void shouldNotFindTestsInCustomSuite2() { - final Collection actual = findWithTestee(CustomTest.class); - assertEquals(2, actual.size()); + public void shouldNotFindTestsInCustomSuite() { + final Collection actual = findWithTestee(CustomSuite.class); + assertTrue(actual.isEmpty()); } public static class Three {