d133a82cd86d9d3c26dab9bf6d4a4f8188c1d4d5
[mono.git] / mcs / class / corlib / Test / System / AggregateExceptionTests.cs
1 // AggregateExceptionTests.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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
21 // THE SOFTWARE.
22 //
23 //
24
25 #if NET_4_0
26
27 using System;
28 using System.Linq;
29 using System.Threading;
30 using System.Collections.Generic;
31
32 using NUnit;
33 using NUnit.Framework;
34
35 namespace MonoTests.System
36 {
37         [TestFixture()]
38         public class AggregateExceptionTest
39         {
40                 AggregateException e;
41                 
42                 [SetUpAttribute]
43                 public void Setup()
44                 {
45                         e = new AggregateException(new Exception("foo"), new AggregateException(new Exception("bar"), new Exception("foobar")));
46                 }
47
48                 [Test]
49                 public void SimpleInnerExceptionTestCase ()
50                 {
51                         var message = "Foo";
52                         var inner = new ApplicationException (message);
53                         var ex = new AggregateException (inner);
54
55                         Assert.IsNotNull (ex.InnerException);
56                         Assert.IsNotNull (ex.InnerExceptions);
57
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 ());
63                 }
64                 
65                 [TestAttribute]
66                 public void FlattenTestCase()
67                 {
68                         AggregateException ex = e.Flatten();
69                         
70                         Assert.AreEqual(3, ex.InnerExceptions.Count, "#1");
71                         Assert.AreEqual(3, ex.InnerExceptions.Where((exception) => !(exception is AggregateException)).Count(), "#2");
72                 }
73
74                 [Test, ExpectedException (typeof (ArgumentException))]
75                 public void InitializationWithNullInnerValuesTest ()
76                 {
77                         var foo = new AggregateException (new Exception[] { new Exception (), null, new ApplicationException ()});
78                 }
79
80                 [Test]
81                 public void InitializationWithNullValuesTest ()
82                 {
83                         Throws (typeof (ArgumentNullException), () => new AggregateException ((IEnumerable<Exception>)null));
84                         Throws (typeof (ArgumentNullException), () => new AggregateException ((Exception[])null));
85                 }
86
87                 [Test]
88                 [ExpectedException (typeof (ArgumentNullException))]
89                 public void Handle_Invalid ()
90                 {
91                         e.Handle (null);
92                 }
93
94                 [Test]
95                 public void Handle_AllHandled ()
96                 {
97                         e.Handle (l => true);
98                 }
99
100                 [Test]
101                 public void Handle_Unhandled ()
102                 {
103                         try {
104                                 e.Handle (l => l is AggregateException);
105                                 Assert.Fail ();
106                         } catch (AggregateException e) {
107                                 Assert.AreEqual (1, e.InnerExceptions.Count);
108                         }
109                 }
110
111                 [Test]
112                 public void GetBaseWithInner ()
113                 {
114                         var ae = new AggregateException ("x", new [] { new ArgumentException (), new ArgumentNullException () });
115                         Assert.AreEqual (ae, ae.GetBaseException (), "#1");
116
117                         var expected = new ArgumentException ();
118                         var ae2 = new AggregateException ("x", new AggregateException (expected, new Exception ()));
119                         Assert.AreEqual (expected, ae2.GetBaseException ().InnerException, "#2");
120                 }
121
122                 static void Throws (Type t, Action action)
123                 {
124                         Exception e = null;
125                         try {
126                                 action ();
127                         } catch (Exception ex) {
128                                 e = ex;
129                         }
130
131                         if (e == null || e.GetType () != t)
132                                 Assert.Fail ();
133                 }
134         }
135 }
136 #endif