-
Notifications
You must be signed in to change notification settings - Fork 38
/
VerboseTest.java
94 lines (80 loc) · 3.03 KB
/
VerboseTest.java
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
package testsmell.smell;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import testsmell.AbstractSmell;
import testsmell.SmellyElement;
import testsmell.TestMethod;
import testsmell.Util;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
/*
If a test methods contains a statements that exceeds a certain threshold, the method is marked as smelly
*/
public class VerboseTest extends AbstractSmell {
private List<SmellyElement> smellyElementList;
public VerboseTest() {
smellyElementList = new ArrayList<>();
}
/**
* Checks of 'Verbose Test' smell
*/
@Override
public String getSmellName() {
return "Verbose Test";
}
/**
* Returns true if any of the elements has a smell
*/
@Override
public boolean getHasSmell() {
return smellyElementList.stream().filter(x -> x.getHasSmell()).count() >= 1;
}
/**
* Analyze the test file for test methods for the 'Verbose Test' smell
*/
@Override
public void runAnalysis(CompilationUnit testFileCompilationUnit, CompilationUnit productionFileCompilationUnit, String testFileName, String productionFileName) throws FileNotFoundException {
VerboseTest.ClassVisitor classVisitor;
classVisitor = new VerboseTest.ClassVisitor();
classVisitor.visit(testFileCompilationUnit, null);
}
/**
* Returns the set of analyzed elements (i.e. test methods)
*/
@Override
public List<SmellyElement> getSmellyElements() {
return smellyElementList;
}
private class ClassVisitor extends VoidVisitorAdapter<Void> {
final int MAX_STATEMENTS = 123;
private MethodDeclaration currentMethod = null;
private int verboseCount = 0;
TestMethod testMethod;
// examine all methods in the test class
@Override
public void visit(MethodDeclaration n, Void arg) {
if (Util.isValidTestMethod(n)) {
currentMethod = n;
testMethod = new TestMethod(n.getNameAsString());
testMethod.setHasSmell(false); //default value is false (i.e. no smell)
//method should not be abstract
if (!currentMethod.isAbstract()) {
if (currentMethod.getBody().isPresent()) {
//get the total number of statements contained in the method
if (currentMethod.getBody().get().getStatements().size() >= MAX_STATEMENTS) {
verboseCount++;
}
}
}
testMethod.setHasSmell(verboseCount >= 1);
testMethod.addDataItem("VerboseCount", String.valueOf(verboseCount));
smellyElementList.add(testMethod);
//reset values for next method
currentMethod = null;
verboseCount = 0;
}
}
}
}