Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runs on any projects (makes csv input on the fly) #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions TestSmellExamples.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ Provided below are examples of test smells that were detected in open source And

- [Sensitive Equality](#sensitive-equality)

- [Unknown Test](#unknown-test) TODO: repleace with: net.cyclestreets / libraries/cyclestreets-core/src/test/java/net/cyclestreets/api/client/RetrofitApiClientIntegrationTest.java
- [Unknown Test](#unknown-test) TODO: repleace with: net.cyclestreets / libraries/cyclestreets-testsmell.core/src/test/java/net/cyclestreets/api/client/RetrofitApiClientIntegrationTest.java

- [Verbose Test (Long Test)](#verbose-test-long-test)

- [Sleepy Test](#sleepy-test)

- TODO: Default Test (com.app.missednotificationsreminder / app/src/test/java/com/app/missednotificationsreminder/ExampleUnitTest.java)

- TODO: Constructor Initialization (org.briarproject.briar.beta / bramble-core/src/test/java/org/briarproject/bramble/crypto/TagEncodingTest.java)
- TODO: Constructor Initialization (org.briarproject.briar.beta / bramble-testsmell.core/src/test/java/org/briarproject/bramble/crypto/TagEncodingTest.java)

- TODO: Ignored Test

Expand Down Expand Up @@ -369,9 +369,9 @@ The test method, `testTrue()`, will always pass as since the assert statement co

App: [com.liveplayergames.finneypoker](https://github.com/liveplayergames/UFP)

Test File: [RLPTest.java](https://github.com/liveplayergames/UFP/blob/master/android/ethereumj-core/src/test/java/org/ethereum/util/RLPTest.java)
Test File: [RLPTest.java](https://github.com/liveplayergames/UFP/blob/master/android/ethereumj-testsmell.core/src/test/java/org/ethereum/util/RLPTest.java)

Production File: [RLP.java](https://github.com/liveplayergames/UFP/blob/master/android/ethereumj-core/src/main/java/org/ethereum/util/RLP.java)
Production File: [RLP.java](https://github.com/liveplayergames/UFP/blob/master/android/ethereumj-testsmell.core/src/main/java/org/ethereum/util/RLP.java)

##### Code Snippet

Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@
<version>3.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>

</project>
</project>
111 changes: 6 additions & 105 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,112 +1,13 @@
import testsmell.AbstractSmell;
import testsmell.ResultsWriter;
import testsmell.TestFile;
import testsmell.TestSmellDetector;
import thresholds.DefaultThresholds;
import thresholds.Thresholds;
import testsmell.core.QualityMonitoringApplication;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* entry point when executing via command line
*/
public class Main {
public static void main(String[] args) throws IOException {
if (args == null) {
System.out.println("Please provide the file containing the paths to the collection of test files");
return;
}
if (!args[0].isEmpty()) {
File inputFile = new File(args[0]);
if (!inputFile.exists() || inputFile.isDirectory()) {
System.out.println("Please provide a valid file containing the paths to the collection of test files");
return;
}
}

TestSmellDetector testSmellDetector = new TestSmellDetector(new DefaultThresholds());

/*
Read the input file and build the TestFile objects
*/
BufferedReader in = new BufferedReader(new FileReader(args[0]));
String str;

String[] lineItem;
TestFile testFile;
List<TestFile> testFiles = new ArrayList<>();
while ((str = in.readLine()) != null) {
// use comma as separator
lineItem = str.split(",");

//check if the test file has an associated production file
if (lineItem.length == 2) {
testFile = new TestFile(lineItem[0], lineItem[1], "");
} else {
testFile = new TestFile(lineItem[0], lineItem[1], lineItem[2]);
}

testFiles.add(testFile);
}

/*
Initialize the output file - Create the output file and add the column names
*/
ResultsWriter resultsWriter = ResultsWriter.createResultsWriter();
List<String> columnNames;
List<String> columnValues;

columnNames = testSmellDetector.getTestSmellNames();
columnNames.add(0, "App");
columnNames.add(1, "TestClass");
columnNames.add(2, "TestFilePath");
columnNames.add(3, "ProductionFilePath");
columnNames.add(4, "RelativeTestFilePath");
columnNames.add(5, "RelativeProductionFilePath");
columnNames.add(6, "NumberOfMethods");

resultsWriter.writeColumnName(columnNames);

/*
Iterate through all test files to detect smells and then write the output
*/
TestFile tempFile;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date;
for (TestFile file : testFiles) {
date = new Date();
System.out.println(dateFormat.format(date) + " Processing: " + file.getTestFilePath());
System.out.println("Processing: " + file.getTestFilePath());

//detect smells
tempFile = testSmellDetector.detectSmells(file);

//write output
columnValues = new ArrayList<>();
columnValues.add(file.getApp());
columnValues.add(file.getTestFileName());
columnValues.add(file.getTestFilePath());
columnValues.add(file.getProductionFilePath());
columnValues.add(file.getRelativeTestFilePath());
columnValues.add(file.getRelativeProductionFilePath());
columnValues.add(String.valueOf(file.getNumberOfTestMethods()));
for (AbstractSmell smell : tempFile.getTestSmells()) {
try {
columnValues.add(String.valueOf(smell.getNumberOfSmellyTests()));
} catch (NullPointerException e) {
columnValues.add("");
}
}
resultsWriter.writeLine(columnValues);
}

System.out.println("end");
public static void main(String[] args) throws IOException {
QualityMonitoringApplication.main(args);
}


}
4 changes: 2 additions & 2 deletions src/main/java/testsmell/AbstractSmell.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.Set;

public abstract class AbstractSmell {
protected Thresholds thresholds;
protected Set<SmellyElement> smellyElementsSet;
protected final Thresholds thresholds;
protected final Set<SmellyElement> smellyElementsSet;

public AbstractSmell(Thresholds thresholds) {
this.thresholds = thresholds;
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/testsmell/DetectionThresholds.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
*/
public class DetectionThresholds {

public static int EAGER_TEST = 4;
public static int ASSERTION_ROULETTE = 3;
public static int VERBOSE_TEST = 13;
public static int CONDITIONAL_TEST_LOGIC = 0;
public static int MAGIC_NUMBER_TEST = 0;
public static int GENERAL_FIXTURE = 0;
public static int MYSTERY_GUEST = 0;
public static int RESOURCE_OPTIMISM = 0;
public static int SLEEPY_TEST = 0;
public static final int EAGER_TEST = 4;
public static final int ASSERTION_ROULETTE = 3;
public static final int VERBOSE_TEST = 13;
public static final int CONDITIONAL_TEST_LOGIC = 0;
public static final int MAGIC_NUMBER_TEST = 0;
public static final int GENERAL_FIXTURE = 0;
public static final int MYSTERY_GUEST = 0;
public static final int RESOURCE_OPTIMISM = 0;
public static final int SLEEPY_TEST = 0;
}
40 changes: 28 additions & 12 deletions src/main/java/testsmell/ResultsWriter.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
package testsmell;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
* This class is utilized to write output to a CSV file
*/
public class ResultsWriter {
private static final DateFormat dateFormatForOutput = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");

private final File outputFile;
private final StringBuilder output = new StringBuilder();

private String outputFile;
private FileWriter writer;

/**
* Creates the file into which output it to be written into. Results from each file will be stored in a new file
* @throws IOException
*/
private ResultsWriter() throws IOException {
String time = String.valueOf(Calendar.getInstance().getTimeInMillis());
outputFile = MessageFormat.format("{0}_{1}_{2}.{3}", "Output","TestSmellDetection",time, "csv");
writer = new FileWriter(outputFile,false);
private ResultsWriter() {
String time = dateFormatForOutput.format(Calendar.getInstance().getTime());
outputFile = new File(".", MessageFormat.format("{0}_{1}_{2}.{3}", "Output","TestSmellDetection",time, "csv"));
}

public void save(){

try (FileWriter writer = new FileWriter(outputFile,false)){
writer.write(output.toString());
writer.flush();
System.out.println("saved: " + outputFile.getCanonicalPath());
} catch (Exception ex) {
ex.printStackTrace();
}
}

/**
Expand Down Expand Up @@ -57,18 +72,19 @@ public void writeLine(List<String> columnValues) throws IOException {
* @throws IOException
*/
private void writeOutput(List<String> dataValues)throws IOException {
writer = new FileWriter(outputFile,true);

for (int i=0; i<dataValues.size(); i++) {
writer.append(String.valueOf(dataValues.get(i)));
output.append(String.valueOf(dataValues.get(i)));

if(i!=dataValues.size()-1)
writer.append(",");
output.append(",");
else
writer.append(System.lineSeparator());
output.append(System.lineSeparator());

}
writer.flush();
writer.close();
}

public String getOutput() {
return output.toString();
}
}
4 changes: 2 additions & 2 deletions src/main/java/testsmell/TestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

public class TestClass extends SmellyElement {

private String className;
private final String className;
private boolean hasSmell;
private Map<String, String> data;
private final Map<String, String> data;

public TestClass(String className) {
this.className = className;
Expand Down
39 changes: 31 additions & 8 deletions src/main/java/testsmell/TestFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,30 @@
import java.util.List;

public class TestFile {
private String app, testFilePath, productionFilePath;
private List<AbstractSmell> testSmells;
private final String app, testFilePath, productionFilePath;
private final List<AbstractSmell> testSmells;
private int numberOfTestMethods = 0;

public TestFile(String app, String testFilePath) {
this(app, testFilePath, "");
}
public TestFile(String app, String testFilePath, String productionFilePath) {
if (new File(app).exists()) {
File appFile = createFromExistingFile(new File(app));
this.app = appFile.getName();
this.testFilePath = createFromExistingFile(new File(appFile, testFilePath)).getPath();
if (productionFilePath.isEmpty()) {
this.productionFilePath = "";
} else {
this.productionFilePath = createFromExistingFile(new File(appFile , productionFilePath)).getPath();
}
} else {
this.app = app;
this.testFilePath = testFilePath;
this.productionFilePath = productionFilePath;
}
this.testSmells = new ArrayList<>();
}
public String getApp() {
return app;
}
Expand All @@ -31,11 +51,14 @@ public boolean getHasProductionFile() {
return ((productionFilePath != null && !productionFilePath.isEmpty()));
}

public TestFile(String app, String testFilePath, String productionFilePath) {
this.app = app;
this.testFilePath = testFilePath;
this.productionFilePath = productionFilePath;
this.testSmells = new ArrayList<>();

private static File createFromExistingFile(File file) {
try {
return file.getCanonicalFile();
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("should not occur");
}
}

public void addSmell(AbstractSmell smell) {
Expand Down Expand Up @@ -121,4 +144,4 @@ public int getNumberOfTestMethods() {
public void setNumberOfTestMethods(int numberOfTestMethods) {
this.numberOfTestMethods = numberOfTestMethods;
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/testsmell/TestMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

public class TestMethod extends SmellyElement {

private String methodName;
private final String methodName;
private boolean hasSmell;
private Map<String, String> data;
private final Map<String, String> data;

public TestMethod(String methodName) {
this.methodName = methodName;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/testsmell/TestSmellDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class TestSmellDetector {

private List<AbstractSmell> testSmells;
private Thresholds thresholds;
private final Thresholds thresholds;

/**
* Instantiates the various test smell analyzer classes and loads the objects into an list.
Expand All @@ -27,11 +27,11 @@ public class TestSmellDetector {
*/
public TestSmellDetector(Thresholds thresholds) {
this.thresholds = thresholds;
initializeSmells();
testSmells = createTestSmells();
}

private void initializeSmells() {
testSmells = new ArrayList<>();
private List<AbstractSmell> createTestSmells() {
List<AbstractSmell> testSmells = new ArrayList<>();
testSmells.add(new AssertionRoulette(thresholds));
testSmells.add(new ConditionalTestLogic(thresholds));
testSmells.add(new ConstructorInitialization(thresholds));
Expand All @@ -53,6 +53,7 @@ private void initializeSmells() {
testSmells.add(new ResourceOptimism(thresholds));
testSmells.add(new MagicNumberTest(thresholds));
testSmells.add(new DependentTest(thresholds));
return testSmells;
}

public void setTestSmells(List<AbstractSmell> testSmells) {
Expand Down
Loading