-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathTestSmellDetector.java
More file actions
102 lines (86 loc) · 3.54 KB
/
Copy pathTestSmellDetector.java
File metadata and controls
102 lines (86 loc) · 3.54 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
package edu.rit.se.testsmells;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import org.apache.commons.lang3.StringUtils;
import edu.rit.se.testsmells.smell.*;
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<AbstractSmell> testSmells;
/**
* Instantiates the various test smell analyzer classes and loads the objects into an List
*/
public TestSmellDetector() {
initializeSmells();
}
private void initializeSmells(){
testSmells = new ArrayList<>();
testSmells.add(new AssertionRoulette());
testSmells.add(new ConditionalTestLogic());
testSmells.add(new ConstructorInitialization());
testSmells.add(new DefaultTest());
testSmells.add(new EmptyTest());
testSmells.add(new ExceptionCatchingThrowing());
testSmells.add(new GeneralFixture());
testSmells.add(new MysteryGuest());
testSmells.add(new PrintStatement());
testSmells.add(new RedundantAssertion());
testSmells.add(new SensitiveEquality());
testSmells.add(new VerboseTest());
testSmells.add(new SleepyTest());
testSmells.add(new EagerTest());
testSmells.add(new LazyTest());
testSmells.add(new DuplicateAssert());
testSmells.add(new UnknownTest());
testSmells.add(new IgnoredTest());
testSmells.add(new ResourceOptimism());
testSmells.add(new MagicNumberTest());
testSmells.add(new DependentTest());
}
/**
* Factory method that provides a new instance of the TestSmellDetector
*
* @return new TestSmellDetector instance
*/
public static TestSmellDetector createTestSmellDetector() {
return new TestSmellDetector();
}
/**
* 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(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, productionFileCompilationUnit=null;
FileInputStream testFileInputStream, productionFileInputStream;
if(!StringUtils.isEmpty(testFile.getTestFilePath())) {
testFileInputStream = new FileInputStream(testFile.getTestFilePath());
testFileCompilationUnit = JavaParser.parse(testFileInputStream);
}
if(!StringUtils.isEmpty(testFile.getProductionFilePath())){
productionFileInputStream = new FileInputStream(testFile.getProductionFilePath());
productionFileCompilationUnit = JavaParser.parse(productionFileInputStream);
}
initializeSmells();
for (AbstractSmell smell : testSmells) {
try {
smell.runAnalysis(testFileCompilationUnit, productionFileCompilationUnit,testFile.getTestFileNameWithoutExtension(),testFile.getProductionFileNameWithoutExtension());
} catch (FileNotFoundException e) {
testFile.addSmell(null);
continue;
}
testFile.addSmell(smell);
}
return testFile;
}
}