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

[WIP] On-going work by Henry #848

Closed
wants to merge 18 commits into from
Closed
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
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ cache:
before_install:
- sudo apt-get update
- sudo apt-get install python3

# Pull Mongodb image to test
- docker pull mongo:latest
- docker run -d --net=host mongo:latest
install:
- export DSPOT_VERSION=`grep '<version>' dspot/pom.xml | head -n 1 | sed -e 's/<version>//' -e 's/<\\/version>//' -e 's/ *//g'`
- export MAVEN_HOME=`mvn --version | grep 'Maven home' | sed -e 's/Maven h.* //'`
Expand Down
6 changes: 5 additions & 1 deletion dspot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@
<artifactId>junit-vintage-engine</artifactId>
<version>5.3.2</version>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.2.2</version>
</dependency>


</dependencies>
Expand Down
5 changes: 5 additions & 0 deletions dspot/src/main/java/eu/stamp_project/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import eu.stamp_project.utils.report.error.ErrorReportImpl;
import eu.stamp_project.utils.report.output.OutputReportImpl;
import eu.stamp_project.utils.report.output.selector.TestSelectorReportImpl;
import eu.stamp_project.mongodb.MongodbManager;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -59,6 +60,10 @@ public static void run() {
// global report handling
Main.GLOBAL_REPORT.output();
Main.GLOBAL_REPORT.reset();
// Send info to Mongodb
if (MongodbManager.getInstance().getDbConnectable()) {
MongodbManager.getInstance().sendInfoToDb();
}
}

public static void createOutputDirectories() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eu.stamp_project.dspot.selector;

import org.bson.Document;
import eu.stamp_project.mongodb.MongodbManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import eu.stamp_project.utils.report.output.selector.TestSelectorElementReport;
Expand Down Expand Up @@ -47,6 +49,8 @@ public class JacocoCoverageSelector extends TakeAllSelector {

private TestSelectorElementReport lastReport;

private Document infoDoc;

@Override
public boolean init() {
super.init();
Expand Down Expand Up @@ -185,7 +189,11 @@ public TestSelectorElementReport report() {
.append(this.selectedAmplifiedTest.size())
.append(" amplified tests.")
.append(AmplificationHelper.LINE_SEPARATOR);

/*Sending info to Mongodb, format original/newkills/percentage*/
if (MongodbManager.getInstance().getDbConnectable()) {
infoDoc = new Document();
infoDoc.append("initialCoverage","" + this.initialCoverage.getInstructionsCovered());
}
// compute the new coverage obtained by the amplification
final CtType<?> clone = this.currentClassTestToBeAmplified.clone();
this.currentClassTestToBeAmplified.getPackage().addType(clone);
Expand Down Expand Up @@ -222,6 +230,15 @@ public TestSelectorElementReport report() {
.append("%")
.append(AmplificationHelper.LINE_SEPARATOR);
lastReport = new TestSelectorElementReportImpl(report.toString(), jsonReport(coverageResults));
/*Sending data to Mongodb*/
if (MongodbManager.getInstance().getDbConnectable()) {
String s = this.currentClassTestToBeAmplified.getQualifiedName();
infoDoc.append("ampCoverage","" + coverageResults.getInstructionsCovered());
infoDoc.append("totalCoverage","" + coverageResults.getInstructionsTotal());

MongodbManager.getInstance().jacocoSelectorDocs.add(new Document(s.replace(".","/D/"),infoDoc));
}

return lastReport;
} catch (TimeoutException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eu.stamp_project.dspot.selector;

import org.bson.Document;
import eu.stamp_project.mongodb.MongodbManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import eu.stamp_project.Main;
Expand Down Expand Up @@ -53,6 +55,8 @@ public enum OutputFormat {XML, CSV}

private TestSelectorElementReport lastReport;

private Document infoDoc;

public PitMutantScoreSelector() {
this(OutputFormat.XML);
}
Expand Down Expand Up @@ -235,6 +239,14 @@ public TestSelectorElementReport report() {
}

private String reportStdout() {
if (MongodbManager.getInstance().getDbConnectable()) {
String s = this.currentClassTestToBeAmplified.getQualifiedName();
infoDoc = new Document();
infoDoc.append("originalKilledMutants","" + this.originalKilledMutants.size());
infoDoc.append("NewMutantKilled","" + getNbTotalNewMutantKilled());
MongodbManager.getInstance().pitMutantScoreSelectorDocs.add(new Document(s.replace(".","/D/"),infoDoc));
}

return "Test class that has been amplified: " + this.currentClassTestToBeAmplified.getQualifiedName() +
AmplificationHelper.LINE_SEPARATOR +
"The original test suite kills " +
Expand Down
192 changes: 192 additions & 0 deletions dspot/src/main/java/eu/stamp_project/mongodb/MongodbManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package eu.stamp_project.mongodb;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mongodb.*;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Projections.*;
import static com.mongodb.client.model.Updates.*;
import org.bson.Document;


/*Parsing date*/
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.List;

import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.io.IOException;

public class MongodbManager {
private static final Logger LOGGER = LoggerFactory.getLogger(MongodbManager.class);
/*Format of date for saving in Mongodb*/
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
private static MongodbManager single_instance = null;

private static MongoClient mongoClient;
private static String mongoUrl;
private static String dbName;
private static String repoSlug;
private static String repoBranch;
private static String colName;
private static boolean dbConnectable;
private static boolean restful;

/*Jacoco Selector*/
public List<Document> jacocoSelectorDocs;

/*PitMutantScoreSelector*/
public List<Document> pitMutantScoreSelectorDocs;/*

/*Argumets from input*/
public Document argsDoc;

/*Output paths for java files*/
public List<String> javaPathList;

private MongodbManager () {
this.jacocoSelectorDocs = new ArrayList<Document>();
this.pitMutantScoreSelectorDocs = new ArrayList<Document>();
this.argsDoc = new Document();
this.javaPathList = new ArrayList<String>();
this.dbConnectable = testConnectionToDb();
}

public static MongodbManager getInstance() {
if (single_instance == null) {
single_instance = new MongodbManager();
}
return single_instance;
}

public static void initMongodbManager (String mongoUrl_ln, String dbName_ln, String colName_ln, String repoSlug_ln, String repoBranch_ln, boolean restful_ln) {
mongoUrl = mongoUrl_ln;
dbName = dbName_ln;
colName = colName_ln;
repoSlug = repoSlug_ln;
repoBranch = repoBranch_ln;
restful = restful_ln;
dbConnectable = testConnectionToDb();
}

public static MongoClient connectToMongo (String mongoUrl) {
return new MongoClient(new MongoClientURI(mongoUrl));
}

public static MongoDatabase getDatabase (String dbName , MongoClient mongoClient) {
return mongoClient.getDatabase(dbName);
}

public static MongoCollection<Document> getCollection (String colName,MongoDatabase database) {
return database.getCollection(colName);
}

private static boolean testConnectionToDb() {
try {
mongoClient = new MongoClient(new MongoClientURI(mongoUrl));
mongoClient.close();
LOGGER.warn("Mongodb connection Successful");
return true;
}catch (Exception e) {
LOGGER.warn("failed to connect to mongodb, No information will be submitted at the end");
return false;
}
}

public boolean getDbConnectable() {
return dbConnectable;
}

public void sendInfoToDb() {
try {
mongoClient = connectToMongo(this.mongoUrl);
MongoDatabase database = getDatabase(this.dbName,mongoClient);
MongoCollection<Document> coll = getCollection(this.colName,database);


Document mainDoc;
if (!restful) {
mainDoc = new Document("RepoSlug", this.repoSlug)
.append("RepoBranch",this.repoBranch)
.append("Date",this.getCurrentDate());
mainDoc.append("AmpOptions",argsDoc);
}else {
mainDoc = coll.find(and(eq("RepoSlug",this.repoSlug),eq("RepoBranch",this.repoBranch),eq("State","pending"))).projection(fields(excludeId())).first();
if (mainDoc != null) {
mainDoc.append("State","recent");
}
}
System.out.println(mainDoc.toString());
if (mainDoc != null) {
if (argsDoc.get("test-criterion").equals("JacocoCoverageSelector")) {
Document mergedDoc = new Document();
for (Document doc : jacocoSelectorDocs) {
mergedDoc.putAll(doc);
}
mainDoc.append("AmpResult",mergedDoc);
}else if (argsDoc.get("test-criterion").equals("PitMutantScoreSelector")) {
Document mergedDoc = new Document();
for (Document doc : pitMutantScoreSelectorDocs) {
mergedDoc.putAll(doc);
}
mainDoc.append("AmpResult",mergedDoc);
}


Document ampFiles = new Document();
for (String path : javaPathList) {
String fileName = this.getFileNameGivenPath(path).replace(".java","");
String content = this.readFile(path,StandardCharsets.US_ASCII).replace(".","/D/");
LOGGER.warn("fileName: " + fileName);
LOGGER.warn("content: " + content);
ampFiles.append(fileName,content);
}
mainDoc.append("AmpTestFiles",ampFiles);
if (!restful) {
coll.insertOne(mainDoc);
} else {
// Set the previous recent repo as old
coll.updateOne(and(eq("RepoSlug",this.repoSlug),eq("RepoBranch",this.repoBranch),eq("State","recent")),new Document("$set",new Document("State","old")));
coll.updateOne(and(eq("RepoSlug",this.repoSlug),eq("RepoBranch",this.repoBranch),eq("State","pending")),new Document("$set",mainDoc));
}
}else {
LOGGER.warn("Main document for submitting is not initialized, No document found in restful mode.");
}

mongoClient.close();
}catch (Exception e) {
System.out.println("failed to connect to mongodb");
}
}

private static String readFile(String path, Charset encoding) {
try {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private String getCurrentDate() {
Date date = new Date();
return this.dateFormat.format(date);
}

private String getFileNameGivenPath(String path) {
String[] bits = path.split("/");
return bits[bits.length - 1];
}
}
8 changes: 8 additions & 0 deletions dspot/src/main/java/eu/stamp_project/utils/DSpotUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.stamp_project.utils;

import eu.stamp_project.Main;
import eu.stamp_project.mongodb.MongodbManager;
import eu.stamp_project.utils.compilation.DSpotCompiler;
import eu.stamp_project.utils.program.InputConfiguration;
import eu.stamp_project.utils.report.error.Error;
Expand Down Expand Up @@ -107,6 +108,13 @@ public static void printAndCompileToCheck(CtType<?> type, File directory) {
final String pathname =
directory.getAbsolutePath() + File.separator +
type.getQualifiedName().replaceAll("\\.", regex) + ".java";

// Save path to mongodb.
if (MongodbManager.getInstance().getDbConnectable()) {
MongodbManager.getInstance().javaPathList.add(pathname);
LOGGER.warn("Henry- Pathname : " + pathname);
}

final CtType<?> existingAmplifiedTestClass;
if (new File(pathname).exists()) {
existingAmplifiedTestClass = getExistingClass(type, pathname);//FIXME: analyse for optimisation (16% total execution time)
Expand Down
Loading