PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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. //===================================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text;
  21. using Microsoft.Practices.Prism;
  22. using Microsoft.VisualStudio.TestTools.UnitTesting;
  23. namespace Microsoft.Practices.Prism.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. ExceptionExtensions.RegisterFrameworkExceptionType(typeof(MockException));
  35. Assert.IsTrue(ExceptionExtensions.IsFrameworkExceptionRegistered(typeof(MockException)));
  36. }
  37. [TestMethod]
  38. public void CanGetRootException()
  39. {
  40. Exception caughtException = null;
  41. ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException1));
  42. try
  43. {
  44. try
  45. {
  46. throw new RootException();
  47. }
  48. catch (Exception ex)
  49. {
  50. throw new FrameworkException1(ex);
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. caughtException = ex;
  56. }
  57. Assert.IsNotNull(caughtException);
  58. Exception exception = caughtException.GetRootException();
  59. Assert.IsInstanceOfType(exception, typeof(RootException));
  60. }
  61. [TestMethod]
  62. public void CanCompensateForInnerFrameworkExceptionType()
  63. {
  64. Exception caughtException = null;
  65. ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException2));
  66. try
  67. {
  68. try
  69. {
  70. try
  71. {
  72. throw new RootException();
  73. }
  74. catch (Exception ex)
  75. {
  76. throw new FrameworkException2(ex);
  77. }
  78. }
  79. catch (Exception ex)
  80. {
  81. throw new NonFrameworkException(ex);
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. caughtException = ex;
  87. }
  88. Assert.IsNotNull(caughtException);
  89. Exception exception = caughtException.GetRootException();
  90. Assert.IsInstanceOfType(exception, typeof(RootException));
  91. }
  92. [TestMethod]
  93. public void GetRootExceptionReturnsTopExceptionWhenNoUserExceptionFound()
  94. {
  95. Exception caughtException = null;
  96. ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException1));
  97. ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException2));
  98. try
  99. {
  100. try
  101. {
  102. throw new FrameworkException1(null);
  103. }
  104. catch (Exception ex)
  105. {
  106. throw new FrameworkException2(ex);
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. caughtException = ex;
  112. }
  113. Assert.IsNotNull(caughtException);
  114. Exception exception = caughtException.GetRootException();
  115. Assert.IsInstanceOfType(exception, typeof(FrameworkException2));
  116. }
  117. private class MockException : Exception
  118. {
  119. }
  120. private class FrameworkException2 : Exception
  121. {
  122. public FrameworkException2(Exception innerException)
  123. : base("", innerException)
  124. {
  125. }
  126. }
  127. private class FrameworkException1:Exception
  128. {
  129. public FrameworkException1(Exception innerException) : base("", innerException)
  130. {
  131. }
  132. }
  133. private class RootException:Exception
  134. {}
  135. private class NonFrameworkException : Exception
  136. {
  137. public NonFrameworkException(Exception innerException)
  138. : base("", innerException)
  139. {
  140. }
  141. }
  142. }
  143. }