-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathDependentTest.java
More file actions
128 lines (106 loc) · 4.25 KB
/
DependentTest.java
File metadata and controls
128 lines (106 loc) · 4.25 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package testsmell.smell;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import testsmell.AbstractSmell;
import testsmell.Util;
import thresholds.Thresholds;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class DependentTest extends AbstractSmell {
private List<TestMethod> testMethods;
public DependentTest(Thresholds thresholds) {
super(thresholds);
testMethods = new ArrayList<>();
}
/**
* Checks of 'DependentTest' smell
*/
@Override
public String getSmellName() {
return "Dependent Test";
}
/**
* Analyze the test file for test methods that call other test methods
*/
@Override
public void runAnalysis(CompilationUnit testFileCompilationUnit, CompilationUnit productionFileCompilationUnit, String testFileName, String productionFileName) throws FileNotFoundException {
DependentTest.ClassVisitor classVisitor;
classVisitor = new DependentTest.ClassVisitor();
classVisitor.visit(testFileCompilationUnit, null);
Set<String> testMethodNames = testMethods.stream()
.map(method -> method.getMethodDeclaration().getNameAsString())
.collect(Collectors.toSet());
for (TestMethod testMethod : testMethods) {
boolean match = testMethod.getCalledMethods()
.stream().anyMatch(method -> testMethodNames.contains(method.getName()));
if (match) {
smellyElementsSet.add(new testsmell.TestMethod(testMethod.getMethodDeclaration().getNameAsString()));
}
}
/*
for (int i = 0; i < testMethods.get(i).getCalledMethods().size(); i++) {
for (TestMethod testMethod : testMethods) {
if (testMethods.get(i).getCalledMethods().stream().anyMatch(x -> x.getName().equals(testMethod.getMethodDeclaration().getNameAsString()))) {
smellyElementList.add(new testsmell.TestMethod(testMethod.getMethodDeclaration().getNameAsString()));
}
}
}*/
}
private class ClassVisitor extends VoidVisitorAdapter<Void> {
private MethodDeclaration currentMethod = null;
List<CalledMethod> calledMethods;
// examine all methods in the test class
@Override
public void visit(MethodDeclaration n, Void arg) {
if (Util.isValidTestMethod(n)) {
currentMethod = n;
calledMethods = new ArrayList<>();
super.visit(n, arg);
testMethods.add(new DependentTest.TestMethod(n, calledMethods));
}
}
// 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 (!calledMethods.contains(new CalledMethod(n.getArguments().size(), n.getNameAsString()))) {
calledMethods.add(new CalledMethod(n.getArguments().size(), n.getNameAsString()));
}
}
}
}
private class TestMethod {
public List<CalledMethod> getCalledMethods() {
return calledMethods;
}
public MethodDeclaration getMethodDeclaration() {
return methodDeclaration;
}
public TestMethod(MethodDeclaration methodDeclaration, List<CalledMethod> calledMethods) {
this.methodDeclaration = methodDeclaration;
this.calledMethods = calledMethods;
}
private List<CalledMethod> calledMethods;
private MethodDeclaration methodDeclaration;
}
private class CalledMethod {
public int getTotalArguments() {
return totalArguments;
}
public String getName() {
return name;
}
public CalledMethod(int totalArguments, String name) {
this.totalArguments = totalArguments;
this.name = name;
}
private int totalArguments;
private String name;
}
}