Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: support deprecated assertions class of junit3 #926

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.visitor.filter.TypeFilter;

import java.util.Arrays;
import java.util.List;

/**
Expand All @@ -19,11 +20,14 @@ public abstract class AbstractTestFramework implements TestFrameworkSupport {

private IsAssertInvocationFilter filter;

protected final List<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;

import java.util.List;
import java.util.regex.Pattern;

/**
Expand All @@ -15,7 +16,7 @@
*/
public class IsAssertInvocationFilter {

private final String assertionClassName;
private final List<String> assertionClassesName;

/**
* This field represent the number of method call to explore to find any assertion.
Expand All @@ -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<String> 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<String> assertionClassesName, int depth) {
this.assertionClassesName = assertionClassesName;
this.depth = depth;
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class JUnit3Support extends JUnitSupport {

public JUnit3Support() {
super("junit.framework.TestCase");
super("junit.framework.TestCase", "junit.framework.Assert");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected boolean isATest(CtMethod<?> candidate) {
return hasAnnotation(getFullQualifiedNameOfAnnotationTest(), candidate);
}

public JUnitSupport(String qualifiedNameOfAssertClass) {
public JUnitSupport(String... qualifiedNameOfAssertClass) {
super(qualifiedNameOfAssertClass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.inria.helper;

import junit.framework.TestCase;
import junit.framework.Assert;

public class SecondClassJUnit3 extends TestCase {

Expand All @@ -27,4 +28,8 @@ public void testExpectingAnException() {
throw new RuntimeException();
}

public void testUsingDeprecatedAssertClass() {
Assert.assertEquals(3, 3);
}

}