Show FitManager.java syntax highlighted
package ca.ucalgary.cpsc.ebe.fitClipse.runner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.internal.resources.WorkspaceRoot;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.internal.WorkbenchPage;
import org.eclipse.ui.part.WorkbenchPart;
import ca.ucalgary.cpsc.ebe.fitClipse.DeveloperCoordinator;
import ca.ucalgary.cpsc.ebe.fitClipse.ICoordinator;
import ca.ucalgary.cpsc.ebe.fitClipse.connector.BeanConnector;
import ca.ucalgary.cpsc.ebe.fitClipse.connector.ServletConnector;
import ca.ucalgary.cpsc.ebe.fitClipse.ui.testHierarchy.TestHierarchyView;
import ca.ucalgary.cpsc.ebe.fitClipse.ui.testResults.TestResultController;
import ca.ucalgary.cpsc.ebe.fitClipse.ui.testResults.TestResultView;
import ca.ucalgary.cpsc.ebe.fitClipse.ui.testResults.model.ResultRoot;
import ca.ucalgary.cpsc.ebe.fitClipse.ui.testResults.model.SingleTestResult;
import ca.ucalgary.cpsc.ebe.fitClipse.ui.testResults.model.TestResultsSummary;
import ca.ucalgary.cpsc.ebe.fitClipse.ui.testResults.model.TestSuiteResult;
//import fitEditor.FitView;
public class FitManager {
private static final Pattern nameSpacePattern = Pattern.compile("\\.([^.]+).*");
// private String FitClassPath = "C:\\Temp\\Temp\\fit.jar";
private String sourceTestRoot;
private String resultTestRoot;
private FileManager fManager = new FileManager();
private String suiteQName = "";
private Date executionDate;
private long startTime = 0;
private long endTime = 0;
private LinkedList<String> executableClassPaths = new LinkedList<String>(); //c:\temp ...
private LinkedList<FitTest> tests = new LinkedList<FitTest>();
private FontRegistry fonts = null;
// private Display display = new Display();
private FitManager(){
fonts = new FontRegistry(Display.getCurrent());
fonts.put("bold", new FontData[]{new FontData("Ariel", 10, SWT.BOLD)});
fonts.put("normal", new FontData[]{new FontData("Ariel", 9, SWT.NORMAL)});
}
private String constructClassPath(){
String classPath=null;
for(String path: this.executableClassPaths){
if(classPath==null) classPath=path;
else{
classPath += ";" + path;
}
}
if (classPath==null) classPath="";
return classPath;
}
private IProject getProjectForWikiNameSpace(String wikiPrjName){
System.out.println("Wiki Project Name: "+ wikiPrjName);
System.out.println("-----");
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for(IProject project: projects){
try {
String wikiNameSpace = project.getPersistentProperty(new QualifiedName("ca.ucalgary.cpsc.ebe.fitClipse", "project.namespace"));
System.out.println("Project link: " + wikiNameSpace);
if(wikiPrjName.equalsIgnoreCase(wikiNameSpace)){
return project;
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public void loadProjectRuntimeProperties(String wikiQName){
// System.out.println("%%%%%%%%wqName is: "+wikiQName);
String wikiPrjName = getWikiProjectName(wikiQName);
System.out.println("%%%%%%%%wikiproject name is: "+wikiPrjName);
IProject project = getProjectForWikiNameSpace(wikiPrjName);
System.out.println("the project is: "+project.getName());
this.clearClassPaths();
String linkedPath;
try {
linkedPath = project.getPersistentProperty(new QualifiedName("ca.ucalgary.cpsc.ebe.fitClipse","test.classpath"));
System.out.println("Saved Path: " + linkedPath);
StringTokenizer tokens = new StringTokenizer(linkedPath, ";");
int numPaths = tokens.countTokens();
for(int i = 0; i < numPaths; i++){
String path = tokens.nextToken();
System.out.println("Adding Path: " + path);
addClassPath(path);
}
} catch (CoreException e) {
//error opening classpath property
e.printStackTrace();
}
try {
this.sourceTestRoot = project.getPersistentProperty(new QualifiedName("ca.ucalgary.cpsc.ebe.fitClipse","test.src"));
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.resultTestRoot = project.getPersistentProperty(new QualifiedName("ca.ucalgary.cpsc.ebe.fitClipse","test.result"));
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultRoot runTests(ResultRoot root){
boolean failed = false;
boolean exception = false;
this.startTime = System.currentTimeMillis();
try{
this.executionDate = new Date();
TestSuiteResult run = null;
if(tests.size()>1){
run = new TestSuiteResult(root, this.suiteQName, this.executionDate, true);
}else{
run = new TestSuiteResult(root, this.suiteQName, this.executionDate, false);
}
//chagne to FitNesse bean connector
// ServletConnector server = ServletConnector.getServletConnector();
for(FitTest test: tests){
String content = test.getContent();
// String content = server.getHTMLForPage(test.getTestName());
fManager.createFile(test.getSourceFile(), content);
test.run();
if(test.getNumExceptions() > 0)
exception = true;
if(test.getNumWrong() > 0)
failed = true;
SingleTestResult testModel = new SingleTestResult(run,test.getTestName(),test.getNumRight(), test.getNumWrong(), test.getNumExceptions(), test.getNumIgnored());
testModel.setFlagged(isTestFAILING(test));
run.addChild(testModel);
}
//persist test results
this.endTime = System.currentTimeMillis();
long executionTime = this.endTime - this.startTime;
// FitClipseNesse & FitClipse MASE: adding the total to result here
// TestResultsSummary totalResult = new TestResultsSummary(tallyRight(),tallyWrong(),tallyExceptions(),tallyIgnored(),executionTime);
BeanConnector connector = BeanConnector.getBeanConnector();
//************FitClipseNesse: commended adding later***************
// long id = connector.persistTestSuite(run.getQName(), run.isSuite(), this.executionDate, executionTime, this.tests);
// run.setSuiteID(id);
System.out.println("Suite ID is: " + run.getID());
root.addChild(run);
// root.addChild()
TestResultView view = TestResultController.getTestResultView();
if(failed)
view.setStatus(TestResultView.FAIL);
else if(exception)
view.setStatus(TestResultView.EXCEPTION);
else
view.setStatus(TestResultView.PASS);
return root;
}catch(Exception e){
//error opening window
// System.out.println("Error Opening " + TestResultView.ID + ": maybe id is invalid?");
TestResultView view;
try {
view = TestResultController.getTestResultView();
view.setStatus(TestResultView.EXCEPTION);
} catch (PartInitException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
return null;
}
}
public void clearTests(){
tests.clear();
}
public void addClassPath(String path){
if(path.endsWith("\\*")){
//include every file in folder
String folderPath = path.substring(0, path.length() -2);
File folder = new File(folderPath);
if(folder.isDirectory()){
//include
String[] files = folder.list();
for(String s: files){
String filePath = folderPath + "\\" + s;
addClassPath(filePath);
}
}else{
}
}else
executableClassPaths.add(path);
}
public String getResultTestRoot() {
return resultTestRoot;
}
public void setResultTestRoot(String resultTestRoot) {
this.resultTestRoot = resultTestRoot;
File resultFolder= new File(resultTestRoot);
String title="Create directory?";
String message="Specified directory "+resultTestRoot+" does not exist. Do you want to create it?";
}
public String getSourceTestRoot() {
return sourceTestRoot;
}
public void setSourceTestRoot(String sourceTestRoot) {
this.sourceTestRoot = sourceTestRoot;
}
public void setSuiteQName(String name){
this.suiteQName = name;
}
public void CreateTest(String testName){
String fileName = testName.substring(1) + ".html";
String sourceFile = sourceTestRoot+ "\\" + fileName;
String resultFile = resultTestRoot+ "\\" + fileName;
sourceFile = sourceFile.replaceAll(" ","_");
sourceFile = sourceFile.replaceAll("/","_");
resultFile = resultFile.replaceAll(" ","_");
resultFile = resultFile.replaceAll("/","_");
/*MASE: use serverlet connector to get the html page.
MASE has webport and beanport, which requires two connector
ServletConnector server = ServletConnector.getServletConnector();
String content = server.getHTMLForPage(testName);
*/
/*FitClipseNesse; use FitNesse to get the page
FitNesse only have webport, everything is done through webport
*/
BeanConnector connector = BeanConnector.getBeanConnector();
String content = connector.getHTMLForPage(testName);
//file is created when running the test later, file has been created twice before: once here the other in run test above
// sourceFile=fManager.createFile(sourceFile, content);
FitTest test = new FitTest(constructClassPath(), sourceFile, resultFile);
test.setTestName(testName);
test.setContent(content);
tests.add(test);
}
public FitTest getFitTestByName(String testName){
for(FitTest f: this.tests){
if(testName.equals(f.getTestName())){
return f;
}
}
return null;
}
public void clearClassPaths(){
executableClassPaths.clear();
}
public boolean removeClassPath(String classPath){
if(classPath.endsWith("\\*")){
//include every file in folder
String folderPath = classPath.substring(0, classPath.length() -2);
File folder = new File(folderPath);
if(folder.isDirectory()){
//include
String[] files = folder.list();
for(String f: files){
String filePath = folderPath + "\\" + f;
removeClassPath(filePath);
}
}
}else{
for(int i=0; i<executableClassPaths.size();i++){
String s=executableClassPaths.get(i);
if(s.equals(classPath)){
executableClassPaths.remove(i);
return true;
}
}
}
return false;
}
public int tallyRight(){
int tally=0;
for(FitTest test:tests){
tally+=test.getNumRight();
}
return tally;
}
public int tallyWrong(){
int tally=0;
for(FitTest test:tests){
tally+=test.getNumWrong();
}
return tally;
}
public int tallyExceptions(){
int tally=0;
for(FitTest test:tests){
tally+=test.getNumExceptions();
}
return tally;
}
public int tallyIgnored(){
int tally=0;
for(FitTest test:tests){
tally+=test.getNumIgnored();
}
return tally;
}
private boolean isTestFAILING(FitTest test){
BeanConnector connector = BeanConnector.getBeanConnector();
List <FitTest> history = connector.getFitTestHistory(test.getTestName());
if(history == null){
return false;
}
for(int i = history.size() - 1; i >= 0; i--){
FitTest item = history.get(i);
if(item.getNumExceptions() == 0 && item.getNumIgnored() == 0 && item.getNumWrong() == 0 ){
int itemTotalNum= item.getNumRight() + item.getNumWrong() + item.getNumIgnored() + item.getNumExceptions();
int currentTotalNum = test.getNumRight() + test.getNumWrong() + test.getNumIgnored() + test.getNumExceptions();
if(itemTotalNum == currentTotalNum){
return true;
}else{
return false;
}
}
}
return false;
}
private String getWikiProjectName(String suiteRootQName){
//FitClipseNesse: the name is: ".root.ProjectName" not proper, so return project name directly for fitnesse
if(suiteRootQName.startsWith(".root.")){
return suiteRootQName.replace(".root.","");
}
Matcher match = nameSpacePattern.matcher(suiteRootQName);
if(match.find()){
return match.group(1);
}
return null;
}
//Methods up here
//singleton stuff
private static FitManager instance =null;
public static FitManager getFitManager(){
if(instance == null)
instance = new FitManager();
return instance;
}
public LinkedList<FitTest> getTests() {
return tests;
}
}
See more files for this project here