/* * Created on Jun 20, 2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package RegressionTest.buttons; import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import org.eclipse.core.runtime.IPath; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.IInputValidator; import org.eclipse.swt.SWT; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import CDBuilder.console.ConsoleDocument; import CDBuilder.console.StreamHandler; /** * @author Ali Monadi * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class TestDevsGUI extends Dialog{ private String newSIMU; /*the String will contain the location of the new SIMU to be tested*/ /*this value will be set in the Open dialog or NewFileGui() function*/ private Shell parent; private String title; private String message1; private String message2; private String message3; private String message4; private String message5; private String message6; private String message7; private Button simuButton; private Button doneButton; private Button killButton; private Button saveSetButton; private Button loadSetButton; private Button saveBatButton; private Button loadBatButton; private Label colon; private Button checkBoxEv; private Button checkBoxOut; private Button checkBoxTime; private Button checkBoxLog; private Button checkBoxAdv; private Button checkBoxComment; private Button fileButtonEv; private Button fileButtonMa; private Button fileButtonLog; private Button fileButtonOut; private Text textSimu; private Text textEv; private Text textOut; private Text textLog; private Text textTime1,textTime2,textTime3,textTime4; private Text textAdv; private static boolean checkedEv = false; private static boolean checkedOut = true; private static boolean checkedLog = true; private static boolean checkedTime = true; private static boolean checkedAdv = false; private boolean simuKilled = false; private RunSimu rsimu; private Process simuProcess; private Label errorMessageLabel; private IInputValidator validator; private IPath projectPath; private FileWriter final_results; class conWrite implements Runnable { String line; public conWrite(String line) { this.line = line; } public void run() { ConsoleDocument.getCDOS().set(line); } } class RunSimu extends Thread { Dialog d; String command; File [] projects; String timeParam; String advancedParam; /* Cunstructor * * @param Dialog d, this class has refference to class TestDevsGUI for closing the dialog box * @param File [] projectPath, is a list of projects to be tested * @param String timeParam, contains the 'time' parameter for the command * @param String advancedParam, contains the 'advanced' parameter for the command * * NOTE: timeParam and Advanced param have to be passed in as Strings * b/c Text boxes can NOT be accessed from another thread */ public RunSimu(Dialog d, File [] projects , String timeParam, String advancedParam) { command = ""; this.d = d; this.projects = projects; this.timeParam = timeParam; this.advancedParam = advancedParam; } public void run() { String results = projects[0].getParentFile().getParentFile().toString().concat("\\Results"); String origFile, newFile, result; try { final_results = new FileWriter( projects[0].getParentFile().getParentFile().toString().concat("\\FINALRESULTS.txt") ); for (int i=0; i "+projects[i].getName()+" <###############")); command = setCommand(projects[i]); ConsoleDocument cdos = ConsoleDocument.getCDOS(); FileWriter tempFile = new FileWriter(projects[i].toString().concat("\\tempSimu.bat").toString()); tempFile.write(newSIMU + command); tempFile.close(); simuProcess = Runtime.getRuntime().exec("cmd /c" + projects[i].toString()+"\\tempSimu.bat",null,new File(projects[i].toString())); StreamHandler outputHandler = new StreamHandler(simuProcess.getInputStream(), "OUTPUT", true); StreamHandler errorHandler = new StreamHandler(simuProcess.getErrorStream(), "ERROR", true); outputHandler.start(); errorHandler.start(); simuProcess.waitFor(); new File(projects[i].toString().concat("\\tempSimu.bat")).delete(); File [] projectFiles = projects[i].listFiles(); for (int j=0; j"+result); tempFile.close(); simuProcess = Runtime.getRuntime().exec("cmd /c" + projects[i].toString()+"\\tempcmplog.bat",null,new File(projects[i].toString())); outputHandler = new StreamHandler(simuProcess.getInputStream(), "OUTPUT", true); errorHandler = new StreamHandler(simuProcess.getErrorStream(), "ERROR", true); outputHandler.start(); errorHandler.start(); simuProcess.waitFor(); System.out.println("length " + new File(result).length()); if( new File(result).length() != 0 ) { final_results.write("\r Project [ "+projects[i].getName()+" ]: '.log' file contains errors, "); final_results.write("\r For details go to -> "+projects[i].toString()+"\\Results\r"); System.out.println(".log error"); } (new File(projects[i].toString().concat("\\tempcmplog.bat"))).delete(); } /* * If there exists a file with extention ".out" and the check box is ckecked, * copy the the file to the results folder, and compare it with the original results. * * Place the difference of the two files in a file called "outResults.out" */ if (extention.equals("OUT") && checkedOut ){ destination = results.concat("/"+projects[i].getName()+"/new.out"); dest = new File(destination); Runtime.getRuntime().exec("cp "+ projectFiles[j] +" "+ destination ); while(!dest.exists() || dest.length() != source.length()){System.out.println("waiting...out");}//polling, can NOT continue if copy is not done new File(projectFiles[j].toString()).delete(); origFile = results.concat("\\"+projects[i].getName()+"\\orig.out"); newFile = results.concat("\\"+projects[i].getName()+"\\new.out"); result = results.concat("\\"+projects[i].getName()+"\\outResults.out"); tempFile = new FileWriter(projects[i].toString().concat("\\tempcmpout.bat").toString()); tempFile.write("diff "+origFile +" "+newFile+" >"+result); tempFile.close(); simuProcess = Runtime.getRuntime().exec("cmd /c" + projects[i].toString()+"\\tempcmpout.bat",null,new File(projects[i].toString())); outputHandler = new StreamHandler(simuProcess.getInputStream(), "OUTPUT", true); errorHandler = new StreamHandler(simuProcess.getErrorStream(), "ERROR", true); outputHandler.start(); errorHandler.start(); simuProcess.waitFor(); System.out.println("length " + new File(result).length()); if( new File(result).length() != 0 ) { // final_results.write("\r Project [ "+projects[i].getName()+" ]: '.out' file contains errors, "); final_results.write("\r For details go to -> "+projects[i].toString()+"\\Results\r"); System.out.println(".out error"); } (new File(projects[i].toString().concat("\\tempcmpout.bat"))).delete(); } } }//forloop loop projects final_results.close(); //closing the results file Display.getDefault().syncExec( new conWrite( "\n************************************************************************************************************\n"+ "Please open project to view results: \n"+ "The following file contains all error logs: "+ projects[0].getParentFile().getParentFile().toString().concat("\\FINALRESULTS.txt")+ "\n************************************************************************************************************\n")); Display.getDefault().syncExec(new Runnable() { public void run(){ doneButton.setEnabled(true); killButton.setEnabled(false); simuButton.setEnabled(true); d.close(); } }); if (simuKilled) { Display.getDefault().syncExec(new conWrite("***User has killed the simulation***" + "\n")); simuKilled = false; } }catch (Exception e) { e.printStackTrace(); } } /* Tis function will collect all parameters and return it all as one string command * * @param File projectPath, will contain the location to an individual project file * @return String, returns a String containing the command */ private String setCommand(File projectPath){ command =""; File [] projectFiles = ( new File(projectPath.toString()) ).listFiles(); String[] param = new String[6]; for (int j = 0 ; j < projectFiles.length; j++ ){ System.out.println("files found "+ projectFiles[j]); String extention = getExtention(projectFiles[j].toString()).toUpperCase(); if (extention != null){ if (extention.equals("MA")) param [0] = "-m" + "'" + projectFiles[j].getName() +"'"; if (extention.equals("EV") && checkedEv) param[1] = "-e" + "'" + projectPath.getName() + "'"; if(checkedOut) param[2] = "-o" + "'" + projectPath.getName() + ".out'"; if(checkedLog) param[3] = "-l" + "'" + projectPath.getName() + ".log'"; if (timeParam != null) param[4] = timeParam; if (advancedParam != null) param[6] = advancedParam; }//end if( extention != null) }// forloop files in project for (int n=0; n < 6; n++) { if (param[n] != null) command = command + " " + param[n]; } return command; } /* * creates an object of type IPath with the path given as the parameter * * @param String thisPath, the wanted path * @return newPath of type IPath */ private String getExtention(String thisPath){ int start = thisPath.indexOf("."); return thisPath.substring(start + 1, thisPath.length()); } } /* * Constructor * * @param Shell parentShell just a reference to the shell * @param IPath projectPath, a path to the the folder that contains all dev projects */ public TestDevsGUI(Shell parentShell, IPath projectPath){ super(parentShell); parent = parentShell; this.title = "Simulate Project"; message1 = "New simu file (.ma)"; message2 = "Event file (.ev)"; message3 = "Output file (.out)"; message4 = "Log file (.log)"; message5 = "Enter simulation stop time"; message6 = "Advanced users Only. Enter desired paramters"; message7 = "Comments"; this.projectPath = projectPath; } protected void buttonPressed(int buttonId) { String timeParam = null; String advancedParam = null; if (buttonId == IDialogConstants.CLOSE_ID) close(); if (buttonId == IDialogConstants.PROCEED_ID) { if (isEmpty("New simu file",textSimu)) return; File [] projects = ( new File( projectPath.toString()) ).listFiles(); if(textTime1.isEnabled()) { if(isEmpty("Hour Value",textTime1) || isEmpty("Minute value",textTime2) || isEmpty("Second Value",textTime3) || isEmpty("Millisecond",textTime3)){ return; } timeParam = textTime1.getText().trim()+":"+textTime2.getText().trim()+":"+textTime3.getText().trim()+":"+textTime4.getText().trim(); timeParam = "-t" + "'" + timeParam + "'"; } if(textAdv.isEnabled()) { if(isEmpty("Advance parameter",textAdv)){ return; } advancedParam = textAdv.getText().trim(); } rsimu = new RunSimu(this, projects, timeParam, advancedParam); rsimu.start(); } super.buttonPressed(buttonId); } /* * check to see if the value entered in the text box is an integer * If NOT an integer, clear the contents of the text box * * @ param Text textBox */ private void checkIntegerValue(Text textBox){ try{ new Integer(textBox.getText()); }catch (Exception e){ textBox.setText(""); } } protected void configureShell(Shell shell) { super.configureShell(shell); if (title != null) shell.setText(title); } /* * Determines if a text box is left empty and displays error message * @return false of empty true otherwise */ private boolean isEmpty(String parameter,Text textBox){ String value = textBox.getText().trim(); if(value.length()== 0){ MessageBox mbox = new MessageBox(new Shell(),SWT.ICON_ERROR | SWT.OK); mbox.setText("Simulation Stopped"); mbox.setMessage(parameter+" is not entered"); mbox.open(); try { mbox.wait(); } catch (InterruptedException e) { e.printStackTrace(); } doneButton.setEnabled(true); killButton.setEnabled(false); simuButton.setEnabled(true); return true; } return false; } protected void createButtonsForButtonBar(Composite parent) { // create OK and Cancel buttons by default simuButton = createButton(parent, IDialogConstants.PROCEED_ID, IDialogConstants.PROCEED_LABEL, true); simuButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e){ killButton.setEnabled(true); simuButton.setEnabled(false); doneButton.setEnabled(false); } }); doneButton = createButton(parent, IDialogConstants.CLOSE_ID, IDialogConstants.CLOSE_LABEL, true); killButton = createButton(parent, IDialogConstants.STOP_ID, "Kill", true); killButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e){ simuProcess.destroy(); try { Process lookProcess = Runtime.getRuntime().exec("ps"); BufferedReader bf= new BufferedReader(new InputStreamReader(lookProcess.getInputStream())); String msg=null; while((msg=bf.readLine())!=null){ if (msg.indexOf(projectPath.toString().substring(2))!=-1){ msg=msg.substring(2).trim(); Process killProcess=Runtime.getRuntime().exec("kill -9 " + msg.substring(0,msg.indexOf(" "))); } } } catch (IOException e1) { e1.printStackTrace(); } doneButton.setEnabled(true); killButton.setEnabled(false); simuButton.setEnabled(true); simuKilled = true; } }); killButton.setEnabled(false); //do this here because setting the text will set enablement on the ok button textSimu.setFocus(); } /* *creats a lable for dialog box * * @param String message * @param Composite composite * @param Composite parent * * @return Label */ private Label createLabel(Composite parent, Composite composite, String message) { Label label = new Label(composite, SWT.NONE); label.setText(message); GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.horizontalSpan=10; label.setLayoutData(gridData); label.setFont(parent.getFont()); return label; } protected Control createDialogArea(Composite parent) { // create composite Composite composite = (Composite)super.createDialogArea(parent); GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 10; composite.setLayout(gridLayout); //Row One - Should be text description of ma text box Label label = createLabel(parent, composite, message1); // Row two - Text Box for the ma file and file button textSimu = new Text(composite, SWT.SINGLE | SWT.BORDER); Color c = new Color(null,255,255,255); textSimu.setBackground(c); GridData gridData3 = new GridData(); gridData3.horizontalSpan=9; gridData3.horizontalAlignment = GridData.CENTER; gridData3.grabExcessHorizontalSpace = true; gridData3.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); textSimu.setLayoutData(gridData3); Button fileButtonMa = new Button(composite, SWT.PUSH); fileButtonMa.setText("Browse"); fileButtonMa.setToolTipText("Select 'NEW' simu File"); GridData gridDataFBMa = new GridData(); gridDataFBMa.horizontalAlignment = GridData.BEGINNING; fileButtonMa.setLayoutData(gridDataFBMa); fileButtonMa.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e){ textSimu.setText( NewFileGui("*.exe") ); } }); // Row Three - Text Description of events text box label = createLabel(parent, composite, message2); //Row Four - The Check Box then the Text Box for events checkBoxEv = new Button(composite, SWT.CHECK); GridData gridData5 = new GridData(); gridData5.horizontalAlignment = GridData.BEGINNING; checkBoxEv.setLayoutData(gridData5); checkBoxEv.setSelection(checkedEv); checkBoxEv.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e){ if(checkedEv) checkedEv = false; else checkedEv = true; } }); //Row Five - Text Description of the Out text box label = createLabel(parent, composite, message3); //Row Six - The check box and text box for out checkBoxOut = new Button(composite, SWT.CHECK); GridData gridData8 = new GridData(); gridData8.horizontalAlignment = GridData.BEGINNING; checkBoxOut.setLayoutData(gridData8); checkBoxOut.setSelection(checkedOut); checkBoxOut.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e){ if(checkedOut) checkedOut = false; else checkedOut = true; } }); //Row Seven - Text Description of the Log text box label = createLabel(parent, composite, message4); //Row Eight - The check box and text box for log checkBoxLog = new Button(composite, SWT.CHECK); GridData gridData11 = new GridData(); gridData11.horizontalAlignment = GridData.BEGINNING; checkBoxLog.setLayoutData(gridData11); checkBoxLog.setSelection(checkedLog); checkBoxLog.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e){ if(checkedLog) checkedLog = false; else checkedLog = true; } }); //Row Nine - Text description of the Time text box label = createLabel(parent, composite, message5); //Row ten - The check box and text box for time checkBoxTime = new Button(composite, SWT.CHECK); GridData gridData14 = new GridData(); gridData14.horizontalAlignment = GridData.BEGINNING; checkBoxTime.setLayoutData(gridData14); checkBoxTime.setSelection(checkedTime); checkBoxTime.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e){ if(textTime1.getEnabled() == true) { textTime1.setEnabled(false); textTime2.setEnabled(false); textTime3.setEnabled(false); textTime4.setEnabled(false); checkedTime = false; }else { textTime1.setEnabled(true); textTime2.setEnabled(true); textTime3.setEnabled(true); textTime4.setEnabled(true); checkedTime = true; } } }); textTime1= new Text(composite, SWT.SINGLE | SWT.BORDER); textTime1.setTextLimit(2); GridData gridData15 = new GridData(); gridData15.widthHint = 15; gridData15.heightHint = 13; textTime1.setLayoutData (gridData15); textTime1.setEnabled(checkedTime); textTime1.setToolTipText("Hours"); textTime1.setText("00"); textTime1.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e) { try{ checkIntegerValue(textTime1); if(e.keyCode == SWT.ARROW_UP){ Integer time= new Integer(textTime1.getText()); time = new Integer(time.intValue() + 1); if (time.intValue() > 99){ textTime1.setText("00"); }else{ textTime1.setText(time.toString()); } } if(e.keyCode==SWT.ARROW_DOWN){ Integer time= new Integer(textTime1.getText()); time = new Integer(time.intValue()-1); if (time.intValue()<0){ textTime1.setText("99"); }else{ textTime1.setText(time.toString()); } } }catch(Exception e1){ e1.printStackTrace(); } } public void keyReleased(KeyEvent e) { } }); colon = new Label(composite,SWT.NONE); colon.setText(":"); textTime2 =new Text(composite, SWT.SINGLE | SWT.BORDER); textTime2.setTextLimit(2); gridData15 = new GridData(); gridData15.widthHint = 15; gridData15.heightHint = 13; textTime2.setLayoutData (gridData15); textTime2.setEnabled(checkedTime); textTime2.setToolTipText("Minutes"); textTime2.setText("1"); textTime2.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e) { try{ if(e.keyCode==SWT.ARROW_UP){ Integer time= new Integer(textTime2.getText()); time=new Integer(time.intValue()+1); if (time.intValue()>99){ textTime2.setText("00"); }else{ textTime2.setText(time.toString()); } } if(e.keyCode==SWT.ARROW_DOWN){ Integer time= new Integer(textTime2.getText()); time=new Integer(time.intValue()-1); if (time.intValue()<0){ textTime2.setText("99"); }else{ textTime2.setText(time.toString()); } } checkIntegerValue(textTime2); }catch(Exception e1){ e1.printStackTrace(); } } public void keyReleased(KeyEvent e) { } }); colon = new Label(composite,SWT.NONE); colon.setText(":"); textTime3= new Text(composite, SWT.SINGLE | SWT.BORDER); textTime3.setTextLimit(2); gridData15 = new GridData(); gridData15.widthHint = 15; gridData15.heightHint = 13; textTime3.setLayoutData (gridData15); textTime3.setEnabled(checkedTime); textTime3.setToolTipText("Seconds"); textTime3.setText("00"); textTime3.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e) { try{ if(e.keyCode==SWT.ARROW_UP){ Integer time= new Integer(textTime3.getText()); time=new Integer(time.intValue()+1); if (time.intValue()>99){ textTime3.setText("00"); }else{ textTime3.setText(time.toString()); } } if(e.keyCode==SWT.ARROW_DOWN){ Integer time= new Integer(textTime3.getText()); time=new Integer(time.intValue()-1); if (time.intValue()<0){ textTime3.setText("99"); }else{ textTime3.setText(time.toString()); } } checkIntegerValue(textTime3); }catch(Exception e1){ e1.printStackTrace(); } } public void keyReleased(KeyEvent e) { } }); colon = new Label(composite,SWT.NONE); colon.setText(":"); textTime4= new Text(composite, SWT.SINGLE | SWT.BORDER); textTime4.setTextLimit(3); textTime4.setText("000"); gridData15 = new GridData(); gridData15.widthHint = 20; gridData15.heightHint = 13; textTime4.setLayoutData (gridData15); textTime4.setToolTipText("Milliseconds"); textTime4.setEnabled(checkedTime); textTime4.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e) { try{ if(e.keyCode==SWT.ARROW_UP){ Integer time= new Integer(textTime4.getText()); time=new Integer(time.intValue()+1); if (time.intValue()>999){ textTime4.setText("000"); }else{ textTime4.setText(time.toString()); } } if(e.keyCode==SWT.ARROW_DOWN){ Integer time= new Integer(textTime4.getText()); time=new Integer(time.intValue()-1); if (time.intValue()<0){ textTime4.setText("999"); }else{ textTime4.setText(time.toString()); } } checkIntegerValue(textTime4); }catch(Exception e1){ e1.printStackTrace(); } } public void keyReleased(KeyEvent e) {} }); //Row Eleven - Text Discription of Advanced label = createLabel(parent, composite, message6); //Row Tweleve - The check box and text box for advanced checkBoxAdv = new Button(composite, SWT.CHECK); GridData gridData17 = new GridData(); gridData17.horizontalAlignment = GridData.BEGINNING; checkBoxAdv.setLayoutData(gridData17); checkBoxAdv.setSelection(checkedAdv); checkBoxAdv.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e){ if(textAdv.getEnabled() == true) { textAdv.setEnabled(false); checkedAdv = false; } else { textAdv.setEnabled(true); checkedAdv = true; } } }); textAdv= new Text(composite, SWT.SINGLE | SWT.BORDER); GridData gridData18 = new GridData(); gridData18.horizontalAlignment = GridData.FILL; gridData18.grabExcessHorizontalSpace = true; gridData18.horizontalSpan = 9; textAdv.setLayoutData(gridData18); textAdv.setEnabled(checkedAdv); errorMessageLabel = new Label(composite, SWT.NONE); errorMessageLabel.setFont(parent.getFont()); return composite; } /* * This function will open up a dialog box to search for a file of type "filter" in the * file system. * * @param String filter, to view files with this type of file extention * @return A String containing the name of the file selected(including extention) * */ private String NewFileGui(String filter) { newSIMU =""; FileDialog fileDialog = new FileDialog(parent); String[] extention = new String[1]; extention[0] = filter; fileDialog.setFilterPath(projectPath.toString()); fileDialog.setFilterExtensions(extention); newSIMU = fileDialog.open(); System.out.println("\nOpened file at this location:: " + newSIMU ); return fileDialog.getFileName(); } }//end of class