/// <summary> /// Compare an exception string thrown at runtime to the expected string in a resource file /// </summary> /// <param name="actual">String that you would like to compare to.</param> /// <param name="resourceId">ID of resource to look up.</param> /// <param name="targetBinary">WPF Resource that contains the object throwing the exception.</param> /// <returns></returns> public static bool CompareMessage(string actual, string resourceId, WpfBinaries targetBinary) { string expected = GetMessage(resourceId, targetBinary); if (expected == null) { GlobalLog.LogEvidence(string.Format("Could not find message with resourceId {0} in binary {1}.", resourceId, targetBinary.ToString())); return(false); } return(CompareMessage(actual, expected)); }
/// <summary> /// Look up exception string as resources and return localized string of exception message. /// </summary> /// <param name="resourceId">The Id of the exception that will be looked up.</param> /// <param name="targetBinary">Wpf Resource that contains the object throwing the exception</param> /// <returns>Localized string of the specified exception message</returns> public static string GetMessage(string resourceId, WpfBinaries targetBinary) { Assembly assembly = null; switch (targetBinary) { // Will look for resource in WindowsBase.dll case WpfBinaries.WindowsBase: assembly = typeof(DependencyObject).Assembly; break; // Will look for resource in PresentationCore.dll case WpfBinaries.PresentationCore: assembly = typeof(UIElement).Assembly; break; // Will look for resource in PresentationFramework.dll case WpfBinaries.PresentationFramework: assembly = typeof(FrameworkElement).Assembly; break; #if TESTBUILD_CLR40 // Will look for resource in System.Xaml.dll case WpfBinaries.SystemXaml: assembly = typeof(System.Xaml.XamlReader).Assembly; break; #endif default: break; } if (assembly != null) { // Use a ResourceManager to locate the resource in the desired assembly. ResourceManager resourceManager = new ResourceManager(String.Format("FxResources.{0}.SR", assembly.GetName().Name), assembly); return(resourceManager.GetString(resourceId)); } else { return(string.Empty); } }
public static void ExpectException <T>(ExceptionTriggerCallback exceptionTrigger, T expectedException, string resourceId, WpfBinaries targetBinary) where T : Exception { ExpectException(exceptionTrigger, delegate(T caughtException) { if (!String.IsNullOrEmpty(resourceId) && !Exceptions.CompareMessage(caughtException.Message, resourceId, targetBinary)) { throw new TestValidationException("Expected Message: " + Exceptions.GetMessage(resourceId, targetBinary) + ", Actual Message: " + caughtException.Message); } PropertyDescriptorCollection baseClassProperties = TypeDescriptor.GetProperties(typeof(Exception)); PropertyDescriptorCollection subClassProperties = TypeDescriptor.GetProperties(typeof(T)); foreach (PropertyDescriptor property in subClassProperties) { if (!baseClassProperties.Contains(property)) { if (!Object.Equals(property.GetValue(caughtException), property.GetValue(expectedException))) { throw new TestValidationException("Property Name: " + property.Name + ", Expected Value: " + property.GetValue(expectedException) + ", Actual Value: " + property.GetValue(caughtException)); } } } return; }); }