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
29 using System.Threading;
30 using System.Collections.Generic;
33 using NUnit.Framework;
35 namespace MonoTests.System
38 public class AggregateExceptionTest
45 e = new AggregateException(new Exception("foo"), new AggregateException(new Exception("bar"), new Exception("foobar")));
49 public void SimpleInnerExceptionTestCase ()
52 var inner = new ApplicationException (message);
53 var ex = new AggregateException (inner);
55 Assert.IsNotNull (ex.InnerException);
56 Assert.IsNotNull (ex.InnerExceptions);
58 Assert.AreEqual (inner, ex.InnerException);
59 Assert.AreEqual (1, ex.InnerExceptions.Count);
60 Assert.AreEqual (inner, ex.InnerExceptions[0]);
61 Assert.AreEqual (message, ex.InnerException.Message);
62 Assert.AreEqual (inner, ex.GetBaseException ());
66 public void FlattenTestCase()
68 AggregateException ex = e.Flatten();
70 Assert.AreEqual(3, ex.InnerExceptions.Count, "#1");
71 Assert.AreEqual(3, ex.InnerExceptions.Where((exception) => !(exception is AggregateException)).Count(), "#2");
74 [Test, ExpectedException (typeof (ArgumentException))]
75 public void InitializationWithNullInnerValuesTest ()
77 var foo = new AggregateException (new Exception[] { new Exception (), null, new ApplicationException ()});
81 public void InitializationWithNullValuesTest ()
83 Throws (typeof (ArgumentNullException), () => new AggregateException ((IEnumerable<Exception>)null));
84 Throws (typeof (ArgumentNullException), () => new AggregateException ((Exception[])null));
88 [ExpectedException (typeof (ArgumentNullException))]
89 public void Handle_Invalid ()
95 public void Handle_AllHandled ()
101 public void Handle_Unhandled ()
104 e.Handle (l => l is AggregateException);
106 } catch (AggregateException e) {
107 Assert.AreEqual (1, e.InnerExceptions.Count);
112 public void GetBaseWithInner ()
114 var ae = new AggregateException ("x", new [] { new ArgumentException (), new ArgumentNullException () });
115 Assert.AreEqual (ae, ae.GetBaseException (), "#1");
117 var expected = new ArgumentException ();
118 var ae2 = new AggregateException ("x", new AggregateException (expected, new Exception ()));
119 Assert.AreEqual (expected, ae2.GetBaseException ().InnerException, "#2");
122 static void Throws (Type t, Action action)
127 } catch (Exception ex) {
131 if (e == null || e.GetType () != t)