|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Windows.Forms; |
| 5 | + |
| 6 | +namespace JavaTestCenter |
| 7 | +{ |
| 8 | + public partial class JavaTestCenter : Form |
| 9 | + { |
| 10 | + OpenFileDialog ofd = new OpenFileDialog(); |
| 11 | + |
| 12 | + public JavaTestCenter() |
| 13 | + { |
| 14 | + InitializeComponent(); |
| 15 | + } |
| 16 | + |
| 17 | + private void Button_Run_Click(object sender, EventArgs e) |
| 18 | + { |
| 19 | + TextBox_Process.Text = ""; |
| 20 | + Process p = new Process(); |
| 21 | + p.StartInfo.UseShellExecute = false; |
| 22 | + p.StartInfo.CreateNoWindow = true; |
| 23 | + p.StartInfo.RedirectStandardOutput = true; |
| 24 | + p.StartInfo.RedirectStandardError = true; |
| 25 | + p.StartInfo.RedirectStandardInput = true; |
| 26 | + p.StartInfo.WorkingDirectory = TextBox_Directory.Text; |
| 27 | + |
| 28 | + p.StartInfo.FileName = "javac"; |
| 29 | + p.StartInfo.Arguments = ((RadioButton_CP.Checked) ? "-cp . " + TextBox_JavaFile.Text : (RadioButton_D.Checked) ? "-d . *.java" : TextBox_JavaFile.Text); |
| 30 | + TextBox_Process.Text += "javac " + ((RadioButton_CP.Checked) ? "-cp . " + TextBox_JavaFile.Text : (RadioButton_D.Checked) ? "-d . *.java" : TextBox_JavaFile.Text) + " [Compiling...]"; |
| 31 | + try |
| 32 | + { |
| 33 | + p.Start(); |
| 34 | + p.WaitForExit(); |
| 35 | + |
| 36 | + p.StartInfo.FileName = "java"; |
| 37 | + p.StartInfo.Arguments = TextBox_JavaFile.Text.Replace(".java", ""); |
| 38 | + TextBox_Process.Text += Environment.NewLine + "java " + TextBox_JavaFile.Text.Replace(".java", "") + " [Running...]"; |
| 39 | + p.Start(); |
| 40 | + |
| 41 | + StreamWriter sw = p.StandardInput; |
| 42 | + foreach (string line in TextBox_Input.Lines) |
| 43 | + { |
| 44 | + sw.WriteLine(line); |
| 45 | + } |
| 46 | + sw.Close(); |
| 47 | + |
| 48 | + p.WaitForExit(); |
| 49 | + |
| 50 | + string error = p.StandardError.ReadToEnd(); |
| 51 | + |
| 52 | + if (error.Length > 0) |
| 53 | + { |
| 54 | + MessageBox.Show("Error occurred: " + Environment.NewLine + error); |
| 55 | + } |
| 56 | + |
| 57 | + string output = p.StandardOutput.ReadToEnd(); |
| 58 | + TextBox_OutputActual.Text = output.Trim(); |
| 59 | + |
| 60 | + if (TextBox_Output.Text.Length > 0) |
| 61 | + { |
| 62 | + if (TextBox_OutputActual.Lines.Length != TextBox_Output.Lines.Length) |
| 63 | + { |
| 64 | + TextBox_OutputActual.Text += Environment.NewLine + "Match: NO - Line count different."; |
| 65 | + return; |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + int count = 0; |
| 70 | + int compareCount = TextBox_OutputActual.Lines.Length; |
| 71 | + TextBox_OutputActual.Text += Environment.NewLine + Environment.NewLine; |
| 72 | + for (int i = 0; i < compareCount; i++) |
| 73 | + { |
| 74 | + if (!TextBox_OutputActual.Lines[i].Equals(TextBox_Output.Lines[i])) |
| 75 | + { |
| 76 | + count++; |
| 77 | + TextBox_OutputActual.Text += "Line " + i + ":" + Environment.NewLine + "Expected \"" + TextBox_Output.Lines[i] + "\" got \"" + TextBox_OutputActual.Lines[i] + "\"." + Environment.NewLine; |
| 78 | + } |
| 79 | + } |
| 80 | + if (count == 0) |
| 81 | + { |
| 82 | + TextBox_OutputActual.Text += Environment.NewLine + "Match: YES"; |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + TextBox_OutputActual.Text += Environment.NewLine + "Match: NO - " + count + " lines different."; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + TextBox_Process.Text += " [Done.]"; |
| 91 | + } |
| 92 | + catch (Exception) |
| 93 | + { |
| 94 | + TextBox_Process.Text += " [Error.]"; |
| 95 | + MessageBox.Show("Error: Verify Java is in PATH."); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void Button_JavaFile_Click(object sender, EventArgs e) |
| 100 | + { |
| 101 | + ofd = new OpenFileDialog(); |
| 102 | + ofd.Filter = "JAVA (*.java)|*.java"; |
| 103 | + |
| 104 | + if (ofd.ShowDialog() == DialogResult.OK) |
| 105 | + { |
| 106 | + TextBox_JavaFile.Text = Path.GetFileName(ofd.FileName); |
| 107 | + TextBox_Directory.Text = ofd.FileName.Substring(0, ofd.FileName.LastIndexOf("\\") + 1); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private void Button_Input_Click(object sender, EventArgs e) |
| 112 | + { |
| 113 | + ofd = new OpenFileDialog(); |
| 114 | + |
| 115 | + if (ofd.ShowDialog() == DialogResult.OK) |
| 116 | + { |
| 117 | + string[] lines = File.ReadAllLines(ofd.FileName); |
| 118 | + TextBox_Input.Text = String.Join(Environment.NewLine, lines); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private void Button_Output_Click(object sender, EventArgs e) |
| 123 | + { |
| 124 | + ofd = new OpenFileDialog(); |
| 125 | + |
| 126 | + if (ofd.ShowDialog() == DialogResult.OK) |
| 127 | + { |
| 128 | + string[] lines = File.ReadAllLines(ofd.FileName); |
| 129 | + TextBox_Output.Text = String.Join(Environment.NewLine, lines); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments