Skip to content

Commit

Permalink
Merge branch 'develop_pmop' of github.com:easy-software-ufal/muJava-A…
Browse files Browse the repository at this point in the history
…UM into remove_idea
  • Loading branch information
pmop committed May 9, 2019
2 parents 6115a08 + bf81621 commit 4e5d1f9
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 182 deletions.
207 changes: 120 additions & 87 deletions src/main/java/mujava/cli/CLIExecution.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package mujava.cli;

import com.sun.org.apache.xerces.internal.xs.StringList;
import com.sun.xml.internal.fastinfoset.util.StringArray;
import mujava.AllMutantsGenerator;
import mujava.MutationSystem;
import mujava.OpenJavaException;
import mujava.op.util.CodeChangeLog;
import mujava.op.util.ExpressionAnalyzer;
import mujava.util.Debug;
import org.kohsuke.args4j.Argument;
Expand All @@ -13,10 +14,12 @@
import org.kohsuke.args4j.spi.BooleanOptionHandler;
import org.kohsuke.args4j.spi.StringArrayOptionHandler;

import java.io.IOException;
import java.io.File;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* Command line execution definitions
Expand All @@ -27,71 +30,43 @@ public class CLIExecution {
/**
* All default class mutation operators
*/
private static String[] DefaultClassMutantsOperators = new String[]{"IHD", "IHI", "IOD", "OMR", "OMD", "JDC", "AMC",
static String[] DefaultClassMutantsOperators = new String[]{"IHD", "IHI", "IOD", "OMR", "OMD", "JDC", "AMC",
"ISD", "IOP", "IPC", "PNC", "PMD", "PPD", "PRV", "PCI", "PCC", "PCD", "JSD", "JSI", "JTD",
"JTI", "JID", "OAN", "EOA", "EOC", "EAM", "EMM"};

/**
* All default traditional mutation operators
*/
private static String[] DefaultTraditionalMutantsOperators = new String[]{"AORB", "AORS", "AODU"
static String[] DefaultTraditionalMutantsOperators = new String[]{"AORB", "AORS", "AODU"
, "AODS", "AOIU", "AOIS", "ROR", "COR", "COD", "COI", "SOR", "LOR", "LOI", "LOD", "ASRS", "SDL",
"VDL", "CDL", "ODL"};

private boolean isValidTraditionalMutantOperator(String operator) {
boolean contains = false;
for (String str : DefaultTraditionalMutantsOperators) {
if (str.equals(operator)) {
contains = true;
break;
}
}
return contains;
}

private boolean isValidClassMutantOperator(String operator) {
boolean contains = false;
for (String str : DefaultClassMutantsOperators) {
if (str.equals(operator)) {
contains = true;
break;
}
}
return contains;
}

/**
* Selected traditional mutation operators. Defaults to all operators.
*/
@Option(name = "-co", handler = StringArrayOptionHandler.class, usage = "Set class mutants operators.")
private String[] classMutantsOperators = DefaultClassMutantsOperators;
String[] classMutantsOperators = DefaultClassMutantsOperators;

/**
* Selected traditional mutation operators. Defaults to all operators.
*/
@Option(name = "-to", handler = StringArrayOptionHandler.class, usage = "Set traditional mutants operators.")
private String[] traditionalMutantsOperators = DefaultTraditionalMutantsOperators;

/**
* Input sessions
*/
@Option(name = "-i", handler = StringArrayOptionHandler.class, usage = "Comma separated list of input sessions.")
private List<String> input = new ArrayList<>();
String[] traditionalMutantsOperators = DefaultTraditionalMutantsOperators;

/**
* Whether nocompile or not. Defaults to true.
*/
@Option(name = "-noc", handler = BooleanOptionHandler.class, usage = "Do not compile.")
private boolean nocompile;
boolean nocompile;

@Argument
private List<String> arguments = new ArrayList<>();
@Argument(handler = StringArrayOptionHandler.class)
String[] arguments = null;


private CLIExecution() {
CLIExecution() {
}

private void doMutation(java.io.File source) throws OpenJavaException {
void doMutation(java.io.File source) throws OpenJavaException {
AllMutantsGenerator amg = new AllMutantsGenerator(source, classMutantsOperators,
traditionalMutantsOperators);
amg.makeMutants();
Expand All @@ -101,56 +76,104 @@ private void doMutation(java.io.File source) throws OpenJavaException {
/**
* Evaluates whether input string corresponds to a Java file or a folder and processes accordingly.
*
* @param input String representing a path to a Java file or a folder containing source code structured by
* supported format
* @param input File representing a path to a Java file or a folder containing source code
* @throws Exception When the string provided is not a path to a Java file or folder, or file/folder is not readable.
*/
private void processInput(String input) throws Exception {
char[] sanitizedPath = new char[input.length()];
for (int i = 0; i < input.length(); ++i) {
if (input.charAt(i) == '\\') sanitizedPath[i] = '/';
else sanitizedPath[i] = input.charAt(i);
void processInput(File input) throws Exception {
if (input.isDirectory()) {
for (File a : Objects.requireNonNull(input.listFiles())) {
processInput(a);
}
}
input = new String(sanitizedPath);
if (input.contains(".java")) {
String before_src = input.substring(0, input.indexOf("/src/"));
String classname = Paths.get(input).getFileName().toString();
classname = classname.replace(".java", "");
configureForProjectFolder(before_src);
configureForFile(before_src, classname);
System.out.println("Generating mutants for " + classname + ".");
System.out.println("Session is" + before_src + ".");
java.io.File source = new java.io.File(input);
doMutation(source);
} else if (Files.isDirectory(Paths.get(input))) {
processInputAsDirectory(input);
} else {
System.out.println("Invalid input. Try -h for help.");
else if (input.getName().endsWith(".java")) {
String temp = input.getAbsolutePath().replace('\\', '/');
temp = temp.substring(0, temp.indexOf("/src/"));
Path source_path = Paths.get(temp);
configureForProjectFolder(source_path.toString().replace('\\', '/'));
MutationSystem.recordInheritanceRelation();
setMutationSystemPathFor(input.getAbsolutePath().replace('\\', '/'));
doMutation(input);
}
}

private void processInputAsDirectory(String session_path) throws IOException {
String source_folder = session_path + "/src/";
try (DirectoryStream<Path> stream = java.nio.file.Files.newDirectoryStream(Paths.get(source_folder), "*.java")) {
for (Path entry : stream)
processInput(entry.toString());
static void configureForProjectFolder(String path) throws Exception {
Debug.setDebugLevel(Debug.DETAILED_LEVEL);
ExpressionAnalyzer.DbgLevel = ExpressionAnalyzer.DebugLevel.NONE;
MutationSystem.setJMutationStructure(path);
}

//Copied from MutantsGenPanel.java
//Modified by Pedro Pinheiro
void setMutationSystemPathFor(String file_name) {
try {
String temp;
temp = file_name.substring(0, file_name.length() - ".java".length());
temp = temp.replace('/', '.');
temp = temp.replace('\\', '.');
int separator_index = temp.lastIndexOf(".");

if (separator_index >= 0) {
MutationSystem.CLASS_NAME = temp.substring(separator_index + 1, temp.length());
} else {
MutationSystem.CLASS_NAME = temp;
}

//Modified. Changed temp to CLASS_NAME. temp behaves strangely on Windows. Ill figure out the cause later.
String mutant_dir_path = MutationSystem.MUTANT_HOME + "/" + MutationSystem.CLASS_NAME;
File mutant_path = new File(mutant_dir_path);
mutant_path.mkdir();

} catch (DirectoryIteratorException ex) {
throw ex.getCause();
String class_mutant_dir_path = mutant_dir_path + "/" + MutationSystem.CM_DIR_NAME;
File class_mutant_path = new File(class_mutant_dir_path);
class_mutant_path.mkdir();

String traditional_mutant_dir_path = mutant_dir_path + "/" + MutationSystem.TM_DIR_NAME;
File traditional_mutant_path = new File(traditional_mutant_dir_path);
traditional_mutant_path.mkdir();

String original_dir_path = mutant_dir_path + "/" + MutationSystem.ORIGINAL_DIR_NAME;
File original_path = new File(original_dir_path);
original_path.mkdir();

MutationSystem.CLASS_MUTANT_PATH = class_mutant_dir_path;
MutationSystem.TRADITIONAL_MUTANT_PATH = traditional_mutant_dir_path;
MutationSystem.ORIGINAL_PATH = original_dir_path;
//Modified. Changed temp to file_name. temp behaves strangely on Windows. Ill figure out the cause later.
MutationSystem.DIR_NAME = file_name;
} catch (Exception e) {
e.printStackTrace();
System.err.println(e);
}
}

private static void configureForProjectFolder(String path) throws Exception {
Debug.setDebugLevel(Debug.DETAILED_LEVEL);
ExpressionAnalyzer.DbgLevel = ExpressionAnalyzer.DebugLevel.NONE;
MutationSystem.setJMutationStructure(path);
//Copied from MutantsGenPanel.java
void deleteDirectory() {
File originalDir = new File(
MutationSystem.MUTANT_HOME + "/" + MutationSystem.DIR_NAME + "/" + MutationSystem.ORIGINAL_DIR_NAME);
while (originalDir.delete()) { // do nothing?
}

File cmDir = new File(
MutationSystem.MUTANT_HOME + "/" + MutationSystem.DIR_NAME + "/" + MutationSystem.CM_DIR_NAME);
while (cmDir.delete()) { // do nothing?
}

File tmDir = new File(
MutationSystem.MUTANT_HOME + "/" + MutationSystem.DIR_NAME + "/" + MutationSystem.TM_DIR_NAME);
while (tmDir.delete()) { // do nothing?
}

CodeChangeLog.openLogFile();
File myHomeDir = new File(MutationSystem.MUTANT_HOME + "/" + MutationSystem.DIR_NAME);
while (myHomeDir.delete()) { // do nothing?
}
}

private void configureForFile(String path, String classname) throws Exception {
/**
* Configures the mutation engine for file containing source code intended to be mutated.
* @param path Path to the project/session containing the source
* @param classname String representing the name of the Class (contained in source) that will be mutated
* @throws Exception
*/
void configureForFile(String path, String classname) throws Exception {
MutationSystem.ORIGINAL_PATH = path + "/result/" + classname + "/original";
MutationSystem.CLASS_NAME = classname;
MutationSystem.TRADITIONAL_MUTANT_PATH = path + "/result/" + classname + "/traditional_mutants";
Expand All @@ -159,22 +182,32 @@ private void configureForFile(String path, String classname) throws Exception {
MutationSystem.recordInheritanceRelation();
}

private void doMain(String[] args) throws Exception {
public static String toString (String[] string_array) {
StringBuilder acm = new StringBuilder();
if (string_array != null) {
for (String s : string_array) {
acm.append(s);
}
}
return acm.toString();
}

void doMain(String[] args) throws Exception {
CmdLineParser cmdLineParser = new CmdLineParser(this);
try {
cmdLineParser.parseArgument(args);
if (!arguments.isEmpty()) {
System.out.println("Traditional mutant operators selected: " + traditionalMutantsOperators.toString() + ".");
System.out.println("Class mutant operators selected: " + classMutantsOperators.toString() + ".");
for (String i : input)
processInput(i);
} else {
System.out.println("No arguments given.");
System.err.println("java mujava [options...] arguments...");
cmdLineParser.printUsage(System.err);
}
} catch (CmdLineException e) {
if (arguments != null) {
System.out.println("Traditional mutant operators selected: " + toString(traditionalMutantsOperators) + ".");
System.out.println("Class mutant operators selected: " + toString(classMutantsOperators) + ".");
for (String i : arguments)
processInput(Paths.get(i).toFile());
} else throw new CmdLineException("Bad arguments provided.");
} catch (Exception e) {
System.err.println(e.getMessage());
System.err.println("java mujava [options...] arguments...");
cmdLineParser.printUsage(System.err);
System.err.println();
deleteDirectory();
}
}

Expand Down
Loading

0 comments on commit 4e5d1f9

Please sign in to comment.