PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/V2.2/trunk/CAL/Desktop/Composite.Tests/ExceptionExtensionsFixture.cs

#
C# | 174 lines | 124 code | 32 blank | 18 comment | 0 complexity | 6d05b5932b3b88f8c6206d1fbc7a3508 MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using System.Collections.Generic;
 19using System.Linq;
 20using System.Text;
 21using Microsoft.VisualStudio.TestTools.UnitTesting;
 22
 23namespace Microsoft.Practices.Composite.Tests
 24{
 25    [TestClass]
 26    public class ExceptionExtensionsFixture
 27    {
 28        [TestMethod]
 29        // Note, this test cannot be run twice in the same test run, because the registeration is static 
 30        // and we're not supplying an 'Unregister' method
 31        public void CanRegisterFrameworkExceptionTypes()
 32        {
 33            Assert.IsFalse(ExceptionExtensions.IsFrameworkExceptionRegistered(typeof(MockException)));
 34
 35            ExceptionExtensions.RegisterFrameworkExceptionType(typeof(MockException));
 36
 37            Assert.IsTrue(ExceptionExtensions.IsFrameworkExceptionRegistered(typeof(MockException)));
 38        }
 39
 40        [TestMethod]
 41        public void CanGetRootException()
 42        {
 43            Exception caughtException = null;
 44            ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException1));
 45            try
 46            {
 47                try
 48                {
 49                    throw new RootException();
 50                }
 51                catch (Exception ex)
 52                {
 53                    
 54                    throw new FrameworkException1(ex);
 55                }
 56            }
 57            catch (Exception ex)
 58            {
 59                caughtException = ex;
 60            }
 61
 62            Assert.IsNotNull(caughtException);
 63
 64            Exception exception = caughtException.GetRootException();
 65
 66            Assert.IsInstanceOfType(exception, typeof(RootException));
 67        }
 68
 69        [TestMethod]
 70        public void CanCompensateForInnerFrameworkExceptionType()
 71        {
 72            Exception caughtException = null;
 73            ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException2));
 74            try
 75            {
 76                try
 77                {
 78                    try
 79                    {
 80                        throw new RootException();
 81                    }
 82                    catch (Exception ex)
 83                    {
 84                        
 85                        throw new FrameworkException2(ex);
 86                    }
 87                }
 88                catch (Exception ex)
 89                {
 90                    
 91                    throw new NonFrameworkException(ex);
 92                }
 93            }
 94            catch (Exception ex)
 95            {
 96                caughtException = ex;
 97            }
 98
 99            Assert.IsNotNull(caughtException);
100
101            Exception exception = caughtException.GetRootException();
102            Assert.IsInstanceOfType(exception, typeof(RootException));
103        }
104
105        [TestMethod]
106        public void GetRootExceptionReturnsTopExceptionWhenNoUserExceptionFound()
107        {
108            Exception caughtException = null;
109            ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException1));
110            ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException2));
111            try
112            {
113                try
114                {
115                    throw new FrameworkException1(null);
116                }
117                catch (Exception ex)
118                {
119
120                    throw new FrameworkException2(ex);
121                }
122            }
123            catch (Exception ex)
124            {
125                caughtException = ex;
126            }
127
128            Assert.IsNotNull(caughtException);
129
130            Exception exception = caughtException.GetRootException();
131            Assert.IsInstanceOfType(exception, typeof(FrameworkException2));
132        }
133            
134        private class MockException : Exception
135        {
136
137        }
138
139
140
141        private class FrameworkException2 : Exception
142        {
143            public FrameworkException2(Exception innerException)
144                : base("", innerException)
145            {
146
147            }
148        }
149
150        private class FrameworkException1:Exception
151        {
152            public FrameworkException1(Exception innerException) : base("", innerException)
153            {
154                
155            }
156        }
157
158        private class RootException:Exception
159        {}
160
161        private class NonFrameworkException : Exception
162        {
163            public NonFrameworkException(Exception innerException)
164                : base("", innerException)
165            {
166                
167            }
168        }
169
170    }
171
172    
173
174}