-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathUnknownTest.java
More file actions
107 lines (91 loc) · 3.79 KB
/
UnknownTest.java
File metadata and controls
107 lines (91 loc) · 3.79 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package testsmell.smell;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.MemberValuePair;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import testsmell.AbstractSmell;
import testsmell.SmellyElement;
import testsmell.TestMethod;
import testsmell.Util;
import thresholds.Thresholds;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class UnknownTest extends AbstractSmell {
public UnknownTest(Thresholds thresholds) {
super(thresholds);
}
/**
* Checks of 'Unknown Test' smell
*/
@Override
public String getSmellName() {
return "Unknown Test";
}
/**
* Analyze the test file for test methods that do not have assert statement or exceptions
*/
@Override
public void runAnalysis(CompilationUnit testFileCompilationUnit, CompilationUnit productionFileCompilationUnit, String testFileName, String productionFileName) throws FileNotFoundException {
UnknownTest.ClassVisitor classVisitor;
classVisitor = new UnknownTest.ClassVisitor();
classVisitor.visit(testFileCompilationUnit, null);
}
private class ClassVisitor extends VoidVisitorAdapter<Void> {
private MethodDeclaration currentMethod = null;
TestMethod testMethod;
List<String> assertMessage = new ArrayList<>();
boolean hasAssert = false;
boolean hasExceptionAnnotation = false;
// examine all methods in the test class
@Override
public void visit(MethodDeclaration n, Void arg) {
if (Util.isValidTestMethod(n)) {
Optional<AnnotationExpr> assertAnnotation = n.getAnnotationByName("Test");
if (assertAnnotation.isPresent()) {
for (Node node : assertAnnotation.get().getChildNodes()) {
if (node instanceof MemberValuePair) {
MemberValuePair pair = (MemberValuePair) node;
if (pair.getName().getIdentifier().equals("expected")
&& pair.getValue().toString().contains("Exception")) {
hasExceptionAnnotation = true;
}
}
}
}
currentMethod = n;
testMethod = new TestMethod(n.getNameAsString());
testMethod.setSmell(false); //default value is false (i.e. no smell)
super.visit(n, arg);
// no assertions and no annotation
if (!hasAssert && !hasExceptionAnnotation)
testMethod.setSmell(true);
smellyElementsSet.add(testMethod);
//reset values for next method
currentMethod = null;
assertMessage = new ArrayList<>();
hasAssert = false;
}
}
// examine the methods being called within the test method
@Override
public void visit(MethodCallExpr n, Void arg) {
super.visit(n, arg);
if (currentMethod != null) {
// if the name of a method being called start with 'assert'
if (n.getNameAsString().startsWith(("assert"))) {
hasAssert = true;
}
// if the name of a method being called is 'fail'
else if (n.getNameAsString().equals("fail")) {
hasAssert = true;
}
}
}
}
}