-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathConditionalTestLogic.java
More file actions
140 lines (116 loc) · 4.53 KB
/
ConditionalTestLogic.java
File metadata and controls
140 lines (116 loc) · 4.53 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
129
130
131
132
133
134
135
136
137
138
139
140
package testsmell.smell;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.ConditionalExpr;
import com.github.javaparser.ast.stmt.*;
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.List;
/*
This class check a test method for the existence of loops and conditional statements in the methods body
*/
public class ConditionalTestLogic extends AbstractSmell {
public ConditionalTestLogic(Thresholds thresholds) {
super(thresholds);
}
/**
* Checks of 'Conditional Test Logic' smell
*/
@Override
public String getSmellName() {
return "Conditional Test Logic";
}
/**
* Analyze the test file for test methods that use conditional statements
*/
@Override
public void runAnalysis(CompilationUnit testFileCompilationUnit, CompilationUnit productionFileCompilationUnit, String testFileName, String productionFileName) throws FileNotFoundException {
ConditionalTestLogic.ClassVisitor classVisitor;
classVisitor = new ConditionalTestLogic.ClassVisitor();
classVisitor.visit(testFileCompilationUnit, null);
}
private class ClassVisitor extends VoidVisitorAdapter<Void> {
private MethodDeclaration currentMethod = null;
private int conditionCount, ifCount, switchCount, forCount, foreachCount, whileCount = 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.setSmell(false); //default value is false (i.e. no smell)
super.visit(n, arg);
boolean isSmelly = conditionCount > thresholds.getConditionalTestLogic() |
ifCount > thresholds.getConditionalTestLogic() |
switchCount > thresholds.getConditionalTestLogic() |
foreachCount > thresholds.getConditionalTestLogic() |
forCount > thresholds.getConditionalTestLogic() |
whileCount > thresholds.getConditionalTestLogic();
testMethod.setSmell(isSmelly);
testMethod.addDataItem("ConditionCount", String.valueOf(conditionCount));
testMethod.addDataItem("IfCount", String.valueOf(ifCount));
testMethod.addDataItem("SwitchCount", String.valueOf(switchCount));
testMethod.addDataItem("ForeachCount", String.valueOf(foreachCount));
testMethod.addDataItem("ForCount", String.valueOf(forCount));
testMethod.addDataItem("WhileCount", String.valueOf(whileCount));
smellyElementsSet.add(testMethod);
//reset values for next method
currentMethod = null;
conditionCount = 0;
ifCount = 0;
switchCount = 0;
forCount = 0;
foreachCount = 0;
whileCount = 0;
}
}
@Override
public void visit(IfStmt n, Void arg) {
super.visit(n, arg);
if (currentMethod != null) {
ifCount++;
}
}
@Override
public void visit(SwitchStmt n, Void arg) {
super.visit(n, arg);
if (currentMethod != null) {
switchCount++;
}
}
@Override
public void visit(ConditionalExpr n, Void arg) {
super.visit(n, arg);
if (currentMethod != null) {
conditionCount++;
}
}
@Override
public void visit(ForStmt n, Void arg) {
super.visit(n, arg);
if (currentMethod != null) {
forCount++;
}
}
@Override
public void visit(ForEachStmt n, Void arg) {
super.visit(n, arg);
if (currentMethod != null) {
foreachCount++;
}
}
@Override
public void visit(WhileStmt n, Void arg) {
super.visit(n, arg);
if (currentMethod != null) {
whileCount++;
}
}
}
}