-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathIgnoredTest.java
More file actions
91 lines (79 loc) · 2.95 KB
/
IgnoredTest.java
File metadata and controls
91 lines (79 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package testsmell.smell;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import testsmell.AbstractSmell;
import testsmell.SmellyElement;
import testsmell.TestClass;
import testsmell.TestMethod;
import thresholds.Thresholds;
import java.io.FileNotFoundException;
import java.util.List;
public class IgnoredTest extends AbstractSmell {
public IgnoredTest(Thresholds thresholds) {
super(thresholds);
}
/**
* Checks of 'Ignored Test' smell
*/
@Override
public String getSmellName() {
return "IgnoredTest";
}
/**
* Analyze the test file for test methods that contain Ignored test methods
*/
@Override
public void runAnalysis(CompilationUnit testFileCompilationUnit, CompilationUnit productionFileCompilationUnit, String testFileName, String productionFileName) throws FileNotFoundException {
IgnoredTest.ClassVisitor classVisitor;
classVisitor = new IgnoredTest.ClassVisitor();
classVisitor.visit(testFileCompilationUnit, null);
}
/**
* Visitor class
*/
private class ClassVisitor extends VoidVisitorAdapter<Void> {
TestMethod testMethod;
TestClass testClass;
/**
* This method will check if the class has the @Ignore annotation
*/
@Override
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
if (n.getAnnotationByName("Ignore").isPresent()) {
testClass = new TestClass(n.getNameAsString());
testClass.setHasSmell(true);
smellyElementsSet.add(testClass);
}
super.visit(n, arg);
}
/**
* The purpose of this method is to 'visit' all test methods in the test file.
*/
@Override
public void visit(MethodDeclaration n, Void arg) {
//JUnit 4
//check if test method has Ignore annotation
if (n.getAnnotationByName("Test").isPresent()) {
if (n.getAnnotationByName("Ignore").isPresent()) {
testMethod = new TestMethod(n.getNameAsString());
testMethod.setSmell(true);
smellyElementsSet.add(testMethod);
return;
}
}
//JUnit 3
//check if test method is not public
if (n.getNameAsString().toLowerCase().startsWith("test")) {
if (n.getModifiers().stream().noneMatch(m -> m.getKeyword() == Modifier.Keyword.PUBLIC)) {
testMethod = new TestMethod(n.getNameAsString());
testMethod.setSmell(true);
smellyElementsSet.add(testMethod);
return;
}
}
}
}
}