Commit 9c61aea9 authored by Alexander's avatar Alexander
Browse files

Added: testing tool draft

parents
package ca.mcgill.cs.test;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
/**
*
* @author AlexanderButyaev
*
* This class is written to process the files from Minerva output (disarchive first). Example of expected files: "182242-332341 - Abasdasfasf Lkasdjks - Feb 5, 2015 1200 AM - COMP251HW1.java"
* The idea is
* 1) to filter only java files with proper names in given folder.
* 2) to find collaborations and give mark them as well (does not affect anything)
* 3) to delete if any packages and put proper one
* 4) to copy files into src/heap/<folder> locations
*
*/
public class FileProcessor {
public static void main(String[] args) throws IOException {
String folderPath = "HERE IS A FOLDER FULL PATH";
String regexpMinervaFileName =
"^[\\d]+-[\\d]+\\s*-\\s*(.+)\\s+-\\s*[A-Z][a-z]{2}\\s*[\\d]+,\\s*[\\d]+\\s*[\\d]+\\s*[AMP]{2}\\s*-*.*\\.java$";
// PROBLEM/FILE NAME (you shouldn't consider any file that has wrong naming)
String problemName = "COMP251HW1";
String fileExtension = "java";
if ( args.length == 1 && args[0].toLowerCase().equals("help") ) {
System.out.println(
"Program to process files from mierva output (disarchive first)\n"
+ "Example of expected file: \"18246-33493 - Benjamin Lourtau - Feb 5, 2015 1200 AM - COMP251HW1.java\" "
+ "Arguments:\n"
+ "1) folder path (where student files are stored)\n"
+ "2) file/problem name (in case of template - there should be a template file name that student file should have). Optional: default value = "+problemName+"\n."
+ "3) regexp for minerva output file naming. Optional (is not recommended to change): default value = "+ regexpMinervaFileName + "\n."
+ "4) file extension. Optional: java. If you change 3rd argument - consider change this too if source code is not java."
+ "\nThis program will create in src/heap folders for each student and put there <problemName>.java with proper package name."
);
System.exit(0);
} else if (args.length > 0) {
for(int i=0, len = args.length; i < len; i++) {
switch(i) {
case 0: // Folder Path
folderPath = args[i];
break;
case 1: // problem name
problemName = args[i];
break;
case 2: // regexp for minerva file output
regexpMinervaFileName = args[i];
break;
case 3: // file extension
fileExtension = args[i];
break;
}
}
} else {
folderPath = "/Users/AlexanderButyaev/Documents/workspace/COMP251/zip/Assignment 1 Download Feb 12, 2015 417 PM";
}
// relations between student and his/her collaboration group
HashMap<String, ArrayList<String>> hmap = new HashMap<String, ArrayList<String>>();
HashMap<String, String> sourceMap = new HashMap<String, String>();
final String problemNameFinal = problemName; // dirty hack :)
final String fileExtensionFinal = "." + fileExtension;
File folder = new File(folderPath);
//filter java files with COMP251HW1 name in the end
File[] filesArray = folder.listFiles(new FileFilter() {
public boolean accept(File pathname) {
if ( pathname.getName().toLowerCase().endsWith(problemNameFinal.toLowerCase() + fileExtensionFinal.toLowerCase()) ) {
return true;
}
return false;
}
});
Scanner sc;
//Iterate over student java files and process them
for (File file: filesArray) {
System.out.println("Processing file: " + file.getName());
//Find person's name (it encrypted inside the name after minerva system)
String folderName = file.getName()
.replaceAll(regexpMinervaFileName, "$1");
sc = new Scanner(file);
String tempLine;
ArrayList<String> studentGroup = new ArrayList<String>();
boolean isStarted = false;
while (sc.hasNextLine()) {
tempLine = sc.nextLine();
// Looking for information about collaborations.
// studentGroup is a list of people who will share the same mark afterwards.
// isStarted - about list of the students. if isStarted = true - there is at least 1 student in the group to get mark, otherwise name will come from the name of the file only
if (tempLine.contains("class")/* || tempLine.contains("package") */) {
if (isStarted) { //already finished - we can use name into the file
for (String studentString : studentGroup) {
hmap.put(studentString, studentGroup);
sourceMap.put(studentString, "src/heap/"+folderName.replaceAll("\\s", "_").replaceAll("-", "__"));
}
}
else { //haven't started yet - no name in files - alone.
studentGroup.add(folderName);
hmap.put(folderName,studentGroup);
sourceMap.put(folderName, "src/heap/"+folderName.replaceAll("\\s", "_").replaceAll("-", "__"));
}
break;
} else if (tempLine.toLowerCase().matches("([/]{2}|[\\*])\\s*student[\\d_]*name.+") ) {
studentGroup.add(tempLine.replaceAll("(?i)([/]{2}|[\\*])\\s*student[\\d_]*name[^a-z]*(([a-z]+\\s*){1,5})", "$2"));
isStarted = true;
} else if (tempLine.toLowerCase().matches("\\s*([/]{2}|[\\*])\\s*([a-zA-Z]+\\s*){1,4}\\s*")
&& !tempLine.toLowerCase().contains("university")
&& !tempLine.toLowerCase().contains("blanchette")
&& !tempLine.toLowerCase().contains("import")) {
studentGroup.add(tempLine.replaceAll("(?i)\\s*([/]{2}|[\\*])\\s*(([a-z]+\\s*){1,5})", "$2"));
isStarted = true;
}
}
}
// Copy line by line (because you can change something inside).
// Inside we delete package but add own package heap.<StudentName>. You can add additional changes for better marking.
// Create heap folder
new File("src/heap").mkdir();
File newFile;
String temp;
for (File file : filesArray) {
String folderName = file.getName()
.replaceAll(regexpMinervaFileName, "$1");
newFile = new File("src/heap/" + folderName.replaceAll("\\s", "_").replaceAll("-", "__"));
if (!newFile.exists()) newFile.mkdir();
newFile = new File("src/heap/" + folderName.replaceAll("\\s", "_").replaceAll("-", "__")
+ "/" + problemName + fileExtensionFinal);
FileWriter fw = new FileWriter(newFile);
sc = new Scanner(file);
fw.append("package heap.").append(folderName.replaceAll("\\s", "_").replaceAll("-", "__")).append(";\n");
while (sc.hasNextLine()) {
temp = sc.nextLine();
if (temp.trim().startsWith("package")) continue;
else fw.append(temp).append("\n");
}
fw.flush();
fw.close();
sc.close();
}
}
}
package ca.mcgill.cs.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author AlexanderButyaev
*
*/
public class UnitTesting {
public class UTDS {
public List<String> messages;
public double mark;
UTDS(List<String> messages, double mark) {
this.messages = messages;
this.mark = mark;
}
public StringBuffer getComments() {
StringBuffer sb = new StringBuffer();
for (String mes: messages) {
sb.append(mes).append("|");
}
return sb;
}
}
private Class<?> clazz;
private Method m1;
private Object obj;
private static String problemName = "COMP251HW1";
public UnitTesting(String packageName) throws ClassNotFoundException, SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException {
clazz = Class.forName("heap."+packageName+"."+problemName);
m1 = clazz.getDeclaredMethod("divisionMethodImpl", int.class, int.class, int.class);//divisionMethodImpl (int D, int n, int w)
m1.setAccessible(true);
obj = clazz.newInstance();
}
public UTDS test_first_method(int w, int r, int m) throws IllegalAccessException, IllegalArgumentException {
double mark = 0;
List<String> messages = new ArrayList<String>();
try {
int n = m;
double res = (double) m1.invoke(obj, r, n, w);
// TODO: Test res - returned value here
// TODO: assign mark for method
mark = 100;
} catch (InvocationTargetException e) {
e.printStackTrace();
// TODO: Save error message to the log file
messages.add("InvocationTargetException: System error");
}
return new UTDS(messages, mark);
}
public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, SecurityException, NoSuchMethodException {
UnitTesting ut;
// log file: write logs (marks/notes) here
File file = new File(problemName + "_result.csv");
FileWriter fw = new FileWriter(file);
File _folder = new File("src/heap");
String[] fldrArray = _folder.list();
for (String packageName : fldrArray) {
if (packageName == null || packageName.startsWith(".") ) continue;
// append decrypted name to log file
fw.append("\"").append(packageName.replace("__", "-").replace("_", " ")).append("\"").append(",");
try {
ut = new UnitTesting(packageName);
} catch (NoSuchMethodException e) {
fw.append("\"").append("one of the methods has been changed").append("\",\n");
fw.flush();
continue;
}
System.out.println("Test for " + packageName);
// change parameters
UTDS res_m1 = ut.test_first_method(0, 0, 0);
// first method mark
fw.append("\"").append( String.valueOf( res_m1.mark ) ).append("\",");
// first method comments
fw.append("\"").append( res_m1.getComments() ).append("\",");
fw.append("\n");
fw.flush();
}
fw.close();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment