1 // AggregateExceptionTests.cs
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 using System.Threading;
29 using System.Collections.Generic;
32 using NUnit.Framework;
34 namespace MonoTests.System
37 public class AggregateExceptionTest
44 e = new AggregateException(new Exception("foo"), new AggregateException(new Exception("bar"), new Exception("foobar")));
48 public void SimpleInnerExceptionTestCase ()
51 var inner = new ApplicationException (message);
52 var ex = new AggregateException (inner);
54 Assert.IsNotNull (ex.InnerException);
55 Assert.IsNotNull (ex.InnerExceptions);
57 Assert.AreEqual (inner, ex.InnerException);
58 Assert.AreEqual (1, ex.InnerExceptions.Count);
59 Assert.AreEqual (inner, ex.InnerExceptions[0]);
60 Assert.AreEqual (message, ex.InnerException.Message);
61 Assert.AreEqual (inner, ex.GetBaseException ());
65 public void FlattenTestCase()
67 AggregateException ex = e.Flatten();
69 Assert.AreEqual(3, ex.InnerExceptions.Count, "#1");
70 Assert.AreEqual(3, ex.InnerExceptions.Where((exception) => !(exception is AggregateException)).Count(), "#2");
73 [Test, ExpectedException (typeof (ArgumentException))]
74 public void InitializationWithNullInnerValuesTest ()
76 var foo = new AggregateException (new Exception[] { new Exception (), null, new ApplicationException ()});
80 public void InitializationWithNullValuesTest ()
82 Throws (typeof (ArgumentNullException), () => new AggregateException ((IEnumerable<Exception>)null));
83 Throws (typeof (ArgumentNullException), () => new AggregateException ((Exception[])null));
87 [ExpectedException (typeof (ArgumentNullException))]
88 public void Handle_Invalid ()
94 public void Handle_AllHandled ()
100 public void Handle_Unhandled ()
103 e.Handle (l => l is AggregateException);
105 } catch (AggregateException e) {
106 Assert.AreEqual (1, e.InnerExceptions.Count);
111 public void GetBaseWithInner ()
113 var ae = new AggregateException ("x", new [] { new ArgumentException (), new ArgumentNullException () });
114 Assert.AreEqual (ae, ae.GetBaseException (), "#1");
116 var expected = new ArgumentException ();
117 var ae2 = new AggregateException ("x", new AggregateException (expected, new Exception ()));
118 Assert.AreEqual (expected, ae2.GetBaseException ().InnerException, "#2");
122 public void GetBaseException_stops_at_first_inner_exception_that_is_not_AggregateException()
124 var inner = new ArgumentNullException ();
125 var outer = new InvalidOperationException ("x", inner);
126 Assert.AreEqual (outer, new AggregateException (outer).GetBaseException ());
129 static void Throws (Type t, Action action)
134 } catch (Exception ex) {
138 if (e == null || e.GetType () != t)