PageRenderTime 34ms CodeModel.GetById 12ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/V4/PrismLibrary/Desktop/Prism.Tests/ExceptionExtensionsFixture.cs

#
C# | 175 lines | 125 code | 32 blank | 18 comment | 0 complexity | b7e8c966e1e391fb22d558cd20d4ba18 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.Practices.Prism;
 22using Microsoft.VisualStudio.TestTools.UnitTesting;
 23
 24namespace Microsoft.Practices.Prism.Tests
 25{
 26    [TestClass]
 27    public class ExceptionExtensionsFixture
 28    {
 29        [TestMethod]
 30        // Note, this test cannot be run twice in the same test run, because the registeration is static 
 31        // and we're not supplying an 'Unregister' method
 32        public void CanRegisterFrameworkExceptionTypes()
 33        {
 34            Assert.IsFalse(ExceptionExtensions.IsFrameworkExceptionRegistered(typeof(MockException)));
 35
 36            ExceptionExtensions.RegisterFrameworkExceptionType(typeof(MockException));
 37
 38            Assert.IsTrue(ExceptionExtensions.IsFrameworkExceptionRegistered(typeof(MockException)));
 39        }
 40
 41        [TestMethod]
 42        public void CanGetRootException()
 43        {
 44            Exception caughtException = null;
 45            ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException1));
 46            try
 47            {
 48                try
 49                {
 50                    throw new RootException();
 51                }
 52                catch (Exception ex)
 53                {
 54                    
 55                    throw new FrameworkException1(ex);
 56                }
 57            }
 58            catch (Exception ex)
 59            {
 60                caughtException = ex;
 61            }
 62
 63            Assert.IsNotNull(caughtException);
 64
 65            Exception exception = caughtException.GetRootException();
 66
 67            Assert.IsInstanceOfType(exception, typeof(RootException));
 68        }
 69
 70        [TestMethod]
 71        public void CanCompensateForInnerFrameworkExceptionType()
 72        {
 73            Exception caughtException = null;
 74            ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException2));
 75            try
 76            {
 77                try
 78                {
 79                    try
 80                    {
 81                        throw new RootException();
 82                    }
 83                    catch (Exception ex)
 84                    {
 85                        
 86                        throw new FrameworkException2(ex);
 87                    }
 88                }
 89                catch (Exception ex)
 90                {
 91                    
 92                    throw new NonFrameworkException(ex);
 93                }
 94            }
 95            catch (Exception ex)
 96            {
 97                caughtException = ex;
 98            }
 99
100            Assert.IsNotNull(caughtException);
101
102            Exception exception = caughtException.GetRootException();
103            Assert.IsInstanceOfType(exception, typeof(RootException));
104        }
105
106        [TestMethod]
107        public void GetRootExceptionReturnsTopExceptionWhenNoUserExceptionFound()
108        {
109            Exception caughtException = null;
110            ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException1));
111            ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException2));
112            try
113            {
114                try
115                {
116                    throw new FrameworkException1(null);
117                }
118                catch (Exception ex)
119                {
120
121                    throw new FrameworkException2(ex);
122                }
123            }
124            catch (Exception ex)
125            {
126                caughtException = ex;
127            }
128
129            Assert.IsNotNull(caughtException);
130
131            Exception exception = caughtException.GetRootException();
132            Assert.IsInstanceOfType(exception, typeof(FrameworkException2));
133        }
134            
135        private class MockException : Exception
136        {
137
138        }
139
140
141
142        private class FrameworkException2 : Exception
143        {
144            public FrameworkException2(Exception innerException)
145                : base("", innerException)
146            {
147
148            }
149        }
150
151        private class FrameworkException1:Exception
152        {
153            public FrameworkException1(Exception innerException) : base("", innerException)
154            {
155                
156            }
157        }
158
159        private class RootException:Exception
160        {}
161
162        private class NonFrameworkException : Exception
163        {
164            public NonFrameworkException(Exception innerException)
165                : base("", innerException)
166            {
167                
168            }
169        }
170
171    }
172
173    
174
175}