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