Changeset 8086 for branches/ScatterSearch (trunk integration)/HeuristicLab.Tests/HeuristicLab-3.3/PluginDependenciesTest.cs
- Timestamp:
- 06/22/12 11:11:38 (12 years ago)
- Location:
- branches/ScatterSearch (trunk integration)
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch (trunk integration)
- Property svn:ignore
-
old new 20 20 bin 21 21 protoc.exe 22 _ReSharper.HeuristicLab 3.3 Tests
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/ScatterSearch (trunk integration)/HeuristicLab.Tests
- Property svn:ignore
-
old new 3 3 HeuristicLab.Tests.csproj.user 4 4 *.vs10x 5 _ReSharper.HeuristicLab 3.3 Tests
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/ScatterSearch (trunk integration)/HeuristicLab.Tests/HeuristicLab-3.3
- Property bugtraq:label deleted
- Property bugtraq:logregex deleted
- Property bugtraq:url deleted
- Property bugtraq:warnifnoissue deleted
-
branches/ScatterSearch (trunk integration)/HeuristicLab.Tests/HeuristicLab-3.3/PluginDependenciesTest.cs
r7450 r8086 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 88 if (pluginDependencyName == "HeuristicLab.MathJax") continue; //is never referenced as this plugin contains HTML files 89 var referencedPluginAssemblies = pluginAssembly.GetReferencedAssemblies().Where(IsPluginAssemblyName); 90 if (referencedPluginAssemblies.Any(a => pluginNames[a.FullName] == pluginDependencyName)) continue; 91 92 var referencedNonPluginAssemblies = pluginAssembly.GetReferencedAssemblies().Where(a => !IsPluginAssemblyName(a)); 93 bool found = (from referencedNonPluginAssemblie in referencedNonPluginAssemblies 94 select referencedNonPluginAssemblie.Name into assemblyName 95 where pluginFilesToPluginLookup.ContainsKey(assemblyName) 96 select GetPluginFromAssembly(pluginFilesToPluginLookup[assemblyName]) into pluginType 97 select GetPluginName(pluginType)).Any(pluginName => pluginName == pluginDependencyName); 98 99 if (!found) errorMessage.AppendLine("Unnecessary plugin dependency in " + GetPluginName(plugin) + " to " + pluginDependencyName + "."); 87 100 } 88 101 } 89 90 102 Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString()); 91 103 } … … 94 106 return assembly.GetExportedTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).FirstOrDefault(); 95 107 } 108 96 109 private static string GetPluginName(Type plugin) { 97 110 string name = string.Empty; … … 101 114 return name; 102 115 } 116 117 private static bool IsPluginAssemblyName(AssemblyName assemblyName) { 118 return pluginNames.ContainsKey(assemblyName.FullName); 119 } 103 120 } 104 121 }
Note: See TracChangeset
for help on using the changeset viewer.