Skip to content

Commit

Permalink
Merge pull request #5 from giograno/master
Browse files Browse the repository at this point in the history
Multi-OS compatibility
  • Loading branch information
shehan authored Jul 1, 2020
2 parents 08be07d + 2fe980b commit 19adeb4
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 49 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@
hs_err_pid*

# IntelliJ IDEA
.idea
.idea
Output_TestSmellDetection*
target/
TestSmellDetector.iml
files-smell.csv
21 changes: 0 additions & 21 deletions TestSmellDetector.iml

This file was deleted.

56 changes: 55 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<groupId>edu.rit.se.testsmells</groupId>
<artifactId>TestSmellDetector</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
Expand All @@ -17,6 +18,42 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<dependencies>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.4.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
Main
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand All @@ -31,7 +68,24 @@
<artifactId>opencsv</artifactId>
<version>3.9</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
9 changes: 5 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import testsmell.ResultsWriter;
import testsmell.TestFile;
import testsmell.TestSmellDetector;
import testsmell.smell.AssertionRoulette;
import testsmell.smell.EagerTest;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -27,8 +29,7 @@ public static void main(String[] args) throws IOException {
}
}


TestSmellDetector testSmellDetector = TestSmellDetector.createTestSmellDetector();
TestSmellDetector testSmellDetector = new TestSmellDetector();

/*
Read the input file and build the TestFile objects
Expand Down Expand Up @@ -63,7 +64,7 @@ public static void main(String[] args) throws IOException {

columnNames = testSmellDetector.getTestSmellNames();
columnNames.add(0, "App");
columnNames.add(1, "Version");
columnNames.add(1, "TestClass");
columnNames.add(2, "TestFilePath");
columnNames.add(3, "ProductionFilePath");
columnNames.add(4, "RelativeTestFilePath");
Expand All @@ -88,7 +89,7 @@ public static void main(String[] args) throws IOException {
//write output
columnValues = new ArrayList<>();
columnValues.add(file.getApp());
columnValues.add(file.getTagName());
columnValues.add(file.getTestFileName());
columnValues.add(file.getTestFilePath());
columnValues.add(file.getProductionFilePath());
columnValues.add(file.getRelativeTestFilePath());
Expand Down
54 changes: 34 additions & 20 deletions src/main/java/testsmell/TestFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -40,13 +41,18 @@ public void addSmell(AbstractSmell smell) {
testSmells.add(smell);
}

/**
* Supposed to return the version of the project.
* Returns the "N.I.Y", Not Implemented Yet string
* todo: not implemented in any way yet
*/
public String getTagName(){
return testFilePath.split("\\\\")[4];
return "N.I.Y";
}

public String getTestFileName(){
int lastIndex = testFilePath.lastIndexOf("\\");
return testFilePath.substring(lastIndex+1,testFilePath.length());
int lastIndex = testFilePath.lastIndexOf(File.separator);
return testFilePath.substring(lastIndex+1);
}

public String getTestFileNameWithoutExtension(){
Expand All @@ -62,32 +68,40 @@ public String getProductionFileNameWithoutExtension(){
}

public String getProductionFileName(){
int lastIndex = productionFilePath.lastIndexOf("\\");
int lastIndex = productionFilePath.lastIndexOf(File.separator);
if(lastIndex==-1)
return "";
return productionFilePath.substring(lastIndex+1,productionFilePath.length());
return productionFilePath.substring(lastIndex+1);
}

/**
* Returns the path of the test file relative to the folder with the name of the project.
* If the project directory has a different name, returns an empty string.
* @return the relative test file path
*/
public String getRelativeTestFilePath() {
String[] splitString = testFilePath.split("\\\\");
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 5; i++) {
stringBuilder.append(splitString[i] + "\\");
}
return testFilePath.substring(stringBuilder.toString().length()).replace("\\", "/");
if (!StringUtils.isEmpty(testFilePath)) {
int projectNameIndex = testFilePath.lastIndexOf(app);
if (projectNameIndex == -1)
return "";
return testFilePath.substring(projectNameIndex+app.length()+File.separator.length());
} else
return "";
}

/**
* Returns the path of the production file relative to the folder with the name of the project.
* If the project directory has a different name, returns an empty string.
* @return the relative production file path
*
*/
public String getRelativeProductionFilePath() {
if (!StringUtils.isEmpty(productionFilePath)) {
String[] splitString = productionFilePath.split("\\\\");
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 5; i++) {
stringBuilder.append(splitString[i] + "\\");
}
return productionFilePath.substring(stringBuilder.toString().length()).replace("\\", "/");
} else {
int projectNameIndex = productionFilePath.lastIndexOf(app);
if (projectNameIndex == -1)
return "";
return productionFilePath.substring(projectNameIndex+app.length()+File.separator.length());
} else
return "";

}
}
}
8 changes: 7 additions & 1 deletion src/main/java/testsmell/TestSmellDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public TestSmellDetector() {
initializeSmells();
}

public TestSmellDetector(boolean initialize) {}

private void initializeSmells(){
testSmells = new ArrayList<>();
testSmells.add(new AssertionRoulette());
Expand All @@ -48,6 +50,10 @@ private void initializeSmells(){
testSmells.add(new DependentTest());
}

public void setTestSmells(List<AbstractSmell> testSmells) {
this.testSmells = testSmells;
}

/**
* Factory method that provides a new instance of the TestSmellDetector
*
Expand Down Expand Up @@ -83,7 +89,7 @@ public TestFile detectSmells(TestFile testFile) throws IOException {
productionFileCompilationUnit = JavaParser.parse(productionFileInputStream);
}

initializeSmells();
// initializeSmells();
for (AbstractSmell smell : testSmells) {
try {
smell.runAnalysis(testFileCompilationUnit, productionFileCompilationUnit,testFile.getTestFileNameWithoutExtension(),testFile.getProductionFileNameWithoutExtension());
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/testsmell/smell/AssertionRoulette.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class AssertionRoulette extends AbstractSmell {

private List<SmellyElement> smellyElementList;
private int assertionsCount = 0;

public AssertionRoulette() {
smellyElementList = new ArrayList<>();
Expand Down Expand Up @@ -50,6 +51,11 @@ public void runAnalysis(CompilationUnit testFileCompilationUnit, CompilationUnit
AssertionRoulette.ClassVisitor classVisitor;
classVisitor = new AssertionRoulette.ClassVisitor();
classVisitor.visit(testFileCompilationUnit, null);
assertionsCount = classVisitor.overallAssertions;
}

public int getAssertionsCount() {
return assertionsCount;
}

/**
Expand All @@ -65,6 +71,7 @@ private class ClassVisitor extends VoidVisitorAdapter<Void> {
private MethodDeclaration currentMethod = null;
private int assertNoMessageCount = 0;
private int assertCount = 0;
private int overallAssertions = 0;
TestMethod testMethod;

// examine all methods in the test class
Expand All @@ -89,6 +96,7 @@ else if (assertNoMessageCount >= 1) //if there is more than one assert statement

//reset values for next method
currentMethod = null;
overallAssertions += assertCount;
assertCount = 0;
assertNoMessageCount = 0;
}
Expand All @@ -104,6 +112,7 @@ public void visit(MethodCallExpr n, Void arg) {
n.getNameAsString().startsWith(("assertEquals")) ||
n.getNameAsString().startsWith(("assertNotSame")) ||
n.getNameAsString().startsWith(("assertSame")) ||
n.getNameAsString().startsWith("assertThrows") ||
n.getNameAsString().startsWith(("assertThat"))) {
assertCount++;
// assert methods that do not contain a message
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/testsmell/smell/EagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class EagerTest extends AbstractSmell {
private String productionClassName;
private List<SmellyElement> smellyElementList;
private List<MethodDeclaration> productionMethods;
private int eagerCount;

public EagerTest() {
productionMethods = new ArrayList<>();
Expand Down Expand Up @@ -62,7 +63,7 @@ public void runAnalysis(CompilationUnit testFileCompilationUnit, CompilationUnit

classVisitor = new EagerTest.ClassVisitor(TEST_FILE);
classVisitor.visit(testFileCompilationUnit, null);

eagerCount = classVisitor.overallEager;
}

/**
Expand All @@ -73,6 +74,9 @@ public List<SmellyElement> getSmellyElements() {
return smellyElementList;
}

public int getEagerCount() {
return eagerCount;
}

/**
* Visitor class
Expand All @@ -81,6 +85,7 @@ private class ClassVisitor extends VoidVisitorAdapter<Void> {
private MethodDeclaration currentMethod = null;
TestMethod testMethod;
private int eagerCount = 0;
private int overallEager = 0;
private List<String> productionVariables = new ArrayList<>();
private List<String> calledMethods = new ArrayList<>();
private String fileType;
Expand Down Expand Up @@ -123,6 +128,7 @@ public void visit(MethodDeclaration n, Void arg) {

//reset values for next method
currentMethod = null;
overallEager += eagerCount;
eagerCount = 0;
productionVariables = new ArrayList<>();
calledMethods = new ArrayList<>();
Expand Down
Loading

0 comments on commit 19adeb4

Please sign in to comment.