Skip to content

Commit c93c3cf

Browse files
committed
Added a collection class for test smell detection.
- Fixed the test smell detection results in the table viewer.
1 parent 4d79fab commit c93c3cf

File tree

4 files changed

+229
-128
lines changed

4 files changed

+229
-128
lines changed
Lines changed: 90 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,90 @@
1-
package main.java.testsmell.plugin.handlers;
2-
3-
import java.io.IOException;
4-
import java.util.Collection;
5-
import java.util.HashMap;
6-
import main.java.testsmell.AbstractSmell;
7-
import main.java.testsmell.TestFile;
8-
import main.java.testsmell.TestSmellDetector;
9-
10-
public class TSmellsDetection {
11-
12-
private HashMap<String, String> paths = new HashMap<>();
13-
private HashMap<String, Boolean> smells = new HashMap<String, Boolean>();
14-
private static TSmellsDetection detectionObj;
15-
private static Collection<TestFile> testFiles;
16-
17-
private TSmellsDetection(Collection<TestFile> t) {
18-
testFiles = t;
19-
}
20-
21-
/**
22-
* Create a static method to get instance.
23-
*/
24-
public static TSmellsDetection getInstance(Collection<TestFile> t) {
25-
if (detectionObj == null) {
26-
detectionObj = new TSmellsDetection(t);
27-
}
28-
return detectionObj;
29-
}
30-
31-
/**
32-
* Create a static method to get instance.
33-
*/
34-
public static TSmellsDetection getInstance() {
35-
return detectionObj;
36-
}
37-
38-
public void detectSmells() {
39-
TestSmellDetector testSmellDetector = TestSmellDetector.createTestSmellDetector();
40-
41-
TestFile tempFile;
42-
for (TestFile file : testFiles) {
43-
try {
44-
tempFile = testSmellDetector.detectSmells(file);
45-
paths.put(file.getProductionFilePath(), file.getTestFilePath());
46-
for (AbstractSmell smell : tempFile.getTestSmells()) {
47-
try {
48-
smells.put(smell.getSmellName(), smell.getHasSmell());
49-
} catch (NullPointerException e) {
50-
51-
}
52-
}
53-
} catch (IOException e) {
54-
e.printStackTrace();
55-
}
56-
}
57-
}
58-
59-
public String[] getProdFilePath() {
60-
String[] tempProdFilePaths = new String[paths.keySet().size()];
61-
Object[] prodFileKeySet = paths.keySet().toArray();
62-
for (int i = 0; i < tempProdFilePaths.length; i++) {
63-
tempProdFilePaths[i] = (String) prodFileKeySet[i];
64-
}
65-
return tempProdFilePaths;
66-
}
67-
68-
public String[] getTestFilePath() {
69-
String[] tempTestFilePaths = new String[paths.values().size()];
70-
Object[] tempTestFileValues = paths.values().toArray();
71-
for (int i = 0; i < tempTestFilePaths.length; i++) {
72-
tempTestFilePaths[i] = (String) tempTestFileValues[i];
73-
}
74-
return tempTestFilePaths;
75-
}
76-
77-
public String[] getSmellNames() {
78-
String[] tempSmellNames = new String[smells.keySet().size()];
79-
Object[] tempSmellNamesKeySet = smells.keySet().toArray();
80-
for (int i = 0; i < tempSmellNames.length; i++) {
81-
tempSmellNames[i] = (String) tempSmellNamesKeySet[i];
82-
}
83-
return tempSmellNames;
84-
}
85-
86-
public String[] hasSmells() {
87-
String[] tempHasSmells = new String[smells.values().size()];
88-
Object[] tempHasSmellsValues = smells.values().toArray();
89-
for (int i = 0; i < tempHasSmells.length; i++) {
90-
tempHasSmells[i] = ((Boolean) tempHasSmellsValues[i]).toString();
91-
}
92-
return tempHasSmells;
93-
}
94-
}
1+
package main.java.testsmell.plugin.handlers;
2+
3+
import java.io.IOException;
4+
import java.util.AbstractMap.SimpleEntry;
5+
import java.util.ArrayList;
6+
import main.java.testsmell.AbstractSmell;
7+
import main.java.testsmell.TestFile;
8+
import main.java.testsmell.TestSmellDetector;
9+
10+
public class TSmellsDetection {
11+
12+
private SimpleEntry<String, String> paths;
13+
private ArrayList<String> smells = new ArrayList<String>();
14+
private TestFile testFile;
15+
16+
protected TSmellsDetection(TestFile tf) {
17+
testFile = tf;
18+
}
19+
20+
public void detectSmells() {
21+
TestSmellDetector testSmellDetector = TestSmellDetector.createTestSmellDetector();
22+
TestFile tempFile;
23+
try {
24+
tempFile = testSmellDetector.detectSmells(testFile);
25+
paths = new SimpleEntry<String, String>(testFile.getProductionFilePath(),testFile.getTestFilePath());
26+
for (AbstractSmell smell : tempFile.getTestSmells()) {
27+
try {
28+
if (smell.getHasSmell()) {
29+
smells.add(smell.getSmellName());
30+
}
31+
} catch (NullPointerException e) {
32+
33+
}
34+
}
35+
} catch (IOException e) {
36+
e.printStackTrace();
37+
}
38+
// for (TestFile file : testFiles) {
39+
// try {
40+
// tempFile = testSmellDetector.detectSmells(file);
41+
// paths.put(file.getProductionFilePath(), file.getTestFilePath());
42+
// for (AbstractSmell smell : tempFile.getTestSmells()) {
43+
// try {
44+
// smells.put(smell.getSmellName(), smell.getHasSmell());
45+
// } catch (NullPointerException e) {
46+
//
47+
// }
48+
// }
49+
// } catch (IOException e) {
50+
// e.printStackTrace();
51+
// }
52+
// }
53+
}
54+
55+
public String[] getProdFilePath() {
56+
String[] tempProdFilePaths = new String[smells.size()];
57+
String prodFile = paths.getKey();
58+
for (int i = 0; i < tempProdFilePaths.length; i++) {
59+
tempProdFilePaths[i] = (String) prodFile;
60+
}
61+
return tempProdFilePaths;
62+
}
63+
64+
public String[] getTestFilePath() {
65+
String[] tempTestFilePaths = new String[smells.size()];
66+
String tempTestFile = paths.getValue();
67+
for (int i = 0; i < tempTestFilePaths.length; i++) {
68+
tempTestFilePaths[i] = tempTestFile;
69+
}
70+
return tempTestFilePaths;
71+
}
72+
73+
public String[] getSmellNames() {
74+
String[] tempSmellNames = new String[smells.size()];
75+
for (int i = 0; i < smells.size(); i++)
76+
{
77+
tempSmellNames[i] = smells.get(i);
78+
}
79+
return tempSmellNames;
80+
}
81+
82+
// public String[] hasSmells() {
83+
// String[] tempHasSmells = new String[smells.size()];
84+
// Object[] tempHasSmellsValues = smells.values().toArray();
85+
// for (int i = 0; i < tempHasSmells.length; i++) {
86+
// tempHasSmells[i] = ((Boolean) tempHasSmellsValues[i]).toString();
87+
// }
88+
// return tempHasSmells;
89+
// }
90+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
*
3+
*/
4+
package main.java.testsmell.plugin.handlers;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
9+
import main.java.testsmell.TestFile;
10+
11+
public class TSmellsDetectionCollection {
12+
13+
private static TSmellsDetectionCollection detectionCollectionObj = null;
14+
private int nextIndx;
15+
private ArrayList<TSmellsDetection> collection;
16+
17+
private TSmellsDetectionCollection() {
18+
collection = new ArrayList<TSmellsDetection>();
19+
nextIndx = 0;
20+
}
21+
22+
/**
23+
* Create a static method to get instance.
24+
*/
25+
protected static TSmellsDetectionCollection getInstance() {
26+
if (detectionCollectionObj == null) {
27+
detectionCollectionObj = new TSmellsDetectionCollection();
28+
}
29+
return detectionCollectionObj;
30+
}
31+
public static TSmellsDetectionCollection getInstanceExisting() {
32+
return detectionCollectionObj;
33+
}
34+
35+
public void addNewDetections(Collection<TestFile> t)
36+
{
37+
for (TestFile tf : t)
38+
{
39+
collection.add(new TSmellsDetection(tf));
40+
}
41+
42+
}
43+
private Boolean hasNextDetection()
44+
{
45+
if (collection.size() > 0 && nextIndx < collection.size())
46+
{
47+
return true;
48+
}
49+
else
50+
{
51+
return false;
52+
}
53+
}
54+
public TSmellsDetection getNextDetection()
55+
{
56+
TSmellsDetection detection;
57+
if (hasNextDetection())
58+
{
59+
detection = collection.get(nextIndx);
60+
nextIndx += 1;
61+
return detection;
62+
}
63+
return null;
64+
}
65+
66+
protected void resetNextIndexVal()
67+
{
68+
nextIndx = 0;
69+
}
70+
71+
72+
/**
73+
* Create a static method to get instance.
74+
*/
75+
}

src/main/java/testsmell/plugin/handlers/TSmellsHandler.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,22 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
5959
project = getCurrentProject(event);
6060
getProjectInfo(project);
6161
} catch (CoreException | NullPointerException ex) {
62-
MessageDialog.openWarning(activeShell, "TestSmellDetector", "Please select a project");
62+
ex.printStackTrace();
63+
MessageDialog.openError(activeShell, "TestSmellDetector", "Please select a project");
6364
return null;
6465
}
6566

6667
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
6768
try {
68-
69-
TSmellsDetection detect = TSmellsDetection.getInstance(testFiles);
70-
detect.detectSmells();
69+
TSmellsDetectionCollection colllectionObj = TSmellsDetectionCollection.getInstance();
70+
colllectionObj.addNewDetections(testFiles);
71+
TSmellsDetection detect = colllectionObj.getNextDetection();
72+
while (detect != null)
73+
{
74+
detect.detectSmells();
75+
detect = colllectionObj.getNextDetection();
76+
}
77+
colllectionObj.resetNextIndexVal();
7178
page.showView("TestSmellsDetector.view");
7279

7380
} catch (PartInitException e) {
@@ -116,16 +123,42 @@ private void getProjectInfo(IProject project) throws CoreException, JavaModelExc
116123
}
117124
}
118125
if (testPaths.size() > 0) {
119-
getProdFiles();
120-
121-
TestFile testFile;
126+
try {
127+
getProdFiles();
122128

123-
for (Entry<IResource, IResource> fullPaths : paths.entrySet()) {
124-
String prodFilePath = fullPaths.getKey().getRawLocation().toString();
125-
String testFilePath = fullPaths.getValue().getRawLocation().toString();
126-
127-
testFile = new TestFile(project.getName(), testFilePath, prodFilePath);
128-
testFiles.add(testFile);
129+
TestFile testFile;
130+
131+
for (Entry<IResource, IResource> fullPaths : paths.entrySet()) {
132+
String prodFilePath = fullPaths.getKey().getRawLocation().toString();
133+
String testFilePath = fullPaths.getValue().getRawLocation().toString();
134+
testFile = new TestFile(project.getName(), testFilePath, prodFilePath);
135+
testFiles.add(testFile);
136+
}
137+
} catch (NullPointerException e) {
138+
Shell activeShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
139+
String errorStr = "Recheck production and test file names. Below are the test file/production file mappings that triggered the exception: \n\n";
140+
for (Entry<IResource, IResource> fullPaths : paths.entrySet())
141+
{
142+
if (fullPaths.getKey() == null)
143+
{
144+
errorStr += "Production File: null ,";
145+
}
146+
else
147+
{
148+
errorStr += "Production File: " + fullPaths.getKey().getRawLocation().toString() + " ,";
149+
}
150+
151+
if (fullPaths.getValue() == null)
152+
{
153+
errorStr += "Test File: null ,";
154+
}
155+
else
156+
{
157+
errorStr += "Test File: " + fullPaths.getValue().getRawLocation().toString() + " \n\n";
158+
}
159+
160+
}
161+
MessageDialog.openError(activeShell, "Failed to load files" , errorStr );
129162
}
130163
}
131164
}

0 commit comments

Comments
 (0)