-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathTestSmellDetector.java
More file actions
108 lines (96 loc) · 4.08 KB
/
TestSmellDetector.java
File metadata and controls
108 lines (96 loc) · 4.08 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
package testsmell;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.TypeDeclaration;
import org.apache.commons.lang3.StringUtils;
import testsmell.smell.*;
import thresholds.Thresholds;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TestSmellDetector {
private List<SmellFactory> testSmells;
private Thresholds thresholds;
/**
* Instantiates the various test smell analyzer classes and loads the objects into an list.
* Each smell analyzer is initialized with a threshold object to set the most appropriate rule for the detection
*
* @param thresholds it could be the default threshold of the ones defined by Spadini
*/
public TestSmellDetector(Thresholds thresholds) {
this.thresholds = thresholds;
initializeSmells();
}
private void initializeSmells() {
testSmells = new ArrayList<>();
testSmells.add(AssertionRoulette::new);
testSmells.add(ConditionalTestLogic::new);
testSmells.add(ConstructorInitialization::new);
testSmells.add(DefaultTest::new);
testSmells.add(EmptyTest::new);
testSmells.add(ExceptionCatchingThrowing::new);
testSmells.add(GeneralFixture::new);
testSmells.add(MysteryGuest::new);
testSmells.add(PrintStatement::new);
testSmells.add(RedundantAssertion::new);
testSmells.add(SensitiveEquality::new);
testSmells.add(VerboseTest::new);
testSmells.add(SleepyTest::new);
testSmells.add(EagerTest::new);
testSmells.add(LazyTest::new);
testSmells.add(DuplicateAssert::new);
testSmells.add(UnknownTest::new);
testSmells.add(IgnoredTest::new);
testSmells.add(ResourceOptimism::new);
testSmells.add(MagicNumberTest::new);
testSmells.add(DependentTest::new);
}
public void setTestSmells(List<SmellFactory> testSmells) {
this.testSmells = testSmells;
}
/**
* Provides the names of the smells that are being checked for in the code
*
* @return list of smell names
*/
public List<String> getTestSmellNames() {
return testSmells.stream()
.map(factory -> factory.createInstance(thresholds))
.map(AbstractSmell::getSmellName)
.collect(Collectors.toList());
}
/**
* Loads the java source code file into an AST and then analyzes it for the existence of the different types of
* test smells
*/
public TestFile detectSmells(TestFile testFile) throws IOException {
CompilationUnit testFileCompilationUnit = null;
CompilationUnit productionFileCompilationUnit = null;
FileInputStream testFileInputStream, productionFileInputStream;
if (!StringUtils.isEmpty(testFile.getTestFilePath())) {
testFileInputStream = new FileInputStream(testFile.getTestFilePath());
testFileCompilationUnit = Util.parseJava(testFileInputStream);
TypeDeclaration typeDeclaration = testFileCompilationUnit.getTypes().get(0);
testFile.setNumberOfTestMethods(typeDeclaration.getMethods().size());
}
if (!StringUtils.isEmpty(testFile.getProductionFilePath())) {
productionFileInputStream = new FileInputStream(testFile.getProductionFilePath());
productionFileCompilationUnit = Util.parseJava(productionFileInputStream);
}
for (SmellFactory smellFactory : testSmells) {
AbstractSmell smell = smellFactory.createInstance(thresholds);
try {
smell.runAnalysis(testFileCompilationUnit, productionFileCompilationUnit,
testFile.getTestFileNameWithoutExtension(),
testFile.getProductionFileNameWithoutExtension());
} catch (FileNotFoundException e) {
testFile.addSmell(null);
continue;
}
testFile.addSmell(smell);
}
return testFile;
}
}