Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System / Test / System.Diagnostics / ProcessTest.cs
index a11ef93f293c65e6651bd9aeefccd017b8170279..7c70b5d6cb119bc848d978a674b78314e8c36693 100644 (file)
@@ -13,6 +13,7 @@ using System.ComponentModel;
 using System.Diagnostics;
 using System.IO;
 using System.Text;
+using System.Threading;
 
 using NUnit.Framework;
 
@@ -706,7 +707,7 @@ namespace MonoTests.System.Diagnostics
                        Assert.AreEqual ("hello", Encoding.Default.GetString (buffer, 0, 5), "#04");
                        */
                }
-
+               
                void Read (IAsyncResult ar)
                {
                        Stream stm = (Stream) ar.AsyncState;
@@ -721,5 +722,108 @@ namespace MonoTests.System.Diagnostics
                }
 
                int bytesRead = -1;
+
+#if NET_2_0
+// Not technically a 2.0 only test, but I use lambdas, so I need gmcs
+
+               [Test]
+               // This was for bug #459450
+               public void TestEventRaising ()
+               {
+                       EventWaitHandle errorClosed = new ManualResetEvent(false);
+                       EventWaitHandle outClosed = new ManualResetEvent(false);
+                       EventWaitHandle exited = new ManualResetEvent(false);
+
+                       Process p = new Process();
+                       
+                       p.StartInfo = GetCrossPlatformStartInfo ();
+                       p.StartInfo.UseShellExecute = false;
+                       p.StartInfo.RedirectStandardOutput = true;
+                       p.StartInfo.RedirectStandardError = true;
+                       p.StartInfo.RedirectStandardInput = false;
+                       p.OutputDataReceived += (object sender, DataReceivedEventArgs e) => {
+                               if (e.Data == null) {
+                                       outClosed.Set();
+                               }
+                       };
+                       
+                       p.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => {
+                               if (e.Data == null) {
+                                       errorClosed.Set();
+                               }
+                       };
+                       
+                       p.Exited += (object sender, EventArgs e) => {
+                               exited.Set ();
+                       };
+                       
+                       p.EnableRaisingEvents = true;
+
+                       p.Start();
+
+                       p.BeginErrorReadLine();
+                       p.BeginOutputReadLine();
+
+                       Console.WriteLine("started, waiting for handles");
+                       bool r = WaitHandle.WaitAll(new WaitHandle[] { errorClosed, outClosed, exited }, 10000, false);
+
+                       Assert.AreEqual (true, r, "Null Argument Events Raised");
+               }
+               
+               private ProcessStartInfo GetCrossPlatformStartInfo ()
+               {
+                       return RunningOnUnix ? new ProcessStartInfo ("/bin/ls", "/") : new ProcessStartInfo ("help", "");
+               }
+               
+               [Test]
+               public void ProcessName_NotStarted ()
+               {
+                       Process p = new Process ();
+                       Exception e = null;
+                       try {
+                               String.IsNullOrEmpty (p.ProcessName);
+                       } catch (Exception ex) {
+                               e = ex;
+                       }
+                       
+                       Assert.IsNotNull (e, "ProcessName should raise if process was not started");
+                       
+                       //msg should be "No process is associated with this object"
+                       Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
+                                        "exception should be IOE, I got: " + e.GetType ().Name);
+                       
+                       Assert.IsNull (e.InnerException, "IOE inner exception should be null");
+               }
+               
+               [Test]
+               public void ProcessName_AfterExit ()
+               {
+                       Process p = new Process ();
+                       p.StartInfo = GetCrossPlatformStartInfo ();
+                       p.StartInfo.UseShellExecute = false;
+                       p.StartInfo.RedirectStandardOutput = true;
+                       p.StartInfo.RedirectStandardError = true;
+                       p.Start ();
+                       p.BeginErrorReadLine();
+                       p.BeginOutputReadLine();
+                       p.WaitForExit ();
+                       String.IsNullOrEmpty (p.ExitCode + "");
+                       
+                       Exception e = null;
+                       try {
+                               String.IsNullOrEmpty (p.ProcessName);
+                       } catch (Exception ex) {
+                               e = ex;
+                       }
+                       
+                       Assert.IsNotNull (e, "ProcessName should raise if process was finished");
+                       
+                       //msg should be "Process has exited, so the requested information is not available"
+                       Assert.AreEqual (e.GetType (), typeof (InvalidOperationException),
+                                        "exception should be IOE, I got: " + e.GetType ().Name);
+                       
+                       Assert.IsNull (e.InnerException, "IOE inner exception should be null");
+               }
+#endif
        }
 }