From e787235d27b9758c271e0fc328a16bef8e2581cf Mon Sep 17 00:00:00 2001 From: danglotb Date: Tue, 26 Nov 2019 15:03:14 +0100 Subject: [PATCH 1/2] feat: support now org.junit.framework.Assert as the class used to asserts --- .../test_framework/AbstractTestFramework.java | 10 +++++++--- .../test_framework/TestFramework.java | 2 +- .../assertions/IsAssertInvocationFilter.java | 19 +++++++++++-------- .../implementations/junit/JUnit3Support.java | 2 +- .../implementations/junit/JUnitSupport.java | 2 +- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/dspot/src/main/java/eu/stamp_project/test_framework/AbstractTestFramework.java b/dspot/src/main/java/eu/stamp_project/test_framework/AbstractTestFramework.java index bdab138a8..8bcf05b7c 100644 --- a/dspot/src/main/java/eu/stamp_project/test_framework/AbstractTestFramework.java +++ b/dspot/src/main/java/eu/stamp_project/test_framework/AbstractTestFramework.java @@ -8,6 +8,7 @@ import spoon.reflect.declaration.CtMethod; import spoon.reflect.visitor.filter.TypeFilter; +import java.util.Arrays; import java.util.List; /** @@ -19,11 +20,14 @@ public abstract class AbstractTestFramework implements TestFrameworkSupport { private IsAssertInvocationFilter filter; + protected final List qualifiedNameOfAssertClasses; + protected final String qualifiedNameOfAssertClass; - public AbstractTestFramework(String qualifiedNameOfAssertClass) { - this.filter = new IsAssertInvocationFilter(qualifiedNameOfAssertClass); - this.qualifiedNameOfAssertClass = qualifiedNameOfAssertClass; + public AbstractTestFramework(String... qualifiedNameOfAssertClasses) { + this.qualifiedNameOfAssertClasses = Arrays.asList(qualifiedNameOfAssertClasses); + this.qualifiedNameOfAssertClass = this.qualifiedNameOfAssertClasses.get(0); + this.filter = new IsAssertInvocationFilter(this.qualifiedNameOfAssertClasses); } @Override diff --git a/dspot/src/main/java/eu/stamp_project/test_framework/TestFramework.java b/dspot/src/main/java/eu/stamp_project/test_framework/TestFramework.java index b98b6767f..049034f60 100644 --- a/dspot/src/main/java/eu/stamp_project/test_framework/TestFramework.java +++ b/dspot/src/main/java/eu/stamp_project/test_framework/TestFramework.java @@ -147,7 +147,7 @@ private TestFrameworkSupport getTestFramework(CtMethod testMethod) { //Retrieving associated test framework from cache TestFrameworkSupport tfs = DSpotCache.getTestFrameworkCache().get(TypeUtils.getQualifiedName(originalMethod)); if (tfs == null){ //If not cached, test framework is computed and stored in cache. - tfs = getTestFrameworkImpl (testMethod); + tfs = getTestFrameworkImpl(testMethod); DSpotCache.getTestFrameworkCache().put(TypeUtils.getQualifiedName(originalMethod), tfs); } return tfs; diff --git a/dspot/src/main/java/eu/stamp_project/test_framework/assertions/IsAssertInvocationFilter.java b/dspot/src/main/java/eu/stamp_project/test_framework/assertions/IsAssertInvocationFilter.java index 726af4ebf..1896ae610 100644 --- a/dspot/src/main/java/eu/stamp_project/test_framework/assertions/IsAssertInvocationFilter.java +++ b/dspot/src/main/java/eu/stamp_project/test_framework/assertions/IsAssertInvocationFilter.java @@ -6,6 +6,7 @@ import spoon.reflect.reference.CtTypeReference; import spoon.reflect.visitor.filter.TypeFilter; +import java.util.List; import java.util.regex.Pattern; /** @@ -15,7 +16,7 @@ */ public class IsAssertInvocationFilter { - private final String assertionClassName; + private final List assertionClassesName; /** * This field represent the number of method call to explore to find any assertion. @@ -25,19 +26,19 @@ public class IsAssertInvocationFilter { private final int depth; /** - * @param assertionClassName the name of the class of the assertions used, e.g. org.junit.Assert. + * @param assertionClassesName the name of the classes of the assertions used, e.g. org.junit.Assert. */ - public IsAssertInvocationFilter(String assertionClassName) { - this.assertionClassName = assertionClassName; + public IsAssertInvocationFilter(List assertionClassesName) { + this.assertionClassesName = assertionClassesName; this.depth = 3; } /** - * @param assertionClassName the name of the class of the assertions used, e.g. org.junit.Assert. + * @param assertionClassesName the name of the class of the assertions used, e.g. org.junit.Assert. * @param depth the number of method chained to find an assertion */ - public IsAssertInvocationFilter(String assertionClassName, int depth) { - this.assertionClassName = assertionClassName; + public IsAssertInvocationFilter(List assertionClassesName, int depth) { + this.assertionClassesName = assertionClassesName; this.depth = depth; } @@ -100,7 +101,9 @@ private boolean _isAssert(CtInvocation invocation) { } else { qualifiedNameOfDeclaringType = invocation.getExecutable().getDeclaringType().getQualifiedName(); } - return Pattern.compile(this.assertionClassName).matcher(qualifiedNameOfDeclaringType).matches(); + return this.assertionClassesName.stream().anyMatch( + assertionClassName -> Pattern.compile(assertionClassName).matcher(qualifiedNameOfDeclaringType).matches() + ); } private boolean containsMethodCallToAssertion(CtInvocation invocation, int deep) { diff --git a/dspot/src/main/java/eu/stamp_project/test_framework/implementations/junit/JUnit3Support.java b/dspot/src/main/java/eu/stamp_project/test_framework/implementations/junit/JUnit3Support.java index cc6304f73..17d8c5478 100644 --- a/dspot/src/main/java/eu/stamp_project/test_framework/implementations/junit/JUnit3Support.java +++ b/dspot/src/main/java/eu/stamp_project/test_framework/implementations/junit/JUnit3Support.java @@ -22,7 +22,7 @@ public class JUnit3Support extends JUnitSupport { public JUnit3Support() { - super("junit.framework.TestCase"); + super("junit.framework.TestCase", "junit.framework.Assert"); } @Override diff --git a/dspot/src/main/java/eu/stamp_project/test_framework/implementations/junit/JUnitSupport.java b/dspot/src/main/java/eu/stamp_project/test_framework/implementations/junit/JUnitSupport.java index 8ab8082a8..39e6b3377 100644 --- a/dspot/src/main/java/eu/stamp_project/test_framework/implementations/junit/JUnitSupport.java +++ b/dspot/src/main/java/eu/stamp_project/test_framework/implementations/junit/JUnitSupport.java @@ -39,7 +39,7 @@ protected boolean isATest(CtMethod candidate) { return hasAnnotation(getFullQualifiedNameOfAnnotationTest(), candidate); } - public JUnitSupport(String qualifiedNameOfAssertClass) { + public JUnitSupport(String... qualifiedNameOfAssertClass) { super(qualifiedNameOfAssertClass); } From e1673a28bc8bbd04dadce53365bfb3ca2cbcb33c Mon Sep 17 00:00:00 2001 From: danglotb Date: Tue, 26 Nov 2019 15:10:41 +0100 Subject: [PATCH 2/2] test: unit test the support by Junit3 of the two assert class --- .../test_framework/TestFrameworkTest.java | 18 ++++++++++++++++++ .../fr/inria/helper/SecondClassJUnit3.java | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/dspot/src/test/java/eu/stamp_project/test_framework/TestFrameworkTest.java b/dspot/src/test/java/eu/stamp_project/test_framework/TestFrameworkTest.java index 4036c0354..795edb6e2 100644 --- a/dspot/src/test/java/eu/stamp_project/test_framework/TestFrameworkTest.java +++ b/dspot/src/test/java/eu/stamp_project/test_framework/TestFrameworkTest.java @@ -30,6 +30,24 @@ private CtMethod findAndRegister(String ctClass, String methodName) { return testExpectingAnException; } + @Test + public void testJUnit3TestFrameworkSupportsTwoAssertClasses() { + /* + The test framework for junit 3 should recognized both class as assert class: + - junit.framework.TestCase + - junit.framework.Assert + */ + + CtMethod testJUnit3 = this.findMethod("fr.inria.helper.SecondClassJUnit3", "testUsingDeprecatedAssertClass"); + System.out.println(testJUnit3); + assertTrue(TestFramework.get().isTest(testJUnit3)); + assertTrue(TestFramework.get().isAssert(testJUnit3.getBody().getStatement(0))); + testJUnit3 = this.findMethod("fr.inria.helper.SecondClassJUnit3", "test"); + System.out.println(testJUnit3); + assertTrue(TestFramework.get().isTest(testJUnit3)); + assertTrue(TestFramework.get().isAssert(testJUnit3.getBody().getStatement(0))); + } + @Test public void testGenerateAfterClassToSaveObservations() { diff --git a/dspot/src/test/resources/sample/src/test/java/fr/inria/helper/SecondClassJUnit3.java b/dspot/src/test/resources/sample/src/test/java/fr/inria/helper/SecondClassJUnit3.java index e3d936383..55f923f81 100644 --- a/dspot/src/test/resources/sample/src/test/java/fr/inria/helper/SecondClassJUnit3.java +++ b/dspot/src/test/resources/sample/src/test/java/fr/inria/helper/SecondClassJUnit3.java @@ -1,6 +1,7 @@ package fr.inria.helper; import junit.framework.TestCase; +import junit.framework.Assert; public class SecondClassJUnit3 extends TestCase { @@ -27,4 +28,8 @@ public void testExpectingAnException() { throw new RuntimeException(); } + public void testUsingDeprecatedAssertClass() { + Assert.assertEquals(3, 3); + } + }