1: using System;
2: using System.Linq;
3: using System.Text.RegularExpressions;
4: using System.Xml;
5: using Microsoft.VisualStudio.TestTools.UnitTesting;
6:
7: [TestMethod]
8: [DeploymentItem(@"UnityConfigTestApp\App.config")]
9: public void TypeAliases_should_reference_valid_types()
10: {
11: XmlDocument unityConfig = new XmlDocument();
12: unityConfig.Load("App.config");
13: XmlNode unityNode = unityConfig.DocumentElement.SelectSingleNode("unity");
14: XmlNodeList typeAliases = unityNode.SelectNodes("typeAliases/typeAlias");
15:
16: foreach (XmlNode typeAliasNode in typeAliases)
17: {
18: string typeFullName = RemoveWhiteSpace(typeAliasNode.Attributes["type"].Value);
19: Type currentType = Type.GetType(typeFullName);
20:
21: Assert.IsNotNull(currentType, String.Format("'{0}' is not a valid type.", typeFullName));
22: }
23: }
24:
25: [TestMethod]
26: [DeploymentItem(@"UnityConfigTestApp\App.config")]
27: public void Type_mappings_should_be_compatible()
28: {
29: XmlDocument unityConfig = new XmlDocument();
30: unityConfig.Load("App.config");
31: XmlNode unityNode = unityConfig.DocumentElement.SelectSingleNode("unity");
32: XmlNodeList typeList = unityNode.SelectNodes("containers/container/types/type");
33:
34: foreach (XmlNode typeNode in typeList)
35: {
36: string typeName = typeNode.Attributes["type"].Value;
37: string mapToName = typeNode.Attributes["mapTo"].Value;
38:
39: XmlNode typeTypeAliasNode = unityNode.SelectSingleNode(String.Format("typeAliases/typeAlias[@alias = \"{0}\"]", typeName));
40: XmlNode mapToTypeAliasNode = unityNode.SelectSingleNode(String.Format("typeAliases/typeAlias[@alias = \"{0}\"]", mapToName));
41:
42: Assert.IsNotNull(typeTypeAliasNode, String.Format("Could not locate a typeAlias element for '{0}'.", typeName));
43: Assert.IsNotNull(mapToTypeAliasNode, String.Format("Could not locate a typeAlias element for '{0}'.", mapToName));
44:
45: string typeFullName = RemoveWhiteSpace(typeTypeAliasNode.Attributes["type"].Value);
46: string mapToFullName = RemoveWhiteSpace(mapToTypeAliasNode.Attributes["type"].Value);
47:
48: Type currentType = Type.GetType(typeFullName);
49: Type mapToType = Type.GetType(mapToFullName);
50: Assert.IsNotNull(currentType, String.Format("'{0}' is not a valid type.", typeFullName));
51: Assert.IsNotNull(mapToType, String.Format("'{0}' is not a valid type.", mapToFullName));
52:
53: bool areTypesCompatible = false;
54:
55: if (currentType.IsAssignableFrom(mapToType) || // test assignabilty
56: mapToType.Name == currentType.Name || // or matching names
57: mapToType.GetInterfaces().Where(x => x.Name == currentType.Name).Count() == 1 || // or generic interfaces
58: (mapToType.BaseType != null && mapToType.BaseType.Name == currentType.Name)) // or generic base classes
59: {
60: areTypesCompatible = true;
61: }
62:
63: Assert.IsTrue(areTypesCompatible, String.Format("Cannot map '{0}' to '{1}'.", typeName, mapToName));
64: }
65: }
66:
67: private static string RemoveWhiteSpace(string inputString)
68: {
69: if (inputString == null)
70: return null;
71:
72: return Regex.Replace(inputString, @"[\s\r\n]", String.Empty);
73: }