Merge pull request #1870 from saper/langinfo_h
[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
26 using System;
27 using System.Linq;
28 using System.Threading;
29 using System.Collections.Generic;
30
31 using NUnit;
32 using NUnit.Framework;
33
34 namespace MonoTests.System
35 {
36         [TestFixture]
37         public class AggregateExceptionTest
38         {
39                 AggregateException e;
40                 
41                 [SetUpAttribute]
42                 public void Setup()
43                 {
44                         e = new AggregateException(new Exception("foo"), new AggregateException(new Exception("bar"), new Exception("foobar")));
45                 }
46
47                 [Test]
48                 public void SimpleInnerExceptionTestCase ()
49                 {
50                         var message = "Foo";
51                         var inner = new ApplicationException (message);
52                         var ex = new AggregateException (inner);
53
54                         Assert.IsNotNull (ex.InnerException);
55                         Assert.IsNotNull (ex.InnerExceptions);
56
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 ());
62                 }
63                 
64                 [TestAttribute]
65                 public void FlattenTestCase()
66                 {
67                         AggregateException ex = e.Flatten();
68                         
69                         Assert.AreEqual(3, ex.InnerExceptions.Count, "#1");
70                         Assert.AreEqual(3, ex.InnerExceptions.Where((exception) => !(exception is AggregateException)).Count(), "#2");
71                 }
72
73                 [Test, ExpectedException (typeof (ArgumentException))]
74                 public void InitializationWithNullInnerValuesTest ()
75                 {
76                         var foo = new AggregateException (new Exception[] { new Exception (), null, new ApplicationException ()});
77                 }
78
79                 [Test]
80                 public void InitializationWithNullValuesTest ()
81                 {
82                         Throws (typeof (ArgumentNullException), () => new AggregateException ((IEnumerable<Exception>)null));
83                         Throws (typeof (ArgumentNullException), () => new AggregateException ((Exception[])null));
84                 }
85
86                 [Test]
87                 [ExpectedException (typeof (ArgumentNullException))]
88                 public void Handle_Invalid ()
89                 {
90                         e.Handle (null);
91                 }
92
93                 [Test]
94                 public void Handle_AllHandled ()
95                 {
96                         e.Handle (l => true);
97                 }
98
99                 [Test]
100                 public void Handle_Unhandled ()
101                 {
102                         try {
103                                 e.Handle (l => l is AggregateException);
104                                 Assert.Fail ();
105                         } catch (AggregateException e) {
106                                 Assert.AreEqual (1, e.InnerExceptions.Count);
107                         }
108                 }
109
110                 [Test]
111                 public void GetBaseWithInner ()
112                 {
113                         var ae = new AggregateException ("x", new [] { new ArgumentException (), new ArgumentNullException () });
114                         Assert.AreEqual (ae, ae.GetBaseException (), "#1");
115
116                         var expected = new ArgumentException ();
117                         var ae2 = new AggregateException ("x", new AggregateException (expected, new Exception ()));
118                         Assert.AreEqual (expected, ae2.GetBaseException ().InnerException, "#2");
119                 }
120
121                 [Test]
122                 public void GetBaseException_stops_at_first_inner_exception_that_is_not_AggregateException()
123                 {
124                         var inner = new ArgumentNullException ();
125                         var outer = new InvalidOperationException ("x", inner);
126                         Assert.AreEqual (outer, new AggregateException (outer).GetBaseException ());
127                 }
128
129                 static void Throws (Type t, Action action)
130                 {
131                         Exception e = null;
132                         try {
133                                 action ();
134                         } catch (Exception ex) {
135                                 e = ex;
136                         }
137
138                         if (e == null || e.GetType () != t)
139                                 Assert.Fail ();
140                 }
141         }
142 }