Changeset 7947 for trunk/sources/HeuristicLab.Tests
- Timestamp:
- 06/01/12 16:29:43 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/PluginDependenciesTest.cs
r7915 r7947 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.IO; 24 25 using System.Linq; 25 26 using System.Reflection; … … 33 34 private static Dictionary<Assembly, Type> loadedPlugins; 34 35 private static Dictionary<string, string> pluginNames; 35 private static HashSet<string> extLibPluginNames; 36 private static Dictionary<string, Assembly> pluginFilesToPluginLookup = new Dictionary<string, Assembly>(); 37 //private static Dictionary<string, string> pluginToPluginFilesLookup = new Dictionary<string, string>(); 36 38 37 39 // Use ClassInitialize to run code before running the first test in the class 38 40 [ClassInitialize] 39 41 public static void MyClassInitialize(TestContext testContext) { 40 loadedPlugins = PluginLoader.Assemblies.Where( x => PluginLoader.IsPluginAssembly(x)).ToDictionary(a => a, GetPluginFromAssembly);42 loadedPlugins = PluginLoader.Assemblies.Where(PluginLoader.IsPluginAssembly).ToDictionary(a => a, GetPluginFromAssembly); 41 43 pluginNames = loadedPlugins.ToDictionary(a => a.Key.GetName().FullName, a => GetPluginName(a.Value)); 42 44 43 extLibPluginNames = new HashSet<string>(); 44 extLibPluginNames.Add("HeuristicLab.ALGLIB"); 45 extLibPluginNames.Add("HeuristicLab.LibSVM"); 46 extLibPluginNames.Add("HeuristicLab.log4net"); 47 extLibPluginNames.Add("HeuristicLab.MathJax"); 48 extLibPluginNames.Add("HeuristicLab.Netron"); 49 extLibPluginNames.Add("HeuristicLab.ProtobufCS"); 50 extLibPluginNames.Add("HeuristicLab.SharpDevelop"); 51 extLibPluginNames.Add("HeuristicLab.WinFormsUI"); 45 foreach (Assembly pluginAssembly in loadedPlugins.Keys) { 46 Type pluginType = GetPluginFromAssembly(pluginAssembly); 47 var pluginFileAttributes = Attribute.GetCustomAttributes(pluginType, false).OfType<PluginFileAttribute>(); 48 foreach (var pluginFileAttribute in pluginFileAttributes) { 49 string fillNameWithoutExtension = Path.GetFileNameWithoutExtension(pluginFileAttribute.FileName); 50 pluginFilesToPluginLookup.Add(fillNameWithoutExtension, pluginAssembly); 51 } 52 } 52 53 } 53 54 … … 57 58 foreach (Assembly pluginAssembly in loadedPlugins.Keys) { 58 59 Type plugin = loadedPlugins[pluginAssembly]; 59 Dictionary<string, PluginDependencyAttribute> pluginDependencies = 60 Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a.Dependency); 61 foreach (AssemblyName referencedPluginName in pluginAssembly.GetReferencedAssemblies()) 62 if (pluginNames.ContainsKey(referencedPluginName.FullName)) { //check if reference assembly is a plugin 63 if (!pluginDependencies.ContainsKey(pluginNames[referencedPluginName.FullName])) 64 errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to referenced plugin " + pluginNames[referencedPluginName.FullName] + "."); 60 Dictionary<string, PluginDependencyAttribute> pluginDependencies = Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a.Dependency); 61 62 foreach (AssemblyName referencedAssemblyName in pluginAssembly.GetReferencedAssemblies()) { 63 if (IsPluginAssemblyName(referencedAssemblyName)) { 64 if (!pluginDependencies.ContainsKey(pluginNames[referencedAssemblyName.FullName])) 65 errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to referenced plugin " + pluginNames[referencedAssemblyName.FullName] + "."); 66 } else { //no plugin assembly => test if the assembly is delivered by another plugin 67 if (pluginFilesToPluginLookup.ContainsKey(referencedAssemblyName.Name)) { 68 string containingPluginFullName = pluginFilesToPluginLookup[referencedAssemblyName.Name].FullName; 69 if (containingPluginFullName != pluginAssembly.FullName && !pluginDependencies.ContainsKey(pluginNames[containingPluginFullName])) 70 errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to plugin " + pluginNames[containingPluginFullName] + " due to a reference to " + referencedAssemblyName.FullName + "."); 71 } 65 72 } 73 } 66 74 } 67 68 75 Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString()); 69 76 } … … 74 81 foreach (Assembly pluginAssembly in loadedPlugins.Keys) { 75 82 Type plugin = loadedPlugins[pluginAssembly]; 76 Dictionary<PluginDependencyAttribute, string> pluginDependencies = 77 Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a, a => a.Dependency); 83 Dictionary<PluginDependencyAttribute, string> pluginDependencies = Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a, a => a.Dependency); 78 84 79 85 foreach (PluginDependencyAttribute attribute in pluginDependencies.Keys) { 80 86 string pluginDependencyName = pluginDependencies[attribute]; 81 //do not check extlib plugins, because the transport assemblies are never referenced in the assemblies 82 if (extLibPluginNames.Contains(pluginDependencyName)) continue; 83 if (pluginAssembly.GetReferencedAssemblies().Where(a => pluginNames.ContainsKey(a.FullName)) 84 .All(a => pluginNames[a.FullName] != pluginDependencyName)) { 85 errorMessage.AppendLine("Unnecessary plugin dependency in " + GetPluginName(plugin) + " to " + pluginDependencyName + "."); 86 } 87 var referencedPluginAssemblies = pluginAssembly.GetReferencedAssemblies().Where(IsPluginAssemblyName); 88 if (referencedPluginAssemblies.Any(a => pluginNames[a.FullName] == pluginDependencyName)) continue; 89 90 var referencedNonPluginAssemblies = pluginAssembly.GetReferencedAssemblies().Where(a => !IsPluginAssemblyName(a)); 91 bool found = (from referencedNonPluginAssemblie in referencedNonPluginAssemblies 92 select referencedNonPluginAssemblie.Name into assemblyName 93 where pluginFilesToPluginLookup.ContainsKey(assemblyName) 94 select GetPluginFromAssembly(pluginFilesToPluginLookup[assemblyName]) into pluginType 95 select GetPluginName(pluginType)).Any(pluginName => pluginName == pluginDependencyName); 96 97 if (!found) errorMessage.AppendLine("Unnecessary plugin dependency in " + GetPluginName(plugin) + " to " + pluginDependencyName + "."); 87 98 } 88 99 } 89 90 100 Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString()); 91 101 } … … 94 104 return assembly.GetExportedTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).FirstOrDefault(); 95 105 } 106 96 107 private static string GetPluginName(Type plugin) { 97 108 string name = string.Empty; … … 101 112 return name; 102 113 } 114 115 private static bool IsPluginAssemblyName(AssemblyName assemblyName) { 116 return pluginNames.ContainsKey(assemblyName.FullName); 117 } 103 118 } 104 119 }
Note: See TracChangeset
for help on using the changeset viewer.