Show ColumnFixtureSource.java syntax highlighted
package ca.ucalgary.cpsc.ebe.fitClipse.fixtureGeneration;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ca.ucalgary.cpsc.ebe.fitClipse.javaSourceModification.JavaClass;
import ca.ucalgary.cpsc.ebe.fitClipse.javaSourceModification.JavaFile;
import ca.ucalgary.cpsc.ebe.fitClipse.javaSourceModification.JavaMethod;
import ca.ucalgary.cpsc.ebe.fitClipse.javaSourceModification.JavaMethodContentNode;
import ca.ucalgary.cpsc.ebe.fitClipse.javaSourceModification.JavaMultilineComment;
import ca.ucalgary.cpsc.ebe.fitClipse.javaSourceModification.JavaProperty;
import ca.ucalgary.cpsc.ebe.fitClipse.javaSourceModification.JavaSourceUtil;
public class ColumnFixtureSource extends FixtureSource {
public ColumnFixtureSource() {
super();
// TODO Auto-generated constructor stub
}
private String sourcePath;
private static String FIXTURE_CLASS_PACKAGE_REGEX = "\\|(!-)?(([a-zA-Z_]+[\\.])*)([a-zA-Z_]+)(-!)?\\|";
private StringBuffer messages = new StringBuffer();
public String generateSource(String wikiData, String sourcePath ) throws Exception{
try{
this.sourcePath = sourcePath;
String qName = parseFixtureName(wikiData);
String packageName = parseFixturePackageName(wikiData);
String[] properties = parsePropertyNames(wikiData);
String[] methods = parseMethodNames(wikiData);
messages.append("Creating java files...\n");
composeJavaContent(qName, packageName, properties, methods);
messages.append("\nNumber of files created: 1");
return messages.toString();
}catch(Exception e){
return e.getMessage();
}
}
private String parseFixtureName(String wikiData) throws Exception{
String firstLine = wikiData.split("\n")[0];
Pattern pattern = Pattern.compile(ColumnFixtureSource.FIXTURE_CLASS_PACKAGE_REGEX);
Matcher match = pattern.matcher(firstLine);
if(!match.matches()){
throw new Exception("Invalid First Row of Table (fixture name)");
}
String qName = match.group(4);
return qName;
}
private String parseFixturePackageName(String wikiData) throws Exception{
String firstLine = wikiData.split("\n")[0];
Pattern pattern = Pattern.compile(ColumnFixtureSource.FIXTURE_CLASS_PACKAGE_REGEX);
Matcher match = pattern.matcher(firstLine);
if(!match.matches()){
throw new Exception("Invalid First Row of Table (fixture name)");
}
String packageName = "";
char[] chars = match.group(2).toCharArray();
for(int i = 0; i < chars.length - 1; i++ ){
//remove the trailing '.' char
packageName += chars[i];
}
return packageName;
}
private String[] parsePropertyNames(String wikiData){
String[] rows = wikiData.split("\n");
LinkedList properties = new LinkedList();
StringTokenizer tokens = new StringTokenizer(rows[1], "|");
while(tokens.hasMoreTokens()){
String possibleProperty = tokens.nextToken();
if(!possibleProperty.endsWith("?") && !possibleProperty.endsWith("()")){
properties.add(possibleProperty);
}
}
String[] result = new String[properties.size()];
for(int i = 0; i < properties.size(); i++){
result[i] = (String)properties.get(i);
}
return result; //will return an empty array, never null
}
private String[] parseMethodNames(String wikiData){
String[] rows = wikiData.split("\n");
LinkedList methods = new LinkedList();
StringTokenizer tokens = new StringTokenizer(rows[1], "|");
while(tokens.hasMoreTokens()){
String possibleMethod = tokens.nextToken();
if(possibleMethod.endsWith("?")){
methods.add(possibleMethod.substring(0, possibleMethod.length() - 1));
}else if(possibleMethod.endsWith("()")){
methods.add(possibleMethod.substring(0, possibleMethod.length() - 2));
}
}
String[] result = new String[methods.size()];
for(int i = 0; i < methods.size(); i++){
result[i] = (String)methods.get(i);
}
return result; //will return an empty array, never null
}
private String composeJavaContent(String qName, String packageName, String[] properties, String[] methods) throws Exception{
JavaFile file = new JavaFile();
file.setPackageName(packageName);
file.addImport("fit.ColumnFixture");
JavaClass fixture = new JavaClass(qName, "public");
fixture.addSuperClass("ColumnFixture");
JavaProperty prop = null;
for(int i = 0; i < properties.length; i++){
prop = new JavaProperty(toCamelCase(properties[i]), "public");
prop.setType(JavaSourceUtil.TYPE_STRING);
fixture.addChildElement(prop);
}
JavaMethod method = null;
for(int i = 0; i < methods.length; i++){
method=new JavaMethod(toCamelCase(methods[i]), "public");
method.setReturnType(JavaSourceUtil.TYPE_STRING);
method.addContentNode(new JavaMultilineComment("Auto-Generated fixture code\nAdd your own test code here"));
method.addContentNode(new JavaMethodContentNode("\nreturn null;"));
fixture.addChildElement(method);
}
file.setMainClass(fixture);
messages.append(file.commitToFile(sourcePath));
return file.renderSource();
}
}
See more files for this project here