Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/01/12 16:29:43 (12 years ago)
Author:
mkommend
Message:

#1863: Updated plugin dependencies unit test to test external libraries.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/PluginDependenciesTest.cs

    r7915 r7947  
    2222using System;
    2323using System.Collections.Generic;
     24using System.IO;
    2425using System.Linq;
    2526using System.Reflection;
     
    3334    private static Dictionary<Assembly, Type> loadedPlugins;
    3435    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>();
    3638
    3739    // Use ClassInitialize to run code before running the first test in the class
    3840    [ClassInitialize]
    3941    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);
    4143      pluginNames = loadedPlugins.ToDictionary(a => a.Key.GetName().FullName, a => GetPluginName(a.Value));
    4244
    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      }
    5253    }
    5354
     
    5758      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
    5859        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            }
    6572          }
     73        }
    6674      }
    67 
    6875      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
    6976    }
     
    7481      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
    7582        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);
    7884
    7985        foreach (PluginDependencyAttribute attribute in pluginDependencies.Keys) {
    8086          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 + ".");
    8798        }
    8899      }
    89 
    90100      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
    91101    }
     
    94104      return assembly.GetExportedTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).FirstOrDefault();
    95105    }
     106
    96107    private static string GetPluginName(Type plugin) {
    97108      string name = string.Empty;
     
    101112      return name;
    102113    }
     114
     115    private static bool IsPluginAssemblyName(AssemblyName assemblyName) {
     116      return pluginNames.ContainsKey(assemblyName.FullName);
     117    }
    103118  }
    104119}
Note: See TracChangeset for help on using the changeset viewer.